Added market price conversation functions

This commit is contained in:
Julien Cassis 2020-11-06 17:36:03 -05:00
parent 889e63cf80
commit 964b116c5d
2 changed files with 67 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package serum
import (
"encoding/json"
"fmt"
"math/big"
"os"
"github.com/dfuse-io/solana-go"
@ -19,6 +20,29 @@ type MarketMeta struct {
MarketV2 *MarketV2
}
func (m *MarketMeta) baseSplTokenMultiplier() *big.Int {
return solana.DecimalsInBigInt(uint32(m.BaseMint.Decimals))
}
func (m *MarketMeta) quoteSplTokenMultiplier() *big.Int {
return solana.DecimalsInBigInt(uint32(m.BaseMint.Decimals))
}
func (m *MarketMeta) priceLotsToNumber(price *big.Int) *big.Float {
ratio := new(big.Int).Mul(big.NewInt(int64(m.MarketV2.QuoteLotSize)), m.baseSplTokenMultiplier())
numerator := new(big.Int).Mul(price, ratio)
denomiator := new(big.Int).Mul(big.NewInt(int64(m.MarketV2.BaseLotSize)), m.quoteSplTokenMultiplier())
return new(big.Float).Quo(new(big.Float).SetInt(numerator), new(big.Float).SetInt(denomiator))
}
func (m *MarketMeta) priceNumberToLots(price *big.Int) *big.Float {
numerator := new(big.Int).Mul(price, m.quoteSplTokenMultiplier())
numerator = new(big.Int).Mul(numerator, big.NewInt(int64(m.MarketV2.BaseLotSize)))
denomiator := new(big.Int).Mul(m.baseSplTokenMultiplier(), big.NewInt(int64(m.MarketV2.QuoteLotSize)))
return new(big.Float).Quo(new(big.Float).SetInt(numerator), new(big.Float).SetInt(denomiator))
}
func KnownMarket() ([]*MarketMeta, error) {
f, err := os.Open("serum/markets.json")
if err != nil {

43
uti.go Normal file
View File

@ -0,0 +1,43 @@
package solana
import "math/big"
var _10b = big.NewInt(10)
var decimalsBigInt = []*big.Int{
new(big.Int).Exp(_10b, big.NewInt(1), nil),
new(big.Int).Exp(_10b, big.NewInt(2), nil),
new(big.Int).Exp(_10b, big.NewInt(3), nil),
new(big.Int).Exp(_10b, big.NewInt(4), nil),
new(big.Int).Exp(_10b, big.NewInt(5), nil),
new(big.Int).Exp(_10b, big.NewInt(6), nil),
new(big.Int).Exp(_10b, big.NewInt(7), nil),
new(big.Int).Exp(_10b, big.NewInt(8), nil),
new(big.Int).Exp(_10b, big.NewInt(9), nil),
new(big.Int).Exp(_10b, big.NewInt(10), nil),
new(big.Int).Exp(_10b, big.NewInt(11), nil),
new(big.Int).Exp(_10b, big.NewInt(12), nil),
new(big.Int).Exp(_10b, big.NewInt(13), nil),
new(big.Int).Exp(_10b, big.NewInt(14), nil),
new(big.Int).Exp(_10b, big.NewInt(15), nil),
new(big.Int).Exp(_10b, big.NewInt(16), nil),
new(big.Int).Exp(_10b, big.NewInt(17), nil),
new(big.Int).Exp(_10b, big.NewInt(18), nil),
}
func DecimalsInBigInt(decimal uint32) *big.Int {
var decimalsBig *big.Int
if decimal <= uint32(len(decimalsBigInt)) {
decimalsBig = decimalsBigInt[decimal-1]
} else {
decimalsBig = new(big.Int).Exp(_10b, big.NewInt(int64(decimal)), nil)
}
return decimalsBig
}
//
//func foo(numerator, denomiator *big.Int) {
// quotient := new(big.Int).Quo(numerator, denomiator)
// remainder := new(big.Int).Rem(numerator, denomiator)
// gcd := new(big.Int).GCD(nil, nil, remainder, denomiator)
//
//}