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
9 changes: 9 additions & 0 deletions docs/attestations.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ Requests and markets with other arguments are refused: every arg present with th

Consumers read the outcome with `tn_utils.parse_attestation_outcome(result_canonical)`, which returns `1`, `0` or `-1`. `parse_attestation_boolean` refuses these ids.

Once markets are created on these actions, the network owner can stop new markets on the versions they replace by listing them in `retired_market_actions` (migration 064); `create_market` refuses a listed action, and markets that already exist on it still settle:

```sql
INSERT INTO retired_market_actions (action_name) VALUES
('price_above_threshold'), ('price_below_threshold'),
('value_in_range'), ('index_change_in_range')
ON CONFLICT (action_name) DO NOTHING;
```

## Retrieving Attestations

Call the `get_signed_attestation` action with the `request_tx_id` from your attestation request.
Expand Down
2 changes: 1 addition & 1 deletion docs/prediction-market-indexer.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Returns a list of prediction markets with their settlement results.
| `query_hash` | Deterministic hash of market parameters |
| `settle_time` | Unix timestamp when market is scheduled to settle |
| `settled` | Whether the market has been settled |
| `winning_outcome` | `true` = YES won, `false` = NO won, `null` = not settled |
| `winning_outcome` | `true` = YES won, `false` = NO won, `null` = not settled yet, or, with `settled` true, refunded with no winner (every share paid $0.50, open buys returned) |
| `settled_at` | Unix timestamp when settlement occurred |
| `created_at` | Block height when the market was created |
| `creator` | Wallet address of market creator (hex, lowercase) |
Expand Down
7 changes: 7 additions & 0 deletions internal/migrations/032-order-book-actions.prod.sql
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ CREATE OR REPLACE ACTION create_market(
if NOT $market_action_allowed {
ERROR('Action not allowed for attestation: ' || COALESCE($market_action, 'NULL'));
}

-- An action the network owner has retired (migration 064) takes no new
-- markets. Markets already on it still settle: settle_market does not read
-- retired_market_actions.
for $row in SELECT 1 AS retired FROM retired_market_actions WHERE action_name = $market_action {
ERROR('Action ' || $market_action || ' is retired for new markets: use an outcome action, ids 13 to 16');
}
tn_utils.validate_query_stream($header_data_provider, $header_stream_id, $query_args);

-- The outcome actions (migration 063) end every market, as YES, NO or
Expand Down
7 changes: 7 additions & 0 deletions internal/migrations/032-order-book-actions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ CREATE OR REPLACE ACTION create_market(
if NOT $market_action_allowed {
ERROR('Action not allowed for attestation: ' || COALESCE($market_action, 'NULL'));
}

-- An action the network owner has retired (migration 064) takes no new
-- markets. Markets already on it still settle: settle_market does not read
-- retired_market_actions.
for $row in SELECT 1 AS retired FROM retired_market_actions WHERE action_name = $market_action {
ERROR('Action ' || $market_action || ' is retired for new markets: use an outcome action, ids 13 to 16');
}
tn_utils.validate_query_stream($header_data_provider, $header_stream_id, $query_args);

-- The outcome actions (migration 063) end every market, as YES, NO or
Expand Down
24 changes: 24 additions & 0 deletions internal/migrations/064-retired-market-actions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* RETIRED MARKET ACTIONS
*
* create_market refuses a new market whose action is listed here. Markets that
* already exist on a retired action keep settling as before: only create_market
* reads this table.
*
* The table ships empty, so this migration changes nothing on its own. Once the
* SDKs and the market bots create markets on the outcome actions (ids 13 to 16,
* migration 063), the network owner retires the versions they replace:
*
* INSERT INTO retired_market_actions (action_name) VALUES
* ('price_above_threshold'), ('price_below_threshold'),
* ('value_in_range'), ('index_change_in_range')
* ON CONFLICT (action_name) DO NOTHING;
*
* Apply order: create_market (032, or 032.prod on mainnet) reads this table on
* every call, so the table must exist first. Apply this file before
* create_market, or in the same exec-sql.
*/

CREATE TABLE IF NOT EXISTS retired_market_actions (
action_name TEXT PRIMARY KEY
);
92 changes: 92 additions & 0 deletions tests/streams/order_book/retired_market_actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//go:build kwiltest

package order_book

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
kwilTesting "github.com/trufnetwork/kwil-db/testing"
"github.com/trufnetwork/node/extensions/tn_utils"
"github.com/trufnetwork/node/internal/migrations"
testutils "github.com/trufnetwork/node/tests/streams/utils"
"github.com/trufnetwork/sdk-go/core/util"
)

// create_market refuses new markets on an action listed in retired_market_actions (migration
// 064). The table ships empty, and markets that already exist on a retired action still settle.

const retiredOldAbove = "price_above_threshold"

func TestRetiredMarketActions(t *testing.T) {
owner := util.Unsafe_NewEthereumAddressFromString("0x1111111111111111111111111111111111111111")

testutils.RunSchemaTest(t, kwilTesting.SchemaTest{
Name: "ORDER_BOOK_RetiredMarketActions",
SeedStatements: migrations.GetSeedScriptStatements(),
Owner: owner.Address(),
FunctionTests: []kwilTesting.TestFunc{
testRetiredActionRefusesNewMarkets(t),
testRetiredActionExistingMarketStillSettles(t),
},
}, testutils.GetTestOptionsWithCache())
}

// Before the owner retires an action, markets on it are created as before. After, a new market on
// it is refused, while its outcome version and an action that is not retired still take markets.
func testRetiredActionRefusesNewMarkets(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
now := time.Now().Unix()
at := now - 7200
fx := setupOutcomeFixture(t, ctx, platform, "0x2222222222222222222222222222222222222222",
"stretiredrefuses0000000000000000", now, []outcomeRecord{{at - 60, "102"}})

fx.createMarket(t, platform, retiredOldAbove, fx.encodeOldThreshold(t, at, "101"))

require.NoError(t, deployerExecute(ctx, platform,
"INSERT INTO retired_market_actions (action_name) VALUES ('"+retiredOldAbove+"')"))

err := fx.createMarketErr(t, platform, retiredOldAbove, fx.encodeOldThreshold(t, at, "100"))
require.Error(t, err, "a new market on a retired action must be refused")
require.Contains(t, err.Error(), "price_above_threshold is retired for new markets")

above := outcomeArgs{action: outcomeAbove, at: at, window: outcomeDay, tolerance: 600}.withThreshold("101")
fx.createMarket(t, platform, outcomeAbove, fx.encode(t, above))
fx.createMarket(t, platform, "price_below_threshold", fx.encodeOldThreshold(t, at, "105"))
return nil
}
}

// A market created on an action before the owner retires it still captures and settles.
func testRetiredActionExistingMarketStillSettles(t *testing.T) func(context.Context, *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
now := time.Now().Unix()
at := now - 7200
fx := setupOutcomeFixture(t, ctx, platform, "0x2222222222222222222222222222222222222222",
"stretiredsettles0000000000000000", now, []outcomeRecord{{at - 60, "102"}})

argsBytes := fx.encodeOldThreshold(t, at, "101")
queryID := fx.createMarket(t, platform, retiredOldAbove, argsBytes)

require.NoError(t, deployerExecute(ctx, platform,
"INSERT INTO retired_market_actions (action_name) VALUES ('"+retiredOldAbove+"')"))

fx.attest(t, platform, retiredOldAbove, argsBytes)
settled, outcome, _ := fx.settle(t, platform, queryID, nil)
require.True(t, settled, "the market must settle after its action is retired")
require.NotNil(t, outcome)
require.True(t, *outcome, "102 is above 101")
return nil
}
}

// encodeOldThreshold encodes price_above_threshold or price_below_threshold args (migration 040):
// data provider, stream, timestamp, threshold and a NULL frozen_at.
func (fx *outcomeFixture) encodeOldThreshold(t *testing.T, at int64, threshold string) []byte {
t.Helper()
encoded, err := tn_utils.EncodeActionArgs([]any{fx.dataProvider, fx.streamID, at, mustDecimal(t, threshold), nil})
require.NoError(t, err)
return encoded
}
Loading