# EstimateTransferTokensGas

## Description

This function can be used to estimate the [Gas Limit and Gas Price](https://ethereum.org/en/developers/docs/gas/#:~:text=Gas%20limit%20refers%20to%20the,of%2021%2C000%20units%20of%20gas.) that will be required by a  [`TransferTokens`](/js-sdk/sdk-reference/transfertokens.md) 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.usemeta.fi/js-sdk/sdk-reference/estimatetransfertokensgas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
