XchainJS
  • Overview
  • Installation
  • Examples instructions
  • Clients
    • xchain-evm
      • xchain-avax
        • How to Use
      • xchain-arbitrum
        • How to Use
      • xchain-bsc
        • How to Use
      • xchain-ethereum
        • How to Use
    • xchain-utxo
      • xchain-bitcoin
        • How to use
      • xchain-bitcoincash
        • How to Use
      • xchain-dash
      • xchain-doge
        • How to Use
      • xchain-litecoin
        • How to Use
    • xchain-cosmos-sdk
      • xchain-cosmos
        • How to Use
      • xchain-kujira
        • How to Use
      • xchain-mayachain
        • How to Use
      • xchain-thorchain
        • How to Use
    • xchain-binance
      • How to Use
    • xchain-solana
      • How to use
      • Examples
        • Generate address
        • Get balances
        • Get token balance
        • Prepare transaction
        • Make transaction
        • Make token transaction
  • Wallet
  • Protocols
    • THORChain
      • xchain-thorchain-amm
        • How to Use
        • Make swap using THORChain
        • Handle liquidity and savers
        • Open and close loans
      • xchain-thorchain-query
        • How to Use
        • Check transaction status
        • Estimate a swap
      • xchain-midgard
        • How to Use
      • xchain-thornode
        • How to Use
      • xchain-midgard-query
        • How to Use
    • MAYAProtocol
      • xchain-mayachain-amm
        • How to Use
        • Make swap using MAYAChain
      • xchain-mayachain-query
        • How to Use
      • xchain-mayamidgard
        • How to Use
      • xchain-mayanode
        • How to Use
      • xchain-mayamidgard-query
        • How to Use
  • Aggregator
  • Providers
    • xchain-utxo-providers
      • How it Works
    • xchain-evm-providers
      • How it Works
  • Others
    • xchain-crypto
      • How it Works
      • How to Use
    • xchain-util
      • How it Works
      • How to Use
  • Contributors
  • Documentation maintenance
Powered by GitBook
On this page
  • Installation
  • Peer Dependencies
  • Testing
  • Example
  • Connect Wallet to New Arbitrum Chain Client
  • Transfer Arbitrum Using arbitrumClient
  • Get Fees
  • Get Transaction Data & History
  1. Clients
  2. xchain-evm
  3. xchain-arbitrum

How to Use

Previousxchain-arbitrumNextxchain-bsc

Last updated 1 year ago

Installation

yarn add @xchainjs/xchain-arbitrum

Peer Dependencies

You can visit the package repository to get the updated peer dependencies by looking at the "peerDependencies" object in the "package.json" file.

Testing

yarn install
yarn test

Example

// Imports
import { Client, defaultArbParams } from "@xchainjs/xchain-arbitrum"
import { FeeOption } from "@xchainjs/xchain-client"
import { assetToBase, baseToAsset, assetAmount, Asset, Chain  } from "@xchainjs/xchain-util"

Connect Wallet to New Arbitrum Chain Client

The network default is Testnet

// Create arbitrum asset
const TestnetUSDCAsset: Asset = {
  chain: 'ARB',
  symbol: 'USDC-0x179522635726710dd7d2035a81d856de4aa7836c',
  ticker: 'USDC',
  synth: false,
}
// Create new Arbitrum Client Instance
const connectWallet = async () => {
    defaultArbParams.phrase = "phrase"
    const arbitrumClient = new Client(defaultArbParams)
    let address = arbitrumClient.getAddress()
    console.log(`Address: ${address}`)
    let isValid = arbitrumClient.validateAddress(address)
    if( isValid === true ){
        try {
            const balance = await arbitrumClient.getBalance(address)
            let assetAmount = (baseToAsset(balance[1].amount)).amount()
            console.log(`With balance: ${assetAmount}`)
    
        } catch (error) {
            console.log(`Caught: ${error}`)
        }
    }
}

Transfer Arbitrum Using arbitrumClient

const transferArbitrum = async () => {
    let amountToTransfer = 0.1
    let recipient = "address"
    defaultArbParams.phrase = "phrase"
    const arbitrumClient = new Client(defaultArbParams)
    let amount = assetToBase(assetAmount(amountToTransfer, 18))
    console.log("Building transaction", JSON.stringify(amount.amount()))
    try {
        const txid = await arbitrumClient.transfer({ 
            "asset": TestnetUSDCAsset,
            "amount": amount,
            "recipient":recipient,
            feeOption: FeeOption.Fast,
        })
        console.log(`Transaction sent: ${txid}`)
    } catch (error) {
        console.log(`Caught: ${error}`)
    } 
}

Get Fees

const returnFees = async () => {
    defaultArbParams.phrase = "phrase"
    const arbitrumClient = new Client(defaultArbParams)
    let amountToTransfer = 20
    let amount = assetToBase(assetAmount(amountToTransfer, 18))
    let recipient = "recipient"
    try {
        const txParams: TxParams = {
            walletIndex: 0,
            asset: TestnetUSDCAsset,
            amount: amount,
            recipient: recipient,
        }
        const {fast, fastest, average} = await arbitrumClient.getFees(txParams)
        console.log(`Fees Fast: ${baseToAsset(fast).amount()} Fastest: ${baseToAsset(fastest).amount()} Average: ${baseToAsset(average).amount()}`)    } catch (error) {
        console.log(`Caught: ${error}`)
    }
}

Get Transaction Data & History

const transactionData = async () => {
    defaultArbParams.phrase = "phrase"
    const arbitrumClient = new Client(defaultArbParams)
    let hash = "0x60721cf788b7cd4e56acf6479e71dfbd12e6c79c15e76595e4e52409bf686d4c"

    try {
        const txData = await arbitrumClient.getTransactionData(hash)
        console.log(`From ${JSON.stringify(txData)}`)
    } catch (error) {
        console.log(`Caught: ${error}`)
    }
}
// Address History
const transactionHistory = async () => {
    defaultArbParams.phrase = "phrase"
    const arbitrumClient = new Client(defaultArbParams)
    let Address = arbitrumClient.getAddress()

    try {
        const txHistory = await arbitrumClient.getTransactions({address: Address, limit: 4 })
        console.log(`Found ${txHistory.total.toString()}`)
        txHistory.txs.forEach(tx => console.log(tx.hash))
    } catch (error) {
        console.log(`Caught: ${error}`)
    }
}
xchain-arbitrum