fix EncodeTxBinary
This commit is contained in:
37
tx_binary.go
37
tx_binary.go
@@ -80,8 +80,8 @@ type SwapBinary struct {
|
||||
ActualLimitAmountSide SwapAmountSide
|
||||
SlippageBps uint64
|
||||
|
||||
BaseReserve uint64
|
||||
QuoteReserve uint64
|
||||
BaseReserve float64
|
||||
QuoteReserve float64
|
||||
Mayhem bool
|
||||
Cashback bool
|
||||
|
||||
@@ -777,10 +777,10 @@ func newSwapBinary(swap Swap, index int, addressIndex *txBinaryAddressIndex) (Sw
|
||||
if out.SlippageBps, err = txBinaryRoundedDecimalToUint64(swap.SlippageBps, fmt.Sprintf("swap[%d].slippage_bps", index)); err != nil {
|
||||
return SwapBinary{}, err
|
||||
}
|
||||
if out.BaseReserve, err = txBinaryDecimalToUint64(swap.BaseReserve, fmt.Sprintf("swap[%d].base_reserve", index)); err != nil {
|
||||
if out.BaseReserve, err = txBinaryDecimalToFloat64Raw(swap.BaseReserve, fmt.Sprintf("swap[%d].base_reserve", index)); err != nil {
|
||||
return SwapBinary{}, err
|
||||
}
|
||||
if out.QuoteReserve, err = txBinaryDecimalToUint64(swap.QuoteReserve, fmt.Sprintf("swap[%d].quote_reserve", index)); err != nil {
|
||||
if out.QuoteReserve, err = txBinaryDecimalToFloat64Raw(swap.QuoteReserve, fmt.Sprintf("swap[%d].quote_reserve", index)); err != nil {
|
||||
return SwapBinary{}, err
|
||||
}
|
||||
if out.UserBaseBalance, err = txBinaryDecimalToUint64(swap.UserBaseBalance, fmt.Sprintf("swap[%d].user_base_balance", index)); err != nil {
|
||||
@@ -878,8 +878,8 @@ func (swap SwapBinary) toSwap(addressTable []solana.PublicKey, index int) (Swap,
|
||||
ActualLimitAmount: decimal.NewFromUint64(swap.ActualLimitAmount),
|
||||
ActualLimitAmountSide: swap.ActualLimitAmountSide,
|
||||
SlippageBps: decimal.NewFromUint64(swap.SlippageBps),
|
||||
BaseReserve: decimal.NewFromUint64(swap.BaseReserve),
|
||||
QuoteReserve: decimal.NewFromUint64(swap.QuoteReserve),
|
||||
BaseReserve: txBinaryFloat64ToDecimalRaw(swap.BaseReserve),
|
||||
QuoteReserve: txBinaryFloat64ToDecimalRaw(swap.QuoteReserve),
|
||||
Mayhem: swap.Mayhem,
|
||||
Cashback: swap.Cashback,
|
||||
UserBaseBalance: decimal.NewFromUint64(swap.UserBaseBalance),
|
||||
@@ -1063,6 +1063,14 @@ func txBinaryDecimalToFloat64(value decimal.Decimal, scale int32, field string)
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func txBinaryDecimalToFloat64Raw(value decimal.Decimal, field string) (float64, error) {
|
||||
f, exact := value.Float64()
|
||||
if !exact && math.IsInf(f, 0) {
|
||||
return 0, fmt.Errorf("%s cannot be represented as float64: %s", field, value.String())
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func txBinaryFloat64ToDecimal(value float64, scale int32) decimal.Decimal {
|
||||
formatted := strconv.FormatFloat(value, 'f', int(scale), 64)
|
||||
out, err := decimal.NewFromString(formatted)
|
||||
@@ -1072,6 +1080,15 @@ func txBinaryFloat64ToDecimal(value float64, scale int32) decimal.Decimal {
|
||||
return out
|
||||
}
|
||||
|
||||
func txBinaryFloat64ToDecimalRaw(value float64) decimal.Decimal {
|
||||
formatted := strconv.FormatFloat(value, 'f', -1, 64)
|
||||
out, err := decimal.NewFromString(formatted)
|
||||
if err != nil {
|
||||
return decimal.Zero
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type txBinaryEncoder struct {
|
||||
buf bytes.Buffer
|
||||
}
|
||||
@@ -1224,8 +1241,8 @@ func (enc *txBinaryEncoder) writeSwaps(swaps []SwapBinary, enumTable *txBinaryEn
|
||||
enc.writeUint64(swap.ActualLimitAmount)
|
||||
enc.writeUint8(uint8(swap.ActualLimitAmountSide))
|
||||
enc.writeUint64(swap.SlippageBps)
|
||||
enc.writeUint64(swap.BaseReserve)
|
||||
enc.writeUint64(swap.QuoteReserve)
|
||||
enc.writeFloat64(swap.BaseReserve)
|
||||
enc.writeFloat64(swap.QuoteReserve)
|
||||
enc.writeBool(swap.Mayhem)
|
||||
enc.writeBool(swap.Cashback)
|
||||
enc.writeUint64(swap.UserBaseBalance)
|
||||
@@ -1720,10 +1737,10 @@ func txBinaryReadSwaps(dec txBinaryBodyReader, enumTable *txBinaryEnumTable) ([]
|
||||
if swap.SlippageBps, err = dec.readUint64(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if swap.BaseReserve, err = dec.readUint64(); err != nil {
|
||||
if swap.BaseReserve, err = dec.readFloat64(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if swap.QuoteReserve, err = dec.readUint64(); err != nil {
|
||||
if swap.QuoteReserve, err = dec.readFloat64(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if swap.Mayhem, err = dec.readBool(); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user