Usage

Examples of basic use.

Static method:

import Chainge from '@chainge/sdk';

// kit
const { bufferToHex, bigIntToHex, toBuffer, intToHex } = Chainge.utils;
const hexStr = intToHex(5); // 0x5

// decodeRaw
const raw = ''; // txRaw
const tempTxObj = Chainge.decodeRaw(raw);

Instance methods:

import Chainge from '@chainge/sdk';
const chainge = new Chainge(ethereum, {signUrl: 'https://xxxx'});

// get supported chains
const getSupportedChains = async () => {
     try {
           const result = await chainge.getSupportChains();
           if (result.code === 200 && result.data.chains) {
           }
     } catch(error) {
           console.log(error);
     }
}

// core 
const crossChain = async () => {
      try {
            const params = {
                  evmAddress: '',
                  feeLevel: '',
                  fromAddress: '',
                  fromAmount: '',
                  fromChain: '',
                  fromToken: '',
                  toChain: '',
                  toToken: ''
            }
            
            /**
            * The crossChainStatus callback returns parameters and states a cross chains
            * data: Results the callback
            * type:  signAndSendTransaction, crossChain, aggregate, waitForTransaction, submit, 
            * crossChain, aggregate: Represents a cross-chaining type, but does not indicate that cross-chaining has occurred
            * submit:Cross-chain behavior has occurred, but it does not mean that cross-chain is completely successful
            */
            const crossChainStatus = (data, type) => {
                  // Do something
            }
            const subscribe = await chainge.execute(window.ethereum, params, time = 1000, crossChainStatus)
            // Subscribe returns the status of the order after the chain crossing is successful
            if(subscribe && typeof subscribe === 'function') {
                  subscribe((orderDetail) => {
                        console.log(orderDetail)
                  })
            }
      } catch(error) {
            console.log(error) 
      }
}

callback parameter details:

type: signAndSendTransaction

data: {response : "start"} or {response : "end"}

type: waitForTransaction

data:

{
    certHash: '',
    response: {
        status: "start",
        result: {} // optional. If status is end and successful, it exists
    }
}

type: crossChain or aggregate

data:

{
    certHash: '',
    response: {
        //... cross chains params
    }
}

type: submit

data:

{
    certHash: '',
    response: {
        // ... Some other information
        response: {
            // ...The result of crossing chains
        }
    }
}

Last updated