Skip to content

Commit

Permalink
chore(target_chains/sui): update sui js packages (#1340)
Browse files Browse the repository at this point in the history
* chore(target_chains/sui): update sui js packages

* chore: bump version

---------

Co-authored-by: Amin Moghaddam <amin@pyth.network>
  • Loading branch information
ali-bahjati and m30m authored Mar 1, 2024
1 parent f9de543 commit 736a202
Show file tree
Hide file tree
Showing 12 changed files with 2,095 additions and 3,164 deletions.
4 changes: 2 additions & 2 deletions contract_manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"@certusone/wormhole-sdk": "^0.9.8",
"@injectivelabs/networks": "1.0.68",
"@mysten/sui.js": "^0.37.1",
"@mysten/sui.js": "^0.49.1",
"@pythnetwork/cosmwasm-deploy-tools": "*",
"@pythnetwork/entropy-sdk-solidity": "*",
"@pythnetwork/price-service-client": "*",
Expand All @@ -32,7 +32,7 @@
"aptos": "^1.5.0",
"bs58": "^5.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
"typescript": "^5.3.3"
},
"devDependencies": {
"prettier": "^2.6.2",
Expand Down
16 changes: 5 additions & 11 deletions contract_manager/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ import {
InjectiveExecutor,
} from "@pythnetwork/cosmwasm-deploy-tools";
import { Network } from "@injectivelabs/networks";
import {
Connection,
Ed25519Keypair,
JsonRpcProvider,
RawSigner,
} from "@mysten/sui.js";
import { SuiClient } from "@mysten/sui.js/client";
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519";

export type ChainConfig = Record<string, string> & {
mainnet: boolean;
Expand Down Expand Up @@ -290,17 +286,15 @@ export class SuiChain extends Chain {
).encode();
}

getProvider(): JsonRpcProvider {
return new JsonRpcProvider(new Connection({ fullnode: this.rpcUrl }));
getProvider(): SuiClient {
return new SuiClient({ url: this.rpcUrl });
}

async getAccountAddress(privateKey: PrivateKey): Promise<string> {
const provider = this.getProvider();
const keypair = Ed25519Keypair.fromSecretKey(
Buffer.from(privateKey, "hex")
);
const wallet = new RawSigner(keypair, provider);
return await wallet.getAddress();
return keypair.toSuiAddress();
}

async getAccountBalance(privateKey: PrivateKey): Promise<number> {
Expand Down
47 changes: 27 additions & 20 deletions contract_manager/src/contracts/sui.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {
Ed25519Keypair,
ObjectId,
RawSigner,
SUI_CLOCK_OBJECT_ID,
TransactionBlock,
} from "@mysten/sui.js";
import { Chain, SuiChain } from "../chains";
import { DataSource } from "xc_admin_common";
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";

type ObjectId = string;

export class SuiPriceFeedContract extends PriceFeedContract {
static type = "SuiPriceFeedContract";
Expand Down Expand Up @@ -96,13 +94,6 @@ export class SuiPriceFeedContract extends PriceFeedContract {
timestamp: string;
};
}) {
const packageId = await this.getPythPackageId();
const expectedType = `${packageId}::price::Price`;
if (priceInfo.type !== expectedType) {
throw new Error(
`Price type mismatch, expected ${expectedType} but found ${priceInfo.type}`
);
}
let expo = priceInfo.fields.expo.fields.magnitude;
if (priceInfo.fields.expo.fields.negative) expo = "-" + expo;
let price = priceInfo.fields.price.fields.magnitude;
Expand Down Expand Up @@ -135,10 +126,14 @@ export class SuiPriceFeedContract extends PriceFeedContract {
}
return {
emaPrice: await this.parsePrice(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
priceInfo.data.content.fields.price_info.fields.price_feed.fields
.ema_price
),
price: await this.parsePrice(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
priceInfo.data.content.fields.price_info.fields.price_feed.fields.price
),
};
Expand Down Expand Up @@ -303,21 +298,25 @@ export class SuiPriceFeedContract extends PriceFeedContract {
keypair: Ed25519Keypair
) {
const provider = this.getProvider();
const txBlock = {
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({
signer: keypair,
transactionBlock: tx,
options: {
showEffects: true,
showEvents: true,
},
};
const wallet = new RawSigner(keypair, provider);
const gasCost = await wallet.getGasCostEstimation(txBlock);
tx.setGasBudget(gasCost * BigInt(2));
return wallet.signAndExecuteTransactionBlock(txBlock);
});
}

async getValidTimePeriod() {
const fields = await this.getStateFields();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return Number(fields.stale_price_threshold);
}

Expand All @@ -338,6 +337,8 @@ export class SuiPriceFeedContract extends PriceFeedContract {
if (result.data.content.dataType !== "moveObject") {
throw new Error("Data Sources type mismatch");
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return result.data.content.fields.value.fields.keys.map(
({
fields,
Expand All @@ -359,6 +360,8 @@ export class SuiPriceFeedContract extends PriceFeedContract {

async getGovernanceDataSource(): Promise<DataSource> {
const fields = await this.getStateFields();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const governanceFields = fields.governance_data_source.fields;
const chainId = governanceFields.emitter_chain;
const emitterAddress =
Expand All @@ -371,11 +374,15 @@ export class SuiPriceFeedContract extends PriceFeedContract {

async getBaseUpdateFee() {
const fields = await this.getStateFields();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return { amount: fields.base_update_fee };
}

async getLastExecutedGovernanceSequence() {
const fields = await this.getStateFields();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return Number(fields.last_executed_governance_sequence);
}

Expand Down
8 changes: 0 additions & 8 deletions contract_manager/store/contracts/SuiPriceFeedContracts.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
- chain: sui_mainnet
stateId: "0xf9ff3ef935ef6cdfb659a203bf2754cebeb63346e29114a535ea6f41315e5a3f"
wormholeStateId: "0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c"
type: SuiPriceFeedContract
- chain: sui_mainnet
stateId: "0x1f9310238ee9298fb703c3419030b35b22bb1cc37113e3bb5007c99aec79e5b8"
wormholeStateId: "0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c"
type: SuiPriceFeedContract
- chain: sui_testnet
stateId: "0xb3142a723792001caafc601b7c6fe38f09f3684e360b56d8d90fc574e71e75f3"
wormholeStateId: "0xebba4cc4d614f7a7cdbe883acc76d1cc767922bc96778e7b68be0d15fce27c02"
type: SuiPriceFeedContract
- chain: sui_testnet
stateId: "0x2d82612a354f0b7e52809fc2845642911c7190404620cec8688f68808f8800d8"
wormholeStateId: "0xebba4cc4d614f7a7cdbe883acc76d1cc767922bc96778e7b68be0d15fce27c02"
Expand Down
Loading

0 comments on commit 736a202

Please sign in to comment.