-
Notifications
You must be signed in to change notification settings - Fork 97
[intent-coprocessor]: Price Discovery Protocol #684
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
Open
dharjeezy
wants to merge
32
commits into
main
Choose a base branch
from
dami/filler-price-pair-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 21 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
b0e0383
allow fillers to sumbmit pair price using membership and non membersh…
dharjeezy fad61b6
vec
dharjeezy c8257eb
Merge branch 'main' of github.com:polytope-labs/hyperbridge into dami…
dharjeezy 004dd9c
dual-path price submission system for verified prices and unverified …
dharjeezy cb2c312
TreasuryAccount in config
dharjeezy 9d13e85
fmt
dharjeezy 6e47e72
introduce price range input, include docs
dharjeezy 6b7dcc1
cross-chain filler verification by inspecting RedeemEscrow messages …
dharjeezy 00d16dd
fully reserve deposit price submission
dharjeezy 4fd12d4
simplex integration for price update
dharjeezy fb824aa
address concerns
dharjeezy 789d109
tested out implementation
dharjeezy dae9ad7
update to doc
dharjeezy d925949
block price submissions when a withdrawal is in progress, use H256 fo…
dharjeezy 9307348
Merge main: resolve conflicts in FX strategy and simplex config
dharjeezy 561ad9d
remove service
dharjeezy ffc5a1e
use price policy to get points
dharjeezy f9cbd1d
clear stale prices per pair, compute pair ids automatically, submit a…
dharjeezy 6762e78
simplify pair IDs to keccak256("base/quote"), replace timestamp with …
dharjeezy ef25136
benchmarks, simplified price entries
dharjeezy a73292f
configurable stablecoin addresses, update simtest, and add per-entry …
dharjeezy dcfb1c1
update docs
dharjeezy 790e716
getQuotes method
dharjeezy 37d57f3
Merge branches 'dami/filler-price-pair-update' and 'main' of github.c…
dharjeezy 51bc01d
refacoring
dharjeezy 46c3574
introduce side to differentiate prices for asks and bids,
dharjeezy 93e5fe6
interpolate
dharjeezy 03dff76
revert side
dharjeezy 8a3f9cd
register pair via reserve deposits
dharjeezy b243ee4
Merge branch 'main' of github.com:polytope-labs/hyperbridge into dami…
dharjeezy 80d0398
fee based submission
dharjeezy 231c1f2
filter 24 hours prices
dharjeezy 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "title": "Intent Gateway", | ||
| "pages": ["overview", "placing-orders", "cancelling-orders", "simplex"], | ||
| "pages": ["overview", "placing-orders", "cancelling-orders", "simplex", "price-submission"], | ||
| "defaultOpen": false | ||
| } |
126 changes: 126 additions & 0 deletions
126
docs/content/developers/intent-gateway/price-submission.mdx
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,126 @@ | ||
| --- | ||
| title: Price Submission Protocol | ||
| description: Deposit-based price submission system for the intents coprocessor pallet | ||
| --- | ||
|
|
||
| # Price Submission Protocol | ||
|
|
||
| ## Overview | ||
|
|
||
| The intents system needs on-chain price data for token pairs to function correctly. Rather than relying on external oracles, the protocol allows anyone to submit prices for governance-approved token pairs by putting up a deposit on their first submission. After that initial deposit, updating prices for the same pair is free. The deposit can be reclaimed later through a two-phase withdrawal process, which gives the system time to detect and respond to malicious data before the submitter disappears with their funds. | ||
|
|
||
| ## Submitting Prices | ||
|
|
||
| The `submit_pair_price` extrinsic on the intents coprocessor pallet is the entry point for all price submissions. It accepts a pair ID and a bounded list of price entries. All prices and amount ranges are encoded as U256 values scaled by 10^18, giving 18 decimal places of precision. | ||
|
|
||
| Submissions are batched. Each call accepts up to `MaxPriceEntries` entries (a compile-time constant configurable per runtime), where each entry specifies a base token amount range and the corresponding price of the base token in terms of the quote token. This makes it possible to quote different rates for different order sizes in a single transaction. For example, a submitter might quote USDC/CNGN at 1414 for orders between 0 and 999, and 1420 for orders between 1000 and 5000. | ||
|
|
||
| Each price entry contains three fields. The `range_start` field is the lower bound of the base token amount range (inclusive). The `range_end` field is the upper bound (also inclusive). The `price` field is the cost of one unit of the base token in terms of the quote token. The pallet validates that `range_start` is less than or equal to `range_end` for every entry and rejects empty submissions. When stored on-chain, each entry becomes a `PriceEntry` that also includes the submission timestamp. | ||
|
|
||
| ## Deposit Model | ||
|
|
||
| On the first price submission for a given account and token pair, a deposit is reserved from the submitter's balance. The deposit amount is set by governance through the `set_price_deposit_amount` extrinsic. After the initial deposit, the submitter can update prices for the same token pair as many times as they want without paying anything further. | ||
|
|
||
| This design discourages spam because each new pair requires locking funds, while keeping ongoing price updates free for active participants. | ||
|
|
||
| ## Two-Phase Withdrawal | ||
|
|
||
| Withdrawing a deposit uses a two-phase process through the `withdraw_price_deposit` extrinsic. The first call initiates the withdrawal by recording the unlock block, which is the current block number plus the governance-configured lock duration. No tokens are moved at this point, and the pallet emits a `PriceDepositWithdrawalInitiated` event. | ||
|
|
||
| Once the unlock block has been reached, a second call to the same extrinsic completes the withdrawal. The pallet unreserves the tokens, removes the deposit record, and emits a `PriceDepositWithdrawn` event. If the second call is made before the unlock block, it fails with a `DepositStillLocked` error. | ||
|
|
||
| The lock duration is measured in blocks and set by governance through `set_price_deposit_lock_duration`. This two-phase model ensures there is always a window during which bad actors can be identified and penalized before they can withdraw their deposit. | ||
|
|
||
| ## Price Windows and Data Lifecycle | ||
|
|
||
| Prices are organized into time-based windows. The window duration is governance-configurable via `PriceWindowDurationValue`, specified in milliseconds. | ||
|
|
||
| The `on_initialize` hook runs every block and checks whether the current window has expired. When it has, it resets a `PricesClearedThisWindow` flag to false. Prices from the previous window are not cleared immediately. They persist so that consumers can still read the previous window's data. | ||
|
|
||
| On the first new submission in a new window, all price entries across all pairs are cleared before the new entries are stored. This lazy clearing approach avoids the cost of iterating all pairs in `on_initialize` (which would be unbounded weight paid by the block producer) while ensuring stale data is replaced as soon as fresh data arrives. After the first submission clears the data, the global boolean flag ensures subsequent submissions in the same window skip the clearing step entirely. | ||
|
|
||
| ## Recognized Pairs | ||
|
|
||
| Only governance-approved token pairs can receive price submissions. This prevents spam for arbitrary token combinations and keeps storage growth under control. Governance manages pairs through the `add_recognized_pair` and `remove_recognized_pair` extrinsics. Removing a pair also cleans up its associated price data and storage. | ||
|
|
||
| ## RPC | ||
|
|
||
| The `intents_getPairPrices(pair_id)` RPC endpoint returns all price entries for a given token pair. The raw on-chain values (U256 scaled by 10^18) are converted to human-readable decimal strings with fractional precision preserved. For example, an on-chain value of 1414500000000000000000 is returned as the string "1414.5". Each returned entry includes the amount range boundaries (`range_start` and `range_end`), the `price`, and the submission `timestamp` in seconds. | ||
|
|
||
| ## Simplex Filler Integration | ||
|
|
||
| The simplex filler submits price updates automatically as part of the FX strategy. When the filler starts and a `hyperfx` strategy is configured with a `pairId`, the FX strategy spawns a periodic task that converts its ask price curve into on-chain price entries and submits them via `IntentsCoprocessor.submitPairPrice()`. There is no separate price configuration section; the prices come directly from the strategy's existing ask price curve, which is the same curve used to evaluate order profitability. | ||
|
|
||
| The task runs on a configurable interval (defaulting to every 5 minutes). An initial submission is triggered shortly after startup so that prices are available immediately rather than waiting for the first interval to elapse. | ||
|
|
||
| ### Configuration | ||
|
|
||
| Price submission is enabled by adding a `pairId` to the `hyperfx` strategy in the filler TOML configuration file. The ask price curve points are converted into price entries automatically, where each point on the curve defines a price range. | ||
|
|
||
| ```toml | ||
| [[strategies]] | ||
| type = "hyperfx" | ||
| pairId = "0x..." # On-chain pair ID (keccak256 of base_address ++ quote_address) | ||
| priceSubmissionIntervalSeconds = 300 # How often to submit prices (default: 300 = 5 minutes) | ||
| maxOrderUsd = "5000" | ||
|
|
||
| [[strategies.askPriceCurve]] | ||
| amount = "0" | ||
| price = "1414" | ||
|
|
||
| [[strategies.askPriceCurve]] | ||
| amount = "1000" | ||
| price = "1420" | ||
|
|
||
| [[strategies.bidPriceCurve]] | ||
| amount = "0" | ||
| price = "1500" | ||
|
|
||
| [[strategies.bidPriceCurve]] | ||
| amount = "1000" | ||
| price = "1490" | ||
|
|
||
| [strategies.exoticTokenAddresses] | ||
Wizdave97 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "EVM-56" = "0xabc..." | ||
| ``` | ||
|
|
||
| The ask curve points are sorted by amount and each point becomes a price entry. The range for each entry spans from that point's amount to just below the next point's amount. The last point extends to a large upper bound. Amounts and prices are converted to 18-decimal format before submission. | ||
|
|
||
| The first submission for each pair will reserve a deposit from the substrate account. Subsequent updates to the same pair are free. When a filler wants to reclaim their deposit, they call `withdrawPriceDeposit` once to initiate the withdrawal (which records the unlock block), then call it again after the governance-configured lock duration has elapsed to unreserve the tokens. | ||
|
|
||
| ### SDK Usage | ||
|
|
||
| The `IntentsCoprocessor` class in `@hyperbridge/sdk` exposes methods for price submission and deposit withdrawal directly. These methods accept raw 18-decimal bigint values. | ||
|
|
||
| ```typescript | ||
| import { IntentsCoprocessor } from "@hyperbridge/sdk" | ||
| import { parseUnits } from "viem" | ||
|
|
||
| const coprocessor = await IntentsCoprocessor.connect(wsUrl, substratePrivateKey) | ||
|
|
||
| // Submit prices for a token pair (values in 18-decimal format) | ||
| await coprocessor.submitPairPrice("0x...", [ | ||
| { rangeStart: parseUnits("0", 18), rangeEnd: parseUnits("999", 18), price: parseUnits("1414", 18) }, | ||
| { rangeStart: parseUnits("1000", 18), rangeEnd: parseUnits("5000", 18), price: parseUnits("1420", 18) }, | ||
| ]) | ||
|
|
||
| // Withdraw deposit (two-phase): | ||
| // Phase 1: initiate withdrawal (records unlock block) | ||
| await coprocessor.withdrawPriceDeposit("0x...") | ||
| // Phase 2: complete withdrawal after unlock block is reached | ||
| await coprocessor.withdrawPriceDeposit("0x...") | ||
| ``` | ||
|
|
||
| ## Governance Parameters | ||
|
|
||
| The protocol has several parameters that are stored on-chain and updatable through governance extrinsics. | ||
|
|
||
| `PriceWindowDurationValue` controls the length of each price window in milliseconds. When a window expires, the next submission triggers a lazy clear of all existing price data. | ||
|
|
||
| `PriceDepositAmount` is the amount reserved from a submitter's balance on their first price submission for a given token pair. This deposit stays locked until explicitly withdrawn through the two-phase process. | ||
|
|
||
| `PriceDepositLockDuration` determines how many blocks the deposit remains locked after a withdrawal is initiated. The submitter must wait this many blocks before completing the withdrawal. | ||
|
|
||
| `MaxPriceEntries` is a compile-time constant (configurable per runtime) that limits how many price entries can be included in a single submission. | ||
|
|
||
| Recognized token pairs are managed through `add_recognized_pair` and `remove_recognized_pair`. Only pairs that have been explicitly added by governance can receive price submissions. | ||
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 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
Oops, something went wrong.
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.