Documentation ¶
Overview ¶
package cc is a library for converting between cryptocurrencies. It implements fixed-point high-precision rate conversion and manipulation, and it also contains some knowledge about the number of digits on certain common cryptocurrencies and ERC20 tokens.
Index ¶
- Constants
- Variables
- type Amount
- func (a *Amount) Add(x *Amount) *Amount
- func (a *Amount) Cmp(x *Amount) int
- func (a *Amount) Copy() *Amount
- func (a *Amount) MarshalText() ([]byte, error)
- func (a *Amount) Neg() *Amount
- func (a *Amount) Scan(s fmt.ScanState, verb rune) error
- func (a *Amount) Set(x *Amount) *Amount
- func (a *Amount) Sign() int
- func (a *Amount) String() string
- func (a *Amount) Sub(x *Amount) *Amount
- func (a *Amount) UnmarshalText(b []byte) error
- type Currency
- func (c Currency) Addr() *seth.Address
- func (c Currency) Digits() int
- func (c Currency) Exists() bool
- func (c Currency) MarshalText() ([]byte, error)
- func (c Currency) Name() string
- func (c Currency) Nil() bool
- func (c Currency) ParseAmount(s string) (*Amount, error)
- func (c Currency) String() string
- func (c Currency) Token() bool
- func (c *Currency) UnmarshalText(b []byte) error
- type Pair
- type Rate
Constants ¶
const RateDigits = 8
RateDigits is the decimal precision of a Rate, where the precision is pow(10, -RateDigits).
Variables ¶
var ( EUR = NewCurrency("EUR", 8) GBP = NewCurrency("GBP", 8) USD = NewCurrency("USD", 8) BTC = NewCurrency("BTC", 8) ETH = NewCurrency("ETH", 18) ZEC = NewCurrency("ZEC", 9) AION = NewToken("AION", 8, "0x4CEdA7906a5Ed2179785Cd3A40A69ee8bc99C466") AIR = NewToken("AIR", 8, "0x27dce1ec4d3f72c3e457cc50354f1f975ddef488") ANT = NewToken("ANT", 18, "0x960b236a07cf122663c4303350609a66a7b288c0") BAT = NewToken("BAT", 18, "0x0d8775f648430679a709e98d2b0cb6250d2887ef") BNT = NewToken("BNT", 18, "0x1f573d6fb3f13d689ff844b4ce37794d79a7ff1c") DGD = NewToken("DGD", 9, "0xe0b7927c4af23765cb51314a0e0521a9645f0e2a") DICE = NewToken("DICE", 16, "0x2e071d2966aa7d8decb1005885ba1977d6038a65") EDG = NewToken("EDG", 0, "0x08711d3b02c8758f2fb3ab4e80228418a7f8e39c") EOS = NewToken("EOS", 18, "0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0") FUCK = NewToken("FUCK", 4, "0xc63e7b1dece63a77ed7e4aeef5efb3b05c81438d") FUN = NewToken("FUN", 8, "0xbbb1bd2d741f05e144e6c4517676a15554fd4b8d") GNO = NewToken("GNO", 18, "0x6810e776880c02933d47db1b9fc05908e5386b96") GNT = NewToken("GNT", 18, "0xa74476443119a942de498590fe1f2454d7d4ac0d") GOOD = NewToken("GOOD", 6, "0xae616e72d3d89e847f74e8ace41ca68bbf56af79") GUP = NewToken("GUP", 3, "0xf7b098298f7c69fc14610bf71d5e02c60792894c") HODL = NewToken("HODL", 8, "0xb4b7d0c65b3618bc8706ab7b3719519ead624067") ICN = NewToken("ICN", 18, "0x888666ca69e0f178ded6d75b5726cee99a87d698") JTT1 = NewToken("JTT1", 8, "0xb1e7688a1cc678a035342b250d348f2c131bd8fb") JTT2 = NewToken("JTT2", 8, "0x4b59841ac0fbe6eaa3ad2978dad8d0e1c76a9237") MCAP = NewToken("MCAP", 8, "0x93e682107d1e9defb0b5ee701c71707a4b2e46bc") MKR = NewToken("MKR", 18, "0xc66ea802717bfb9833400264dd12c2bceaa34a6d") MLN = NewToken("MLN", 18, "0xbeb9ef514a379b997e0798fdcc901ee474b6d9a1") OMG = NewToken("OMG", 18, "0xd26114cd6ee289accf82350c8d8487fedb8a0c07") OST = NewToken("1ST", 18, "0xaf30d2a7e90d7dc361c8c4585e9bb7d2f6f15bc7") PLBT = NewToken("PLBT", 6, "0x0affa06e7fbe5bc9a764c979aa66e8256a631f02") PLU = NewToken("PLU", 18, "0xd8912c10681d8b21fd3742244f44658dba12264e") QTUM = NewToken("QTUM", 18, "0x9a642d6b3368ddc662CA244bAdf32cDA716005BC") REP = NewToken("REP", 18, "0x48c80f1f4d53d5951e5d5438b54cba84f29f32a5") RLC = NewToken("RLC", 9, "0x607f4c5bb672230e8672085532f7e901544a7375") SCAM = NewToken("SCAM", 18, "0x49488350b4b2ed2fd164dd0d50b00e7e3f531651") SNGLS = NewToken("SNGLS", 0, "0xaec2e87e0a235266d9c5adc9deb4b2e29b54d009") SNT = NewToken("SNT", 18, "0x744d70fdbe2ba4cf95131626614a1763df805b9e") SWT = NewToken("SWT", 18, "0xb9e7f8568e08d5659f5d29c4997173d84cdf2607") TKN = NewToken("TKN", 8, "0xaaaf91d9b90df800df4f55c205fd6989c977e73a") UET = NewToken("UET", 18, "0x27f706edde3ad952ef647dd67e24e38cd0803dd6") UNI = NewToken("🦄", 0, "0x89205a3a3b2a69de6dbf7f01ed13b2108b2c43e7") VEROS = NewToken("VEROS", 5, "0xedbaf3c5100302dcdda53269322f3730b1f0416d") VSL = NewToken("VSL", 18, "0x5c543e7ae0a1104f78406c340e9c64fd9fce5170") WINGS = NewToken("WINGS", 18, "0x667088b212ce3d06a1b553a7221e1fd19000d9af") XAUR = NewToken("XAUR", 8, "0x4df812f6064def1e5e029f1ca858777cc98d2d81") ZRX = NewToken("ZRX", 18, "0xe41d2489571d322189246dafa5ebde1f4699f498") )
Well-known currencies and tokens.
var One = NewRate(1)
One is a rate corresponding to one.
Functions ¶
This section is empty.
Types ¶
type Amount ¶
Amount is a combination of a currency and a raw amount.
func Convert ¶
Convert converts the given amount to a new currency using the given Rate. The conversion is done with at least RateDigits decimals of precision.
func ParseAmount ¶
ParseAmount parses an amount from a string.
func (*Amount) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Amount) Scan ¶
Scan implements fmt.Scanner for the verbs '%s' and '%v,' and supports text produced by (*Amount).String()
func (*Amount) String ¶
String prints the currency amount next to the currency name, e.g.
124.09123402 BTC
func (*Amount) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Currency ¶
type Currency string
Currency represents a currency along with its significant digits.
func CurrencyByName ¶
CurrencyByName returns the currency with the given name.
func NewCurrency ¶
NewCurrency creates and registers a new currency. This is not thread-safe and should only be used at initialization time.
func NewToken ¶
NewToken creates and registers a new ERC20 token. This is not thread-safe and should only be used at initialization time.
func TokenByAddress ¶
TokenByAddress returns the currency with the given contract address.
func (Currency) Addr ¶
Addr returns the contract address, if this represents an ERC20 token. This panics if the currency is not registered.
func (Currency) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (Currency) ParseAmount ¶
ParseAmount parses a currency amount to the maximum precision of the currency.
func (Currency) Token ¶
Token returns whether this is an ERC20 token. This panics if the currency is not registered.
func (*Currency) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Pair ¶
type Pair struct {
From, To Currency // From and To are the pair currencies.
Rate Rate // Rate is the exchange rate of the pair.
}
A Pair represents a conversion rate between a pair of currencies.
func (*Pair) FromString ¶
FromString parses a pair from a string.
func (*Pair) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Pair) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Rate ¶
Rate represents a fixed-point rate.
func ParseRate ¶
ParseRate parses a string into a Rate. The string should be a decimal number like 123.456, with up to 8 digits after the decimal.
func (*Rate) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Rate) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.