Xai Gas Subsidy
XAI Gas Subsidy
The XAI Gas Subsidy uses the ERC-2771 standard for secure protocol for native meta transactions. With the XAI Gas Subsidy XAI can sponsor your user's transactions so they don't have to own native XAI.
How the XAI Gas Subsidy works
Instead of using a contract write function, the user will sign a typed data message (EIP-712) with the message implementing the ERC-2771 meta-transaction
. This means when you use the XAI Gas Subsidy your user does not call the contract directly, instead the XAI relayer will forward to your smart contract.
Relayer Endpoints:
Xai Mainnet Relayer: https://relayer.xai.games/ Xai Mainnet API Docs: https://docs.xai.games/gas-relayer/#/relayer
Xai Testnet Relayer: https://develop.relayer.xai.games/ Xai Testnet API Docs: https://develop.docs.xai.games/gas-relayer/#/relayer
Requirements
To use the XAI Gas Subsidy your contracts need to follow the ERC-2771 _msgSender() overwrite and make sure to only use the _msgSender() when updating a user's state.
The ERC-2771 requires the validation for a trusted forwarder contract when interacting on behalf of a user. The trusted forwarder contract for your ecosystem's contracts should be owned by you. The trusted forwarder contract will be able to execute transactions on behalf of your user's wallet.
Interacting with ERC-20 tokens while using the Xai subsidy requires implementing the ERC-2771 standard. Gasless approvals can be achieved by implementing the ERC-2612 Permit Extension. Any ERC-20 contracts that don't either implement ERC-2771 or ERC-2612 can not be used for sponsored transaction.
FOR ALL INTERACTIONS ALLOWING THE FORWARDER, DO NOT USE msg.sender
IN YOUR CONTRACTS, YOU MUST USE _msgSender()
or your contract's state will not be updated properly
For testing purposes there is a Xai mainnet and testnet forwarder deployed:
Xai mainnet Forwarder: 0x4AfA378FDDc9829B1D40655ED5EDBCC31404dee6 https://explorer.xai-chain.net/address/0x4AfA378FDDc9829B1D40655ED5EDBCC31404dee6/contracts#address-tabs
Xai testnet Forwarder: 0x31635b37347258d7c6f83bDC664c5516Da8e6fD7 https://testnet-explorer-v2.xai-chain.net/address/0x31635b37347258d7c6f83bDC664c5516Da8e6fD7/read-proxy#address-tabs
The _msgSender() overrides the default msg.sender. As the default msg.sender would be the address of the forward contract instead of the user wallet address:
The _msgDate() overrides the default msg.data:
The trusted forwarder check is important to only allow a trusted forwarder contract to call the functions on user's behalf:
How to sign meta transactions
The user needs to signs a message based on the EIP-712 sign typed message using the 2771 forward request standard:
The nonce must be the latest nonce from the forwarder for the specific user:
Now to sign this typedData we use the EIP-712 standard to get the signature. We use the signTypedData() from wagmi/core:
How to forward the meta-transaction to the XAI Relayer API
Use the signed request signature and the raw request body to forward the transaction and get the transaction fee sponsored by the XAI Gas Subsidy:
The request will return the transaction hash as a success response or an error string if the transaction would fail or the user has not enough balance left for their sponsoring with your project.
You can request the users current balance from a GET request to the XAI Relayer API https://relayer.xai.games/quota/[YOUR_PROJECT_ID]/[USER_WALLET]
This will return the user's quota Object:
Get a Xai Subsidy ProjectID
Contact us on our Discord or Telegram channels to get your ProjectID.
Last updated