solana-go/serum/market.go

74 lines
2.3 KiB
Go
Raw Normal View History

2020-11-09 10:09:50 -08:00
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2020-11-06 13:40:05 -08:00
package serum
import (
"math/big"
2020-11-06 13:52:19 -08:00
2020-11-06 13:40:05 -08:00
"github.com/dfuse-io/solana-go"
2020-11-06 14:04:34 -08:00
"github.com/dfuse-io/solana-go/token"
2020-11-06 13:40:05 -08:00
)
type MarketMeta struct {
2020-11-06 14:02:52 -08:00
Address solana.PublicKey `json:"address"`
Name string `json:"name"`
Deprecated bool `json:"deprecated"`
QuoteMint token.Mint
BaseMint token.Mint
2020-11-06 13:52:19 -08:00
MarketV2 MarketV2
2020-11-06 13:40:05 -08:00
}
func (m *MarketMeta) baseSplTokenMultiplier() *big.Int {
return solana.DecimalsInBigInt(uint32(m.BaseMint.Decimals))
}
func (m *MarketMeta) quoteSplTokenMultiplier() *big.Int {
2020-11-06 17:03:49 -08:00
return solana.DecimalsInBigInt(uint32(m.QuoteMint.Decimals))
}
func divideBnToNumber(numerator, denomiator *big.Float) *big.Float {
return F().Quo(numerator, denomiator)
}
func (m *MarketMeta) PriceLotsToNumber(price *big.Int) *big.Float {
ratio := I().Mul(I().SetInt64(int64(m.MarketV2.QuoteLotSize)), m.baseSplTokenMultiplier())
numerator := F().Mul(F().SetInt(price), F().SetInt(ratio))
denomiator := F().Mul(F().SetFloat64(float64(m.MarketV2.BaseLotSize)), F().SetInt(m.quoteSplTokenMultiplier()))
v := divideBnToNumber(numerator, denomiator)
return v
}
2020-11-06 14:54:26 -08:00
func (m *MarketMeta) BaseSizeLotsToNumber(size *big.Int) *big.Float {
numerator := I().Mul(size, I().SetInt64(int64(m.MarketV2.BaseLotSize)))
2020-11-06 14:54:26 -08:00
denomiator := m.baseSplTokenMultiplier()
return F().Quo(F().SetInt(numerator), F().SetInt(denomiator))
2020-11-06 14:54:26 -08:00
}
func (m *MarketMeta) PriceNumberToLots(price *big.Int) *big.Float {
numerator := I().Mul(price, m.quoteSplTokenMultiplier())
numerator = I().Mul(numerator, big.NewInt(int64(m.MarketV2.BaseLotSize)))
denomiator := I().Mul(m.baseSplTokenMultiplier(), I().SetInt64(int64(m.MarketV2.QuoteLotSize)))
return F().Quo(F().SetInt(numerator), F().SetInt(denomiator))
}
func I() *big.Int {
return new(big.Int)
}
func F() *big.Float {
return new(big.Float)
}