github.com/gagliardetto/solana-go@v1.11.0/programs/serum/market.go (about) 1 // Copyright 2021 github.com/gagliardetto 2 // This file has been modified by github.com/gagliardetto 3 // 4 // Copyright 2020 dfuse Platform Inc. 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package serum 19 20 import ( 21 "math/big" 22 23 "github.com/gagliardetto/solana-go" 24 "github.com/gagliardetto/solana-go/programs/token" 25 ) 26 27 type MarketMeta struct { 28 Address solana.PublicKey `json:"address"` 29 Name string `json:"name"` 30 Deprecated bool `json:"deprecated"` 31 QuoteMint token.Mint 32 BaseMint token.Mint 33 34 MarketV2 MarketV2 35 } 36 37 func (m *MarketMeta) baseSplTokenMultiplier() *big.Int { 38 return solana.DecimalsInBigInt(uint32(m.BaseMint.Decimals)) 39 } 40 41 func (m *MarketMeta) quoteSplTokenMultiplier() *big.Int { 42 return solana.DecimalsInBigInt(uint32(m.QuoteMint.Decimals)) 43 } 44 45 func (m *MarketMeta) PriceLotsToNumber(price *big.Int) *big.Float { 46 ratio := I().Mul(I().SetInt64(int64(m.MarketV2.QuoteLotSize)), m.baseSplTokenMultiplier()) 47 numerator := F().Mul(F().SetInt(price), F().SetInt(ratio)) 48 denomiator := F().Mul(F().SetFloat64(float64(m.MarketV2.BaseLotSize)), F().SetInt(m.quoteSplTokenMultiplier())) 49 v := divideBnToNumber(numerator, denomiator) 50 return v 51 } 52 53 func (m *MarketMeta) BaseSizeLotsToNumber(size *big.Int) *big.Float { 54 numerator := I().Mul(size, I().SetInt64(int64(m.MarketV2.BaseLotSize))) 55 denomiator := m.baseSplTokenMultiplier() 56 return F().Quo(F().SetInt(numerator), F().SetInt(denomiator)) 57 } 58 59 func (m *MarketMeta) PriceNumberToLots(price *big.Int) *big.Float { 60 numerator := I().Mul(price, m.quoteSplTokenMultiplier()) 61 numerator = I().Mul(numerator, big.NewInt(int64(m.MarketV2.BaseLotSize))) 62 denomiator := I().Mul(m.baseSplTokenMultiplier(), I().SetInt64(int64(m.MarketV2.QuoteLotSize))) 63 return F().Quo(F().SetInt(numerator), F().SetInt(denomiator)) 64 } 65 66 type OpenOrdersMeta struct { 67 OpenOrders OpenOrders 68 } 69 70 type SideLayoutType string 71 72 const ( 73 SideLayoutTypeUnknown SideLayoutType = "UNKNOWN" 74 SideLayoutTypeBid SideLayoutType = "BID" 75 SideLayoutTypeAsk SideLayoutType = "ASK" 76 ) 77 78 type SideLayout uint32 79 80 func (s SideLayout) getSide() SideLayoutType { 81 switch s { 82 case 0: 83 return SideLayoutTypeBid 84 case 1: 85 return SideLayoutTypeAsk 86 } 87 return SideLayoutTypeUnknown 88 } 89 90 type OrderType uint32 91 92 const ( 93 OrderTypeLimit = OrderType(iota) 94 OrderTypeImmediateOrCancel 95 OrderTypePostOnly 96 )