CallGaslessFunction
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.
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, the developer. 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.
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 project 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.For the functions that you would like to use Gasless transactions on, replace
msg.sender()
statements in your contract with_msgSender()
, andmsg.data()
with_msgData()
as per the EIPS docs linked above. - 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 |
async CallGasslessFunction(args, apiKey)
Parameter | Type | Definition |
---|---|---|
args.callback? | Function | Function to callback upon getting result from the smart contract function call
For numerical return values, please refer to the section below.
Structure of result object passed into callback function: 1 { 2 "status": 1/0, // success or fail 3 "data": "data", // the transaction object 4 "error": "error", // error string if any 5 } |
args.contractAddress | String | Contract address of the smart contract to be called |
args.functionABI | JSON | |
args.functionName | String | Name of the function to be called |
args.value? | BigNumber | The value to be sent along with the smart contract invocation. Please refer to the section below for more information. |
args.chain | chain (class) | |
args.params | array | 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.
For large numerical inputs, please refer to the section below. |
apiKey | String |
None
Applicable to EVM Chains only
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 numbersConvertBigNumberToNumber(bigNumber, decimal)
- Use this method while converting response values from a smart contract function call (which are in bigNumber format) to a JS Number1
import stakeContractAbi from './contracts/abi/stakeContract.json'
2
import {
3
CallGaslessFunction,
4
ConvertNumberToBigNumber,
5
chains
6
} from '@metafi/metafi-js-package';
7
8
function App() {
9
const stakeTokensGasless = () => {
10
CallGaslessFunction({
11
chain: chains.eth,
12
contractAddress: "0xF5C9F957705bea56a7e806943f98F7877B995826",
13
functionABI: stakeContractAbi.abi,
14
functionName: "stake",
15
value: ConvertNumberToBigNumber(1, 18),
16
params: [],
17
callback: (res) => {
18
console.log(res);
19
}
20
},
21
"YOUR-API-KEY"
22
);
23
}
24
25
return (
26
<div>
27
<button onClick={stakeTokensGasless}>stakeTokensGasless</button>
28
</div>
29
);
30
}
31
32
export default App;
33
Last modified 3mo ago