eth

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 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 SecretHashSize = 32

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

Variables

View Source
var (
	UnitInfo = dex.UnitInfo{
		AtomicUnit: "gwei",
		Conventional: dex.Denomination{
			Unit:             "ETH",
			ConversionFactor: 1e9,
		},
	}

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

	ContractAddresses = map[uint32]map[dex.Network]common.Address{
		0: {
			dex.Mainnet: common.HexToAddress("0x8C17e4968B6903E1601be82Ca989c5B5E2c7b400"),
			dex.Simnet:  common.HexToAddress("0x2f68e723b8989ba1c6a9f03e42f33cb7dc9d606f"),
			dex.Testnet: common.HexToAddress("0x198463496037754564e9bea5418Bf4117Db0520C"),
		},
	}
)
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 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

WeiToGwei converts *big.Int Wei to uint64 Gwei. If v is determined to be unsuitable for a uint64, zero is returned.

func WeiToGweiUint64 added in v0.6.0

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

WeiToGweiUint64 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.

Types

type DecodedCallData added in v0.6.0

type DecodedCallData struct {
	Name string
	Args []interface{}
	// 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.
	Approve uint64 `json:"approve"`
	// Transfer is the amount of gas needed to transfer tokens.
	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 VersionedNetworkToken added in v0.6.0

func VersionedNetworkToken(assetID uint32, contractVer uint32, net dex.Network) (token *Token,
	tokenAddr, contractAddr common.Address, err error)

VersionedNetworkToken retrieves the token, token address, and swap contract address for the token asset.

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