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
18 changes: 12 additions & 6 deletions internal/migrations/031-order-book-vault.prod.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ PRIVATE {
}

-- Lock collateral using bridge (user -> network ownedBalance)
if $bridge != 'eth_usdc' {
ERROR('Invalid bridge. Supported: eth_usdc');
if $bridge = 'eth_usdc' {
eth_usdc.lock($amount);
} else if $bridge = 'eth_truf' {
eth_truf.lock($amount);
} else {
ERROR('Invalid bridge. Supported: eth_usdc, eth_truf');
}
eth_usdc.lock($amount);
};

CREATE OR REPLACE ACTION ob_unlock_collateral($bridge TEXT, $user_address TEXT, $amount NUMERIC(78, 0))
Expand All @@ -44,8 +47,11 @@ PRIVATE {
}

-- Unlock collateral using bridge (network ownedBalance -> user)
if $bridge != 'eth_usdc' {
ERROR('Invalid bridge. Supported: eth_usdc');
if $bridge = 'eth_usdc' {
eth_usdc.unlock($user_address, $amount);
} else if $bridge = 'eth_truf' {
eth_truf.unlock($user_address, $amount);
} else {
ERROR('Invalid bridge. Supported: eth_usdc, eth_truf');
}
eth_usdc.unlock($user_address, $amount);
};
45 changes: 28 additions & 17 deletions internal/migrations/032-order-book-actions.prod.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ CREATE OR REPLACE ACTION validate_bridge($bridge TEXT) PRIVATE {
ERROR('bridge parameter is required');
}

if $bridge != 'eth_usdc' {
ERROR('Invalid bridge. Supported: eth_usdc');
if $bridge != 'eth_usdc' AND
$bridge != 'eth_truf' {
ERROR('Invalid bridge. Supported: eth_usdc, eth_truf');
}

RETURN;
Expand Down Expand Up @@ -253,9 +254,11 @@ CREATE OR REPLACE ACTION place_buy_order(
-- ==========================================================================

$caller_balance NUMERIC(78, 0);
$caller_balance := COALESCE(eth_usdc.balance(@caller), 0::NUMERIC(78, 0));

if $caller_balance < $collateral_needed {
if $bridge = 'eth_usdc' {
$caller_balance := COALESCE(eth_usdc.balance(@caller), 0::NUMERIC(78, 0));
} else if $bridge = 'eth_truf' {
$caller_balance := COALESCE(eth_truf.balance(@caller), 0::NUMERIC(78, 0));
}if $caller_balance < $collateral_needed {
-- Note: Division by 10^18 for display purposes (convert wei to TRUF)
ERROR('Insufficient balance. Required: ' || $collateral_needed::TEXT || ' wei (' ||
($collateral_needed / '1000000000000000000'::NUMERIC(78, 0))::TEXT || ' TRUF)');
Expand Down Expand Up @@ -296,9 +299,11 @@ CREATE OR REPLACE ACTION place_buy_order(

-- Lock tokens from user to vault (network-owned balance)
-- Note: Bridge lock() throws ERROR on failure (insufficient balance, etc.)
eth_usdc.lock($collateral_needed);

-- Record initial impact (collateral spent)
if $bridge = 'eth_usdc' {
eth_usdc.lock($collateral_needed);
} else if $bridge = 'eth_truf' {
eth_truf.lock($collateral_needed);
}-- Record initial impact (collateral spent)
ob_record_tx_impact($participant_id, $outcome, 0::INT8, $collateral_needed, TRUE);

-- ==========================================================================
Expand Down Expand Up @@ -418,9 +423,11 @@ CREATE OR REPLACE ACTION place_split_limit_order(
-- ==========================================================================

$caller_balance NUMERIC(78, 0);
$caller_balance := COALESCE(eth_usdc.balance(@caller), 0::NUMERIC(78, 0));

if $caller_balance < $collateral_needed {
if $bridge = 'eth_usdc' {
$caller_balance := COALESCE(eth_usdc.balance(@caller), 0::NUMERIC(78, 0));
} else if $bridge = 'eth_truf' {
$caller_balance := COALESCE(eth_truf.balance(@caller), 0::NUMERIC(78, 0));
}if $caller_balance < $collateral_needed {
-- Note: Division by 10^18 for display purposes (convert wei to TRUF)
ERROR('Insufficient balance. Required: ' || $collateral_needed::TEXT || ' wei (' ||
($collateral_needed / '1000000000000000000'::NUMERIC(78, 0))::TEXT || ' TRUF)');
Expand Down Expand Up @@ -461,9 +468,11 @@ CREATE OR REPLACE ACTION place_split_limit_order(

-- Lock tokens from user to vault (network-owned balance)
-- Note: Bridge lock() throws ERROR on failure (insufficient balance, etc.)
eth_usdc.lock($collateral_needed);

-- Record initial impacts:
if $bridge = 'eth_usdc' {
eth_usdc.lock($collateral_needed);
} else if $bridge = 'eth_truf' {
eth_truf.lock($collateral_needed);
}-- Record initial impacts:
-- Calculate split collateral (50/50 split for YES/NO legs)
$collateral_per_leg NUMERIC(78, 0) := $collateral_needed / 2::NUMERIC(78, 0);
-- Handle dust: add remainder to YES leg if odd amount
Expand Down Expand Up @@ -669,9 +678,11 @@ CREATE OR REPLACE ACTION change_bid(
if $collateral_delta > $zero {
-- New order needs MORE collateral
-- Lock additional amount (will ERROR if insufficient balance)
eth_usdc.lock($collateral_delta);

-- Record initial impact (lock)
if $bridge = 'eth_usdc' {
eth_usdc.lock($collateral_delta);
} else if $bridge = 'eth_truf' {
eth_truf.lock($collateral_delta);
}-- Record initial impact (lock)
ob_record_tx_impact($participant_id, $outcome, 0::INT8, $collateral_delta, TRUE);
} else if $collateral_delta < $zero {
-- New order needs LESS collateral
Expand Down
23 changes: 15 additions & 8 deletions internal/migrations/033-order-book-settlement.prod.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ CREATE OR REPLACE ACTION ob_batch_unlock_collateral(
SELECT u.wallet_hex, u.amount
FROM UNNEST($wallet_hexes, $amounts) AS u(wallet_hex, amount)
{
eth_usdc.unlock($payout.wallet_hex, $payout.amount);
}
if $bridge = 'eth_usdc' {
eth_usdc.unlock($payout.wallet_hex, $payout.amount);
} else if $bridge = 'eth_truf' {
eth_truf.unlock($payout.wallet_hex, $payout.amount);
}}

-- 2. Record all impacts using a loop to avoid "unknown variable" in INSERT...SELECT
$next_id INT;
Expand Down Expand Up @@ -100,9 +103,11 @@ CREATE OR REPLACE ACTION distribute_fees(
$actual_dp_fees NUMERIC(78, 0) := '0'::NUMERIC(78, 0);
if $dp_addr IS NOT NULL AND $infra_share > '0'::NUMERIC(78, 0) {
$dp_wallet_hex TEXT := '0x' || encode($dp_addr, 'hex');
eth_usdc.unlock($dp_wallet_hex, $infra_share);

$actual_dp_fees := $infra_share;
if $bridge = 'eth_usdc' {
eth_usdc.unlock($dp_wallet_hex, $infra_share);
} else if $bridge = 'eth_truf' {
eth_truf.unlock($dp_wallet_hex, $infra_share);
}$actual_dp_fees := $infra_share;

-- Ensure DP has a participant record so the fee is tracked in ob_net_impacts
$dp_pid INT;
Expand Down Expand Up @@ -154,9 +159,11 @@ CREATE OR REPLACE ACTION distribute_fees(
-- Skip validators with zero payout (e.g., infra_share < validator_count)
if $v_payout > '0'::NUMERIC(78, 0) {
-- Unlock funds via bridge
eth_usdc.unlock($v_wallet_hex, $v_payout);

-- Ensure validator has a participant record
if $bridge = 'eth_usdc' {
eth_usdc.unlock($v_wallet_hex, $v_payout);
} else if $bridge = 'eth_truf' {
eth_truf.unlock($v_wallet_hex, $v_payout);
}-- Ensure validator has a participant record
$v_pid INT;
for $p in SELECT id FROM ob_participants WHERE wallet_address = $v_wallet_bytes { $v_pid := $p.id; }
if $v_pid IS NULL {
Expand Down
18 changes: 12 additions & 6 deletions internal/migrations/037-order-book-validation.prod.sql
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,18 @@ PUBLIC VIEW RETURNS (
$vault_balance NUMERIC(78, 0) := 0::NUMERIC(78, 0);
$row_count INT := 0;

if $bridge != 'eth_usdc' {
ERROR('Invalid bridge. Supported: eth_usdc');
}
for $info in eth_usdc.info() {
$vault_balance := $info.balance;
$row_count := $row_count + 1;
if $bridge = 'eth_usdc' {
for $info in eth_usdc.info() {
$vault_balance := $info.balance;
$row_count := $row_count + 1;
}
} else if $bridge = 'eth_truf' {
for $info in eth_truf.info() {
$vault_balance := $info.balance;
$row_count := $row_count + 1;
}
} else {
ERROR('Invalid bridge. Supported: eth_usdc, eth_truf');
}

-- Validate that bridge returned data (distinguish unavailable from empty vault)
Expand Down
Loading
Loading