# CallGaslessFunction

{% hint style="info" %}
This function triggers a modal for the user to confirm the transaction
{% endhint %}

{% hint style="danger" %}
This is an advanced feature and <mark style="color:red;">can drain user wallet funds</mark> if you interact with a malicious contract. Only link to trusted smart contract authors.
{% endhint %}

## Description

This function behaves similar to the [CallGenericWriteFunction](/unity-sdk/sdk-reference/callgenericwritefunction.md), however it allows your users to make transactions without paying Gas Fees - the gas fees is instead borne by you. This is useful for users new to crypto or new to your game, since they do not need to own or purchase crypto before making transactions in your game, which reduces player churn.&#x20;

Metafi follows the ERC2771 Standard for relaying gasless transactions. For more information on the gasless transaction standard, please refer to the [official EIPS Docs](https://eips.ethereum.org/EIPS/eip-2771).

## Details

### Implementation Details

When you create an API key, a Relayer wallet will be created for you across your supported chains, which you will need to fund with crypto that will be used to pay the user gas fees. You can find the public wallet address in your dashboard settings page. Additionally, you will need to perform the following steps.

1. Make your smart contract ERC2771 compatible. You can do this by inheriting from Metafi's ERC2771Context contract that can be found [here](https://github.com/metafi-nft/openzeppelin-contracts/blob/master/contracts/metatx/ERC2771Context.sol) (This is based on OpenZeppelin's [ERC2771Context contract](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/metatx/ERC2771Context.sol), however allows for updating the list of trusted forwarders in the future)
2. Replace  `msg.sender()` statements in your contract with `_msgSender()`, and `msg.data()` with `_msgData()` as per the EIPS docs linked above, for any functions that you would like users to interact with gas-lessly.
3. Register Metafi's Forwarder Contracts as a trusted forwarder address. You can do this by calling the `setTrustedForwarder` function in the ERC2771Context contract with Metafi's forwarder addresses that have been shown below.

Metafi Forwarder Addresses

<table><thead><tr><th width="213">Chain</th><th>Forwarder Contract Address</th></tr></thead><tbody><tr><td>Ethereum Mainnet</td><td>TBD</td></tr><tr><td>Goerli</td><td><code>0x04f1e3E36667e747DB9878c2c677E291DD942701</code></td></tr><tr><td>Matic Mainnet</td><td>TBD</td></tr><tr><td>Matic Mumbai</td><td><code>0xD7aD34eEbDE6cB057A11F3f824260799961Ab01a</code></td></tr></tbody></table>

### Signature

```csharp
Task CallGaslessFunction(dynamic args, System.Action<dynamic> callback = null)
```

### Arguments

<table><thead><tr><th width="258">Parameter</th><th width="114">Type</th><th>Definition</th></tr></thead><tbody><tr><td><code>callback?</code></td><td>Action&#x3C;dynamic></td><td><p>Function to callback upon getting result from the smart contract function call<br><br>For numerical return values, please refer to the <a href="#handling-numerical-values">section below</a>.<br><br>Structure of result object passed into callback function:</p><pre class="language-json" data-line-numbers><code class="lang-json"><strong>{
</strong><strong>    "status": 1/0, // success or fail
</strong><strong>    "error": "", // error string if any
</strong><strong>    "data": {any}, // data returned from the smart contract function call
</strong><strong>}
</strong></code></pre></td></tr><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>String</td><td>The path to the JSON object for the <a href="https://ethereum.org/en/developers/docs/smart-contracts/compiling/#web-applications">smart contract ABI</a>. You can store it in the Resources folder and pass in a path such as <code>@"Assets/Resources/Contracts/Contract.json"</code></td></tr><tr><td><code>args.functionName</code></td><td>String</td><td>Name of the function to be called as per the ABI.</td></tr><tr><td><code>args.value?</code></td><td>String</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>Metafi.Unity.Chain</td><td>Which chain to broadcast the transaction on. Pick a chain from the <a href="/pages/RsPwmVr7xSQeT0vZHluX">supported chains list</a></td></tr><tr><td><code>args.params</code></td><td>List&#x3C;object></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></tbody></table>

### Return Value

None

### 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 string format needs to be made. You can use the following methods that convert the number to the appropriate format:

```csharp
// For int values
string ConvertNumberToBigNumber(int value, int decimals);

// For double values
string ConvertNumberToBigNumber(double value, int decimals);

// For float values
string ConvertNumberToBigNumber(float value, int decimals);
```

## Example

{% code lineNumbers="true" %}

```csharp
using System;
using Metafi.Unity;

public class StakeTokenButton : MonoBehaviour {
    public async void StakeTokensWithoutGas(){
        await MetafiProvider.Instance.CallGaslessFunction(new {
            contractAddress = "contract-address",
            functionABIPath = @"Assets/Resources/Contracts/path-to-contract.json",
            functionName = "stakeTokens",
            value = Utils.ConvertNumberToBigNumber(0.1, 18),
            @params = new [] {"param1", "param2"},
            chain = Chains.MUMBAI,
        },
        ((System.Action<dynamic>) (result => {
            Debug.Log("CallGaslessFunction complete, result: " + result.ToString());
        })));
    }
}
```

{% 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/unity-sdk/sdk-reference/callgaslessfunction.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.
