|
|
@@ -6,6 +6,10 @@ import { HexString } from "@pythnetwork/price-service-client";
|
|
|
import { Buffer } from "buffer";
|
|
|
|
|
|
const MAX_ARGUMENT_SIZE = 16 * 1024;
|
|
|
+type NestedTransactionResult = {
|
|
|
+ $kind: "NestedResult";
|
|
|
+ NestedResult: [number, number];
|
|
|
+};
|
|
|
export type ObjectId = string;
|
|
|
|
|
|
export class SuiPythClient {
|
|
|
@@ -104,20 +108,11 @@ 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<NestedTransactionResult> {
|
|
|
if (updates.length > 1) {
|
|
|
throw new Error(
|
|
|
"SDK does not support sending multiple accumulator messages in a single transaction",
|
|
|
@@ -125,7 +120,7 @@ export class SuiPythClient {
|
|
|
}
|
|
|
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: NestedTransactionResult[],
|
|
|
+ ) {
|
|
|
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: NestedTransactionResult[],
|
|
|
+ ): 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) {
|