solana-go/programs/serum/market.go

100 lines
2.8 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"
"github.com/dfuse-io/solana-go/programs/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 (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))
}
2020-11-20 10:15:27 -08:00
type OpenOrdersMeta struct {
OpenOrdersV2 OpenOrdersV2
}
type Order struct {
Limit *big.Int `json:"limit"`
Side SideLayout `json:"side"`
Price *big.Int `json:"price"`
}
2020-11-20 10:15:27 -08:00
type SideLayoutType string
const (
SideLayoutTypeUnknown SideLayoutType = "UNKNOWN"
SideLayoutTypeBid SideLayoutType = "BID"
SideLayoutTypeAsk SideLayoutType = "ASK"
)
type SideLayout uint32
func (s SideLayout) getSide() SideLayoutType {
switch s {
case 0:
return SideLayoutTypeBid
case 1:
return SideLayoutTypeAsk
}
return SideLayoutTypeUnknown
}
2020-12-21 06:59:46 -08:00
type OrderType uint32
2020-11-20 10:15:27 -08:00
const (
2020-12-21 08:51:15 -08:00
OrderTypeLimit = OrderType(iota)
2020-12-21 06:59:46 -08:00
OrderTypeImmediateOrCancel
OrderTypePostOnly
2020-11-20 10:15:27 -08:00
)