CallGenericWriteFunctionTest
This function can be used to estimate the Gas Limit and Gas Price that will be required by a
CallGenericWriteFunction
invocation. The information can be presented to users before they confirm the transaction.async CallGenericWriteFunctionTest(args, apiKey)
Parameter | Type | Definition |
---|---|---|
args.contractAddress | String | Contract address of the smart contract to be called |
args.functionABI | JSON | |
args.functionName | String | Name of the function to be called |
args.value? | BigNumber | The value to be sent along with the smart contract invocation. Please refer to the section below for more information. |
args.chain | chain (class) | |
args.params | array | Array of objects that will be sent as the arguments to the smart contract function. Please ensure that the order of params matches the contract ABI.
For large numerical inputs, please refer to the section below. |
apiKey | String |
{
"statusCode": 1/0,
"data": {
"estimatedGas": "estimatedGas", // the gas limit of the transaction in BigNumber
"estimatedGasPrice": "estimatedGasPrice" // the gas price of the transaction in BigNumber
"testExecutionResult": "testExecutionResult" // any return values from the smart contract
}
"error": "error" // error if any
}
Applicable to EVM Chains only
While passing in large numerical values (values above 9007199254740991) in
args.params
, a conversion to BigNumber format needs to be made. You can use the following methods that convert the number to and from the appropriate format:ConvertNumberToBigNumber(number, decimal)
- Use this method while passing in numbers as params to a smart function that requires uint256 numbersConvertBigNumberToNumber(bigNumber, decimal)
- Use this method while converting response values from a smart contract function call (which are in bigNumber format) to a JS Number1
import {
2
TransferTokens,
3
EstimateTransferTokensGas,
4
CallGenericWriteFunctionTest,
5
ConvertBigNumberToNumber,
6
assets
7
} from '@metafi/metafi-js-package';
8
9
function App() {
10
11
// estimate gas fees
12
const estimateGas = () => {
13
const result = await CallGenericWriteFunctionTest(
14
{
15
chain: chains.eth,
16
contractAddress: "0xF5C9F957705bea56a7e806943f98F7877B995826",
17
functionABI: stakeContractAbi.abi,
18
functionName: "stake",
19
value: ConvertNumberToBigNumber(1, 18),
20
params: [],
21
},
22
"YOUR-API-KEY",
23
);
24
25
console.log(
26
"gas fees are: ",
27
ConvertBigNumberToNumber(
28
result.data.estimatedGas.mul(result.data.estimatedGasPrice)
29
)
30
);
31
}
32
33
// transfer tokens
34
const stakeTokens = () => {
35
CallGenericWriteFunction({
36
chain: chains.eth,
37
contractAddress: "0xF5C9F957705bea56a7e806943f98F7877B995826",
38
functionABI: stakeContractAbi.abi,
39
functionName: "stake",
40
value: ConvertNumberToBigNumber(1, 18),
41
params: [],
42
callback: (res) => {
43
console.log(res);
44
}
45
},
46
"YOUR-API-KEY"
47
);
48
}
49
50
return (
51
<div>
52
<button onClick={estimateGas}>Estimate Gas Fees</button>
53
<button onClick={stakeTokens}>stakeTokens</button>
54
</div>
55
);
56
}
57
58
export default App;
59
Last modified 3mo ago