Tx Client
About Tx
A transaction contains at least:
- construct a transaction: The SDK already provides each transaction type
- simulate: txClient.simulate
- broadcast txClient.broadcast
simulate
Just Simulate a transaction and a valid transaction.
params | description |
---|---|
denom | the 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.
params | description |
---|---|
denom | the coin denom to query balances for |
gasLimit | can be set to any number, but not too small or the transaction may fail (recommended use simulateInfo.gasLimit ) |
gasPrice | 1 unit of Gas that the transaction sender is willing to pay. |
payer | transaction sender |
granter | transaction ganter (Generally empty '' ) |
signTypedDataCallback | broadcast use window.ethereum as signature provider by default. |
privateKey | If you broadcast in Nodejs, you can broadcast a tx by privateKey |
- Browser
- Nodejs
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],
});
},
});
If 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: '',
privateKey: '0x.......',
});
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: '',
});