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:

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:

function _msgSender() internal view returns (address) {
    uint256 calldataLength = msg.data.length;
    if (isTrustedForwarder(msg.sender) && calldataLength >= 20) {
        return address(bytes20(msg.data[calldataLength - 20:]));
    } else {
        return super._msgSender();
    }
}

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