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
91 changes: 87 additions & 4 deletions internal/migrations/032-order-book-actions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,10 @@ CREATE OR REPLACE ACTION match_direct(
-- Transfer payment from vault to seller
ob_unlock_collateral($bridge, $seller_wallet_address, $seller_payment);

-- Record impacts for P&L
ob_record_tx_impact($sell_participant_id, $outcome, -$match_amount, $seller_payment, FALSE);
ob_record_tx_impact($buy_participant_id, $outcome, $match_amount, 0::NUMERIC(78,0), FALSE);

-- Transfer shares from seller to buyer
-- Step 1: Delete fully matched orders FIRST (prevents amount=0 constraint violation)
DELETE FROM ob_positions
Expand Down Expand Up @@ -699,6 +703,10 @@ CREATE OR REPLACE ACTION match_mint(
ON CONFLICT (query_id, participant_id, outcome, price) DO UPDATE
SET amount = ob_positions.amount + EXCLUDED.amount;

-- Record impacts for P&L
ob_record_tx_impact($yes_participant_id, true, $mint_amount, 0::NUMERIC(78,0), FALSE);
ob_record_tx_impact($no_participant_id, false, $mint_amount, 0::NUMERIC(78,0), FALSE);

-- Reduce buy orders (only if partial fill)
UPDATE ob_positions
SET amount = amount - $mint_amount
Expand Down Expand Up @@ -859,6 +867,10 @@ CREATE OR REPLACE ACTION match_burn(
ob_unlock_collateral($bridge, $yes_wallet_address, $yes_payout);
ob_unlock_collateral($bridge, $no_wallet_address, $no_payout);

-- Record impacts for P&L
ob_record_tx_impact($yes_participant_id, TRUE, -$burn_amount, $yes_payout, FALSE);
ob_record_tx_impact($no_participant_id, FALSE, -$burn_amount, $no_payout, FALSE);

-- Delete fully matched sell orders FIRST
DELETE FROM ob_positions
WHERE query_id = $query_id
Expand Down Expand Up @@ -1140,6 +1152,8 @@ CREATE OR REPLACE ACTION place_buy_order(
}
}



-- ==========================================================================
-- SECTION 5: LOCK COLLATERAL (bridge-specific)
-- ==========================================================================
Expand All @@ -1154,6 +1168,9 @@ CREATE OR REPLACE ACTION place_buy_order(
ethereum_bridge.lock($collateral_needed);
}

-- Record initial impact (collateral spent)
ob_record_tx_impact($participant_id, $outcome, 0::INT8, $collateral_needed, TRUE);

-- ==========================================================================
-- SECTION 6: INSERT BUY ORDER (UPSERT)
-- ==========================================================================
Expand All @@ -1178,6 +1195,12 @@ CREATE OR REPLACE ACTION place_buy_order(
-- Attempt to match this buy order with existing sell orders
match_orders($query_id, $outcome, $price, $bridge);

-- ==========================================================================
-- SECTION 8: CLEANUP & MATERIALIZE IMPACTS
-- ==========================================================================

ob_cleanup_tx_payouts($query_id);

-- Success: Order placed (may be partially or fully matched by future matching engine)
};

Expand Down Expand Up @@ -1320,6 +1343,8 @@ CREATE OR REPLACE ACTION place_sell_order(
' shares, trying to sell: ' || $amount::TEXT);
}



-- ==========================================================================
-- SECTION 4: MOVE SHARES FROM HOLDING TO SELL ORDER
-- ==========================================================================
Expand Down Expand Up @@ -1362,6 +1387,12 @@ CREATE OR REPLACE ACTION place_sell_order(
-- Attempt to match this sell order with existing buy orders
match_orders($query_id, $outcome, $price, $bridge);

-- ==========================================================================
-- SECTION 6: CLEANUP & MATERIALIZE IMPACTS
-- ==========================================================================

ob_cleanup_tx_payouts($query_id);

-- Success: Order placed (may be partially or fully matched by future matching engine)
};

Expand Down Expand Up @@ -1541,6 +1572,8 @@ CREATE OR REPLACE ACTION place_split_limit_order(
}
}



-- ==========================================================================
-- SECTION 5: LOCK COLLATERAL (bridge-specific)
-- ==========================================================================
Expand All @@ -1555,10 +1588,26 @@ CREATE OR REPLACE ACTION place_split_limit_order(
ethereum_bridge.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
$collateral_yes NUMERIC(78, 0) := $collateral_per_leg + ($collateral_needed - (2::NUMERIC(78, 0) * $collateral_per_leg));

-- 1. Collateral lock (split between outcomes)
ob_record_tx_impact($participant_id, TRUE, 0::INT8, $collateral_yes, TRUE);
ob_record_tx_impact($participant_id, FALSE, 0::INT8, $collateral_per_leg, TRUE);

-- 2. Mint YES shares
ob_record_tx_impact($participant_id, TRUE, $amount, 0::NUMERIC(78,0), FALSE);
-- 3. Mint NO shares
ob_record_tx_impact($participant_id, FALSE, $amount, 0::NUMERIC(78,0), FALSE);

-- ==========================================================================
-- SECTION 6: MINT YES SHARES (HOLDING)
-- SECTION 7: CREATE POSITIONS
-- ==========================================================================


-- Mint YES shares and hold them (not for sale)
-- These are stored with price = 0 to indicate holding (not listed)
--
Expand Down Expand Up @@ -1598,6 +1647,12 @@ CREATE OR REPLACE ACTION place_split_limit_order(
-- Match is attempted on the FALSE (NO) outcome at the false_price
match_orders($query_id, FALSE, $false_price, $bridge);

-- ==========================================================================
-- SECTION 9: CLEANUP & MATERIALIZE IMPACTS
-- ==========================================================================

ob_cleanup_tx_payouts($query_id);

-- Success: Split order placed
-- - YES shares held at price=0 (not for sale)
-- - NO shares listed for sale at price=false_price
Expand Down Expand Up @@ -1725,6 +1780,8 @@ CREATE OR REPLACE ACTION cancel_order(
ERROR('No participant record found for this wallet');
}



-- ==========================================================================
-- SECTION 5: GET ORDER DETAILS
-- ==========================================================================
Expand Down Expand Up @@ -1766,6 +1823,9 @@ CREATE OR REPLACE ACTION cancel_order(
-- Unlock collateral back to user using helper from 031-order-book-vault.sql
-- Passes bridge parameter to unlock from correct bridge
ob_unlock_collateral($bridge, @caller, $refund_amount);

-- Record impact for refund (Buy orders)
ob_record_tx_impact($participant_id, $outcome, 0::INT8, $refund_amount, FALSE);
}

-- For sell orders (price > 0): Return shares to holding wallet
Expand All @@ -1792,6 +1852,12 @@ CREATE OR REPLACE ACTION cancel_order(
AND outcome = $outcome
AND price = $price;

-- ==========================================================================
-- SECTION 8: CLEANUP & MATERIALIZE IMPACTS
-- ==========================================================================

ob_cleanup_tx_payouts($query_id);

-- Success: Order cancelled
-- - For buy orders: Collateral has been refunded
-- - For sell orders: Shares have been returned to holdings
Expand Down Expand Up @@ -1999,13 +2065,18 @@ CREATE OR REPLACE ACTION change_bid(
ethereum_bridge.lock($collateral_delta);
}

} else if $collateral_delta < $zero {
-- 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
-- Unlock excess amount
$unlock_amount NUMERIC(78, 0) := $zero - $collateral_delta; -- Make positive
ob_unlock_collateral($bridge, @caller, $unlock_amount);
}
-- If $collateral_delta = 0, no collateral adjustment needed

-- Record initial impact (refund)
ob_record_tx_impact($participant_id, $outcome, 0::INT8, $unlock_amount, FALSE);
}
-- If $collateral_delta = 0, no collateral adjustment needed

-- ==========================================================================
-- SECTION 7: DELETE OLD ORDER
Expand Down Expand Up @@ -2042,6 +2113,12 @@ CREATE OR REPLACE ACTION change_bid(
-- Note: match_orders expects positive price (1-99), so use $new_abs_price not $new_price
match_orders($query_id, $outcome, $new_abs_price, $bridge);

-- ==========================================================================
-- SECTION 10: CLEANUP & MATERIALIZE IMPACTS
-- ==========================================================================

ob_cleanup_tx_payouts($query_id);

-- Success: Buy order price modified atomically
-- - Old order deleted, new order placed with preserved timestamp
-- - Collateral adjusted (net change only)
Expand Down Expand Up @@ -2304,6 +2381,12 @@ CREATE OR REPLACE ACTION change_ask(
-- Try to match new order immediately
match_orders($query_id, $outcome, $new_price, $bridge);

-- ==========================================================================
-- SECTION 9: CLEANUP & MATERIALIZE IMPACTS
-- ==========================================================================

ob_cleanup_tx_payouts($query_id);

-- Success: Sell order price modified atomically
-- - Old order deleted, new order placed with preserved timestamp
-- - Shares adjusted (pulled from or returned to holdings)
Expand Down
Loading