payment

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package payment implements blockchain payment services for USDC on EVM chains.

Index

Constants

View Source
const (
	DefaultBaseFeeWei        = 1_000_000_000 // 1 gwei
	DefaultMaxPriorityFeeWei = 1_500_000_000 // 1.5 gwei
	BaseFeeMultiplier        = 2
	EthAddressLength         = 42
)

Gas fee defaults for EIP-1559 transactions.

View Source
const DefaultHistoryLimit = 20

DefaultHistoryLimit is the default number of transactions returned by History.

View Source
const DefaultMaxRetries = 3

DefaultMaxRetries is the default number of transaction submission attempts.

View Source
const DefaultReceiptTimeout = 2 * time.Minute

DefaultReceiptTimeout is the maximum time to wait for on-chain confirmation.

Variables

View Source
var BalanceOfSelector = []byte{0x70, 0xa0, 0x82, 0x31}

BalanceOfSelector is the function selector for balanceOf(address).

View Source
var ERC20TransferMethodID = crypto.Keccak256([]byte("transfer(address,uint256)"))[:4]

ERC20TransferMethodID is the function selector for transfer(address,uint256).

Functions

func ValidateAddress

func ValidateAddress(addr string) error

ValidateAddress checks if a string is a valid Ethereum address.

Types

type PaymentReceipt

type PaymentReceipt struct {
	TxHash      string    `json:"txHash"`
	Status      string    `json:"status"`
	Amount      string    `json:"amount"`
	From        string    `json:"from"`
	To          string    `json:"to"`
	ChainID     int64     `json:"chainId"`
	GasUsed     uint64    `json:"gasUsed,omitempty"`
	BlockNumber uint64    `json:"blockNumber,omitempty"`
	Timestamp   time.Time `json:"timestamp"`
}

PaymentReceipt is returned after a payment is confirmed on-chain.

type PaymentRequest

type PaymentRequest struct {
	// To is the recipient wallet address.
	To string `json:"to"`

	// Amount is the USDC amount as a decimal string (e.g. "1.50").
	Amount string `json:"amount"`

	// Purpose is a human-readable description of the payment.
	Purpose string `json:"purpose,omitempty"`

	// SessionKey is the agent session that initiated the payment.
	SessionKey string `json:"sessionKey,omitempty"`

	// X402URL is the URL that triggered an X402 payment (if applicable).
	X402URL string `json:"x402Url,omitempty"`

	// PaymentMethod indicates how the payment was made ("direct_transfer" or "x402_v2").
	PaymentMethod string `json:"paymentMethod,omitempty"`
}

PaymentRequest describes a payment to be sent.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service orchestrates blockchain payment operations.

func NewService

func NewService(
	wp wallet.WalletProvider,
	limiter wallet.SpendingLimiter,
	builder *TxBuilder,
	client *ent.Client,
	rpcClient *ethclient.Client,
	chainID int64,
) *Service

NewService creates a payment service.

func (*Service) Balance

func (s *Service) Balance(ctx context.Context) (string, error)

Balance returns the wallet's USDC balance as a formatted string.

func (*Service) ChainID

func (s *Service) ChainID() int64

ChainID returns the configured chain ID.

func (*Service) History

func (s *Service) History(ctx context.Context, limit int) ([]TransactionInfo, error)

History returns recent payment transactions.

func (*Service) RecordX402Payment

func (s *Service) RecordX402Payment(ctx context.Context, record X402PaymentRecord) error

RecordX402Payment records an X402 automatic payment for audit trail. Unlike Send(), this does not build or submit a transaction — the SDK handles payment signing. This only creates the database record for tracking.

func (*Service) Send

func (s *Service) Send(ctx context.Context, req PaymentRequest) (*PaymentReceipt, error)

Send executes a payment: limit check → build tx → sign → submit → record.

func (*Service) WalletAddress

func (s *Service) WalletAddress(ctx context.Context) (string, error)

WalletAddress returns the wallet's public address.

type TransactionInfo

type TransactionInfo struct {
	TxHash        string    `json:"txHash,omitempty"`
	Status        string    `json:"status"`
	Amount        string    `json:"amount"`
	From          string    `json:"from"`
	To            string    `json:"to"`
	ChainID       int64     `json:"chainId"`
	Purpose       string    `json:"purpose,omitempty"`
	X402URL       string    `json:"x402Url,omitempty"`
	PaymentMethod string    `json:"paymentMethod,omitempty"`
	ErrorMessage  string    `json:"errorMessage,omitempty"`
	CreatedAt     time.Time `json:"createdAt"`
}

TransactionInfo combines a receipt with contextual information.

type TxBuilder

type TxBuilder struct {
	// contains filtered or unexported fields
}

TxBuilder constructs ERC-20 transfer transactions.

func NewTxBuilder

func NewTxBuilder(client *ethclient.Client, chainID int64, usdcContract string) *TxBuilder

NewTxBuilder creates a transaction builder for the given chain.

func (*TxBuilder) BuildTransferTx

func (b *TxBuilder) BuildTransferTx(ctx context.Context, from common.Address, to common.Address, amount *big.Int) (*types.Transaction, error)

BuildTransferTx constructs an EIP-1559 ERC-20 transfer transaction. Returns the sighash (transaction hash to sign) and the unsigned transaction.

func (*TxBuilder) USDCContract

func (b *TxBuilder) USDCContract() common.Address

USDCContract returns the configured USDC contract address.

type X402PaymentRecord

type X402PaymentRecord struct {
	URL     string `json:"url"`
	Amount  string `json:"amount"`
	From    string `json:"from"`
	To      string `json:"to"`
	ChainID int64  `json:"chainId"`
}

X402PaymentRecord captures an X402 automatic payment for audit trail.

Directories

Path Synopsis
Package contracts provides canonical USDC contract addresses and on-chain verification utilities for supported EVM chains.
Package contracts provides canonical USDC contract addresses and on-chain verification utilities for supported EVM chains.
Package eip3009 implements EIP-3009 transferWithAuthorization typed data building and signing for USDC gasless transfers.
Package eip3009 implements EIP-3009 transferWithAuthorization typed data building and signing for USDC gasless transfers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL