Skip to content

Commit cc32d40

Browse files
committed
add update price feeds function for sui which takes coin input
1 parent 8e4049f commit cc32d40

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

target_chains/sui/sdk/js/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ You can run this example with `pnpm turbo --filter @pythnetwork/pyth-sui-js run
104104

105105
```bash
106106
export SUI_KEY=YOUR_PRIV_KEY;
107-
pnpm turbo --filter @pythnetwork/pyth-sui-js run example-relay -- --feed-id "5a035d5440f5c163069af66062bac6c79377bf88396fa27e6067bfca8096d280" \
108-
--price-service "https://hermes-beta.pyth.network" \
107+
pnpm turbo run example-relay --filter @pythnetwork/pyth-sui-js -- \
108+
--feed-id "5a035d5440f5c163069af66062bac6c79377bf88396fa27e6067bfca8096d280" \
109+
--hermes "https://hermes-beta.pyth.network" \
109110
--full-node "https://fullnode.testnet.sui.io:443" \
110111
--pyth-state-id "0xd3e79c2c083b934e78b3bd58a490ec6b092561954da6e7322e1e2b3c8abfddc0" \
111112
--wormhole-state-id "0x31358d198147da50db32eda2562951d53973a0c0ad5ed738e9b17d88b213d790"

target_chains/sui/sdk/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/pyth-sui-js",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Pyth Network Sui Utilities",
55
"homepage": "https://pyth.network",
66
"author": {

target_chains/sui/sdk/js/src/client.ts

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { HexString } from "@pythnetwork/price-service-client";
66
import { Buffer } from "buffer";
77

88
const MAX_ARGUMENT_SIZE = 16 * 1024;
9+
type Coin = {
10+
$kind: "NestedResult";
11+
NestedResult: [number, number];
12+
};
913
export type ObjectId = string;
1014

1115
export class SuiPythClient {
@@ -104,19 +108,7 @@ export class SuiPythClient {
104108
return verifiedVaas;
105109
}
106110

107-
/**
108-
* Adds the necessary commands for updating the pyth price feeds to the transaction block.
109-
* @param tx transaction block to add commands to
110-
* @param updates array of price feed updates received from the price service
111-
* @param feedIds array of feed ids to update (in hex format)
112-
*/
113-
async updatePriceFeeds(
114-
tx: Transaction,
115-
updates: Buffer[],
116-
feedIds: HexString[],
117-
): Promise<ObjectId[]> {
118-
const packageId = await this.getPythPackageId();
119-
111+
async verifyVaasAndGetHotPotato(tx: Transaction, updates: Buffer[], packageId: string): Promise<any> {
120112
let priceUpdatesHotPotato;
121113
if (updates.length > 1) {
122114
throw new Error(
@@ -141,13 +133,17 @@ export class SuiPythClient {
141133
tx.object(SUI_CLOCK_OBJECT_ID),
142134
],
143135
});
136+
return priceUpdatesHotPotato;
137+
}
144138

139+
async executePriceFeedUpdates(
140+
tx: Transaction,
141+
packageId: string,
142+
feedIds: HexString[],
143+
priceUpdatesHotPotato: any,
144+
coins: Coin[]
145+
) {
145146
const priceInfoObjects: ObjectId[] = [];
146-
const baseUpdateFee = await this.getBaseUpdateFee();
147-
const coins = tx.splitCoins(
148-
tx.gas,
149-
feedIds.map(() => tx.pure.u64(baseUpdateFee)),
150-
);
151147
let coinId = 0;
152148
for (const feedId of feedIds) {
153149
const priceInfoObjectId = await this.getPriceFeedObjectId(feedId);
@@ -176,6 +172,49 @@ export class SuiPythClient {
176172
});
177173
return priceInfoObjects;
178174
}
175+
176+
/**
177+
* Adds the necessary commands for updating the pyth price feeds to the transaction block.
178+
* @param tx transaction block to add commands to
179+
* @param updates array of price feed updates received from the price service
180+
* @param feedIds array of feed ids to update (in hex format)
181+
*/
182+
async updatePriceFeeds(
183+
tx: Transaction,
184+
updates: Buffer[],
185+
feedIds: HexString[],
186+
): Promise<ObjectId[]> {
187+
const packageId = await this.getPythPackageId();
188+
let priceUpdatesHotPotato = await this.verifyVaasAndGetHotPotato(tx, updates, packageId);
189+
190+
const baseUpdateFee = await this.getBaseUpdateFee();
191+
const coins = tx.splitCoins(
192+
tx.gas,
193+
feedIds.map(() => tx.pure.u64(baseUpdateFee)),
194+
);
195+
196+
return await this.executePriceFeedUpdates(tx, packageId, feedIds, priceUpdatesHotPotato, coins);
197+
}
198+
199+
/**
200+
* Updates price feeds using the coin input for payment. Coins can be generated by calling splitCoin on tx.gas.
201+
* @param tx transaction block to add commands to
202+
* @param updates array of price feed updates received from the price service
203+
* @param feedIds array of feed ids to update (in hex format)
204+
* @param coins array of Coins for payment of update operations
205+
*/
206+
async updatePriceFeedsWithCoins(
207+
tx: Transaction,
208+
updates: Buffer[],
209+
feedIds: HexString[],
210+
coins: Coin[],
211+
): Promise<ObjectId[]> {
212+
const packageId = await this.getPythPackageId();
213+
let priceUpdatesHotPotato = await this.verifyVaasAndGetHotPotato(tx, updates, packageId);
214+
215+
return await this.executePriceFeedUpdates(tx, packageId, feedIds, priceUpdatesHotPotato, coins);
216+
}
217+
179218
async createPriceFeed(tx: Transaction, updates: Buffer[]) {
180219
const packageId = await this.getPythPackageId();
181220
if (updates.length > 1) {

0 commit comments

Comments
 (0)