eth

package
v1.0.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2024 License: BlueOak-1.0.0 Imports: 20 Imported by: 1

Documentation

Overview

This file lifted from go-ethereum/signer/fourbyte at v1.10.6 commit 576681f29b895dd39e559b7ba17fcd89b42e4833 and modified to make parseCalldata take an abi instead of a string.

Package eth implements methods to work with ethereum swap contracts and transactions. The LGPL is a more restrictive license that may be more of a burden for closed source software.

Index

Constants

View Source
const (
	// GweiFactor is the amount of wei in one gwei.
	GweiFactor = 1e9
	// MaxBlockInterval is the number of seconds since the last header came
	// in over which we consider the chain to be out of sync.
	MaxBlockInterval = 180
	EthBipID         = 60
	MinGasTipCap     = 2 //gwei
)
View Source
const (
	MainnetChainID = 1
	TestnetChainID = 5  // Görli
	SimnetChainID  = 42 // see dex/testing/eth/harness.sh
)

These are the chain IDs of the various Ethereum network supported.

View Source
const SecretHashSize = 32

SecretHashSize is the byte-length of the hash of the secret key used in swaps.

Variables

View Source
var (
	// ChainIDs is a map of the network name to it's chain ID.
	ChainIDs = map[dex.Network]int64{
		dex.Mainnet: MainnetChainID,
		dex.Testnet: TestnetChainID,
		dex.Simnet:  SimnetChainID,
	}

	UnitInfo = dex.UnitInfo{
		AtomicUnit: "gwei",
		Conventional: dex.Denomination{
			Unit:             "ETH",
			ConversionFactor: 1e9,
		},
		Alternatives: []dex.Denomination{
			{
				Unit:             "Szabos",
				ConversionFactor: 1e6,
			},
			{
				Unit:             "Finneys",
				ConversionFactor: 1e3,
			},
		},
		FeeRateDenom: "gas",
	}

	VersionedGases = map[uint32]*Gases{
		0: v0Gases,
	}

	ContractAddresses = map[uint32]map[dex.Network]common.Address{
		0: {
			dex.Mainnet: common.HexToAddress("0x8C17e4968B6903E1601be82Ca989c5B5E2c7b400"),
			dex.Testnet: common.HexToAddress("0x73bc803A2604b2c58B8680c3CE1b14489842EF16"),
			dex.Simnet:  common.HexToAddress("0x2f68e723b8989ba1c6a9f03e42f33cb7dc9d606f"),
		},
	}

	MultiBalanceAddresses = map[dex.Network]common.Address{
		dex.Mainnet: common.HexToAddress("0x73bc803A2604b2c58B8680c3CE1b14489842EF16"),
		dex.Testnet: common.HexToAddress("0x8Bd6F6dBe69588D94953EE289Fd3E1db3e8dB43D"),
	}
)
View Source
var ABIs = initAbis()

ABIs maps each swap contract's version to that version's parsed ABI.

View Source
var Tokens = map[uint32]*Token{
	// contains filtered or unexported fields
}

Functions

func CheckAPIModules added in v0.6.0

func CheckAPIModules(c *rpc.Client, endpoint string, log dex.Logger, reqModules []string) (err error)

CheckAPIModules checks that the geth node supports the required modules.

func DecodeCoinID added in v0.6.0

func DecodeCoinID(coinID []byte) (common.Hash, error)

DecodeCoinID decodes the coin ID into a common.Hash. For eth, there are no funding coin IDs, just an account address. Care should be taken not to use DecodeCoinID or (Driver).DecodeCoinID for account addresses.

func DecodeContractData added in v0.6.0

func DecodeContractData(data []byte) (contractVersion uint32, swapKey [SecretHashSize]byte, err error)

DecodeContractData unpacks the contract version and secret hash.

func EncodeContractData added in v0.6.0

func EncodeContractData(contractVersion uint32, swapKey [SecretHashSize]byte) []byte

EncodeContractData packs the contract version and the secret hash into a byte slice for communicating a swap's identity.

func GweiToWei added in v0.6.0

func GweiToWei(v uint64) *big.Int

GweiToWei converts uint64 Gwei to *big.Int Wei.

func InitGas added in v0.6.0

func InitGas(n int, contractVer uint32) uint64

InitGas calculates the gas required for a batch of n inits.

func LoadGenesisFile added in v0.6.0

func LoadGenesisFile(genesisFile string) (*core.Genesis, error)

LoadGenesisFile loads a Genesis config from a json file.

func MaybeReadSimnetAddrs added in v0.6.0

func MaybeReadSimnetAddrs()

MaybeReadSimnetAddrs attempts to read the info files generated by the eth simnet harness to populate swap contract and token addresses in ContractAddresses and Tokens.

func MaybeReadSimnetAddrsDir added in v1.0.0

func MaybeReadSimnetAddrsDir(
	dir string,
	contractsAddrs map[uint32]map[dex.Network]common.Address,
	multiBalandAddresses map[dex.Network]common.Address,
	usdcToken *NetToken,
	usdtToken *NetToken,
)

func ParseInitiateData added in v0.6.0

func ParseInitiateData(calldata []byte, contractVersion uint32) (map[[SecretHashSize]byte]*Initiation, error)

ParseInitiateData parses the calldata used to call the initiate function of a specific version of the swap contract. It returns the the list of initiations done in the call and errors if the call data does not call initiate initiate with expected argument types.

func ParseRedeemData added in v0.6.0

func ParseRedeemData(calldata []byte, contractVersion uint32) (map[[SecretHashSize]byte]*Redemption, error)

ParseRedeemData parses the calldata used to call the redeem function of a specific version of the swap contract. It returns the the list of redemptions done in the call and errors if the call data does not call redeem with expected argument types.

func ParseRefundData added in v0.6.0

func ParseRefundData(calldata []byte, contractVersion uint32) ([SecretHashSize]byte, error)

ParseRefundData parses the calldata used to call the refund function of a specific version of the swap contract. It returns the secret hash and errors if the call data does not call refund with expected argument types.

func RedeemGas added in v0.6.0

func RedeemGas(n int, contractVer uint32) uint64

RedeemGas calculates the gas required for a batch of n redemptions.

func RefundGas added in v0.6.0

func RefundGas(contractVer uint32) uint64

RefundGas calculates the gas required for a refund.

func WeiToGwei added in v0.6.0

func WeiToGwei(v *big.Int) uint64

WeiToGweiFloor converts *big.Int Wei to uint64 Gwei. If v is determined to be unsuitable for a uint64, zero is returned. For values that are not even multiples of 1 gwei, this function returns the floor.

func WeiToGweiCeil added in v1.0.0

func WeiToGweiCeil(v *big.Int) uint64

WeiToGweiCeil converts *big.Int Wei to uint64 Gwei. If v is determined to be unsuitable for a uint64, zero is returned. For values that are not even multiples of 1 gwei, this function returns the ceiling. In general, WeiToWeiCeil should be used with gwei-unit fee rates are generated or validated and WeiToGwei should be used with balances and values.

func WeiToGweiSafe added in v1.0.0

func WeiToGweiSafe(wei *big.Int) (uint64, error)

WeiToGweiSafe converts a *big.Int in wei (1e18 unit) to gwei (1e9 unit) as a uint64. Errors if the amount of gwei is too big to fit fully into a uint64. For values that are not even multiples of 1 gwei, this function returns the ceiling. As such, WeiToGweiSafe is more suitable for validating or generating fee rates. If balance or value validation is the goal, use truncation e.g. WeiToGwei.

Types

type DecodedCallData added in v0.6.0

type DecodedCallData struct {
	Name string
	Args []any
	// contains filtered or unexported fields
}

DecodedCallData is an internal type to represent a method call parsed according to an ABI method signature.

func ParseCallData added in v0.6.0

func ParseCallData(calldata []byte, abispec *abi.ABI) (*DecodedCallData, error)

ParseCallData matches the provided call data against the ABI definition and returns a struct containing the actual go-typed values.

func (*DecodedCallData) String added in v0.6.0

func (cd *DecodedCallData) String() string

String implements stringer interface for decodedCallData

type Gases added in v0.6.0

type Gases struct {
	// Approve is the amount of gas needed to approve the swap contract for
	// transferring tokens. The first approval for an address uses more gas than
	// subsequent approvals for the same address.
	Approve uint64 `json:"approve"`
	// Transfer is the amount of gas needed to transfer tokens. The first
	// transfer to an address uses more gas than subsequent transfers to the
	// same address.
	Transfer uint64 `json:"transfer"`
	// Swap is the amount of gas needed to initialize a single ethereum swap.
	Swap uint64 `json:"swap"`
	// SwapAdd is the amount of gas needed to initialize additional swaps in
	// the same transaction.
	SwapAdd uint64 `json:"swapAdd"`
	// Redeem is the amount of gas it costs to redeem a swap.
	Redeem uint64 `json:"redeem"`
	// RedeemAdd is the amount of gas needed to redeem additional swaps in the
	// same transaction.
	RedeemAdd uint64 `json:"redeemAdd"`
	// Refund is the amount of gas needed to refund a swap.
	Refund uint64 `json:"refund"`
}

Gases lists the expected gas required for various DEX and wallet operations.

func (*Gases) RedeemN added in v0.6.0

func (g *Gases) RedeemN(n int) uint64

RedeemN calculates the gas needed to redeem n swaps.

func (*Gases) SwapN added in v0.6.0

func (g *Gases) SwapN(n int) uint64

SwapN calculates the gas needed to initiate n swaps.

type Initiation added in v0.6.0

type Initiation struct {
	LockTime    time.Time
	SecretHash  [32]byte
	Participant common.Address
	Value       *big.Int
}

Initiation is the data used to initiate a swap.

type NetToken added in v0.6.0

type NetToken struct {
	// Address is the token contract address.
	Address common.Address `json:"address"`
	// SwapContracts is the versioned swap contracts bound to the token address.
	SwapContracts map[uint32]*SwapContract `json:"swapContracts"`
}

NetToken are the addresses associated with the token and its versioned swap contracts.

type Redemption added in v0.6.0

type Redemption struct {
	Secret     [32]byte
	SecretHash [32]byte
}

Redemption is the data used to redeem a swap.

type SwapContract added in v0.6.0

type SwapContract struct {
	Address common.Address
	Gas     Gases
}

SwapContract represents a single swap contract instance.

type SwapState added in v0.6.0

type SwapState struct {
	BlockHeight uint64
	LockTime    time.Time
	Secret      [32]byte
	Initiator   common.Address
	Participant common.Address
	Value       *big.Int
	State       SwapStep
}

SwapState is the current state of an in-process swap.

func SwapStateFromV0 added in v0.6.0

func SwapStateFromV0(state *v0.ETHSwapSwap) *SwapState

SwapStateFromV0 converts a version 0 contract *ETHSwapSwap to the generalized *SwapState type.

type SwapStep added in v0.6.0

type SwapStep uint8

SwapStep is the state of a swap and corresponds to values in the Solidity swap contract.

const (
	// SSNone indicates that the swap is not initiated. This is the default
	// state of a swap.
	SSNone SwapStep = iota
	// SSInitiated indicates that the swap has been initiated.
	SSInitiated
	// SSRedeemed indicates that the swap was initiated and then redeemed.
	// This is one of two possible end states of a swap.
	SSRedeemed
	// SSRefunded indicates that the swap was initiated and then refunded.
	// This is one of two possible end states of a swap.
	SSRefunded
)

Swap states represent the status of a swap. The default state of a swap is SSNone. A swap in status SSNone does not exist. SSInitiated indicates that a party has initiated the swap and funds have been sent to the contract. SSRedeemed indicates a successful swap where the participant was able to redeem with the secret hash. SSRefunded indicates a failed swap, where the initiating party refunded their coins after the locktime passed. A swap no longer changes states after reaching SSRedeemed or SSRefunded.

func (SwapStep) String added in v0.6.0

func (ss SwapStep) String() string

String satisfies the Stringer interface.

type Token added in v0.6.0

type Token struct {
	*dex.Token
	// NetTokens is a mapping of token addresses for each network available.
	NetTokens map[dex.Network]*NetToken `json:"netAddrs"`
	// EVMFactor allows for arbitrary ERC20 decimals. For an ERC20 contract,
	// the relation
	//    math.Log10(UnitInfo.Conventional.ConversionFactor) + Token.EVMFactor = decimals
	// should hold true.
	// Since most assets will use a value of 9 here, a default value of 9 will
	// be used in AtomicToEVM and EVMToAtomic if EVMFactor is not set.
	EVMFactor *int64 `json:"evmFactor"` // default 9
}

Token is the definition of an ERC20 token, including all of its network and version variants.

func (*Token) AtomicToEVM added in v0.6.0

func (t *Token) AtomicToEVM(v uint64) *big.Int

AtomicToEVM converts from DEX atomic units to EVM units.

func (*Token) EVMToAtomic added in v0.6.0

func (t *Token) EVMToAtomic(v *big.Int) uint64

EVMToAtomic converts from raw EVM units to DEX atomic units.

Directories

Path Synopsis
contracts
v0
Package v0 contains pre-generated code that should not be directly edited.
Package v0 contains pre-generated code that should not be directly edited.

Jump to

Keyboard shortcuts

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