CallGaslessFunction

This function triggers a modal for the user to confirm the transaction

This is an advanced feature and can drain user wallet funds if you interact with a malicious contract. Only link to trusted smart contract authors.

Description

This function behaves similar to the CallGenericWriteFunction, 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.

Metafi follows the ERC2771 Standard for relaying gasless transactions. For more information on the gasless transaction standard, please refer to the official EIPS Docs.

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 (This is based on OpenZeppelin's ERC2771Context contract, 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

Chain
Forwarder Contract Address

Ethereum Mainnet

TBD

Goerli

0x04f1e3E36667e747DB9878c2c677E291DD942701

Matic Mainnet

TBD

Matic Mumbai

0xD7aD34eEbDE6cB057A11F3f824260799961Ab01a

Signature

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

Arguments

Parameter
Type
Definition

callback?

Action<dynamic>

args.contractAddress

String

Contract address of the smart contract to be called

args.functionABI

String

args.functionName

String

Name of the function to be called as per the ABI.

args.value?

String

args.chain

Metafi.Unity.Chain

args.params

List<object>

Return Value

None

Handling Large Numerical Values

Applicable to EVM Chains only

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:

// 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

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());
        })));
    }
}

Last updated