Skip to main content

Tx Client

About Tx​

A transaction contains at least:

  1. construct a transaction: The SDK already provides each transaction type
  2. simulate: txClient.simulate
  3. broadcast txClient.broadcast

simulate​

Just Simulate a transaction and a valid transaction.

paramsdescription
denomthe coin denom to query balances for
simulate tx
// `tx` is a transaction constructed by the SDK
const simulateInfo = await tx.simulate({
denom: 'BNB',
});

broadcast​

Broadcast the transaction to the chain.

paramsdescription
denomthe coin denom to query balances for
gasLimitcan be set to any number, but not too small or the transaction may fail (recommended use simulateInfo.gasLimit)
gasPrice1 unit of Gas that the transaction sender is willing to pay.
payertransaction sender
grantertransaction ganter (Generally empty '')
signTypedDataCallbackbroadcast use window.ethereum as signature provider by default.
privateKeyIf you broadcast in Nodejs, you can broadcast a tx by privateKey
broadcast tx
const broadcastRes = await transferTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo.gasLimit),
gasPrice: simulateInfo.gasPrice,
payer: '0x0000000000000000000000000000000000000001',
granter: '',
// If you want to use others wallet(such as trustwallet), you can set `signTypedDataCallback`:
signTypedDataCallback: async (addr: string, message: string) => {
return await window.trustwallet.request({
method: 'eth_signTypedData_v4',
params: [addr, message],
});
},
});

Example​

Take transfer tx as a complete example:

1. construct a transaction​

construct tx
const transferTx = await client.account.transfer({
fromAddress: address,
toAddress: transferInfo.to,
amount: [
{
denom: 'BNB',
amount: '1000000000',
},
],
});

2. simulate​

simulate tx
const simulateInfo = await transferTx.simulate({
denom: 'BNB',
});

3. broadcast​

broadcast tx
const broadcastRes = await transferTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo.gasLimit),
gasPrice: simulateInfo.gasPrice,
payer: address,
granter: '',
});