Create

The library currently supplies the following helper functions for creating transactions:

addFeeProxy()

Helper function used to add a fee proxy to the extrinsic. It expects two parameters:

  • assetId: the asset ID to pay the fee in (1 for ROOT, 2 for XRP, 3 for Vortex, etc).

  • slippage: the slippage to use for the fee (in percentage).

Example:

// The example shows a token transfer to a wallet address on a Root Network Collection, but minting using the FeeProxy to pay for the gas with a chosen token.

const nftBuilder = await TransactionBuilder.nft(trnApi, signer, userSession.eoa, collectionId).transfer({ destinationAddress: userSession?.futurepass, amount: 1 }).addFeeProxy({ assetId: ROOT_TOKEN_ID, slippage: 5 });

To get more information and retrieve a list of available tokens and their asset IDs, refer to the Tokens section in the Block Explorer of The Root Network.

addFuturePass()

Helper function used to add a FuturePass to the extrinsic. It expects one parameter:

  • futurePass: the FuturePass address to proxy the extrinsic through.

Example:

// The example shows an NFT mint adding the FuturePass to pay for the gas.

const nftBuilder = await TransactionBuilder.nft(trnApi, signer, userSession.eoa, collectionId).mint({ quantity: 1, walletAddress: userSession?.futurepass }).addFuturePass(userSession.futurepass);

addFuturePassAndFeeProxy()

Helper function used to add a FuturePass and a FeeProxy to the extrinsic. It expects three parameters:

  • futurePass: the FuturePass address to proxy the extrinsic through.

  • assetId: the asset ID to pay the fee in (1 for ROOT, 2 for XRP, 3 for Vortex etc).

  • slippage: the slippage to use for the fee (in percentage).

Example:

// The example shows batch multiple transactions on The Root Network Collection sent using FuturePass to pay the gas combined with the FeeProxy to pay with a chosen token.

const batchBuilder = await TransactionBuilder.batch(trnApi, signer, userSession.eoa).addExtrinsic(tx1).addExtrinsic(tx2).addExtrinsic(tx3).addExtrinsic(tx4).addExtrinsic(tx5).batchAll().addFuturePassAndFeeProxy({
    futurePass: userSession.futurepass,
    assetId: assetId,
    slippage: 5,
  });

To get more information and retrieve a list of available tokens and their asset IDs, refer to the Tokens section in the Block Explorer of The Root Network.

checkBalance()

Helper function used to get the balance of an asset. It can receive two parameters:

  • walletAddress: the wallet address to get the balance. Wallet address is optional. When not provided, the wallet address of the instance will be used instead.

  • assetId: the asset ID to get the balance for.

Example:

    const transaction = await TransactionBuilder.nft(
      trnApi,
      signer,
      userSession.eoa,
      collectionId
    )
      .mint({ quantity: qtyToMint, walletAddress: userSession?.futurepass })
      .addFuturePassAndFeeProxy({
        futurePass: userSession.futurepass,
        assetId: assetId,
        slippage: 5,
      });

    const balance = await transaction.checkBalance({
      assetId: assetId,
    });

checkBalances()

Helper function used to check the balances of multiple assets for a given wallet address. It can receive one parameter:

  • walletAddress and assetsId: an array of the [walletAddresses, assetId] pair to check balances for. Wallet address is optional. When not provided, the wallet address of the instance will be used instead.

This function throws an error if the API is not connected or if no wallet address and asset IDs are provided as parameters or by the instantiated class.

Example:

const transaction = await TransactionBuilder.nft(
      trnApi,
      signer,
      userSession.eoa,
      collectionId
    )
      .mint({ quantity: qtyToMint, walletAddress: userSession?.futurepass })
      .addFuturePassAndFeeProxy({
        futurePass: userSession.futurepass,
        assetId: assetId,
        slippage: 5,
      });

const balances = await transaction.checkBalances([
      { assetId: 1 },
      { assetId: 2 },
      { assetId: 3 },
      { assetId: 3172 },
      { assetId: 17508 },
    ]);

filterExtrinsicEvents()

Helper function used to filter extrinsic events. It expects two parameters:

  • events: array of events to filter.

  • names: array of names to filter by.

Example:

filteredExtrinsicEvents = await TransactionBuilder.filterExtrinsicEvents(events, names);

getAmountsIn()

Helper function used to get the extrinsic to send. It expects three parameters:

  • amount: the amount to get the amounts in for.

  • assetId: the asset ID to get the amounts in for.

  • slippage: the slippage to use for the amounts in. Defaults to value 5.

Example:

const amountsIn = await TransactionBuilder.getAmountsIn(amount, assetId, slippage);

getExtrinsicToSend()

Helper function used to get the extrinsic to send. It has no parameters.

Example:

const extrinsicToSend = await TransactionBuilder.getExtrinsicToSend();

getFuturePass()

Helper function used to get the FuturePass for the wallet address. It can receive one parameter:

  • walletAddress: the wallet address to get the FuturePass for. Wallet address is optional. When not provided, the wallet address of the instance will be used instead.

This function throws an error if the API is not connected or if no wallet address is provided as a parameter or by the instantiated class.

Example:

const futurePass = await TransactionBuilder.getFuturePass(walletAddress);

getGasFees()

Helper function used to get the gas fees for the extrinsic. It has no parameters.

Example:

const gasEstimate = await TransactionBuilder.getGasFees();
      if (gasEstimate) {
        setGas(gasEstimate);
      };

getPayloads()

Helper function used to get the payloads for the extrinsic. It has no parameters.

Example:

    const getExtrinsic = async (builder: TransactionBuilder) => {
      const payloads = await builder?.getPayloads();
      if (!payloads) {
        return;
      }
      setPayload(payloads);
    }

signAndSend()

Helper function used to sign and send the extrinsic. It can receive two parameters. Both are optional:

  • onSign: callback function to call when extrinsic is signed.

  • onSend: callback function to call when extrinsic is sent.

Example:

  const builder = TransactionBuilder;
  
  const onSign = useCallback(() => {
    setSigned(true);
  }, [setSigned]);

  const onSend = useCallback(() => {
    setSent(true);
  }, [setSent]);

const signExtrinsic = useCallback(async () => {
  if (builder) {
    const result = await builder?.signAndSend({ onSign, onSend });
    setResult(result as ExtrinsicResult);
  }
}, [builder, onSend, onSign, toSign]);

Last updated

© 2023 -> ♾️