Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ require (
github.com/spf13/cobra v1.9.1
github.com/stretchr/testify v1.10.0
github.com/testcontainers/testcontainers-go v0.37.0
github.com/trufnetwork/kwil-db v0.10.3-0.20260120153326-4fab48fcfa11
github.com/trufnetwork/kwil-db/core v0.4.3-0.20260120153326-4fab48fcfa11
github.com/trufnetwork/kwil-db v0.10.3-0.20260201152833-1a21f34293d9
github.com/trufnetwork/kwil-db/core v0.4.3-0.20260201152833-1a21f34293d9
Comment thread
MicBun marked this conversation as resolved.
github.com/trufnetwork/sdk-go v0.3.2-0.20250630062504-841b40cdb709
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,8 @@ github.com/trufnetwork/kwil-db v0.10.3-0.20260120151048-5905ff3c6c71 h1:JFgC8zsd
github.com/trufnetwork/kwil-db v0.10.3-0.20260120151048-5905ff3c6c71/go.mod h1:LiBAC48uZl2B0IiLtD2hpOce7RNfpuDdghVAOc3u1Qo=
github.com/trufnetwork/kwil-db v0.10.3-0.20260120153326-4fab48fcfa11 h1:9oUJRGlPMIlpY1t6hOEdw/Lf0iluTnordB66XAXlfGk=
github.com/trufnetwork/kwil-db v0.10.3-0.20260120153326-4fab48fcfa11/go.mod h1:LiBAC48uZl2B0IiLtD2hpOce7RNfpuDdghVAOc3u1Qo=
github.com/trufnetwork/kwil-db v0.10.3-0.20260201152833-1a21f34293d9 h1:xNcapuINfWoqGIfaaVXUF1TR/CGeSnkt0e9UWB7Kj/s=
github.com/trufnetwork/kwil-db v0.10.3-0.20260201152833-1a21f34293d9/go.mod h1:LiBAC48uZl2B0IiLtD2hpOce7RNfpuDdghVAOc3u1Qo=
github.com/trufnetwork/kwil-db/core v0.4.3-0.20260107154136-b8af58932e24 h1:5RcJ0Cyt9UaXwv71d9jYgwGL2zwyTJdP9m4wkk6B6Z8=
github.com/trufnetwork/kwil-db/core v0.4.3-0.20260107154136-b8af58932e24/go.mod h1:HnOsh9+BN13LJCjiH0+XKaJzyjWKf+H9AofFFp90KwQ=
github.com/trufnetwork/kwil-db/core v0.4.3-0.20260108132315-b1fcfb33a848 h1:/0naLqfmAqfL5XWdN1yulk5auImOP14Taw0B1baq3GU=
Expand Down Expand Up @@ -1292,6 +1294,8 @@ github.com/trufnetwork/kwil-db/core v0.4.3-0.20260120151048-5905ff3c6c71 h1:rH1V
github.com/trufnetwork/kwil-db/core v0.4.3-0.20260120151048-5905ff3c6c71/go.mod h1:HnOsh9+BN13LJCjiH0+XKaJzyjWKf+H9AofFFp90KwQ=
github.com/trufnetwork/kwil-db/core v0.4.3-0.20260120153326-4fab48fcfa11 h1:/q4xCpZrI2oBxbkIyVC7WlKzFA0z2lIF5o1LPqsxdhs=
github.com/trufnetwork/kwil-db/core v0.4.3-0.20260120153326-4fab48fcfa11/go.mod h1:HnOsh9+BN13LJCjiH0+XKaJzyjWKf+H9AofFFp90KwQ=
github.com/trufnetwork/kwil-db/core v0.4.3-0.20260201152833-1a21f34293d9 h1:blscjdYlio+RF6lnaEYLo5d0iiiBvDgt0g6Dx8z3WNI=
github.com/trufnetwork/kwil-db/core v0.4.3-0.20260201152833-1a21f34293d9/go.mod h1:HnOsh9+BN13LJCjiH0+XKaJzyjWKf+H9AofFFp90KwQ=
github.com/trufnetwork/openzeppelin-merkle-tree-go v0.0.2 h1:DCq8MzbWH0wZmICNmMVsSzUHUPl+2vqRhluEABjxl88=
github.com/trufnetwork/openzeppelin-merkle-tree-go v0.0.2/go.mod h1:Y0MJpPp9QXU5vC6Gpoilql2NkgmGNcbHm9HYC2v2N8s=
github.com/trufnetwork/sdk-go v0.3.2-0.20250630062504-841b40cdb709 h1:d9EqPXIjbq/atzEncK5dM3Z9oStx1BxCGuL/sjefeCw=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func testCreateMarketStoresHashAndComponents(t *testing.T) func(ctx context.Cont

// Create query components
dataProvider := userAddr.Address()
streamID := "ststorage00000000000000000000000000" // Exactly 32 chars
streamID := "ststorage00000000000000000000000" // Exactly 32 chars
actionID := "get_record"
argsBytes := []byte{0x01, 0x02, 0x03}

Expand Down Expand Up @@ -171,7 +171,7 @@ func testCreateMarketRejectsDuplicateHash(t *testing.T) func(ctx context.Context

// Create query components
dataProvider := userAddr.Address()
streamID := "stduplicate000000000000000000000000" // Exactly 32 chars
streamID := "stduplicate000000000000000000000" // Exactly 32 chars
actionID := "get_record"
argsBytes := []byte{0xAA, 0xBB}

Expand Down Expand Up @@ -262,7 +262,7 @@ func testGetMarketInfoReturnsComponentsAndBridge(t *testing.T) func(ctx context.

// Create query components
dataProvider := userAddr.Address()
streamID := "stgetinfo00000000000000000000000000" // Exactly 32 chars
streamID := "stgetinfo00000000000000000000000" // Exactly 32 chars
actionID := "get_index"
argsBytes := []byte{0x11, 0x22, 0x33, 0x44}

Expand Down Expand Up @@ -327,7 +327,7 @@ func testCreateMarketWithDifferentBridges(t *testing.T) func(ctx context.Context

// Create query components
dataProvider := userAddr.Address()
streamID := "stbridgetest000000000000000000000000" // Exactly 32 chars
streamID := "stbridgetest00000000000000000000" // Exactly 32 chars
actionID := "get_record"
argsBytes := []byte{0x00}

Expand Down
12 changes: 6 additions & 6 deletions tests/streams/order_book/market_creation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func testCreateMarketHappyPath(t *testing.T) func(ctx context.Context, platform

// Encode query components
dataProvider := userAddr.Address()
streamID := "sthappypath000000000000000000000000" // Exactly 32 chars
streamID := "sthappypath000000000000000000000" // Exactly 32 chars
actionID := "get_record"
argsBytes := []byte{0x01, 0x02, 0x03}

Expand Down Expand Up @@ -158,7 +158,7 @@ func testCreateMarketValidation(t *testing.T) func(ctx context.Context, platform

// Encode valid query components
dataProvider := userAddr.Address()
streamID := "stvalidation00000000000000000000000" // Exactly 32 chars
streamID := "stvalidation0000000000000000000000" // Exactly 32 chars
actionID := "get_record"
argsBytes := []byte{0x01}

Expand Down Expand Up @@ -207,7 +207,7 @@ func testCreateMarketDuplicateHash(t *testing.T) func(ctx context.Context, platf

// Encode query components
dataProvider := userAddr.Address()
streamID := "stduplicate000000000000000000000000" // Exactly 32 chars
streamID := "stduplicate0000000000000000000000" // Exactly 32 chars
actionID := "get_record"
argsBytes := []byte{0x01}

Expand Down Expand Up @@ -239,7 +239,7 @@ func testCreateMarketInsufficientBalance(t *testing.T) func(ctx context.Context,

// Encode query components
dataProvider := userAddr.Address()
streamID := "stinsufficient00000000000000000000000" // Exactly 32 chars
streamID := "stinsufficient000000000000000000" // Exactly 32 chars
actionID := "get_record"
argsBytes := []byte{0x01}

Expand Down Expand Up @@ -268,7 +268,7 @@ func testGetMarketInfo(t *testing.T) func(ctx context.Context, platform *kwilTes

// Encode query components
dataProvider := userAddr.Address()
streamID := "stgetmarketinfo000000000000000000000" // Exactly 32 chars
streamID := "stgetmarketinfo00000000000000000" // Exactly 32 chars
actionID := "get_index"
argsBytes := []byte{0x01, 0x02}

Expand Down Expand Up @@ -367,7 +367,7 @@ func testMarketExists(t *testing.T) func(ctx context.Context, platform *kwilTest

// Encode query components
dataProvider := userAddr.Address()
streamID := "stmarketexists0000000000000000000000" // Exactly 32 chars
streamID := "stmarketexists000000000000000000" // Exactly 32 chars
actionID := "get_record"
argsBytes := []byte{0x01}

Expand Down
73 changes: 58 additions & 15 deletions tests/streams/order_book/settlement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ func TestSettlement(t *testing.T) {
// Validation tests:
testSettleMarketValidationIntegration(t),
testSettleMarketBlockedByBinaryParityViolation(t),
testSettleMarketBlockedByCollateralMismatch(t),
testSettleMarketMultiMarketCollateral(t),
},
}, testutils.GetTestOptionsWithCache())
}

func testSettleMarketHappyPath(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
// Reset balance point tracker for this test
lastBalancePoint = nil
lastTrufBalancePoint = nil

// Use a valid Ethereum address as deployer
deployer := util.Unsafe_NewEthereumAddressFromString("0x1111111111111111111111111111111111111111")
platform.Deployer = deployer.Bytes()
Expand Down Expand Up @@ -242,6 +246,10 @@ func testSettleMarketHappyPath(t *testing.T) func(context.Context, *kwilTesting.

func testSettleMarketWithNoOutcome(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
// Reset balance point tracker for this test
lastBalancePoint = nil
lastTrufBalancePoint = nil

deployer := util.Unsafe_NewEthereumAddressFromString("0x2222222222222222222222222222222222222222")
platform.Deployer = deployer.Bytes()

Expand Down Expand Up @@ -361,6 +369,10 @@ func testSettleMarketWithNoOutcome(t *testing.T) func(context.Context, *kwilTest

func testSettleMarketWithMultipleDatapoints(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
// Reset balance point tracker for this test
lastBalancePoint = nil
lastTrufBalancePoint = nil

deployer := util.Unsafe_NewEthereumAddressFromString("0x3333333333333333333333333333333333333333")
platform.Deployer = deployer.Bytes()

Expand Down Expand Up @@ -492,6 +504,10 @@ func testSettleMarketInvalidQueryID(t *testing.T) func(context.Context, *kwilTes

func testSettleMarketAlreadySettled(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
// Reset balance point tracker for this test
lastBalancePoint = nil
lastTrufBalancePoint = nil

deployer := util.Unsafe_NewEthereumAddressFromString("0x5555555555555555555555555555555555555555")
platform.Deployer = deployer.Bytes()

Expand Down Expand Up @@ -583,6 +599,10 @@ func testSettleMarketAlreadySettled(t *testing.T) func(context.Context, *kwilTes

func testSettleMarketTooEarly(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
// Reset balance point tracker for this test
lastBalancePoint = nil
lastTrufBalancePoint = nil

deployer := util.Unsafe_NewEthereumAddressFromString("0x6666666666666666666666666666666666666666")
platform.Deployer = deployer.Bytes()

Expand Down Expand Up @@ -666,6 +686,10 @@ func testSettleMarketTooEarly(t *testing.T) func(context.Context, *kwilTesting.P

func testSettleMarketNoAttestation(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
// Reset balance point tracker for this test
lastBalancePoint = nil
lastTrufBalancePoint = nil

deployer := util.Unsafe_NewEthereumAddressFromString("0x7777777777777777777777777777777777777777")
platform.Deployer = deployer.Bytes()

Expand Down Expand Up @@ -715,6 +739,10 @@ func testSettleMarketNoAttestation(t *testing.T) func(context.Context, *kwilTest

func testSettleMarketAttestationNotSigned(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
// Reset balance point tracker for this test
lastBalancePoint = nil
lastTrufBalancePoint = nil

deployer := util.Unsafe_NewEthereumAddressFromString("0x8888888888888888888888888888888888888888")
platform.Deployer = deployer.Bytes()

Expand Down Expand Up @@ -808,6 +836,10 @@ func testSettleMarketAttestationNotSigned(t *testing.T) func(context.Context, *k
// validation blocking using admin context (OverrideAuthz: true) to corrupt state.
func testSettleMarketValidationIntegration(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
// Reset balance point tracker for this test
lastBalancePoint = nil
lastTrufBalancePoint = nil

deployer := util.Unsafe_NewEthereumAddressFromString("0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
platform.Deployer = deployer.Bytes()

Expand Down Expand Up @@ -915,6 +947,10 @@ func testSettleMarketValidationIntegration(t *testing.T) func(context.Context, *

func testSettleMarketBlockedByBinaryParityViolation(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
// Reset balance point tracker for this test
lastBalancePoint = nil
lastTrufBalancePoint = nil

deployer := util.Unsafe_NewEthereumAddressFromString("0x9999999999999999999999999999999999999999")
platform.Deployer = deployer.Bytes()

Expand Down Expand Up @@ -1055,11 +1091,18 @@ func testSettleMarketBlockedByBinaryParityViolation(t *testing.T) func(context.C
}

// =============================================================================
// Test: Settlement Blocked by Collateral Mismatch
// Test: Multi-Market Settlement with Global Collateral Validation
// =============================================================================

func testSettleMarketBlockedByCollateralMismatch(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
// testSettleMarketMultiMarketCollateral verifies that settlement works correctly
// in multi-market scenarios where the validation function uses GLOBAL expected
// collateral (sum across all unsettled markets) instead of per-market values.
func testSettleMarketMultiMarketCollateral(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
// Reset balance point tracker for this test
lastBalancePoint = nil
lastTrufBalancePoint = nil

deployer := util.Unsafe_NewEthereumAddressFromString("0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
platform.Deployer = deployer.Bytes()

Expand Down Expand Up @@ -1207,24 +1250,24 @@ func testSettleMarketBlockedByCollateralMismatch(t *testing.T) func(context.Cont
t.Logf("Placed 100 shares in market 2")

// Now vault has 200 USDC total (100 from each market)
// But market 1's expected_collateral is only 100 USDC
// This triggers collateral mismatch because vault_balance is GLOBAL

// Try to settle market 1 (should fail with collateral mismatch)
// Validation uses GLOBAL expected collateral = 200 USDC (sum across all markets)
// Vault (200) = Expected (200) → Collateral validation PASSES
//
// NOTE: This test verifies that multi-market settlement works correctly
// when the vault balance matches the global expected collateral.
// The validation function is designed to validate GLOBAL collateral, not per-market.

// Settle market 1 (should succeed - collateral matches globally)
engineCtx = helper.NewEngineContext()
engineCtx.TxContext.BlockContext.Timestamp = 200
settleRes, err := platform.Engine.Call(engineCtx, platform.DB, "", "settle_market",
[]any{queryID1}, nil)
require.NoError(t, err)
require.NotNil(t, settleRes.Error, "should error when collateral mismatched")
require.Contains(t, settleRes.Error.Error(), "Vault collateral mismatch",
"error message should mention collateral mismatch")
require.Contains(t, settleRes.Error.Error(), "Expected=",
"error message should include expected collateral")
require.Contains(t, settleRes.Error.Error(), "Actual=",
"error message should include actual vault balance")
// Collateral should match globally (200 USDC vault = 200 USDC expected)
// Settlement should succeed for valid market
require.Nil(t, settleRes.Error, "settlement should succeed when global collateral matches")

t.Logf("Settlement correctly blocked: %v", settleRes.Error)
t.Logf("Multi-market settlement succeeded - global collateral validation passed")

return nil
}
Expand Down
Loading
Loading