Documentation ¶
Overview ¶
Package coins provides types, conversions and exchange calculations for dealing with cryptocurrency coin and ERC20 token representations.
Index ¶
- Constants
- Variables
- func DecimalCtx() *apd.Context
- func ExceedsDecimals(val *apd.Decimal, maxDecimals uint8) bool
- func FmtPiconeroAsXMR(piconeros uint64) string
- func FmtWeiAsETH(wei *big.Int) string
- func NumDecimals(value *apd.Decimal) int32
- func StrToDecimal(amount string) *apd.Decimal
- func ValidatePositive(jsonFieldName string, maxDecimals uint8, value *apd.Decimal) error
- type ERC20TokenAmount
- func (a *ERC20TokenAmount) AsStd() *apd.Decimal
- func (a *ERC20TokenAmount) AsStdString() string
- func (a *ERC20TokenAmount) BigInt() *big.Int
- func (a *ERC20TokenAmount) IsToken() bool
- func (a *ERC20TokenAmount) NumStdDecimals() uint8
- func (a *ERC20TokenAmount) StdSymbol() string
- func (a *ERC20TokenAmount) String() string
- func (a *ERC20TokenAmount) TokenAddress() ethcommon.Address
- type ERC20TokenInfo
- type EthAssetAmount
- type ExchangeRate
- func (r *ExchangeRate) Decimal() *apd.Decimal
- func (r *ExchangeRate) MarshalText() ([]byte, error)
- func (r *ExchangeRate) String() string
- func (r *ExchangeRate) ToERC20Amount(xmrAmount *apd.Decimal, token *ERC20TokenInfo) (*apd.Decimal, error)
- func (r *ExchangeRate) ToETH(xmrAmount *apd.Decimal) (*apd.Decimal, error)
- func (r *ExchangeRate) ToXMR(ethAssetAmt EthAssetAmount) (*apd.Decimal, error)
- func (r *ExchangeRate) UnmarshalText(b []byte) error
- type PiconeroAmount
- func (a *PiconeroAmount) AsMonero() *apd.Decimal
- func (a *PiconeroAmount) AsMoneroString() string
- func (a *PiconeroAmount) Cmp(other *PiconeroAmount) int
- func (a *PiconeroAmount) CmpU64(other uint64) int
- func (a *PiconeroAmount) Decimal() *apd.Decimal
- func (a *PiconeroAmount) MarshalText() ([]byte, error)
- func (a *PiconeroAmount) String() string
- func (a *PiconeroAmount) Uint64() (uint64, error)
- func (a *PiconeroAmount) UnmarshalText(b []byte) error
- type ProvidesCoin
- type WeiAmount
- func (a *WeiAmount) AsEther() *apd.Decimal
- func (a *WeiAmount) AsEtherString() string
- func (a *WeiAmount) AsStd() *apd.Decimal
- func (a *WeiAmount) AsStdString() string
- func (a *WeiAmount) BigInt() *big.Int
- func (a *WeiAmount) Cmp(b *WeiAmount) int
- func (a *WeiAmount) Decimal() *apd.Decimal
- func (a *WeiAmount) IsToken() bool
- func (a *WeiAmount) MarshalText() ([]byte, error)
- func (a *WeiAmount) NumStdDecimals() uint8
- func (a *WeiAmount) StdSymbol() string
- func (a *WeiAmount) String() string
- func (a *WeiAmount) Sub(b *WeiAmount) *WeiAmount
- func (a *WeiAmount) TokenAddress() ethcommon.Address
- func (a *WeiAmount) UnmarshalText(b []byte) error
Constants ¶
const ( // NumEtherDecimals is the number of decimal points needed to represent whole units of Wei in Ether NumEtherDecimals = 18 // NumMoneroDecimals is the number of decimal points needed to represent whole units of piconero in XMR NumMoneroDecimals = 12 // MaxExchangeRateDecimals is the number of decimal points we allow in an exchange rate MaxExchangeRateDecimals = 6 // MaxCoinPrecision is a somewhat arbitrary precision upper bound (2^256 consumes 78 digits) MaxCoinPrecision = 100 )
Variables ¶
var ( RelayerFeeWei = big.NewInt(1e16) RelayerFeeETH = NewWeiAmount(RelayerFeeWei).AsEther() )
RelayerFeeWei and RelayerFeeETH are the fixed 0.01 ETH fee for using a swap relayer to claim.
var ( // ErrInvalidCoin is generated when a ProvidesCoin type has an invalid string ErrInvalidCoin = errors.New("invalid ProvidesCoin") )
Functions ¶
func DecimalCtx ¶
func DecimalCtx() *apd.Context
DecimalCtx clones and returns the apd.Context we use for coin math operations.
func ExceedsDecimals ¶ added in v0.3.0
ExceedsDecimals returns `true` if the the number, written without an exponent, would require more digits after the decimal place than the passed value `decimals`. Otherwise, `false` is returned.
func FmtPiconeroAsXMR ¶
FmtPiconeroAsXMR takes piconeros as input and produces a formatted string of the amount in XMR.
func FmtWeiAsETH ¶
FmtWeiAsETH takes Wei as input and produces a formatted string of the amount in ETH.
func NumDecimals ¶ added in v0.3.0
func NumDecimals(value *apd.Decimal) int32
NumDecimals returns the minimum number digits needed to represent the passed value after the decimal point.
func StrToDecimal ¶
func StrToDecimal(amount string) *apd.Decimal
StrToDecimal converts strings to apd.Decimal for tests, panicking on error. This function is intended for use with string constants, so panic is arguably correct and allows variables to be declared outside a test function.
func ValidatePositive ¶
ValidatePositive is for doing additional input validation on apd.Decimal values that should only contain positive values (like min, max and provided amounts).
Types ¶
type ERC20TokenAmount ¶
type ERC20TokenAmount struct { Amount *apd.Decimal `json:"amount" validate:"required"` // in standard units TokenInfo *ERC20TokenInfo `json:"tokenInfo" validate:"required"` }
ERC20TokenAmount represents some amount of an ERC20 token in the smallest denomination
func NewERC20TokenAmountFromBigInt ¶
func NewERC20TokenAmountFromBigInt(amount *big.Int, token *ERC20TokenInfo) *ERC20TokenAmount
NewERC20TokenAmountFromBigInt converts some amount in the smallest token denomination into an ERC20TokenAmount.
func NewTokenAmountFromDecimals ¶ added in v0.3.0
func NewTokenAmountFromDecimals(amount *apd.Decimal, token *ERC20TokenInfo) *ERC20TokenAmount
NewTokenAmountFromDecimals converts an amount in standard units from apd.Decimal into the ERC20TokenAmount type. During the conversion, rounding may occur if the input value is too precise for the token's decimals.
func (*ERC20TokenAmount) AsStd ¶ added in v0.3.0
func (a *ERC20TokenAmount) AsStd() *apd.Decimal
AsStd returns the amount in standard units
func (*ERC20TokenAmount) AsStdString ¶ added in v0.3.0
func (a *ERC20TokenAmount) AsStdString() string
AsStdString returns the ERC20TokenAmount as a base10 string in standard units.
func (*ERC20TokenAmount) BigInt ¶
func (a *ERC20TokenAmount) BigInt() *big.Int
BigInt returns the ERC20TokenAmount as a *big.Int
func (*ERC20TokenAmount) IsToken ¶
func (a *ERC20TokenAmount) IsToken() bool
IsToken returns true, because ERC20TokenAmount represents and ERC20 token
func (*ERC20TokenAmount) NumStdDecimals ¶ added in v0.3.0
func (a *ERC20TokenAmount) NumStdDecimals() uint8
NumStdDecimals returns the max decimal precision of the token's standard representation
func (*ERC20TokenAmount) StdSymbol ¶ added in v0.3.0
func (a *ERC20TokenAmount) StdSymbol() string
StdSymbol returns the token's symbol in a format that is safe to log and display
func (*ERC20TokenAmount) String ¶
func (a *ERC20TokenAmount) String() string
String returns the ERC20TokenAmount as a base10 string in standard units.
func (*ERC20TokenAmount) TokenAddress ¶
func (a *ERC20TokenAmount) TokenAddress() ethcommon.Address
TokenAddress returns the ERC20 token's ethereum contract address
type ERC20TokenInfo ¶
type ERC20TokenInfo struct { Address ethcommon.Address `json:"address" validate:"required"` NumDecimals uint8 `json:"decimals"` // digits after the Decimal point needed for smallest denomination Name string `json:"name"` Symbol string `json:"symbol"` }
ERC20TokenInfo stores the token contract address and basic info that most ERC20 tokens support
func NewERC20TokenInfo ¶
func NewERC20TokenInfo(address ethcommon.Address, decimals uint8, name string, symbol string) *ERC20TokenInfo
NewERC20TokenInfo constructs and returns a new ERC20TokenInfo object
func (*ERC20TokenInfo) SanitizedSymbol ¶
func (t *ERC20TokenInfo) SanitizedSymbol() string
SanitizedSymbol quotes the symbol ensuring escape sequences, newlines, etc. are escaped.
type EthAssetAmount ¶
type EthAssetAmount interface { BigInt() *big.Int AsStd() *apd.Decimal AsStdString() string StdSymbol() string IsToken() bool NumStdDecimals() uint8 TokenAddress() ethcommon.Address }
EthAssetAmount represents an amount of an Ethereum asset (ie. ether or an ERC20)
func NewEthAssetAmount ¶ added in v0.3.0
func NewEthAssetAmount(amount *apd.Decimal, token *ERC20TokenInfo) EthAssetAmount
NewEthAssetAmount accepts an amount, in standard units, for ETH or a token and returns a type implementing EthAssetAmount. If the token is nil, we assume the asset is ETH.
func StrToETHAsset ¶ added in v0.3.0
func StrToETHAsset(amount string, optionalToken *ERC20TokenInfo) EthAssetAmount
StrToETHAsset converts a string into the EthAssetAmount. Pass nil for the token if the asset is ETH.
type ExchangeRate ¶
type ExchangeRate apd.Decimal
ExchangeRate defines an exchange rate between ETH and XMR. It is defined as the ratio of ETH:XMR that the node wishes to provide. ie. an ExchangeRate of 0.1 means that the node considers 1 ETH = 10 XMR.
func CalcExchangeRate ¶
func CalcExchangeRate(xmrPrice *apd.Decimal, ethPrice *apd.Decimal) (*ExchangeRate, error)
CalcExchangeRate computes and returns an exchange rate using ETH and XRM prices. The price can be relative to USD, bitcoin or something else, but both values should be relative to the same alternate currency.
func StrToExchangeRate ¶
func StrToExchangeRate(rate string) *ExchangeRate
StrToExchangeRate converts strings to ExchangeRate for tests, panicking on error.
func ToExchangeRate ¶
func ToExchangeRate(rate *apd.Decimal) *ExchangeRate
ToExchangeRate casts an *apd.Decimal to *ExchangeRate
func (*ExchangeRate) Decimal ¶
func (r *ExchangeRate) Decimal() *apd.Decimal
Decimal casts *ExchangeRate to *apd.Decimal
func (*ExchangeRate) MarshalText ¶
func (r *ExchangeRate) MarshalText() ([]byte, error)
MarshalText hands off JSON encoding to apd.Decimal
func (*ExchangeRate) String ¶
func (r *ExchangeRate) String() string
func (*ExchangeRate) ToERC20Amount ¶
func (r *ExchangeRate) ToERC20Amount(xmrAmount *apd.Decimal, token *ERC20TokenInfo) (*apd.Decimal, error)
ToERC20Amount converts an XMR amount to a token amount in standard units with the given exchange rate. If the result requires more decimal places than the token allows, an error is returned.
func (*ExchangeRate) ToETH ¶
func (r *ExchangeRate) ToETH(xmrAmount *apd.Decimal) (*apd.Decimal, error)
ToETH converts an XMR amount to an ETH amount with the given exchange rate. If the calculated result would have fractional wei, an error is returned.
func (*ExchangeRate) ToXMR ¶
func (r *ExchangeRate) ToXMR(ethAssetAmt EthAssetAmount) (*apd.Decimal, error)
ToXMR converts an ETH amount to an XMR amount with the given exchange rate. If the calculated value would have fractional piconeros, an error is returned.
func (*ExchangeRate) UnmarshalText ¶
func (r *ExchangeRate) UnmarshalText(b []byte) error
UnmarshalText hands off JSON decoding to apd.Decimal
type PiconeroAmount ¶
type PiconeroAmount apd.Decimal
PiconeroAmount represents some amount of piconero (the smallest denomination of monero)
func MoneroToPiconero ¶
func MoneroToPiconero(xmrAmt *apd.Decimal) *PiconeroAmount
MoneroToPiconero converts an amount in Monero to Piconero
func NewPiconeroAmount ¶
func NewPiconeroAmount(amount uint64) *PiconeroAmount
NewPiconeroAmount converts piconeros in uint64 to PicnoneroAmount
func (*PiconeroAmount) AsMonero ¶
func (a *PiconeroAmount) AsMonero() *apd.Decimal
AsMonero converts the piconero PiconeroAmount into standard units
func (*PiconeroAmount) AsMoneroString ¶
func (a *PiconeroAmount) AsMoneroString() string
AsMoneroString converts a PiconeroAmount into a formatted XMR amount string.
func (*PiconeroAmount) Cmp ¶
func (a *PiconeroAmount) Cmp(other *PiconeroAmount) int
Cmp compares a and other and returns:
-1 if a < other 0 if a == other +1 if a > other
func (*PiconeroAmount) CmpU64 ¶
func (a *PiconeroAmount) CmpU64(other uint64) int
CmpU64 compares a and other and returns:
-1 if a < other 0 if a == other +1 if a > other
func (*PiconeroAmount) Decimal ¶
func (a *PiconeroAmount) Decimal() *apd.Decimal
Decimal casts *PiconeroAmount to *apd.Decimal
func (*PiconeroAmount) MarshalText ¶
func (a *PiconeroAmount) MarshalText() ([]byte, error)
MarshalText hands off JSON encoding to apd.Decimal
func (*PiconeroAmount) String ¶
func (a *PiconeroAmount) String() string
String returns the PiconeroAmount as a base10 string
func (*PiconeroAmount) Uint64 ¶
func (a *PiconeroAmount) Uint64() (uint64, error)
Uint64 converts piconero amount to uint64. Errors if a is negative or larger than 2^63-1.
func (*PiconeroAmount) UnmarshalText ¶
func (a *PiconeroAmount) UnmarshalText(b []byte) error
UnmarshalText hands off JSON decoding to apd.Decimal
type ProvidesCoin ¶
type ProvidesCoin string
ProvidesCoin represents a coin that a swap participant can provide.
var ( ProvidesXMR ProvidesCoin = "XMR" //nolint ProvidesETH ProvidesCoin = "ETH" //nolint )
func NewProvidesCoin ¶
func NewProvidesCoin(s string) (ProvidesCoin, error)
NewProvidesCoin converts a string to a ProvidesCoin.
func (*ProvidesCoin) MarshalText ¶
func (c *ProvidesCoin) MarshalText() ([]byte, error)
MarshalText hands off JSON encoding to apd.Decimal
func (*ProvidesCoin) String ¶
func (c *ProvidesCoin) String() string
func (*ProvidesCoin) UnmarshalText ¶
func (c *ProvidesCoin) UnmarshalText(data []byte) error
UnmarshalText hands off JSON decoding to apd.Decimal
type WeiAmount ¶
type WeiAmount apd.Decimal
WeiAmount represents some amount of ETH in the smallest denomination (Wei)
func EtherToWei ¶
func EtherToWei(ethAmt *apd.Decimal) *WeiAmount
EtherToWei converts some amount of standard ETH to a WeiAmount.
func NewWeiAmount ¶
NewWeiAmount converts the passed *big.Int representation of a Wei amount to the WeiAmount type. The returned value is a copy with no references to the passed value.
func ToWeiAmount ¶
func ToWeiAmount(wei *apd.Decimal) *WeiAmount
ToWeiAmount casts an *apd.Decimal that is already in Wei to *WeiAmount
func (*WeiAmount) AsEther ¶
func (a *WeiAmount) AsEther() *apd.Decimal
AsEther returns the Wei amount as ETH
func (*WeiAmount) AsEtherString ¶
AsEtherString converts the Wei amount to an ETH amount string
func (*WeiAmount) AsStd ¶ added in v0.3.0
func (a *WeiAmount) AsStd() *apd.Decimal
AsStd is an alias for AsEther, returning the Wei amount as ETH
func (*WeiAmount) AsStdString ¶ added in v0.3.0
AsStdString is an alias for AsEtherString, returning the Wei amount as an ETH string
func (*WeiAmount) Decimal ¶
func (a *WeiAmount) Decimal() *apd.Decimal
Decimal exists to reduce ugly casts
func (*WeiAmount) MarshalText ¶
MarshalText hands off JSON encoding to apd.Decimal
func (*WeiAmount) NumStdDecimals ¶ added in v0.3.0
NumStdDecimals returns 18
func (*WeiAmount) Sub ¶
Sub returns the value of a-b in a newly allocated WeiAmount variable. If a or b is NaN, this function will panic, but we exclude such values during input validation.
func (*WeiAmount) TokenAddress ¶
TokenAddress returns the all-zero address as WeiAmount is not an ERC20 token
func (*WeiAmount) UnmarshalText ¶
UnmarshalText hands off JSON decoding to apd.Decimal