๐Ÿ’ฐ Sending a Transaction

Once the connection with the BoltX extension has been successfully established, the transaction can be submitted to the respective blockchain network for the processing. BoltX provides API for this.

Sample code for Ethereum:

const provider = new ethers.providers.Web3Provider(window.boltX.ethereum)

// To get the account signer...
const signer = provider.getSigner()

//NOTE: the most important thing is to set the provided to the BoltX and then 
// the transaction can be submitted by using the standard ethers.JS methods.

//Similar way , it can be done using the web3 also.

Sample code for Zilliqa:

if (typeof window.boltX === 'undefined') {
  console.log('Please install BoltX!');
}

const zilliqa = window.boltX.zilliqa;

await zilliqa.wallet.connect();

let currentAccount = null;
let network = null;
const onAccountChange = (account) => {
    if (!account) {
        console.log('Not connected');
        return;
    }

    const {base16, bech32} = account;
    currentAccount = account;
    console.log(bech32);
}

const onNetworkChange = (net) => {
    console.log('Network changed to', net);
    network = net;
}

/**
 * Note: 
 * zilliqa.events.ACCOUNT_CHANGED === 'accountsChanged'
 * zilliqa.events.NETWORK_CHANGED === 'networkChanged'
 */

zilliqa.wallet.on('accountsChanged', onAccountChange)
zilliqa.wallet.on('networkChanged', onNetworkChange)

// use boltX.zilliqa similar to @zilliqa-js module
// @see https://dev.zilliqa.com/docs/dev/dev-tools-zilliqajs/#demo---zrc-2-wallet
// @see https://github.com/Zilliqa/Zilliqa-JavaScript-Library

// sample usage
const tx = await zilliqa.blockchain.createTransaction(
    zilliqa.transactions.new(
      {
        // version: VERSION, ( will be filled by extension )
        toAddr: '0xA54E49719267E8312510D7b78598ceF16ff127CE',
        amount: new BN(units.toQa('1', units.Units.Zil)), // Sending an amount in Zil (1) and converting the amount to Qa
        gasPrice: myGasPrice, // Minimum gasPrice veries. Check the `GetMinimumGasPrice` on the blockchain
        gasLimit: Long.fromNumber(50),
      },
      false,
    ),
);

Sample code for Carbon(Former Tradehub):

if (typeof window.boltX === 'undefined') {
  console.log('Please install BoltX!');
}

const tradehub = await window.boltX.tradehub;

await tradehub.connect();

/**********************************************************/
/* Handle network and networkChanged 
/**********************************************************/

let network = tradehub.network;
let currentAccount = tradehub.selectedAddress;
// tradehub.selectedAddress === tradehub.state.accounts[0]

function handleNetworkChanged(_network) {
    console.log('Network changed to', net);
    network = net;
}

function handleAccountsChanged(accounts) {
  if (accounts.length === 0) {
    console.log('Please connect to MetaMask.');
  } else if (accounts[0] !== currentAccount) {
    currentAccount = accounts[0];
  }
}

/**
 * Note: 
 * tradehub.events.ACCOUNT_CHANGED === 'accountsChanged'
 * tradehub.events.NETWORK_CHANGED === 'networkChanged'
 */
tradehub.on('accountsChanged', handleAccountsChanged)
tradehub.on('networkChanged', handleNetworkChanged);

const supportedEndpoints = tradehub.TradehubEndpoints;

if (supportedEndpoints.includes('/get_txns_fees')) {
  // send request to public endpoint
  const fees = await tradehub.request({path: '/get_txns_fees'});
  
  
  // send request to public endpoint with query
  const accountDetails = await tradehub.request({
    path: '/get_account', 
    params: {
      account: currentAccount
    }
  });
}

const tx = {
    type: "cosmos-sdk/MsgSend",
    value: {
      amount: [{ denom: "swth", amount: "100000000" }],
      to_address: "swth1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq",
      from_address: boltX.tradehub.selectedAddress,
    },
}

// send tx request to private endpoint
const txResult = await tradehub.sendTx(tx);

Last updated