# EstimateTransferTokensGas

## 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  [`TransferTokens`](https://docs.usemeta.fi/js-sdk/sdk-reference/transfertokens) invocation. The information can be presented to users before they confirm the transaction.

## Details

### Signature

`async EstimateTransferTokensGas(args, apiKey)`

### Arguments

<table><thead><tr><th width="258">Parameter</th><th width="114">Type</th><th>Definition</th></tr></thead><tbody><tr><td><code>args.to</code></td><td>String</td><td>Address to send tokens to.</td></tr><tr><td><code>args.amount</code></td><td>String</td><td>Amount to send in <strong>display units</strong> of the currency. So if you want the user to pay 0.5 ETH, pass in '0.5' to this field. </td></tr><tr><td><code>args.currency</code></td><td>Asset (class)</td><td>Asset object of the currency you want to send. You can pass a custom token here as well, please refer to the example below.</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
    }
    "error": "error" // error if any
}

```

### Handling Large Numerical Values

The result of the estimation is returned in the [BigNumber](https://docs.ethers.org/v5/api/utils/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:

{% code lineNumbers="true" %}

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

function App() {

    const tx = {
        to: "0xd4594dECd0ed8BA4C7d5810dbB8D004C74250BD5",
        amount: "1",
        currency: assets.matic_matic, // use your custom token object here
    }

    // estimate gas fees
    const estimateGas = () => {
        const result = await EstimateTransferTokensGas(
            tx,
            "YOUR-API-KEY",
        );
        
        console.log(
            "gas fees are: ", 
            ConvertBigNumberToNumber(
                result.data.estimatedGas.mul(result.data.estimatedGasPrice)
            )
        );
    }
    
    // transfer tokens
    const transferTokens = () => {
        TransferTokens({
                ...tx,
                callback: (result) => {
                    console.log("callback result", result);
                },
            },
            "YOUR-API-KEY"
        );
    }
    
    return (
        <div>
            <button onClick={estimateGas}>Estimate Gas Fees</button>
	    <button onClick={transferTokens}>Transfer 1 Matic</button>
        </div>
    );
}

export default App;

```

{% endcode %}
