Files
maker/v4state_test.go
zeekayandHanzo Dev b52cb4efc5 coherence: cross-venue reconciler + position rebalance + V4 discovery
Venue reconciler pegs every live venue (0x9999 book, V3 slot0, V4 via
state.json) to the Kraken oracle; drift past band => bounded corrective
trade with notional caps + inventory floors. Rebalance re-centers ops
range positions on oracle as price moves (rate-limited, idempotent). V4
pools auto-discovered from the migration's state.json (absent => book+V3
only). Tests: reconcile/rebalance/v4state all pass.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-04 20:48:56 -07:00

106 lines
3.8 KiB
Go

// Copyright (C) 2019-2026, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package maker
import (
"os"
"path/filepath"
"testing"
"github.com/luxfi/geth/common"
)
// TestLoadV4StateAbsence proves the canonical "V4 not deployed" signal: a missing
// file with no env override is (nil, nil), never an error — the reconciler tolerates
// absence by design.
func TestLoadV4StateAbsence(t *testing.T) {
st, err := LoadV4State(filepath.Join(t.TempDir(), "does-not-exist.json"), "")
if err != nil {
t.Fatalf("absent file must not error, got %v", err)
}
if st != nil {
t.Fatalf("absent + no override must be nil state, got %+v", st)
}
// Empty path, no override: also nil.
if st, err := LoadV4State("", ""); err != nil || st != nil {
t.Fatalf("empty path = (%v, %v), want (nil, nil)", st, err)
}
}
// TestLoadV4StateEnvOverride proves V4 can be armed by env alone (V4_POOL_MANAGER)
// before the file lands: the pool keys are then derived per-market from base/quote.
func TestLoadV4StateEnvOverride(t *testing.T) {
mgr := "0x00000000000000000000000000000000000090aa"
st, err := LoadV4State("", mgr)
if err != nil {
t.Fatal(err)
}
if st == nil || st.PoolManager != common.HexToAddress(mgr) {
t.Fatalf("env override state = %+v, want manager %s", st, mgr)
}
}
// TestLoadV4StateFile parses a representative descriptor: manager + one pool with an
// explicit poolId, and one relying on derivation. Unknown top-level fields must be
// tolerated (the migration workstream owns a richer schema).
func TestLoadV4StateFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "state.json")
js := `{
"network": "mainnet",
"chainId": 96369,
"poolManager": "0x00000000000000000000000000000000000099f4",
"extraFieldWeIgnore": {"nested": true},
"pools": {
"LBTC/LUX": {"base":"0x1E48D32a4F5e9f08DB9aE4959163300FaF8A6C8e","quote":"0x4888E4a2Ee0F03051c72D2BD3ACf755eD3498B3E","fee":3000,"tickSpacing":60,"poolId":"0x00000000000000000000000000000000000000000000000000000000000000ab"},
"LSOL/LUX": {"base":"0x26B40f650156C7EbF9e087Dd0dca181Fe87625B7","quote":"0x4888E4a2Ee0F03051c72D2BD3ACf755eD3498B3E"}
}
}`
if err := os.WriteFile(path, []byte(js), 0o600); err != nil {
t.Fatal(err)
}
st, err := LoadV4State(path, "")
if err != nil {
t.Fatalf("parse: %v", err)
}
if st == nil {
t.Fatal("state is nil")
}
if st.PoolManager != common.HexToAddress("0x00000000000000000000000000000000000099f4") {
t.Errorf("manager = %s", st.PoolManager.Hex())
}
if len(st.Pools) != 2 {
t.Fatalf("got %d pools, want 2", len(st.Pools))
}
lbtc, ok := st.Pools["LBTC/LUX"]
if !ok || lbtc.Fee != 3000 || lbtc.PoolID != common.HexToHash("0xab") {
t.Errorf("LBTC pool mis-parsed: %+v", lbtc)
}
// A market resolving through the explicit descriptor uses its published poolId.
m := CoherenceMarket{Symbol: "LBTC/LUX", Base: lbtc.Base, Quote: lbtc.Quote}
if _, id := st.poolKeyFor(m); id != common.HexToHash("0xab") {
t.Errorf("poolKeyFor(LBTC) id = %s, want 0xab", id.Hex())
}
// A market without an explicit fee derives the canonical key id.
msol := CoherenceMarket{Symbol: "LSOL/LUX", Base: st.Pools["LSOL/LUX"].Base, Quote: st.Pools["LSOL/LUX"].Quote}
_, idSol := st.poolKeyFor(msol)
if idSol != common.Hash(PoolKeyFor(msol.Base, msol.Quote).ID()) {
t.Errorf("LSOL derived id mismatch: %s", idSol.Hex())
}
}
// TestLoadV4StateMalformed proves a present-but-corrupt file fails LOUD (an error),
// distinct from the absence case which is silent — corruption must never read as an
// empty/valid deployment.
func TestLoadV4StateMalformed(t *testing.T) {
path := filepath.Join(t.TempDir(), "bad.json")
if err := os.WriteFile(path, []byte("{not json"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := LoadV4State(path, ""); err == nil {
t.Fatal("malformed file must error")
}
}