Mechanisms

mechanisms/evm/exact/server

github.com/x402-foundation/x402/go/v2/mechanisms/evm/exact/server

import "github.com/x402-foundation/x402/go/v2/mechanisms/evm/exact/server"

Constants

Server error constants for the exact EVM scheme (V2)

Source: mechanisms/evm/exact/server/errors.go:5

const (
	ErrAmountMustBeString    = "invalid_exact_evm_server_amount_must_be_string"
	ErrAssetAddressRequired  = "invalid_exact_evm_server_asset_address_required"
	ErrFailedToParsePrice    = "invalid_exact_evm_server_failed_to_parse_price"
	ErrUnsupportedPriceType  = "invalid_exact_evm_server_unsupported_price_type"
	ErrFailedToConvertAmount = "invalid_exact_evm_server_failed_to_convert_amount"
	ErrNoAssetSpecified      = "invalid_exact_evm_server_no_asset_specified"
	ErrFailedToParseAmount   = "invalid_exact_evm_server_failed_to_parse_amount"
	ErrInvalidPayToAddress   = "invalid_exact_evm_server_invalid_payto_address"
	ErrAmountRequired        = "invalid_exact_evm_server_amount_required"
	ErrInvalidAmount         = "invalid_exact_evm_server_invalid_amount"
	ErrInvalidAsset          = "invalid_exact_evm_server_invalid_asset"
	ErrInvalidTokenAmount    = "invalid_exact_evm_server_invalid_token_amount"
)

Functions

func NewExactEvmScheme() *ExactEvmScheme

Source: mechanisms/evm/exact/server/scheme.go:22

NewExactEvmScheme creates a new ExactEvmScheme

Types

type ExactEvmScheme

Source: mechanisms/evm/exact/server/scheme.go:17

ExactEvmScheme implements the SchemeNetworkServer interface for EVM exact payments (V2)

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

func ConvertFromTokenAmount(tokenAmount string, network string) (string, error)

Source: mechanisms/evm/exact/server/scheme.go:366

ConvertFromTokenAmount converts from token smallest unit to decimal

func ConvertToTokenAmount(decimalAmount string, network string) (string, error)

Source: mechanisms/evm/exact/server/scheme.go:351

ConvertToTokenAmount converts a decimal amount to token smallest unit

func EnhancePaymentRequirements( ctx context.Context, requirements types.PaymentRequirements, supportedKind types.SupportedKind, extensionKeys []string, ) (types.PaymentRequirements, error)

Source: mechanisms/evm/exact/server/scheme.go:234

EnhancePaymentRequirements adds scheme-specific enhancements to V2 payment requirements

func GetAssetDecimals(asset string, network x402.Network) int

Source: mechanisms/evm/exact/server/scheme.go:35

GetAssetDecimals implements AssetDecimalsProvider. Returns the decimal precision for the given asset on the given network, falling back to 6 if the asset is not recognized.

func GetDisplayAmount(amount string, network string, asset string) (string, error)

Source: mechanisms/evm/exact/server/scheme.go:298

GetDisplayAmount formats an amount for display

func GetSupportedNetworks() []string

Source: mechanisms/evm/exact/server/scheme.go:381

GetSupportedNetworks returns the list of supported networks

func ParsePrice(price x402.Price, network x402.Network) (x402.AssetAmount, error)

Source: mechanisms/evm/exact/server/scheme.go:88

ParsePrice parses a price string and converts it to an asset amount (V2) If price is already an AssetAmount, returns it directly. If price is Money (string | number), parses to decimal and tries custom parsers. Falls back to default conversion if all custom parsers return nil.

Args:

price: The price to parse (can be string, number, or AssetAmount map)
network: The network identifier

Returns:

AssetAmount with amount, asset, and optional extra fields

func RegisterMoneyParser(parser x402.MoneyParser) *ExactEvmScheme

Source: mechanisms/evm/exact/server/scheme.go:70

RegisterMoneyParser registers a custom money parser in the parser chain. Multiple parsers can be registered - they will be tried in registration order. Each parser receives a decimal amount (e.g., 1.50 for $1.50). If a parser returns nil, the next parser in the chain will be tried. The default parser is always the final fallback.

Args:

parser: Custom function to convert amount to AssetAmount (or nil to skip)

Returns:

The server instance for chaining

Example:

evmServer.RegisterMoneyParser(func(amount float64, network x402.Network) (*x402.AssetAmount, error) {
    // Use DAI for large amounts
    if amount > 100 {
        return &x402.AssetAmount{
            Amount: fmt.Sprintf("%.0f", amount * 1e18),
            Asset:  "0x6B175474E89094C44Da98b954EedeAC495271d0F", // DAI
            Extra:  map[string]interface{}{"token": "DAI"},
        }, nil
    }
    return nil, nil // Use next parser
})

func Scheme) Scheme() string

Source: mechanisms/evm/exact/server/scheme.go:29

Scheme returns the scheme identifier

func ValidatePaymentRequirements(requirements x402.PaymentRequirements) error

Source: mechanisms/evm/exact/server/scheme.go:320

ValidatePaymentRequirements validates that requirements are valid for this scheme. All EVM networks are supported - this validates required fields only.