common

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2020 License: MIT Imports: 29 Imported by: 35

Documentation

Index

Constants

View Source
const (
	// BNBSymbol BNB
	BNBSymbol = Symbol("BNB")
	// Rune67CSymbol RUNE on binance testnet
	Rune67CSymbol = Symbol("RUNE-67C")
	// RuneB1ASymbol RUNE on binance mainnet
	RuneB1ASymbol = Symbol("RUNE-B1A")
)
View Source
const (
	// BNBTicker BNB
	BNBTicker = Ticker("BNB")
	// RuneTicker RUNE
	RuneTicker = Ticker("RUNE")
)
View Source
const NoSigningAlgo = keys.SigningAlgo("")

NoSigningAlgo empty signing algorithm

View Source
const One = 100000000

One is useful type so THORNode doesn't need to manage 8 zeroes all the time

Variables

View Source
var (
	// EmptyAsset empty asset, not valid
	EmptyAsset = Asset{Chain: EmptyChain, Symbol: "", Ticker: ""}
	// BNBAsset BNB
	BNBAsset = Asset{Chain: BNBChain, Symbol: "BNB", Ticker: "BNB"}
	// BTCAsset BTC
	BTCAsset = Asset{Chain: BTCChain, Symbol: "BTC", Ticker: "BTC"}
	// ETHAsset ETH
	ETHAsset = Asset{Chain: ETHChain, Symbol: "ETH", Ticker: "ETH"}
	// Rune67CAsset RUNE on Binance test net
	Rune67CAsset = Asset{Chain: BNBChain, Symbol: "RUNE-67C", Ticker: "RUNE"} // testnet asset on binance ganges
	// RuneB1AAsset RUNE on Binance main net
	RuneB1AAsset = Asset{Chain: BNBChain, Symbol: "RUNE-B1A", Ticker: "RUNE"} // mainnet
	// RuneNative RUNE on thorchain
	RuneNative = Asset{Chain: THORChain, Symbol: "RUNE", Ticker: "RUNE"}
)
View Source
var (
	EmptyChain = Chain("")
	BNBChain   = Chain("BNB")
	ETHChain   = Chain("ETH")
	BTCChain   = Chain("BTC")
	THORChain  = Chain("THOR")
)
View Source
var BNBGasFeeMulti = Gas{
	{Asset: BNBAsset, Amount: bnbMultiTxFee},
}

BNBGasFeeMulti gas fee for multi send

View Source
var BNBGasFeeSingleton = Gas{
	{Asset: BNBAsset, Amount: bnbSingleTxFee},
}

BNBGasFeeSingleton fee charged by Binance for transfer with a single coin

View Source
var BlankTxID = TxID("0000000000000000000000000000000000000000000000000000000000000000")

BlankTxID represent blank

View Source
var ETHGasFeeTransfer = Gas{
	{Asset: ETHAsset, Amount: ethTransferFee},
}

ETHGasFeeTransfer gas fee for ETH

View Source
var NoCoin = Coin{
	Amount: cosmos.ZeroUint(),
}

Functions

func BlockHeight

func BlockHeight(ctx cosmos.Context) int64

BlockHeight return the adjusted block height

func ConvertAndEncode

func ConvertAndEncode(hrp string, data []byte) (string, error)

ConvertAndEncode converts from a base64 encoded byte string to hex or base32 encoded byte string and then to bech32

func Decrypt

func Decrypt(data []byte, passphrase string) ([]byte, error)

Decrypt the input data with passphrase

func Encrypt

func Encrypt(data []byte, passphrase string) ([]byte, error)

Encrypt the input data with passphrase

func GetShare

func GetShare(part, total, allocation cosmos.Uint) cosmos.Uint

GetShare this method will panic if any of the input parameter can't be convert to cosmos.Dec which shouldn't happen

func RandStringBytesMask

func RandStringBytesMask(n int) string

RandStringBytesMask generate random string used for test purpose

func SafeSub

func SafeSub(input1, input2 cosmos.Uint) cosmos.Uint

SafeSub subtract input2 from input1, given cosmos.Uint can't be negative , otherwise it will panic thus in this method,when input2 is larger than input 1, it will just return cosmos.ZeroUint

func UpdateGasPrice

func UpdateGasPrice(tx Tx, asset Asset, units []cosmos.Uint) []cosmos.Uint

UpdateGasPrice update gas based on the input tx

Types

type Account

type Account struct {
	Sequence      int64
	AccountNumber int64
	Coins         AccountCoins
	HasMemoFlag   bool
}

func NewAccount

func NewAccount(sequence, accountNumber int64, coins AccountCoins, hasMemoFlag bool) Account

NewAccount

type AccountCoin

type AccountCoin struct {
	Amount uint64
	Denom  string
}

type AccountCoins

type AccountCoins []AccountCoin

func GetCoins

func GetCoins(accCoins []types.Coin) AccountCoins

GetCoins transforms from binance coins

type Address

type Address string
var NoAddress Address = Address("")

func NewAddress

func NewAddress(address string) (Address, error)

NewAddress create a new Address. Supports Binance, Bitcoin, and Ethereum

func (Address) Equals

func (addr Address) Equals(addr2 Address) bool

func (Address) IsChain

func (addr Address) IsChain(chain Chain) bool

func (Address) IsEmpty

func (addr Address) IsEmpty() bool

func (Address) String

func (addr Address) String() string

type Asset

type Asset struct {
	Chain  Chain  `json:"chain"`
	Symbol Symbol `json:"symbol"`
	Ticker Ticker `json:"ticker"`
}

Asset represent an asset in THORChain it is in BNB.BNB format for example BNB.RUNE-67C , BNB.RUNE-B1A

func BEP2RuneAsset

func BEP2RuneAsset() Asset

BEP2RuneAsset is RUNE on BEP2

func NewAsset

func NewAsset(input string) (Asset, error)

NewAsset parse the given input into Asset object

func RuneAsset

func RuneAsset() Asset

RuneAsset return RUNE Asset depends on different environment

func (Asset) Equals

func (a Asset) Equals(a2 Asset) bool

Equals determinate whether two assets are equivalent

func (Asset) IsBNB

func (a Asset) IsBNB() bool

IsBNB is a helper function, return true only when the asset represent BNB

func (Asset) IsEmpty

func (a Asset) IsEmpty() bool

IsEmpty will be true when any of the field is empty, chain,symbol or ticker

func (Asset) IsRune

func (a Asset) IsRune() bool

IsRune is a helper function ,return true only when the asset represent RUNE

func (Asset) MarshalJSON

func (a Asset) MarshalJSON() ([]byte, error)

MarshalJSON implement Marshaler interface

func (Asset) Native

func (a Asset) Native() string

Native return native asset, only relevant on THORChain

func (Asset) String

func (a Asset) String() string

String implement fmt.Stringer , return the string representation of Asset

func (*Asset) UnmarshalJSON

func (a *Asset) UnmarshalJSON(data []byte) error

UnmarshalJSON implement Unmarshaler interface

type Chain

type Chain string

Chain is an alias of string , represent a block chain

func NewChain

func NewChain(chainID string) (Chain, error)

NewChain create a new Chain and default the siging_algo to Secp256k1

func (Chain) AddressPrefix

func (c Chain) AddressPrefix(cn ChainNetwork) string

AddressPrefix return the address prefix used by the given network (testnet/mainnet)

func (Chain) Equals

func (c Chain) Equals(c2 Chain) bool

Equals compare two chain to see whether they represent the same chain

func (Chain) GetGasAsset

func (c Chain) GetGasAsset() Asset

GetGasAsset chain's base asset

func (Chain) GetSigningAlgo

func (c Chain) GetSigningAlgo() keys.SigningAlgo

GetSigningAlgo get the signing algorithm for the given chain

func (Chain) IsBNB

func (c Chain) IsBNB() bool

IsBNB determinate whether it is BNBChain

func (Chain) IsEmpty

func (c Chain) IsEmpty() bool

IsEmpty is to determinate whether the chain is empty

func (Chain) String

func (c Chain) String() string

String implement fmt.Stringer

func (Chain) Validate

func (c Chain) Validate() error

Validate validates chain format, should consist only of uppercase letters

type ChainNetwork

type ChainNetwork uint8

ChainNetwork is to indicate which chain environment THORNode are working with

const (
	// TestNet network for test
	TestNet ChainNetwork = iota
	// MainNet network for main net
	MainNet
	// MockNet network for main net
	MockNet
)

func GetCurrentChainNetwork

func GetCurrentChainNetwork() ChainNetwork

GetCurrentChainNetwork determinate what kind of network currently it is working with

type ChainPoolInfo

type ChainPoolInfo struct {
	Chain       Chain   `json:"chain"`
	PubKey      PubKey  `json:"pub_key"`
	PoolAddress Address `json:"pool_address"`
}

ChainPoolInfo represent the pool address specific for a chain

var EmptyChainPoolInfo ChainPoolInfo

EmptyChainPoolInfo everything is empty

func NewChainPoolInfo

func NewChainPoolInfo(chain Chain, pubKey PubKey) (ChainPoolInfo, error)

NewChainPoolInfo create a new instance of ChainPoolInfo

func (ChainPoolInfo) IsEmpty

func (cpi ChainPoolInfo) IsEmpty() bool

IsEmpty whether the struct is empty

type Chains

type Chains []Chain

Chains represent a slice of Chain

func (Chains) Distinct

func (chains Chains) Distinct() Chains

Distinct return a distinct set of chains, no duplicates

func (Chains) Has

func (chains Chains) Has(c Chain) bool

Has check whether chain c is in the list

type Coin

type Coin struct {
	Asset  Asset       `json:"asset"`
	Amount cosmos.Uint `json:"amount"`
}

func NewCoin

func NewCoin(asset Asset, amount cosmos.Uint) Coin

NewCoin return a new instance of Coin

func (Coin) Equals

func (c Coin) Equals(cc Coin) bool

func (Coin) IsEmpty

func (c Coin) IsEmpty() bool

func (Coin) IsNative

func (c Coin) IsNative() bool

func (Coin) Native

func (c Coin) Native() (cosmos.Coin, error)

func (Coin) String

func (c Coin) String() string

func (Coin) Valid

func (c Coin) Valid() error

type Coins

type Coins []Coin

func (Coins) Contains

func (cs Coins) Contains(c Coin) bool

Contains check whether the given coin is in the list

func (Coins) Equals

func (cs1 Coins) Equals(cs2 Coins) bool

Check if two lists of coins are equal to each other. Order does not matter

func (Coins) GetCoin

func (cs Coins) GetCoin(asset Asset) Coin

Gets a specific coin by asset. Assumes there is only one of this coin in the list.

func (Coins) IsEmpty

func (cs Coins) IsEmpty() bool

func (Coins) Native

func (cs Coins) Native() (cosmos.Coins, error)

func (Coins) String

func (cs Coins) String() string

func (Coins) Valid

func (cs Coins) Valid() error

type Duration

type Duration struct {
	time.Duration
}

Duration embedded time.Duration so THORNode could use string to represent duration in json file for example ,1s ,1h , 5m etc

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON marshal the duration to json string

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON convert the json value back to time.Duration

type Fee

type Fee struct {
	Coins      Coins       `json:"coins"`
	PoolDeduct cosmos.Uint `json:"pool_deduct"`
}

Fee represent fee

func NewFee

func NewFee(coins Coins, poolDeduct cosmos.Uint) Fee

NewFee return a new instance of Fee

type Gas

type Gas Coins

Gas coins

func CalcBinanceGasPrice

func CalcBinanceGasPrice(tx Tx, asset Asset, units []cosmos.Uint) Gas

CalcBinanceGasPrice calculate gas price for Binance chain

func GetETHGasFee

func GetETHGasFee(gasPrice *big.Int, msgLen uint64) Gas

GetETHGasFee return the gas for ETH

func MakeETHGas

func MakeETHGas(gasPrice *big.Int, gas uint64) Gas

MakeETHGas return the gas for ETH

func (Gas) Add

func (g Gas) Add(g2 Gas) Gas

Add combines two gas objects into one, adding amounts where needed or appending new coins.

func (Gas) Equals

func (g Gas) Equals(gas2 Gas) bool

Equals Check if two lists of coins are equal to each other. Order does not matter

func (Gas) IsEmpty

func (g Gas) IsEmpty() bool

IsEmpty return true as long as there is one coin in it that is not empty

func (Gas) ToCoins

func (g Gas) ToCoins() Coins

ToCoins convert the gas to Coins

func (Gas) Valid

func (g Gas) Valid() error

Valid return nil when it is valid, otherwise return an error

type PubKey

type PubKey string

PubKey used in thorchain, it should be bech32 encoded string thus it will be something like tthorpub1addwnpepqt7qug8vk9r3saw8n4r803ydj2g3dqwx0mvq5akhnze86fc536xcycgtrnv tthorpub1addwnpepqdqvd4r84lq9m54m5kk9sf4k6kdgavvch723pcgadulxd6ey9u70k6zq8qe

var EmptyPubKey PubKey

EmptyPubKey

func NewPubKey

func NewPubKey(key string) (PubKey, error)

NewPubKey create a new instance of PubKey key is bech32 encoded string

func NewPubKeyFromCrypto

func NewPubKeyFromCrypto(pk crypto.PubKey) (PubKey, error)

NewPubKeyFromCrypto

func (PubKey) Equals

func (pubKey PubKey) Equals(pubKey1 PubKey) bool

Equals check whether two are the same

func (PubKey) GetAddress

func (pubKey PubKey) GetAddress(chain Chain) (Address, error)

GetAddress will return an address for the given chain

func (PubKey) GetThorAddress

func (pubKey PubKey) GetThorAddress() (cosmos.AccAddress, error)

func (PubKey) IsEmpty

func (pubKey PubKey) IsEmpty() bool

IsEmpty to check whether it is empty

func (PubKey) MarshalJSON

func (pubKey PubKey) MarshalJSON() ([]byte, error)

MarshalJSON to Marshals to JSON using Bech32

func (PubKey) String

func (pubKey PubKey) String() string

String stringer implementation

func (*PubKey) UnmarshalJSON

func (pubKey *PubKey) UnmarshalJSON(data []byte) error

UnmarshalJSON to Unmarshal from JSON assuming Bech32 encoding

type PubKeySet

type PubKeySet struct {
	Secp256k1 PubKey `json:"secp256k1"`
	Ed25519   PubKey `json:"ed25519"`
}

PubKeySet contains two pub keys , secp256k1 and ed25519

var EmptyPubKeySet PubKeySet

EmptyPubKeySet

func NewPubKeySet

func NewPubKeySet(secp256k1, ed25519 PubKey) PubKeySet

NewPubKeySet create a new instance of PubKeySet , which contains two keys

func (PubKeySet) Contains

func (pks PubKeySet) Contains(pk PubKey) bool

func (PubKeySet) Equals

func (pks PubKeySet) Equals(pks1 PubKeySet) bool

Equals check whether two PubKeySet are the same

func (PubKeySet) GetAddress

func (pks PubKeySet) GetAddress(chain Chain) (Address, error)

GetAddress

func (PubKeySet) IsEmpty

func (pks PubKeySet) IsEmpty() bool

IsEmpty will determinate whether PubKeySet is an empty

func (PubKeySet) String

func (pks PubKeySet) String() string

String implement fmt.Stinger

type PubKeys

type PubKeys []PubKey

PubKey used in thorchain, it should be bech32 encoded string thus it will be something like tthorpub1addwnpepqt7qug8vk9r3saw8n4r803ydj2g3dqwx0mvq5akhnze86fc536xcycgtrnv tthorpub1addwnpepqdqvd4r84lq9m54m5kk9sf4k6kdgavvch723pcgadulxd6ey9u70k6zq8qe

func (PubKeys) Contains

func (pks PubKeys) Contains(pk PubKey) bool

func (PubKeys) String

func (pks PubKeys) String() string

String implement stringer interface

func (PubKeys) Valid

func (pks PubKeys) Valid() error

type Symbol

type Symbol string

Symbol represent an asset

func NewSymbol

func NewSymbol(input string) (Symbol, error)

NewSymbol parse the input as symbol

func (Symbol) Equals

func (s Symbol) Equals(s2 Symbol) bool

Equals check whether two symbol are the same

func (Symbol) IsEmpty

func (s Symbol) IsEmpty() bool

IsEmpty return true when symbol is just empty string

func (Symbol) IsMiniToken

func (s Symbol) IsMiniToken() bool

IsMiniToken is to determine whether it is a mini token on binance chain

func (Symbol) String

func (s Symbol) String() string

String implement fmt.Stringer

func (Symbol) Ticker

func (s Symbol) Ticker() Ticker

Ticker return the ticker part of symbol

type Ticker

type Ticker string

Ticker The trading 'symbol' or shortened name (typically in capital letters) that refer to a coin on a trading platform. For example: BNB

func NewTicker

func NewTicker(ticker string) (Ticker, error)

NewTicker parse the given string as ticker, return error if it is not legitimate ticker

func (Ticker) Equals

func (t Ticker) Equals(t2 Ticker) bool

Equals compare whether two ticker is the same

func (Ticker) IsEmpty

func (t Ticker) IsEmpty() bool

IsEmpty return true when the ticker is an empty string

func (Ticker) String

func (t Ticker) String() string

String implement fmt.Stringer

type Tickers

type Tickers []Ticker

Tickers a list of ticker

type Tx

type Tx struct {
	ID          TxID    `json:"id"`
	Chain       Chain   `json:"chain"`
	FromAddress Address `json:"from_address"`
	ToAddress   Address `json:"to_address"`
	Coins       Coins   `json:"coins"`
	Gas         Gas     `json:"gas"`
	Memo        string  `json:"memo"`
}

Tx transaction

func GetRagnarokTx

func GetRagnarokTx(chain Chain, fromAddr, toAddr Address) Tx

GetRagnarokTx return a tx used for ragnarok

func NewTx

func NewTx(txID TxID, from, to Address, coins Coins, gas Gas, memo string) Tx

NewTx create a new instance of Tx based on the input information

func (Tx) Equals

func (tx Tx) Equals(tx2 Tx) bool

Equals compare two Tx to see whether they represent the same Tx

func (Tx) Hash

func (tx Tx) Hash() string

Hash calculate a hash based on from address, coins and to address

func (Tx) IsEmpty

func (tx Tx) IsEmpty() bool

IsEmpty check whether the ID field is empty or not

func (Tx) String

func (tx Tx) String() string

String implement fmt.Stringer return a string representation of the tx

func (Tx) ToAttributes

func (tx Tx) ToAttributes() []cosmos.Attribute

ToAttributes push all the tx fields into a slice of cosmos Attribute(key value pairs)

func (Tx) Valid

func (tx Tx) Valid() error

Valid do some data sanity check , if the tx contains invalid information it will return an none nil error

type TxID

type TxID string

TxID is a string that can uniquely represent a transaction on different block chain

func NewTxID

func NewTxID(hash string) (TxID, error)

NewTxID parse the input hash as TxID

func (TxID) Equals

func (tx TxID) Equals(tx2 TxID) bool

Equals check whether two TxID are the same

func (TxID) IsEmpty

func (tx TxID) IsEmpty() bool

IsEmpty return true when the tx represent empty string

func (TxID) String

func (tx TxID) String() string

String implement fmt.Stringer

type TxIDs

type TxIDs []TxID

TxIDs is a slice of TxID

type Txs

type Txs []Tx

Txs a list of Tx

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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