mirror of
https://github.com/luxfi/maker.git
synced 2026-10-30 16:20:04 +00:00
The scale prices cross the wire on was declared five times — priceGridMul, gridMul, priceMultiplier, refPriceMultiplier, and again per command — and read back three ways: priceToGrid, gridToFloat, gridToPrice. Same number every time. dex.go said so out loud: "The maker re-declares it so it can size a deposit." The place a value is used had been braided into the value itself. It is one value, so it gets one name: PriceGridMul, exported because the commands need the same scale and were each declaring their own. Conversions become one inverse pair, GridOf and PriceOf, with PriceOfBig for a grid read off the wire as a word — no open-coded /1e8 left anywhere. Co-authored-by: Hanzo Dev <[email protected]>
37 lines
1.2 KiB
Go
37 lines
1.2 KiB
Go
// Copyright (C) 2019-2026, Lux Industries, Inc. All rights reserved.
|
||
// See the file LICENSE for licensing terms.
|
||
|
||
// grid.go is the one place prices become integers. Exported, because the commands
|
||
// need the same scale and were each re-declaring it.
|
||
package maker
|
||
|
||
import (
|
||
"math"
|
||
"math/big"
|
||
)
|
||
|
||
// PriceGridMul is dex pkg/lx PriceMultiplier: a grid value is price × 1e8, so
|
||
// parity (1.0) is 100_000_000. Untyped, so callers can use it as any numeric kind.
|
||
const PriceGridMul = 100_000_000
|
||
|
||
// GridOf scales a price onto the grid. Zero for a non-positive price: there is no
|
||
// grid value that means "no price", so the caller has to check.
|
||
func GridOf(price float64) uint64 {
|
||
if price <= 0 {
|
||
return 0
|
||
}
|
||
return uint64(math.Round(price * PriceGridMul))
|
||
}
|
||
|
||
// PriceOf is GridOf's inverse.
|
||
func PriceOf(grid uint64) float64 { return float64(grid) / PriceGridMul }
|
||
|
||
// PriceOfBig is PriceOf for a grid read off the wire as a big-endian word. Zero
|
||
// for nil, non-positive, or over-wide — all of which mean "no price this side".
|
||
func PriceOfBig(grid *big.Int) float64 {
|
||
if grid == nil || grid.Sign() <= 0 || !grid.IsUint64() {
|
||
return 0
|
||
}
|
||
return PriceOf(grid.Uint64())
|
||
}
|