Skip to content

Latest commit

 

History

History
246 lines (185 loc) · 7.13 KB

PAYMASTER_COMPATIBILITY.md

File metadata and controls

246 lines (185 loc) · 7.13 KB

Paymaster Compatibility with TriviaBattle Contract

✅ Yes, Paymaster is Fully Compatible!

Your TriviaBattle contract is 100% compatible with paymasters (Base Account Paymaster, CDP Paymaster, etc.). All security improvements (SafeERC20, CEI pattern, ReentrancyGuard) work seamlessly with paymaster sponsorship.

Functions That Can Use Paymaster

✅ User Functions (Should be in Paymaster Allowlist)

These functions are called by users and should be sponsored by paymaster:

1. USDC Contract Functions (Required in Allowlist)

Contract: USDC Token Address
- approve(address spender, uint256 amount)

2. TriviaBattle Contract Functions (Required in Allowlist)

Contract: Your TriviaBattle Contract Address
- joinBattle()                    // Paid players join (uses USDC transferFrom)
- joinTrialBattle(string)         // Trial players join (no USDC, but can be gasless)
- submitScore(uint256)            // Submit game score
- submitTrialScore(string, uint256) // Submit trial score
- claimWinnings()                 // Claim prize winnings (uses USDC transfer)

❌ Owner Functions (Do NOT Need Paymaster)

These functions are called by the contract owner and should NOT be in the allowlist:

- startSession(uint256)           // Owner only
- distributePrizes()              // Owner/Chainlink only
- updatePlatformFeeRecipient(address) // Owner only
- setChainlinkForwarder(address)  // Owner only
- emergencyWithdraw()             // Owner only
- setTimeLockDelay(uint256)       // Owner only

Paymaster Allowlist Configuration

Required Allowlist Entries

Add these to your paymaster allowlist in the CDP Dashboard:

  1. USDC Token Contract

    • Address: 0x833589fcd6edb6e08f4c7c32d4f71b54bda02913 (Base Mainnet)
    • Function: approve(address,uint256)
  2. TriviaBattle Contract

    • Address: YOUR_DEPLOYED_CONTRACT_ADDRESS
    • Functions:
      • joinBattle()
      • joinTrialBattle(string)
      • submitScore(uint256)
      • submitTrialScore(string,uint256)
      • claimWinnings()

How to Add to Allowlist

  1. Go to CDP Dashboard
  2. Navigate to Paymaster → Contract Allowlist
  3. Add each contract address and function signature
  4. Save changes

Contract Compatibility Features

✅ SafeERC20 Compatibility

The contract uses SafeERC20 which is fully compatible with paymasters:

  • ✅ Handles non-standard ERC20 tokens (like USDC)
  • ✅ Detects paused tokens and reverts appropriately
  • ✅ Works with paymaster-sponsored transactions

✅ ReentrancyGuard Compatibility

The nonReentrant modifier works correctly with paymasters:

  • ✅ Prevents reentrancy attacks
  • ✅ Compatible with paymaster transaction flow
  • ✅ No conflicts with paymaster execution

✅ CEI Pattern Compatibility

The Checks-Effects-Interactions pattern is maintained:

  • ✅ State updates happen before external calls
  • ✅ Paymaster transactions follow the same pattern
  • ✅ No issues with paymaster sponsorship

Gasless Transaction Flow

Example: joinBattle() with Paymaster

// User calls joinBattle() - paymaster sponsors gas
const tx = await contract.joinBattle({
  // Paymaster automatically sponsors if:
  // 1. Contract is in allowlist
  // 2. Function is in allowlist
  // 3. Paymaster has sufficient balance
});

// Transaction flow:
// 1. User signs transaction
// 2. Paymaster validates (checks allowlist)
// 3. Paymaster sponsors gas fees
// 4. Contract executes:
//    - Updates state (prizePool, playerCount, etc.)
//    - Calls usdcToken.safeTransferFrom() (also in allowlist)
// 5. User pays 1 USDC entry fee (not gas!)

Example: claimWinnings() with Paymaster

// User claims winnings - paymaster sponsors gas
const tx = await contract.claimWinnings({
  // Paymaster sponsors the gas
  // User receives USDC winnings
  // No gas cost to user!
});

Important Considerations

1. USDC Transfer Functions Must Be in Allowlist

Both the USDC contract's approve() and the TriviaBattle contract's functions that use safeTransferFrom() need to be in the allowlist:

  • ✅ USDC: approve(address,uint256)
  • ✅ TriviaBattle: joinBattle() (calls safeTransferFrom internally)
  • ✅ TriviaBattle: claimWinnings() (calls safeTransfer internally)

2. Paymaster Balance

Ensure your paymaster has sufficient balance to sponsor transactions:

  • Monitor in CDP Dashboard
  • Top up as needed
  • Set spending limits if desired

3. Gas Limits

The contract functions have reasonable gas limits:

  • joinBattle(): ~100,000 gas (typical)
  • submitScore(): ~50,000 gas (typical)
  • claimWinnings(): ~80,000 gas (typical)

Ensure your paymaster can handle these gas costs.

4. Batch Transactions

You can batch multiple operations in a single paymaster-sponsored transaction:

// Batch: approve + joinBattle
const calls = [
  {
    to: USDC_ADDRESS,
    data: encodeFunctionData({
      abi: usdcAbi,
      functionName: 'approve',
      args: [CONTRACT_ADDRESS, ENTRY_FEE]
    })
  },
  {
    to: CONTRACT_ADDRESS,
    data: encodeFunctionData({
      abi: triviaAbi,
      functionName: 'joinBattle'
    })
  }
];

// Single paymaster-sponsored transaction
await wallet.sendCalls({ calls });

Testing Paymaster Integration

1. Verify Allowlist

// Check if contract is in allowlist
const isAllowed = await paymaster.isContractAllowed(CONTRACT_ADDRESS);
console.log('Contract in allowlist:', isAllowed);

2. Test Gasless Transaction

// Try joining battle with paymaster
try {
  const tx = await contract.joinBattle();
  console.log('✅ Paymaster sponsored transaction:', tx.hash);
} catch (error) {
  if (error.message.includes('paymaster')) {
    console.error('❌ Paymaster issue - check allowlist');
  }
}

3. Monitor Paymaster Usage

  • Check CDP Dashboard for sponsored transactions
  • Monitor gas costs
  • Track paymaster balance

Security Notes

✅ Paymaster Security

The contract's security features work correctly with paymasters:

  • ReentrancyGuard: Prevents attacks even with paymaster
  • SafeERC20: Handles USDC transfers safely
  • CEI Pattern: Maintains state consistency
  • Access Control: Owner functions remain protected

⚠️ Paymaster Trust

Remember that paymaster sponsorship requires trust in the paymaster service:

  • Paymaster can see transaction details
  • Paymaster controls gas sponsorship
  • Use reputable paymaster services (Coinbase CDP, etc.)

Summary

✅ Your contract is fully paymaster-compatible!

To enable paymaster:

  1. Add USDC approve() to allowlist
  2. Add TriviaBattle user functions to allowlist
  3. Ensure paymaster has sufficient balance
  4. Test with a small transaction first

Functions that benefit from paymaster:

  • joinBattle() - Users join games gaslessly
  • joinTrialBattle() - Trial players join gaslessly
  • submitScore() - Score submissions gasless
  • claimWinnings() - Prize claims gasless

Functions that DON'T need paymaster:

  • Owner/admin functions (owner pays gas)
  • Chainlink automation (sponsored separately)

Your contract is ready for production with paymaster support! 🚀