# How to Use

## Installation <a href="#installation" id="installation"></a>

```
yarn add @xchainjs/xchain-doge
```

### Peer Dependencies

You can visit the [xchain-doge](https://github.com/xchainjs/xchainjs-lib/blob/7c7776da37315f636d7a8dc6adde2d327f47c277/packages/xchain-doge/package.json#L49) package repository to get the updated peer dependencies by looking at the "peerDependencies" object in the "package.json" file.

### Testing <a href="#doge-client-testing" id="doge-client-testing"></a>

```
yarn install
yarn test
```

## Examples <a href="#basic-usage-examples" id="basic-usage-examples"></a>

#### Connect Wallet to New Doge Client and Check Balance <a href="#connect-wallet-to-new-doge-client-and-check-balance" id="connect-wallet-to-new-doge-client-and-check-balance"></a>

Create a new instance of the Dogecoin Client\
Retrieve and validate an address\
Check the balance of assets on address\
The network default is Mainnet

```
//Imports 
import { Client, DOGE_DECIMAL} from "@xchainjs/xchain-doge"

// Connect wallet to new Client 
const connectWallet = async () => {
    let phrase = "phrase"
    let dogeClient = new Client({ network: Network.Mainnet, phrase})
    let address = dogeClient.getAddress()
    let isValid = dogeClient.validateAddress(address)
    if(isValid === true){
        try {
            const balance = await dogeClient.getBalance(address)
            let assetAmount = (baseToAsset(balance[0].amount)).amount()
            console.log(`Adress: ${address} with balance ${assetAmount}`)
        } catch (error) {
            console.log(`Caught: ${error}`)
        }
    } else {
        console.log(`Address ${address} is not valid`)
    }
}

```

#### Transfer Dogecoin using dogeClient Instance <a href="#transfer-dogecoin-using-dogeclient-instance" id="transfer-dogecoin-using-dogeclient-instance"></a>

Create a new client instance\
Convert amount to transfer to base\
Build transaction with correct Tx Parameters\
The default fee is set to 1

```
//Imports
import { assetToBase, baseToAsset, assetAmount} from "@xchainjs/xchain-util"

// Call Transfer with TxParams
const transferDoge = async () => {
    let amountToTransfer = 0.01
    let recipient = await getRecipientAddress()
    let phrase = "phrase"
    let dogeClient = new Client({phrase})
    let amount = assetToBase(assetAmount(amountToTransfer, DOGE_DECIMAL))
    console.log("Building transaction", JSON.stringify(amount.amount()))
    try {
        const txid = await dogeClient.transfer({
            "amount": amount,
            "recipient": recipient,
            "memo": "test"
        })
        console.log(`Transaction sent: ${txid}`)

    } catch (error) {
         console.log(`Caught: ${error}`)
    }
}
// Transfer with fee rate set 
    let feeRate = await dogeClient.getFeeRates()
    try {
        const txid = await dogeClient.transfer({
            "amount": amount,
            "recipient": recipient,
            "memo": "test",
            feeRate: feeRate.fastest
        })
        console.log(`Transaction sent: ${txid}`)

    } catch (error) {
         console.log(`Caught: ${error}`)
    }
```

#### Get Transfer Fees and FeeRate Estimations <a href="#get-transfer-fees-and-feerate-estimations" id="get-transfer-fees-and-feerate-estimations"></a>

Create new dogeClient and query getFees & getFeeRates functions\
Fees are returned as base Amounts.

```
// Get fees 
const feeData = async () => {
    let phrase = "phrase"
    let dogeClient = new Client({phrase})
    try {
        const {fast, fastest, average} = await dogeClient.getFees()
        console.log(`Fees Fast: ${baseToAsset(fast).amount()} Fastest: ${baseToAsset(fastest).amount()} Average: ${baseToAsset(average).amount()}`)

    } catch (error) {
         console.log(`Caught: ${error}`)
    }   
}
// Get Fee Rates
const feeRates = async () => {
    let phrase = "phrase"
    let dogeClient = new Client({phrase})
    try {
        const feeRates = await dogeClient.getFeeRates() // returned as number
        console.log(feeRates.average, feeRates.fast, feeRates.fastest)
    } catch (error) {
         console.log(`Caught: ${error}`)
    }   
}

// Get both Fees and Rates
const getFeesWithRates = async () => {
    let phrase = "phrase"
    let dogeClient = new Client({phrase})
    try {
        const feesWithRates = await dogeClient.getFeesWithRates()
        console.log(feesWithRates.fees, feesWithRates.rates)
    } catch (error) {
         console.log(`Caught: ${error}`)
    }  
}

```

#### Get Transaction Data & History <a href="#get-transaction-data--history" id="get-transaction-data--history"></a>

Create a new client instance and query chain data with a hash\
getTransactions() can be filtered with `limit? offset? startTime?`

```

// Retrieve transaction data for a transaction hash 

const transactionData = async () => {
    let phrase = "phrase"
    let dogeClient = new Client({ phrase})
    let hash = "insert hash"
    try {
        const txData = await dogeClient.getTransactionData(hash)
        console.log(`From ${JSON.stringify(txData)}`)   
    } catch (error) {
        console.log(`Caught: ${error}`)
    }
}

// Retrieve transaction history for a particular address

const transactionHistory = async () => {
    let phrase = "phrase"
    let dogeClient = new Client({phrase})
    let Address = dogeClient.getAddress()
    try {
        const txHistory = await dogeClient.getTransactions({address: Address}) 
        console.log(`Found ${txHistory.total.toString()}`)
        txHistory.txs.forEach(tx => console.log(tx.hash))
    } catch (error) {
         console.log(`Caught: ${error}`)
    }    
}
```
