Skip to content
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

feat(target_chains/sui): use sui sdk v1 #1802

Merged
merged 13 commits into from
Jul 30, 2024
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
4 changes: 2 additions & 2 deletions apps/price_pusher/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/price-pusher",
"version": "7.0.2",
"version": "7.1.0",
"description": "Pyth Price Pusher",
"homepage": "https://pyth.network",
"main": "lib/index.js",
Expand Down Expand Up @@ -56,7 +56,7 @@
"@coral-xyz/anchor": "^0.30.0",
"@injectivelabs/networks": "^1.14.6",
"@injectivelabs/sdk-ts": "1.10.72",
"@mysten/sui.js": "^0.49.1",
"@mysten/sui": "^1.3.0",
"@pythnetwork/price-service-client": "workspace:*",
"@pythnetwork/price-service-sdk": "workspace:^",
"@pythnetwork/pyth-sdk-solidity": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion apps/price_pusher/src/sui/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PythPriceListener } from "../pyth-price-listener";
import { Controller } from "../controller";
import { Options } from "yargs";
import { SuiPriceListener, SuiPricePusher } from "./sui";
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519";
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
import pino from "pino";

export default {
Expand Down
36 changes: 17 additions & 19 deletions apps/price_pusher/src/sui/sui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
import { DurationInSeconds } from "../utils";
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
import { SuiPythClient } from "@pythnetwork/pyth-sui-js";
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { SuiClient, SuiObjectRef, PaginatedCoins } from "@mysten/sui.js/client";
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
import { Transaction } from "@mysten/sui/transactions";
import { SuiClient, SuiObjectRef, PaginatedCoins } from "@mysten/sui/client";
import { Logger } from "pino";

const GAS_FEE_FOR_SPLIT = 2_000_000_000;
Expand Down Expand Up @@ -219,7 +219,7 @@ export class SuiPricePusher implements IPricePusher {
// 3 price feeds per transaction is the optimal number for gas cost.
const priceIdChunks = chunkArray(priceIds, 3);

const txBlocks: TransactionBlock[] = [];
const txBlocks: Transaction[] = [];

await Promise.all(
priceIdChunks.map(async (priceIdChunk) => {
Expand All @@ -232,7 +232,7 @@ export class SuiPricePusher implements IPricePusher {
);
}
const vaa = vaas[0];
const tx = new TransactionBlock();
const tx = new Transaction();
await this.pythClient.updatePriceFeeds(
tx,
[Buffer.from(vaa, "base64")],
Expand All @@ -246,14 +246,12 @@ export class SuiPricePusher implements IPricePusher {
}

/** Send every transaction in txs in parallel, returning when all transactions have completed. */
private async sendTransactionBlocks(
txs: TransactionBlock[]
): Promise<void[]> {
private async sendTransactionBlocks(txs: Transaction[]): Promise<void[]> {
return Promise.all(txs.map((tx) => this.sendTransactionBlock(tx)));
}

/** Send a single transaction block using a gas coin from the pool. */
private async sendTransactionBlock(tx: TransactionBlock): Promise<void> {
private async sendTransactionBlock(tx: Transaction): Promise<void> {
const gasObject = this.gasPool.shift();
if (gasObject === undefined) {
this.logger.warn("No available gas coin. Skipping push.");
Expand All @@ -264,9 +262,9 @@ export class SuiPricePusher implements IPricePusher {
try {
tx.setGasPayment([gasObject]);
tx.setGasBudget(this.gasBudget);
const result = await this.provider.signAndExecuteTransactionBlock({
const result = await this.provider.signAndExecuteTransaction({
signer: this.signer,
transactionBlock: tx,
transaction: tx,
options: {
showEffects: true,
},
Expand Down Expand Up @@ -422,20 +420,20 @@ export class SuiPricePusher implements IPricePusher {
gasCoin: SuiObjectRef
): Promise<SuiObjectRef[]> {
// TODO: implement chunking if numGasObjects exceeds MAX_NUM_CREATED_OBJECTS
const tx = new TransactionBlock();
const tx = new Transaction();
const coins = tx.splitCoins(
tx.gas,
Array.from({ length: numGasObjects }, () => tx.pure(splitAmount))
Array.from({ length: numGasObjects }, () => tx.pure.u64(splitAmount))
);

tx.transferObjects(
Array.from({ length: numGasObjects }, (_, i) => coins[i]),
tx.pure(signerAddress)
tx.pure.address(signerAddress)
);
tx.setGasPayment([gasCoin]);
const result = await provider.signAndExecuteTransactionBlock({
const result = await provider.signAndExecuteTransaction({
signer,
transactionBlock: tx,
transaction: tx,
options: { showEffects: true },
});
const error = result?.effects?.status.error;
Expand Down Expand Up @@ -474,7 +472,7 @@ export class SuiPricePusher implements IPricePusher {
const lockedAddresses: Set<string> = new Set();
initialLockedAddresses.forEach((value) => lockedAddresses.add(value));
for (let i = 0; i < gasCoinsChunks.length; i++) {
const mergeTx = new TransactionBlock();
const mergeTx = new Transaction();
let coins = gasCoinsChunks[i];
coins = coins.filter((coin) => !lockedAddresses.has(coin.objectId));
if (finalCoin) {
Expand All @@ -483,9 +481,9 @@ export class SuiPricePusher implements IPricePusher {
mergeTx.setGasPayment(coins);
let mergeResult;
try {
mergeResult = await provider.signAndExecuteTransactionBlock({
mergeResult = await provider.signAndExecuteTransaction({
signer,
transactionBlock: mergeTx,
transaction: mergeTx,
options: { showEffects: true },
});
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion contract_manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@cosmjs/cosmwasm-stargate": "^0.32.3",
"@cosmjs/stargate": "^0.32.3",
"@injectivelabs/networks": "^1.14.6",
"@mysten/sui.js": "^0.49.1",
"@mysten/sui": "^1.3.0",
"@pythnetwork/client": "^2.21.1",
"@pythnetwork/contract-manager": "workspace:*",
"@pythnetwork/cosmwasm-deploy-tools": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions contract_manager/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
InjectiveExecutor,
} from "@pythnetwork/cosmwasm-deploy-tools";
import { Network } from "@injectivelabs/networks";
import { SuiClient } from "@mysten/sui.js/client";
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519";
import { SuiClient } from "@mysten/sui/client";
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
import { TokenId } from "./token";
import { BN, Provider, Wallet, WalletUnlocked } from "fuels";
import { FUEL_ETH_ASSET_ID } from "@pythnetwork/pyth-fuel-js";
Expand Down
42 changes: 18 additions & 24 deletions contract_manager/src/contracts/sui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { DataSource } from "@pythnetwork/xc-admin-common";
import { WormholeContract } from "./wormhole";
import { PriceFeedContract, PrivateKey, TxResult } from "../base";
import { SuiPythClient } from "@pythnetwork/pyth-sui-js";
import { SUI_CLOCK_OBJECT_ID } from "@mysten/sui.js/utils";
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { SUI_CLOCK_OBJECT_ID } from "@mysten/sui/utils";
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
import { Transaction } from "@mysten/sui/transactions";
import { uint8ArrayToBCS } from "@certusone/wormhole-sdk/lib/cjs/sui";

type ObjectId = string;
Expand Down Expand Up @@ -148,7 +148,7 @@ export class SuiPriceFeedContract extends PriceFeedContract {
* @param keypair used to sign the transaction
*/
async executeMigrateInstruction(vaa: Buffer, keypair: Ed25519Keypair) {
const tx = new TransactionBlock();
const tx = new Transaction();
const packageId = await this.getPythPackageId();
const verificationReceipt = await this.getVaaVerificationReceipt(
tx,
Expand Down Expand Up @@ -177,7 +177,7 @@ export class SuiPriceFeedContract extends PriceFeedContract {
vaas: Buffer[],
feedIds: string[]
): Promise<TxResult> {
const tx = new TransactionBlock();
const tx = new Transaction();
await this.client.updatePriceFeeds(tx, vaas, feedIds);
const keypair = Ed25519Keypair.fromSecretKey(
Buffer.from(senderPrivateKey, "hex")
Expand All @@ -189,7 +189,7 @@ export class SuiPriceFeedContract extends PriceFeedContract {
senderPrivateKey: string,
vaas: Buffer[]
): Promise<TxResult> {
const tx = new TransactionBlock();
const tx = new Transaction();
await this.client.createPriceFeed(tx, vaas);
const keypair = Ed25519Keypair.fromSecretKey(
Buffer.from(senderPrivateKey, "hex")
Expand All @@ -206,7 +206,7 @@ export class SuiPriceFeedContract extends PriceFeedContract {
const keypair = Ed25519Keypair.fromSecretKey(
Buffer.from(senderPrivateKey, "hex")
);
const tx = new TransactionBlock();
const tx = new Transaction();
const packageId = await this.getPythPackageId();
const verificationReceipt = await this.getVaaVerificationReceipt(
tx,
Expand All @@ -229,7 +229,7 @@ export class SuiPriceFeedContract extends PriceFeedContract {
modules: number[][],
dependencies: string[]
) {
const tx = new TransactionBlock();
const tx = new Transaction();
const packageId = await this.getPythPackageId();
const verificationReceipt = await this.getVaaVerificationReceipt(
tx,
Expand All @@ -245,7 +245,7 @@ export class SuiPriceFeedContract extends PriceFeedContract {
const [upgradeReceipt] = tx.upgrade({
modules,
dependencies,
packageId: packageId,
package: packageId,
ticket: upgradeTicket,
});

Expand All @@ -266,7 +266,7 @@ export class SuiPriceFeedContract extends PriceFeedContract {
* @private
*/
async getVaaVerificationReceipt(
tx: TransactionBlock,
tx: Transaction,
packageId: string,
vaa: Buffer
) {
Expand All @@ -276,7 +276,7 @@ export class SuiPriceFeedContract extends PriceFeedContract {
target: `${wormholePackageId}::vaa::parse_and_verify`,
arguments: [
tx.object(this.wormholeStateId),
tx.pure(Array.from(vaa)),
tx.pure.arguments(Array.from(vaa)),
tx.object(SUI_CLOCK_OBJECT_ID),
],
});
Expand All @@ -295,19 +295,16 @@ export class SuiPriceFeedContract extends PriceFeedContract {
* @param keypair
* @private
*/
private async executeTransaction(
tx: TransactionBlock,
keypair: Ed25519Keypair
) {
private async executeTransaction(tx: Transaction, keypair: Ed25519Keypair) {
const provider = this.getProvider();
tx.setSender(keypair.toSuiAddress());
const dryRun = await provider.dryRunTransactionBlock({
transactionBlock: await tx.build({ client: provider }),
});
tx.setGasBudget(BigInt(dryRun.input.gasData.budget.toString()) * BigInt(2));
return provider.signAndExecuteTransactionBlock({
return provider.signAndExecuteTransaction({
signer: keypair,
transactionBlock: tx,
transaction: tx,
options: {
showEffects: true,
showEvents: true,
Expand Down Expand Up @@ -489,7 +486,7 @@ export class SuiWormholeContract extends WormholeContract {
senderPrivateKey: PrivateKey,
vaa: Buffer
): Promise<TxResult> {
const tx = new TransactionBlock();
const tx = new Transaction();
const coreObjectId = this.stateId;
const corePackageId = await this.client.getWormholePackageId();
const [verifiedVaa] = tx.moveCall({
Expand Down Expand Up @@ -552,19 +549,16 @@ export class SuiWormholeContract extends WormholeContract {
* @param keypair
* @private
*/
private async executeTransaction(
tx: TransactionBlock,
keypair: Ed25519Keypair
) {
private async executeTransaction(tx: Transaction, keypair: Ed25519Keypair) {
const provider = this.chain.getProvider();
tx.setSender(keypair.toSuiAddress());
const dryRun = await provider.dryRunTransactionBlock({
transactionBlock: await tx.build({ client: provider }),
});
tx.setGasBudget(BigInt(dryRun.input.gasData.budget.toString()) * BigInt(2));
return provider.signAndExecuteTransactionBlock({
return provider.signAndExecuteTransaction({
signer: keypair,
transactionBlock: tx,
transaction: tx,
options: {
showEffects: true,
showEvents: true,
Expand Down
Loading
Loading