# CallGenericWriteFunctionTest

## Description

This function can be used to estimate the [Gas Limit and Gas Price](https://ethereum.org/en/developers/docs/gas/) that will be required by a  [`CallGenericWriteFunction`](https://docs.usemeta.fi/js-sdk/sdk-reference/callgenericwritefunction) invocation. The information can be presented to users before they confirm the transaction.

## Details

### Signature

`async CallGenericWriteFunctionTest(args, apiKey)`

### Arguments

<table><thead><tr><th width="258">Parameter</th><th width="128">Type</th><th>Definition</th></tr></thead><tbody><tr><td><code>args.contractAddress</code></td><td>String</td><td>Contract address of the smart contract to be called</td></tr><tr><td><code>args.functionABI</code></td><td>JSON</td><td>The JSON object for the <a href="https://ethereum.org/en/developers/docs/smart-contracts/compiling/#web-applications">smart contract ABI</a></td></tr><tr><td><code>args.functionName</code></td><td>String</td><td>Name of the function to be called</td></tr><tr><td><code>args.value?</code></td><td>BigNumber</td><td>The value to be sent along with the smart contract invocation. Please refer to the <a href="#handling-numerical-values">section below</a> for more information.</td></tr><tr><td><code>args.chain</code></td><td>chain (class)</td><td>Which chain to broadcast the transaction on. Pick a chain from the <a href="../constants/chain">supported chains list</a></td></tr><tr><td><code>args.params</code></td><td>array</td><td>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.<br><br>For large numerical inputs, please refer to the <a href="#handling-numerical-values">section below</a>.</td></tr><tr><td><code>apiKey</code></td><td>String</td><td>The API Key generated from the <a href="https://developer-test.usemeta.fi/">Developer Portal</a></td></tr></tbody></table>

### Return Value

```json
{
    "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
}

```

### Handling Large Numerical Values

{% hint style="warning" %}
Applicable to EVM Chains only
{% endhint %}

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 numbers

`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

{% code lineNumbers="true" %}

```jsx
import { 
    TransferTokens,
    EstimateTransferTokensGas,
    CallGenericWriteFunctionTest,
    ConvertBigNumberToNumber,
    assets
} from '@metafi/metafi-js-package';

function App() {

    // estimate gas fees
    const estimateGas = () => {
        const result = await CallGenericWriteFunctionTest(
            {
                chain: chains.eth,             
                contractAddress: "0xF5C9F957705bea56a7e806943f98F7877B995826",   
                functionABI: stakeContractAbi.abi,   
                functionName: "stake",
                value: ConvertNumberToBigNumber(1, 18),
                params: [],
            },
            "YOUR-API-KEY",
        );
        
        console.log(
            "gas fees are: ", 
            ConvertBigNumberToNumber(
                result.data.estimatedGas.mul(result.data.estimatedGasPrice)
            )
        );
    }
    
    // transfer tokens
    const stakeTokens = () => {
        CallGenericWriteFunction({
                chain: chains.eth,             
                contractAddress: "0xF5C9F957705bea56a7e806943f98F7877B995826",   
                functionABI: stakeContractAbi.abi,   
                functionName: "stake",
                value: ConvertNumberToBigNumber(1, 18),
                params: [],
                callback: (res) => {
                    console.log(res);
                }
            },
            "YOUR-API-KEY"
        );
    }
    
    return (
        <div>
            <button onClick={estimateGas}>Estimate Gas Fees</button>
	    <button onClick={stakeTokens}>stakeTokens</button>
        </div>
    );
}

export default App;

```

{% endcode %}
