2021-10-07 10:28:16 -07:00
|
|
|
// Copyright 2021 github.com/gagliardetto
|
|
|
|
// This file has been modified by github.com/gagliardetto
|
|
|
|
//
|
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 (
|
2020-11-06 14:36:03 -08:00
|
|
|
"math/big"
|
2020-11-06 13:52:19 -08:00
|
|
|
|
2021-07-06 09:18:26 -07:00
|
|
|
"github.com/gagliardetto/solana-go"
|
|
|
|
"github.com/gagliardetto/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"`
|
2020-11-09 09:59:24 -08:00
|
|
|
QuoteMint token.Mint
|
|
|
|
BaseMint token.Mint
|
2020-11-06 13:52:19 -08:00
|
|
|
|
2020-11-09 09:59:24 -08:00
|
|
|
MarketV2 MarketV2
|
2020-11-06 13:40:05 -08:00
|
|
|
}
|
|
|
|
|
2020-11-06 14:36:03 -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))
|
|
|
|
}
|
|
|
|
|
2020-11-09 07:02:28 -08:00
|
|
|
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:36:03 -08:00
|
|
|
}
|
|
|
|
|
2020-11-06 14:54:26 -08:00
|
|
|
func (m *MarketMeta) BaseSizeLotsToNumber(size *big.Int) *big.Float {
|
2020-11-09 07:02:28 -08:00
|
|
|
numerator := I().Mul(size, I().SetInt64(int64(m.MarketV2.BaseLotSize)))
|
2020-11-06 14:54:26 -08:00
|
|
|
denomiator := m.baseSplTokenMultiplier()
|
2020-11-09 07:02:28 -08:00
|
|
|
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 {
|
2020-11-09 07:02:28 -08:00
|
|
|
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-06 14:36:03 -08:00
|
|
|
}
|
|
|
|
|
2020-11-20 10:15:27 -08:00
|
|
|
type OpenOrdersMeta struct {
|
2021-02-15 10:08:45 -08:00
|
|
|
OpenOrders OpenOrders
|
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
|
|
|
)
|