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
13 changes: 9 additions & 4 deletions extensions/tn_settlement/settlement_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import (
// NO_OUTCOME_VALUE represents a NO outcome in settlement tests
const NO_OUTCOME_VALUE = "-1.000000000000000000"

// Test constants - match existing erc20-bridge configuration
const (
testExtensionName = "sepolia_bridge"
)

// TestSettlementIntegration tests the automatic settlement functionality
func TestSettlementIntegration(t *testing.T) {
owner := util.Unsafe_NewEthereumAddressFromString("0x4444444444444444444444444444444444444444")
Expand Down Expand Up @@ -77,7 +82,7 @@ func testFindUnsettledMarkets(t *testing.T) func(context.Context, *kwilTesting.P

var queryID int
createRes, err := platform.Engine.Call(engineCtx, platform.DB, "", "create_market",
[]any{attestationHash, settleTime, int64(5), int64(1)},
[]any{testExtensionName, attestationHash, settleTime, int64(5), int64(1)},
func(row *common.Row) error {
queryID = int(row.Values[0].(int64))
return nil
Expand Down Expand Up @@ -174,7 +179,7 @@ func testSettleMarketViaAction(t *testing.T) func(context.Context, *kwilTesting.

var queryID int
createRes, err := platform.Engine.Call(engineCtx, platform.DB, "", "create_market",
[]any{attestationHash, settleTime, int64(5), int64(1)},
[]any{testExtensionName, attestationHash, settleTime, int64(5), int64(1)},
func(row *common.Row) error {
queryID = int(row.Values[0].(int64))
return nil
Expand Down Expand Up @@ -274,7 +279,7 @@ func testSkipMarketWithoutAttestation(t *testing.T) func(context.Context, *kwilT

var queryID int
createRes, err := platform.Engine.Call(engineCtx, platform.DB, "", "create_market",
[]any{attestationHash, settleTime, int64(5), int64(1)},
[]any{testExtensionName, attestationHash, settleTime, int64(5), int64(1)},
func(row *common.Row) error {
queryID = int(row.Values[0].(int64))
return nil
Expand Down Expand Up @@ -349,7 +354,7 @@ func testMultipleMarketsProcessing(t *testing.T) func(context.Context, *kwilTest

var queryID int
createRes, err := platform.Engine.Call(engineCtx, platform.DB, "", "create_market",
[]any{attestationHash, settleTime, int64(5), int64(1)},
[]any{testExtensionName, attestationHash, settleTime, int64(5), int64(1)},
func(row *common.Row) error {
queryID = int(row.Values[0].(int64))
return nil
Expand Down
1 change: 1 addition & 0 deletions internal/migrations/030-order-book-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CREATE TABLE IF NOT EXISTS ob_queries (
min_order_size INT8 NOT NULL DEFAULT 20,
created_at INT8 NOT NULL,
creator BYTEA NOT NULL,
bridge TEXT NOT NULL,

CONSTRAINT chk_ob_queries_max_spread CHECK (max_spread >= 1 AND max_spread <= 50),
CONSTRAINT chk_ob_queries_min_order CHECK (min_order_size >= 1)
Expand Down
44 changes: 33 additions & 11 deletions internal/migrations/031-order-book-vault.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,44 @@
* IMPORTANT: The "vault" is the network's ownedBalance in the ERC20 bridge,
* stored in reward_instances.balance. We use lock()/unlock() methods.
*
* TODO: When USDC bridge is deployed (see "Goal: Deposit/withdraw contracts"),
* replace ethereum_bridge with usdc_bridge in all collateral operations.
* Bridge Support:
* - All vault operations accept a $bridge parameter
* - Supported bridges: hoodi_tt2, sepolia_bridge, ethereum_bridge
*/

-- =============================================================================
-- validate_bridge: Helper to validate bridge parameter
-- =============================================================================
-- Shared validation procedure (defined in 032-order-book-actions.sql)
-- Note: This procedure is defined in the order book actions file but used here too

Comment thread
MicBun marked this conversation as resolved.
-- =============================================================================
-- ob_lock_collateral: Lock user's collateral into network vault
-- =============================================================================
-- Uses the ERC20 bridge's lock() method which:
-- - Decreases user's balance in kwil_erc20_meta.balances
-- - Increases network's ownedBalance in reward_instances.balance
--
-- TODO: Replace ethereum_bridge with usdc_bridge when USDC bridge is deployed
CREATE OR REPLACE ACTION ob_lock_collateral($amount NUMERIC(78, 0))
CREATE OR REPLACE ACTION ob_lock_collateral($bridge TEXT, $amount NUMERIC(78, 0))
PRIVATE {
-- Validate bridge (will ERROR if invalid)
-- Note: validate_bridge procedure defined in 032-order-book-actions.sql
-- Since migrations run in order, that procedure will exist when this is called

-- Validate amount
if $amount IS NULL OR $amount <= 0::NUMERIC(78, 0) {
ERROR('Lock amount must be positive');
}

-- Lock collateral using bridge (user -> network ownedBalance)
-- TODO: Change to usdc_bridge.lock($amount) when USDC bridge is available
ethereum_bridge.lock($amount);
if $bridge = 'hoodi_tt2' {
hoodi_tt2.lock($amount);
} else if $bridge = 'sepolia_bridge' {
sepolia_bridge.lock($amount);
} else if $bridge = 'ethereum_bridge' {
ethereum_bridge.lock($amount);
} else {
ERROR('Invalid bridge. Supported: hoodi_tt2, sepolia_bridge, ethereum_bridge');
}
};
Comment thread
MicBun marked this conversation as resolved.

-- =============================================================================
Expand All @@ -41,8 +57,7 @@ PRIVATE {
-- - Increases user's balance
--
-- NOTE: This is effectively a SYSTEM action - unlock() requires owner permission
-- TODO: Replace ethereum_bridge with usdc_bridge when USDC bridge is deployed
CREATE OR REPLACE ACTION ob_unlock_collateral($user_address TEXT, $amount NUMERIC(78, 0))
CREATE OR REPLACE ACTION ob_unlock_collateral($bridge TEXT, $user_address TEXT, $amount NUMERIC(78, 0))
PRIVATE {
-- Validate inputs (must be 0x-prefixed 40 hex character address)
if $user_address IS NULL OR length($user_address) != 42 OR substring(LOWER($user_address), 1, 2) != '0x' {
Expand All @@ -54,8 +69,15 @@ PRIVATE {
}

-- Unlock collateral using bridge (network ownedBalance -> user)
-- TODO: Change to usdc_bridge.unlock($user_address, $amount) when USDC bridge is available
ethereum_bridge.unlock($user_address, $amount);
if $bridge = 'hoodi_tt2' {
hoodi_tt2.unlock($user_address, $amount);
} else if $bridge = 'sepolia_bridge' {
sepolia_bridge.unlock($user_address, $amount);
} else if $bridge = 'ethereum_bridge' {
ethereum_bridge.unlock($user_address, $amount);
} else {
ERROR('Invalid bridge. Supported: hoodi_tt2, sepolia_bridge, ethereum_bridge');
}
};

-- =============================================================================
Expand Down
Loading
Loading