-
Notifications
You must be signed in to change notification settings - Fork 306
feat: Support Sui Price Update with Coin Input #2861
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc32d40
add update price feeds function for sui which takes coin input
darunrs 05dc3e2
lint
darunrs e754a89
fix prettier issues
darunrs c631d23
prettier on fuel
darunrs 14cf4bc
fix eslint
darunrs c6dd2c9
use explicit types
darunrs 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 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 |
|---|---|---|
|
|
@@ -6,6 +6,10 @@ import { HexString } from "@pythnetwork/price-service-client"; | |
| import { Buffer } from "buffer"; | ||
|
|
||
| const MAX_ARGUMENT_SIZE = 16 * 1024; | ||
| type Coin = { | ||
| $kind: "NestedResult"; | ||
| NestedResult: [number, number]; | ||
| }; | ||
| export type ObjectId = string; | ||
|
|
||
| export class SuiPythClient { | ||
|
|
@@ -104,28 +108,19 @@ export class SuiPythClient { | |
| return verifiedVaas; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the necessary commands for updating the pyth price feeds to the transaction block. | ||
| * @param tx transaction block to add commands to | ||
| * @param updates array of price feed updates received from the price service | ||
| * @param feedIds array of feed ids to update (in hex format) | ||
| */ | ||
| async updatePriceFeeds( | ||
| async verifyVaasAndGetHotPotato( | ||
| tx: Transaction, | ||
| updates: Buffer[], | ||
| feedIds: HexString[], | ||
| ): Promise<ObjectId[]> { | ||
| const packageId = await this.getPythPackageId(); | ||
|
|
||
| let priceUpdatesHotPotato; | ||
| packageId: string, | ||
| ): Promise<any> { | ||
|
||
| if (updates.length > 1) { | ||
| throw new Error( | ||
| "SDK does not support sending multiple accumulator messages in a single transaction", | ||
| ); | ||
| } | ||
| const vaa = this.extractVaaBytesFromAccumulatorMessage(updates[0]); | ||
| const verifiedVaas = await this.verifyVaas([vaa], tx); | ||
| [priceUpdatesHotPotato] = tx.moveCall({ | ||
| const [priceUpdatesHotPotato] = tx.moveCall({ | ||
| target: `${packageId}::pyth::create_authenticated_price_infos_using_accumulator`, | ||
| arguments: [ | ||
| tx.object(this.pythStateId), | ||
|
|
@@ -141,13 +136,17 @@ export class SuiPythClient { | |
| tx.object(SUI_CLOCK_OBJECT_ID), | ||
| ], | ||
| }); | ||
| return priceUpdatesHotPotato; | ||
| } | ||
|
|
||
| async executePriceFeedUpdates( | ||
| tx: Transaction, | ||
| packageId: string, | ||
| feedIds: HexString[], | ||
| priceUpdatesHotPotato: any, | ||
| coins: Coin[], | ||
| ) { | ||
| const priceInfoObjects: ObjectId[] = []; | ||
| const baseUpdateFee = await this.getBaseUpdateFee(); | ||
| const coins = tx.splitCoins( | ||
| tx.gas, | ||
| feedIds.map(() => tx.pure.u64(baseUpdateFee)), | ||
| ); | ||
| let coinId = 0; | ||
| for (const feedId of feedIds) { | ||
| const priceInfoObjectId = await this.getPriceFeedObjectId(feedId); | ||
|
|
@@ -176,6 +175,69 @@ export class SuiPythClient { | |
| }); | ||
| return priceInfoObjects; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the necessary commands for updating the pyth price feeds to the transaction block. | ||
| * @param tx transaction block to add commands to | ||
| * @param updates array of price feed updates received from the price service | ||
| * @param feedIds array of feed ids to update (in hex format) | ||
| */ | ||
| async updatePriceFeeds( | ||
| tx: Transaction, | ||
| updates: Buffer[], | ||
| feedIds: HexString[], | ||
| ): Promise<ObjectId[]> { | ||
| const packageId = await this.getPythPackageId(); | ||
| const priceUpdatesHotPotato = await this.verifyVaasAndGetHotPotato( | ||
| tx, | ||
| updates, | ||
| packageId, | ||
| ); | ||
|
|
||
| const baseUpdateFee = await this.getBaseUpdateFee(); | ||
| const coins = tx.splitCoins( | ||
| tx.gas, | ||
| feedIds.map(() => tx.pure.u64(baseUpdateFee)), | ||
| ); | ||
|
|
||
| return await this.executePriceFeedUpdates( | ||
| tx, | ||
| packageId, | ||
| feedIds, | ||
| priceUpdatesHotPotato, | ||
| coins, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Updates price feeds using the coin input for payment. Coins can be generated by calling splitCoin on tx.gas. | ||
| * @param tx transaction block to add commands to | ||
| * @param updates array of price feed updates received from the price service | ||
| * @param feedIds array of feed ids to update (in hex format) | ||
| * @param coins array of Coins for payment of update operations | ||
| */ | ||
| async updatePriceFeedsWithCoins( | ||
| tx: Transaction, | ||
| updates: Buffer[], | ||
| feedIds: HexString[], | ||
| coins: Coin[], | ||
| ): Promise<ObjectId[]> { | ||
| const packageId = await this.getPythPackageId(); | ||
| const priceUpdatesHotPotato = await this.verifyVaasAndGetHotPotato( | ||
| tx, | ||
| updates, | ||
| packageId, | ||
| ); | ||
|
|
||
| return await this.executePriceFeedUpdates( | ||
| tx, | ||
| packageId, | ||
| feedIds, | ||
| priceUpdatesHotPotato, | ||
| coins, | ||
| ); | ||
| } | ||
|
|
||
| async createPriceFeed(tx: Transaction, updates: Buffer[]) { | ||
| const packageId = await this.getPythPackageId(); | ||
| if (updates.length > 1) { | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder whether they had a type for this themselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I couldn't find one myself. Their TS docs use the splitCoins function but they never provide an example of what explicit type it is. In their github repo, they have that function return this type:
Extract<Argument, { Result: unknown }> & Extract<Argument, { NestedResult: unknown }>[]In any case, I figured why not just use the type the IDE supplies me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So there is a TransactionResult type which almost matches this. But we seem to explicitly use the nestedResult version. So I just redefined it and use that. We can use the SDK type if we dont do [hotPotato] and such but I dont know what that would do and don't want to change things so drastically right now.