-
Notifications
You must be signed in to change notification settings - Fork 3
feat: view prediction market data by stream #1320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| -- Migration: Order Book Discovery Action | ||
| -- Adds get_markets_by_stream discovery view for high-performance indexed lookups. | ||
|
|
||
| -- ============================================================================= | ||
| -- get_markets_by_stream: Discovery view for asset pages | ||
| -- ============================================================================= | ||
| /** | ||
| * Returns all markets associated with a specific stream ID. | ||
| * This lookup is high-performance (indexed). | ||
| * | ||
| * Parameters: | ||
| * - $stream_id: The 32-byte stream identifier | ||
| * - $limit_val: Maximum number of results (default 100, max 100) | ||
| * - $offset_val: Number of results to skip (default 0) | ||
| * | ||
| * Returns table of market summaries. | ||
| */ | ||
| CREATE OR REPLACE ACTION get_markets_by_stream( | ||
| $stream_id BYTEA, | ||
| $limit_val INT, | ||
| $offset_val INT | ||
| ) | ||
| PUBLIC VIEW RETURNS TABLE ( | ||
| id INT, | ||
| hash BYTEA, | ||
| data_provider BYTEA, | ||
| action_id TEXT, | ||
| settle_time INT8, | ||
| settled BOOLEAN, | ||
| winning_outcome BOOLEAN, | ||
| max_spread INT, | ||
| min_order_size INT8, | ||
| created_at INT8 | ||
| ) { | ||
| if $stream_id IS NULL { | ||
| ERROR('stream_id is required'); | ||
| } | ||
|
|
||
| -- Apply default and max limits | ||
| -- Note: This logic is intentionally duplicated from list_markets (032-order-book-actions.sql) | ||
| -- to maintain consistency in pagination behavior. | ||
| $effective_limit INT := 100; | ||
| $effective_offset INT := 0; | ||
|
|
||
| if $limit_val IS NOT NULL AND $limit_val > 0 AND $limit_val <= 100 { | ||
| $effective_limit := $limit_val; | ||
| } | ||
| if $offset_val IS NOT NULL AND $offset_val >= 0 { | ||
| $effective_offset := $offset_val; | ||
| } | ||
|
|
||
| RETURN SELECT id, hash, data_provider, action_id, settle_time, settled, | ||
| winning_outcome, max_spread, min_order_size, created_at | ||
| FROM ob_queries | ||
| WHERE stream_id = $stream_id | ||
| ORDER BY created_at DESC, id DESC | ||
| LIMIT $effective_limit OFFSET $effective_offset; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| //go:build kwiltest | ||
|
|
||
| package order_book | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| gethCommon "github.com/ethereum/go-ethereum/common" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/trufnetwork/kwil-db/common" | ||
| coreauth "github.com/trufnetwork/kwil-db/core/crypto/auth" | ||
| erc20bridge "github.com/trufnetwork/kwil-db/node/exts/erc20-bridge/erc20" | ||
| kwilTesting "github.com/trufnetwork/kwil-db/testing" | ||
| "github.com/trufnetwork/node/internal/migrations" | ||
| testutils "github.com/trufnetwork/node/tests/streams/utils" | ||
| "github.com/trufnetwork/sdk-go/core/util" | ||
| ) | ||
|
|
||
| // TestOrderBookDiscovery verifies that new markets populate denormalized columns | ||
| // and can be discovered via the indexed get_markets_by_stream view. | ||
| func TestOrderBookDiscovery(t *testing.T) { | ||
| testutils.RunSchemaTest(t, kwilTesting.SchemaTest{ | ||
| Name: "ORDER_BOOK_Discovery", | ||
| SeedStatements: migrations.GetSeedScriptStatements(), | ||
| FunctionTests: []kwilTesting.TestFunc{ | ||
| testDiscoveryWorkflow(t), | ||
| }, | ||
| }, testutils.GetTestOptionsWithCache()) | ||
| } | ||
|
|
||
| func testDiscoveryWorkflow(t *testing.T) func(ctx context.Context, platform *kwilTesting.Platform) error { | ||
| return func(ctx context.Context, platform *kwilTesting.Platform) error { | ||
| // Reset balance point tracker | ||
| lastBalancePointComponents = nil | ||
| userAddr := util.Unsafe_NewEthereumAddressFromString("0x1111111111111111111111111111111111111111") | ||
|
|
||
| // Initialize ERC20 extension | ||
| err := erc20bridge.ForTestingInitializeExtension(ctx, platform) | ||
| require.NoError(t, err) | ||
|
|
||
| // Give user balance for fees | ||
| err = giveBalanceChainedComponents(ctx, platform, userAddr.Address(), "100000000000000000000") | ||
| require.NoError(t, err) | ||
|
|
||
| // 1. Create a Market | ||
| dataProvider := "0x2222222222222222222222222222222222222222" | ||
| streamID := "stdiscovery000000000000000000000" // Exactly 32 chars | ||
| actionID := "price_above_threshold" | ||
| argsBytes := []byte{0xDE, 0xAD, 0xBE, 0xEF} | ||
|
|
||
| queryComponents, err := encodeQueryComponentsABI(dataProvider, streamID, actionID, argsBytes) | ||
| require.NoError(t, err) | ||
|
|
||
| settleTime := time.Now().Add(1 * time.Hour).Unix() | ||
| var queryID int | ||
| err = callCreateMarketWithComponents(ctx, platform, &userAddr, queryComponents, settleTime, int64(5), int64(100), func(row *common.Row) error { | ||
| queryID = int(row.Values[0].(int64)) | ||
| return nil | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // 2. Verify Structured Columns via get_market_info | ||
| // This also tests that get_market_info now returns the denormalized columns | ||
| engineCtx := engCtx(ctx, platform, userAddr.Address(), 1) | ||
|
|
||
| var dbProvider []byte | ||
| var dbStreamID []byte | ||
| var dbActionID string | ||
| var dbQueryArgs []byte | ||
|
|
||
| res, err := platform.Engine.Call(engineCtx, platform.DB, "", "get_market_info", []any{int64(queryID)}, func(row *common.Row) error { | ||
| // get_market_info now returns 15 columns. Denormalized ones are at 11, 12, 13, 14 | ||
| require.Equal(t, 15, len(row.Values), "get_market_info should return 15 columns") | ||
|
|
||
| dbProvider = row.Values[11].([]byte) | ||
| dbStreamID = row.Values[12].([]byte) | ||
| dbActionID = row.Values[13].(string) | ||
| dbQueryArgs = row.Values[14].([]byte) | ||
| return nil | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Nil(t, res.Error) | ||
|
|
||
| require.Equal(t, gethCommon.HexToAddress(dataProvider).Bytes(), dbProvider, "data_provider should be denormalized") | ||
|
|
||
| var expectedStreamID [32]byte | ||
| copy(expectedStreamID[:], []byte(streamID)) | ||
| require.Equal(t, expectedStreamID[:], dbStreamID, "stream_id should be denormalized") | ||
|
|
||
| require.Equal(t, actionID, dbActionID, "action_id should be denormalized") | ||
| require.Equal(t, argsBytes, dbQueryArgs, "query_args should be denormalized") | ||
|
|
||
| // 3. Test Discovery View | ||
| var discoveryCount int | ||
| // Parameters: $stream_id, $limit, $offset | ||
| res, err = platform.Engine.Call(engineCtx, platform.DB, "", "get_markets_by_stream", []any{expectedStreamID[:], int64(10), int64(0)}, func(row *common.Row) error { | ||
| discoveryCount++ | ||
| require.Equal(t, int64(queryID), row.Values[0].(int64), "discovered ID should match") | ||
| require.Equal(t, actionID, row.Values[3].(string), "discovered action_id should match") | ||
| return nil | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Nil(t, res.Error) | ||
| require.Equal(t, 1, discoveryCount, "should find exactly one market for this stream") | ||
|
|
||
| // 4. Create another market for the same stream to test indexing/multiple results | ||
| queryComponents2, err := encodeQueryComponentsABI(dataProvider, streamID, "price_below_threshold", []byte{0x00}) | ||
| require.NoError(t, err) | ||
|
|
||
| err = callCreateMarketWithComponents(ctx, platform, &userAddr, queryComponents2, settleTime + 100, int64(5), int64(100), nil) | ||
| require.NoError(t, err) | ||
|
|
||
| discoveryCount = 0 | ||
| res, err = platform.Engine.Call(engineCtx, platform.DB, "", "get_markets_by_stream", []any{expectedStreamID[:], int64(10), int64(0)}, func(row *common.Row) error { | ||
| discoveryCount++ | ||
| return nil | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 2, discoveryCount, "should now find two markets for this stream") | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // engCtx helper from other tests | ||
| func engCtx(ctx context.Context, platform *kwilTesting.Platform, caller string, height int64) *common.EngineContext { | ||
| return &common.EngineContext{ | ||
| TxContext: &common.TxContext{ | ||
| Ctx: ctx, | ||
| BlockContext: &common.BlockContext{ | ||
| Height: height, | ||
| Timestamp: time.Now().Unix(), | ||
| }, | ||
| Caller: caller, | ||
| TxID: platform.Txid(), | ||
| Authenticator: coreauth.EthPersonalSignAuth, | ||
| }, | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.