For this example we’ll use tronweb to interact with the contract, but you can replace it with any Tron library of your choice.Make sure to install it first:
To execute a bridge transaction, call the depositWithId (TRC20) function as follows:
The commitmentId required by the contract corresponds to the quoteId returned by the API.To obtain the commitmentId, first request a quote from the API, then commit it. The response will include the quoteId, which should be used as the commitmentId in the contract call.See the API bridge example for details.
import { TronWeb, providers } from 'tronweb'import getBridgeConfigs from './getBridgeConfigs'// You can find the bridgeABI above in the ABI sectionimport bridgeAbi from './bridgeAbi'export const callTronBridgeContract = async ( { token, amount, commitmentId, tronWalletAddress }) => { // See API Integration -> Fetch Bridge Configs for the full implementation const configs = await getBridgeConfigs() const chainConfig = configs[chain] // Replace with your wallet implementation const tronWeb = new TronWeb({ fullHost: new providers.HttpProvider('https://api.trongrid.io'), // headers: { "TRON-PRO-API-KEY": 'your api key' }, -- if using a custom RPC privateKey: TRON_PRIVATE_KEY }) const tokenConfig = chainConfig.tokens[token] const amountWithDecimals = +amount * 10 ** tokenConfig.decimals const tokenAddress = tokenConfig.address const bridgeContractAddress = chainConfig.contractAddress const { abi } = await tronWeb.trx.getContract(tokenAddress); const trc20Contract = tronWeb.contract(abi.entrys, tokenAddress); const allowance = await trc20Contract.allowance(tronWalletAddress, bridgeContractAddress).call(); const formattedAllowance = parseInt(allowance, 10) / 10 ** tokenConfig.decimals; if (formattedAllowance < parseFloat(amount)) { await trc20Contract.approve(bridgeContractAddress, amountWithDecimals).send() } const contract = tronWeb.contract(bridgeAbi, bridgeContractAddress) const tx = await contract.depositWithId(tokenAddress, amountWithDecimals, `0x${commitmentId}`).send({shouldPollResponse: true}) return tx}