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
  • Examples
  1. Clients
  2. xchain-cosmos-sdk
  3. xchain-mayachain

How to Use

Previousxchain-mayachainNextxchain-thorchain

Last updated 1 year ago

Installation

yarn add @xchainjs/xchain-mayachain

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

Examples

Connect Wallet to New Maya Client

Create new mayachain client The client has two different sets of parameters XchainClientParams & MayachainClientParams MayachainClient includes chainIds. getChainIds() returns all chain id's for default Client Url. The network default is Mainnet


// Imports 
import { Client, getChainIds, getDefaultClientUrl} from '@xchainjs/xchain-mayachain'
import { assetToBase, baseToAsset, assetAmount } from "@xchainjs/xchain-util"


// Create new instance of the client and query chain for balances. 
const connectWallet = async () => {

    let phrase = "phrase"
    const mayaClient = new Client({phrase})
    let address = mayaClient.getAddress()
    console.log(`Address: ${address}`)
    try {
        const balance = await mayaClient.getBalance(address)
        let assetAmount = (baseToAsset(balance[0].amount)).amount()
        console.log(`With balance: ${assetAmount}`)
    } catch (error) {
        console.log(`Caught ${error}`)
    }
}

Transfer CACAO using Maya Client

Create a new Mayachain client instance Convert amount to transfer to base amount Build transaction.


//Imports
import { assetToBase, baseToAsset, assetAmount } from "@xchainjs/xchain-util"
 
const transferCacao = async () => {

    let phrase = "phrase"
    const mayaClient = new Client({phrase})
    let amountToTransfer = 0.1
    let amount = assetToBase(assetAmount(amountToTransfer, DECIMAL ))
    let recipient = "maya1ka2v9exy8ata00pch87wgzf9dsmyag94tq8mug" 
    try {
        const txid = await mayaClient.transfer({
            "amount": amount,
            "recipient": recipient,
            "memo": "test",
            "asset": AssetCacao,
            "walletIndex": 0 
        })
        console.log(`Transaction sent: ${JSON.stringify(txid)}`)
    } catch (error) {
        console.log(`Caught ${error}`)
    }
}

Get Transaction Data & Transaction History

Retrieve transaction data using transaction hash and address


const transactionData = async () => {
 
    let phrase = "phrase"
    const mayaClient = new Client({phrase})
    let hash = "insert hash"
    let address = mayaClient.getAddress()
    try {
        const txData = await mayaClient.getTransactionData(hash, address)
        console.log(`From ${JSON.stringify(txData)}`)
    } catch (error) {
        console.log(`Caught ${error}`)
    }
}
// By default getTransactions() returns the transactions for the current address
const transactionHistory = async () => {

    let phrase = "phrase"
    const mayaClient = new Client({phrase})
    
    try {
        const txHistory = await mayaClient.getTransactions() 
        console.log(`Found ${txHistory.total.toString()}`)
        txHistory.txs.forEach(tx => console.log(tx.hash))
    } catch (error) {
        console.log(`Caught ${error}`)
    }
}

Get Transfer Fees

MayaChain runs on a fee type of Flatfee set to 0.2 Cacao

// Returns Fees Fast: 0.2 Fastest: 0.2 Average: 0.2
const fee = async () => {

    let phrase = "phrase"
    const mayaClient = new Client({phrase})
    try {
        const {fast, fastest, average} = await mayaClient.getFees()
        console.log(`Fees Fast: ${baseToAsset(fast).amount()} Fastest: ${baseToAsset(fastest).amount()} Average: ${baseToAsset(average).amount()}`)
    } catch (error) {
        console.log(`Caught ${error}`)
    }
}

Get Network and Explorer Data

// Query mayachain client for network data and explorer data

const explorerUrl = async () => {

    let phrase = "phrase"
    const mayaClient = new Client({phrase})
    let hash = "insert hash"
    try {
        const networkData = mayaClient.getExplorerUrl()
        console.log(`Explorer url: ${networkData}`)
        const transactionUrl = mayaClient.getExplorerTxUrl(hash)
        console.log(`Explorer transaction: ${transactionUrl}`)

    } catch (error) {
        console.log(`Caught ${error}`)
    }
}
xchain-mayachain