Links

EstimateTransferTokensGas

Description

This function can be used to estimate the Gas Limit and Gas Price that will be required by a TransferTokens invocation. The information can be presented to users before they confirm the transaction.

Details

Signature

async EstimateTransferTokensGas(args, apiKey)

Arguments

Parameter
Type
Definition
args.to
String
Address to send tokens to.
args.amount
String
Amount to send in display units of the currency. So if you want the user to pay 0.5 ETH, pass in '0.5' to this field.
args.currency
Asset (class)
Asset object of the currency you want to send. You can pass a custom token here as well, please refer to the example below.
apiKey
String
The API Key generated from the Developer Portal

Return Value

{
"statusCode": 1/0,
"data": {
"estimatedGas": "estimatedGas", // the gas limit of the transaction in BigNumber
"estimatedGasPrice": "estimatedGasPrice" // the gas price of the transaction in BigNumber
}
"error": "error" // error if any
}

Handling Large Numerical Values

The result of the estimation is returned in the BigNumber format. A conversion will need to be made to display the result in a human-readable format, and the following function can be used:
ConvertBigNumberToNumber(bigNumber, decimal) - Use this method while converting response values from a smart contract function call (which are in BigNumber format) to a JS Number.

Example

Calling TransferTokens for a native currency:
1
import {
2
TransferTokens,
3
EstimateTransferTokensGas,
4
ConvertBigNumberToNumber,
5
assets
6
} from '@metafi/metafi-js-package';
7
8
function App() {
9
10
const tx = {
11
to: "0xd4594dECd0ed8BA4C7d5810dbB8D004C74250BD5",
12
amount: "1",
13
currency: assets.matic_matic, // use your custom token object here
14
}
15
16
// estimate gas fees
17
const estimateGas = () => {
18
const result = await EstimateTransferTokensGas(
19
tx,
20
"YOUR-API-KEY",
21
);
22
23
console.log(
24
"gas fees are: ",
25
ConvertBigNumberToNumber(
26
result.data.estimatedGas.mul(result.data.estimatedGasPrice)
27
)
28
);
29
}
30
31
// transfer tokens
32
const transferTokens = () => {
33
TransferTokens({
34
...tx,
35
callback: (result) => {
36
console.log("callback result", result);
37
},
38
},
39
"YOUR-API-KEY"
40
);
41
}
42
43
return (
44
<div>
45
<button onClick={estimateGas}>Estimate Gas Fees</button>
46
<button onClick={transferTokens}>Transfer 1 Matic</button>
47
</div>
48
);
49
}
50
51
export default App;
52
Last modified 3mo ago