Skip to content

Add OPG token Permit2 approval utilities for x402 payments#8

Merged
adambalogh merged 2 commits intomainfrom
claude/check-opg-approval-workflow-weK8b
May 5, 2026
Merged

Add OPG token Permit2 approval utilities for x402 payments#8
adambalogh merged 2 commits intomainfrom
claude/check-opg-approval-workflow-weK8b

Conversation

@adambalogh
Copy link
Copy Markdown
Contributor

@adambalogh adambalogh commented May 5, 2026

Summary

This PR adds utilities for managing OPG token Permit2 approvals on Base mainnet, enabling seamless integration with x402 payment infrastructure. The new module provides a high-level API for ensuring sufficient allowances with minimal transaction overhead.

Key Changes

  • New opgToken.ts module with Permit2 approval management:

    • ensureOpgApproval() - Main function that checks and maintains minimum Permit2 allowance thresholds
    • Helper functions for reading allowances, balances, and sending approval transactions
    • Utility functions for converting between OPG decimal amounts and base units
  • Exported public API:

    • ensureOpgApproval() - Idempotent approval function for backend services
    • BASE_OPG_ADDRESS - OPG token contract address on Base
    • BASE_MAINNET_RPC - Base mainnet RPC endpoint (configurable via env var)
    • Permit2ApprovalResult - Type for approval operation results
  • Updated index.ts to export new OPG approval utilities

Notable Implementation Details

  • Smart approval buffering: Defaults to approving 2x the minimum threshold to reduce transaction frequency across service restarts
  • Robust confirmation handling: Polls allowance state after transaction confirmation to ensure visibility before returning
  • Balance validation: Checks wallet balance before approval and caps approval amount if needed
  • Precision-safe conversion: Uses string arithmetic for OPG amount conversions to avoid floating-point precision loss
  • Configurable RPC: Supports BASE_MAINNET_RPC environment variable for custom RPC endpoints
  • Comprehensive error handling: Provides clear error messages for insufficient balance, invalid amounts, and transaction failures

claude added 2 commits May 5, 2026 15:39
Port ensureOpgApproval from the Python SDK (src/opengradient/client/opg_token.py).
Checks the Permit2 allowance for the OPG token on Base mainnet and only
sends an approve tx when the current allowance falls below minAllowance,
approving approveAmount (default 2x minAllowance) so subsequent restarts
are free.

Uses viem and the PERMIT2_ADDRESS constant re-exported from @x402/evm.
Mirrors the OPG Token Approval section in the Python SDK README so
users discover the approval step before their first LLM call.
@adambalogh adambalogh merged commit e271f9c into main May 5, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants