Native Token Handling
MetaTxGateway supports meta-transactions that require native tokens (ETH/BNB). The contract ensures the correct amount is provided and refunds any unused value.
How It Works
Each
MetaTransaction
in the batch can specify avalue
(amount of native token to send).The relayer must send exactly the total required value (
sum(metaTx.value)
for all transactions) asmsg.value
.If some transactions fail, the unused value is refunded to the user.
Example
Suppose you want to batch two meta-transactions:
First sends 0.1 ETH to an address.
Second calls a contract with no ETH.
const metaTxs = [
{
to: recipientAddress,
value: ethers.utils.parseEther('0.1'),
data: '0x'
},
{
to: contractAddress,
value: 0,
data: contract.interface.encodeFunctionData('doSomething', [arg])
}
];
const requiredValue = await gateway.calculateRequiredValue(metaTxs);
const tx = await gateway.executeMetaTransactions(
userAddress,
metaTxs,
signature,
nonce,
deadline,
{ value: requiredValue }
);
Refunds
If a transaction fails, its
value
is not spent.After execution, any unused value is refunded to the user (
from
address).The
NativeTokenUsed
event logs the required, used, and refunded amounts.
Security
The contract checks that
msg.value
matches the total required value.No overpayment or underpayment is allowed.
Refunds are automatic and cannot be intercepted by relayers.
Best Practices
For Developers
Always validate value amounts before execution
Implement proper refund logic for failed transactions
Use reentrancy guards on all value-handling functions
Check contract balance consistency regularly
Handle edge cases like zero values and failed transfers
For Users
Monitor the balance of native tokens of Relayer for better execution guarantees
Monitor your credits to cover costs of native tokens in the gateway
Be aware of gas costs when using native token features
Test with small amounts before large transactions
Keep backup funds for emergency withdrawals
For Relayers
Calculate value requirements accurately before execution
Implement retry logic for failed value transfers
Monitor contract balance for anomalies
Use batch operations to optimize gas costs
Implement proper accounting for fronted values
Gas Considerations
Native token handling adds gas overhead:
Simple value transfer: +2,100 gas (native send)
Contract call with value: +2,100 + call overhead
Refund operations: +21,000 gas per refund
Balance checks: +2,100 gas per check
Plan your gas limits accordingly when using native token features.
Related Topics:
Batch Processing - Combining multiple value transfers
EIP-712 Signatures - Signing value transactions
MetaTxGateway Overview - Main contract documentation
Last updated