erc20

package
v0.0.0-...-eccf466 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2024 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GasTransfer          = 3_000_000
	GasApprove           = 30_956
	GasIncreaseAllowance = 34_605
	GasDecreaseAllowance = 34_519
	GasName              = 3_421
	GasSymbol            = 3_464
	GasDecimals          = 427
	GasTotalSupply       = 2_477
	GasBalanceOf         = 2_851
	GasAllowance         = 3_246
)
View Source
const (
	ErrIntegerOverflow           = "amount %s causes integer overflow"
	ErrNoAllowanceForToken       = "allowance for token %s does not exist"
	ErrSubtractMoreThanAllowance = "subtracted value cannot be greater than existing allowance for denom %s: %s > %s"
)

Errors that have formatted information are defined here as a string.

View Source
const (
	// NameMethod defines the ABI method name for the ERC-20 Name
	// query.
	NameMethod = "name"
	// SymbolMethod defines the ABI method name for the ERC-20 Symbol
	// query.
	SymbolMethod = "symbol"
	// DecimalsMethod defines the ABI method name for the ERC-20 Decimals
	// query.
	DecimalsMethod = "decimals"
	// TotalSupplyMethod defines the ABI method name for the ERC-20 TotalSupply
	// query.
	TotalSupplyMethod = "totalSupply"
	// BalanceOfMethod defines the ABI method name for the ERC-20 BalanceOf
	// query.
	BalanceOfMethod = "balanceOf"
)
View Source
const (
	// TransferMethod defines the ABI method name for the ERC-20 transfer
	// transaction.
	TransferMethod = "transfer"
	// TransferFromMethod defines the ABI method name for the ERC-20 transferFrom
	// transaction.
	TransferFromMethod = "transferFrom"
)
View Source
const (
	// WEVMOSContractMainnet is the WEVMOS contract address for mainnet
	WEVMOSContractMainnet = "0xD4949664cD82660AaE99bEdc034a0deA8A0bd517"
	// WEVMOSContractTestnet is the WEVMOS contract address for testnet
	WEVMOSContractTestnet = "0xcc491f589b45d4a3c679016195b3fb87d7848210"
)
View Source
const (
	// EventTypeTransfer defines the event type for the ERC-20 Transfer and TransferFrom transactions.
	EventTypeTransfer = "Transfer"
)

Variables

View Source
var (

	// Precompile errors
	ErrDecreaseNonPositiveValue = errors.New("cannot decrease allowance with non-positive values")
	ErrDenomTraceNotFound       = errors.New("denom trace not found")
	ErrIncreaseNonPositiveValue = errors.New("cannot increase allowance with non-positive values")
	ErrNegativeAmount           = errors.New("cannot approve negative values")
	ErrNoIBCVoucherDenom        = errors.New("denom is not an IBC voucher")
	ErrSpenderIsOwner           = errors.New("spender cannot be the owner")

	// ERC20 errors
	ErrDecreasedAllowanceBelowZero  = errors.New("ERC20: decreased allowance below zero")
	ErrInsufficientAllowance        = errors.New("ERC20: insufficient allowance")
	ErrTransferAmountExceedsBalance = errors.New("ERC20: transfer amount exceeds balance")
)
View Source
var SendMsgURL = sdk.MsgTypeURL(&banktypes.MsgSend{})

SendMsgURL defines the authorization type for MsgSend

Functions

func BuildExecRevertedErr

func BuildExecRevertedErr(reason string) (error, error)

BuildExecRevertedErr returns a mocked error that should align with the behavior of the original ERC20 Solidity implementation.

FIXME: This is not yet producing the correct reason bytes. Will fix on a follow up PR.

func ConvertErrToERC20Error

func ConvertErrToERC20Error(err error) error

ConvertErrToERC20Error is a helper function which maps errors raised by the Cosmos SDK stack to the corresponding errors which are raised by an ERC20 contract.

TODO: Create the full RevertError types instead of just the standard error type.

TODO: Return ERC-6093 compliant errors.

func GetAuthzExpirationAndAllowance

func GetAuthzExpirationAndAllowance(
	authzKeeper authzkeeper.Keeper,
	ctx sdk.Context,
	grantee, granter common.Address,
	denom string,
) (authz.Authorization, *time.Time, *big.Int, error)

GetAuthzExpirationAndAllowance returns the authorization, its expiration as well as the amount of denom that the grantee is allowed to spend on behalf of the granter.

func GetDenomTrace

func GetDenomTrace(
	transferKeeper transferkeeper.Keeper,
	ctx sdk.Context,
	denom string,
) (transfertypes.DenomTrace, error)

GetDenomTrace returns the denomination trace from the corresponding IBC denomination. If the denomination is not an IBC voucher or the trace is not found, it returns an error.

func ParseAllowanceArgs

func ParseAllowanceArgs(args []interface{}) (
	owner, spender common.Address, err error,
)

ParseAllowanceArgs parses the allowance arguments and returns the owner and the spender addresses.

func ParseApproveArgs

func ParseApproveArgs(args []interface{}) (
	spender common.Address, amount *big.Int, err error,
)

ParseApproveArgs parses the approval arguments and returns the spender address and amount.

func ParseBalanceOfArgs

func ParseBalanceOfArgs(args []interface{}) (common.Address, error)

ParseBalanceOfArgs parses the balanceOf arguments and returns the account address.

func ParseTransferArgs

func ParseTransferArgs(args []interface{}) (
	to common.Address, amount *big.Int, err error,
)

ParseTransferArgs parses the arguments from the transfer method and returns the destination address (to) and amount.

func ParseTransferFromArgs

func ParseTransferFromArgs(args []interface{}) (
	from, to common.Address, amount *big.Int, err error,
)

ParseTransferFromArgs parses the arguments from the transferFrom method and returns the sender address (from), destination address (to) and amount.

Types

type EventApproval

type EventApproval struct {
	Owner   common.Address
	Spender common.Address
	Value   *big.Int
}

EventApproval defines the event data for the ERC20 Approval events.

type EventTransfer

type EventTransfer struct {
	From  common.Address
	To    common.Address
	Value *big.Int
}

EventTransfer defines the event data for the ERC20 Transfer events.

type Precompile

type Precompile struct {
	cmn.Precompile
	// contains filtered or unexported fields
}

Precompile defines the precompiled contract for ERC-20.

func NewPrecompile

func NewPrecompile(
	tokenPair erc20types.TokenPair,
	bankKeeper bankkeeper.Keeper,
	authzKeeper authzkeeper.Keeper,
	transferKeeper transferkeeper.Keeper,
) (*Precompile, error)

NewPrecompile creates a new ERC-20 Precompile instance as a PrecompiledContract interface.

func (Precompile) Address

func (p Precompile) Address() common.Address

Address defines the address of the ERC-20 precompile contract.

func (Precompile) Allowance

func (p Precompile) Allowance(
	ctx sdk.Context,
	_ *vm.Contract,
	_ vm.StateDB,
	method *abi.Method,
	args []interface{},
) ([]byte, error)

Allowance returns the remaining allowance of a spender to the contract by checking the existence of a bank SendAuthorization.

func (Precompile) Approve

func (p Precompile) Approve(
	ctx sdk.Context,
	contract *vm.Contract,
	stateDB vm.StateDB,
	method *abi.Method,
	args []interface{},
) ([]byte, error)

Approve sets the given amount as the allowance of the spender address over the caller’s tokens. It returns a boolean value indicating whether the operation succeeded and emits the Approval event on success.

The Approve method handles 4 cases:

  1. no authorization, amount negative -> return error
  2. no authorization, amount positive -> create a new authorization
  3. authorization exists, amount 0 or negative -> delete authorization
  4. authorization exists, amount positive -> update authorization
  5. no authorizaiton, amount 0 -> no-op but still emit Approval event

func (Precompile) BalanceOf

func (p Precompile) BalanceOf(
	ctx sdk.Context,
	_ *vm.Contract,
	_ vm.StateDB,
	method *abi.Method,
	args []interface{},
) ([]byte, error)

BalanceOf returns the amount of tokens owned by account. It fetches the balance of the coin from the bank keeper and returns zero if not found.

func (Precompile) Decimals

func (p Precompile) Decimals(
	ctx sdk.Context,
	_ *vm.Contract,
	_ vm.StateDB,
	method *abi.Method,
	_ []interface{},
) ([]byte, error)

Decimals returns the decimals places of the token. If the token metadata is registered in the bank module, it returns the display denomination exponent. Otherwise, it infers the decimal value from the first character of the base denomination (e.g. uatom -> 6).

func (Precompile) DecreaseAllowance

func (p Precompile) DecreaseAllowance(
	ctx sdk.Context,
	contract *vm.Contract,
	stateDB vm.StateDB,
	method *abi.Method,
	args []interface{},
) ([]byte, error)

DecreaseAllowance decreases the allowance of the spender address over the caller’s tokens by the given subtracted value. It returns a boolean value indicating whether the operation succeeded and emits the Approval event on success.

The DecreaseAllowance method handles 4 cases:

  1. subtractedValue 0 or negative -> return error
  2. no authorization -> return error
  3. authorization exists, subtractedValue positive and subtractedValue less than allowance -> update authorization
  4. authorization exists, subtractedValue positive and subtractedValue equal to allowance -> delete authorization
  5. authorization exists, subtractedValue positive but no allowance for given denomination -> return error
  6. authorization exists, subtractedValue positive and subtractedValue higher than allowance -> return error

func (Precompile) EmitApprovalEvent

func (p Precompile) EmitApprovalEvent(ctx sdk.Context, stateDB vm.StateDB, owner, spender common.Address, value *big.Int) error

EmitApprovalEvent creates a new approval event emitted on Approve, IncreaseAllowance and DecreaseAllowance transactions.

func (Precompile) EmitTransferEvent

func (p Precompile) EmitTransferEvent(ctx sdk.Context, stateDB vm.StateDB, from, to common.Address, value *big.Int) error

EmitTransferEvent creates a new Transfer event emitted on transfer and transferFrom transactions.

func (Precompile) HandleMethod

func (p Precompile) HandleMethod(
	ctx sdk.Context,
	contract *vm.Contract,
	stateDB vm.StateDB,
	method *abi.Method,
	args []interface{},
) (bz []byte, err error)

HandleMethod handles the execution of each of the ERC-20 methods.

func (Precompile) IncreaseAllowance

func (p Precompile) IncreaseAllowance(
	ctx sdk.Context,
	contract *vm.Contract,
	stateDB vm.StateDB,
	method *abi.Method,
	args []interface{},
) ([]byte, error)

IncreaseAllowance increases the allowance of the spender address over the caller’s tokens by the given added value. It returns a boolean value indicating whether the operation succeeded and emits the Approval event on success.

The IncreaseAllowance method handles 3 cases:

  1. addedValue 0 or negative -> return error
  2. no authorization, addedValue positive -> create a new authorization
  3. authorization exists, addedValue positive -> update authorization

func (Precompile) IsTransaction

func (Precompile) IsTransaction(methodName string) bool

IsTransaction checks if the given method name corresponds to a transaction or query.

func (Precompile) Name

func (p Precompile) Name(
	ctx sdk.Context,
	_ *vm.Contract,
	_ vm.StateDB,
	method *abi.Method,
	_ []interface{},
) ([]byte, error)

Name returns the name of the token. If the token metadata is registered in the bank module, it returns its name. Otherwise, it returns the base denomination of the token capitalized (e.g. uatom -> Atom).

func (Precompile) RequiredGas

func (p Precompile) RequiredGas(input []byte) uint64

RequiredGas calculates the contract gas used for the

func (Precompile) Run

func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz []byte, err error)

Run executes the precompiled contract ERC-20 methods defined in the ABI.

func (Precompile) Symbol

func (p Precompile) Symbol(
	ctx sdk.Context,
	_ *vm.Contract,
	_ vm.StateDB,
	method *abi.Method,
	_ []interface{},
) ([]byte, error)

Symbol returns the symbol of the token. If the token metadata is registered in the bank module, it returns its symbol. Otherwise, it returns the base denomination of the token in uppercase (e.g. uatom -> ATOM).

func (Precompile) TotalSupply

func (p Precompile) TotalSupply(
	ctx sdk.Context,
	_ *vm.Contract,
	_ vm.StateDB,
	method *abi.Method,
	_ []interface{},
) ([]byte, error)

TotalSupply returns the amount of tokens in existence. It fetches the supply of the coin from the bank keeper and returns zero if not found.

func (Precompile) Transfer

func (p Precompile) Transfer(
	ctx sdk.Context,
	contract *vm.Contract,
	stateDB vm.StateDB,
	method *abi.Method,
	args []interface{},
) ([]byte, error)

Transfer executes a direct transfer from the caller address to the destination address.

func (Precompile) TransferFrom

func (p Precompile) TransferFrom(
	ctx sdk.Context,
	contract *vm.Contract,
	stateDB vm.StateDB,
	method *abi.Method,
	args []interface{},
) ([]byte, error)

TransferFrom executes a transfer on behalf of the specified from address in the call data to the destination address.

Jump to

Keyboard shortcuts

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