cc

package
v0.0.0-...-d48d2ef Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2018 License: MIT Imports: 6 Imported by: 0

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

View Source
const RateDigits = 8

RateDigits is the decimal precision of a Rate, where the precision is pow(10, -RateDigits).

Variables

View Source
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.

View Source
var One = NewRate(1)

One is a rate corresponding to one.

Functions

This section is empty.

Types

type Amount

type Amount struct {
	Currency
	Amount big.Int
}

Amount is a combination of a currency and a raw amount.

func Convert

func Convert(a *Amount, to Currency, r *Rate) *Amount

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

func ParseAmount(s string) (*Amount, error)

ParseAmount parses an amount from a string.

func (*Amount) Add

func (a *Amount) Add(x *Amount) *Amount

Add x to a and return a. Panics if currencies do not match.

func (*Amount) Cmp

func (a *Amount) Cmp(x *Amount) int

Cmp a to x (see big.Int.Cmp). Panics if currencies do not match.

func (*Amount) Copy

func (a *Amount) Copy() *Amount

Copy a into a new Amount.

func (*Amount) MarshalText

func (a *Amount) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Amount) Neg

func (a *Amount) Neg() *Amount

Neg negates and returns a.

func (*Amount) Scan

func (a *Amount) Scan(s fmt.ScanState, verb rune) error

Scan implements fmt.Scanner for the verbs '%s' and '%v,' and supports text produced by (*Amount).String()

func (*Amount) Set

func (a *Amount) Set(x *Amount) *Amount

Set a to x and return a.

func (*Amount) Sign

func (a *Amount) Sign() int

Sign gets the sign of a (see big.Int.Sign).

func (*Amount) String

func (a *Amount) String() string

String prints the currency amount next to the currency name, e.g.

124.09123402 BTC

func (*Amount) Sub

func (a *Amount) Sub(x *Amount) *Amount

Sub x from a and return a. Panics if currencies do not match.

func (*Amount) UnmarshalText

func (a *Amount) UnmarshalText(b []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Currency

type Currency string

Currency represents a currency along with its significant digits.

func CurrencyByName

func CurrencyByName(s string) (Currency, bool)

CurrencyByName returns the currency with the given name.

func NewCurrency

func NewCurrency(name string, digits int) Currency

NewCurrency creates and registers a new currency. This is not thread-safe and should only be used at initialization time.

func NewToken

func NewToken(name string, digits int, addr string) Currency

NewToken creates and registers a new ERC20 token. This is not thread-safe and should only be used at initialization time.

func TokenByAddress

func TokenByAddress(addr *seth.Address) (Currency, bool)

TokenByAddress returns the currency with the given contract address.

func (Currency) Addr

func (c Currency) Addr() *seth.Address

Addr returns the contract address, if this represents an ERC20 token. This panics if the currency is not registered.

func (Currency) Digits

func (c Currency) Digits() int

Digits in the currency. This panics if the currency is not registered.

func (Currency) Exists

func (c Currency) Exists() bool

Exists returns whether this currency is registered.

func (Currency) MarshalText

func (c Currency) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Currency) Name

func (c Currency) Name() string

Name of the currency. This panics if the currency is not registered.

func (Currency) Nil

func (c Currency) Nil() bool

Nil returns whether c == "".

func (Currency) ParseAmount

func (c Currency) ParseAmount(s string) (*Amount, error)

ParseAmount parses a currency amount to the maximum precision of the currency.

func (Currency) String

func (c Currency) String() string

String returns a string representation of the currency.

func (Currency) Token

func (c Currency) Token() bool

Token returns whether this is an ERC20 token. This panics if the currency is not registered.

func (*Currency) UnmarshalText

func (c *Currency) UnmarshalText(b []byte) error

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 ParsePair

func ParsePair(s string) (*Pair, error)

ParsePair parses a pair from a string.

func (*Pair) Convert

func (p *Pair) Convert(a *Amount) *Amount

Convert the given amount.

func (*Pair) FromString

func (p *Pair) FromString(s string) error

FromString parses a pair from a string.

func (*Pair) Inverse

func (p *Pair) Inverse() *Pair

Inverse returns the inverse of the pair.

func (*Pair) MarshalText

func (p *Pair) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Pair) String

func (p *Pair) String() string

String returns a string representation of the pair.

func (*Pair) UnmarshalText

func (p *Pair) UnmarshalText(b []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Rate

type Rate big.Int

Rate represents a fixed-point rate.

func NewRate

func NewRate(f float64) *Rate

NewRate returns the rate most closely approximating f.

func ParseRate

func ParseRate(s string) (*Rate, error)

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) Float

func (r *Rate) Float() float64

Float returns a floating-point approximation of the rate.

func (*Rate) Inverse

func (r *Rate) Inverse() *Rate

Inverse calculates the inverse of the rate.

func (*Rate) MarshalJSON

func (r *Rate) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Rate) Scan

func (r *Rate) Scan(s fmt.ScanState, verb rune) error

Scan implements fmt.Scanner for the verbs '%s' and '%v'

func (*Rate) Sign

func (r *Rate) Sign() int

Sign returns the sign of the rate.

func (*Rate) String

func (r *Rate) String() string

String returns the rate as a string like "0.17439082"

func (*Rate) UnmarshalJSON

func (r *Rate) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

Jump to

Keyboard shortcuts

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