Skip to content

Commit 0526bd8

Browse files
committed
feat(target_chains/sui): add iota cli lib
1 parent 2749ed7 commit 0526bd8

File tree

6 files changed

+104
-88
lines changed

6 files changed

+104
-88
lines changed

target_chains/sui/cli-iota/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pyth-iota-cli",
33
"version": "0.1.0",
4-
"description": "Pyth Sui Integration Cli tools",
4+
"description": "Pyth IOTA Integration Cli tools",
55
"main": "index.js",
66
"license": "Apache-2.0",
77
"scripts": {

target_chains/sui/cli-iota/src/cli.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { hideBin } from "yargs/helpers";
33
import {
44
DefaultStore,
55
getDefaultDeploymentConfig,
6-
SuiChain,
7-
SuiPriceFeedContract,
6+
IotaChain,
7+
IotaPriceFeedContract,
88
} from "@pythnetwork/contract-manager";
99
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
1010
import { execSync } from "child_process";
1111
import { initPyth, publishPackage } from "./pyth_deploy";
12-
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
12+
import { Ed25519Keypair } from "@iota/iota-sdk/keypairs/ed25519";
1313
import { resolve } from "path";
1414
import {
1515
buildForBytecodeAndDigest,
@@ -26,15 +26,16 @@ const OPTIONS = {
2626
contract: {
2727
type: "string",
2828
demandOption: true,
29-
desc: "Contract to use for the command (e.g sui_testnet_0xe8c2ddcd5b10e8ed98e53b12fcf8f0f6fd9315f810ae61fa4001858851f21c88)",
29+
desc: "Contract to use for the command (e.g FIXME)",
3030
},
3131
path: {
3232
type: "string",
3333
default: "../../contracts",
34-
desc: "Path to the sui contracts, will use ../../contracts by default",
34+
desc: "Path to the iota contracts, will use ../../contracts by default",
3535
},
3636
endpoint: {
3737
type: "string",
38+
default: "https://hermes.pyth.network",
3839
desc: "Price service endpoint to use, defaults to https://hermes.pyth.network for mainnet and https://hermes-beta.pyth.network for testnet",
3940
},
4041
"feed-id": {
@@ -44,24 +45,14 @@ const OPTIONS = {
4445
},
4546
} as const;
4647

47-
function getContract(contractId: string): SuiPriceFeedContract {
48-
const contract = DefaultStore.contracts[contractId] as SuiPriceFeedContract;
48+
function getContract(contractId: string): IotaPriceFeedContract {
49+
const contract = DefaultStore.contracts[contractId] as IotaPriceFeedContract;
4950
if (!contract) {
5051
throw new Error(`Contract ${contractId} not found`);
5152
}
5253
return contract;
5354
}
5455

55-
function getPriceService(
56-
contract: SuiPriceFeedContract,
57-
endpointOverride: string | undefined
58-
): PriceServiceConnection {
59-
const defaultEndpoint = contract.getChain().isMainnet()
60-
? "https://hermes.pyth.network"
61-
: "https://hermes-beta.pyth.network";
62-
return new PriceServiceConnection(endpointOverride || defaultEndpoint);
63-
}
64-
6556
yargs(hideBin(process.argv))
6657
.command(
6758
"create",
@@ -80,7 +71,7 @@ yargs(hideBin(process.argv))
8071
},
8172
async (argv) => {
8273
const contract = getContract(argv.contract);
83-
const priceService = getPriceService(contract, argv.endpoint);
74+
const priceService = new PriceServiceConnection(argv.endpoint);
8475
const feedIds = argv["feed-id"] as string[];
8576
const vaas = await priceService.getLatestVaas(feedIds);
8677
const digest = await contract.executeCreatePriceFeed(
@@ -106,7 +97,7 @@ yargs(hideBin(process.argv))
10697
},
10798
async (argv) => {
10899
const contract = getContract(argv.contract);
109-
const priceService = getPriceService(contract, argv.endpoint);
100+
const priceService = new PriceServiceConnection(argv.endpoint);
110101
const feedIds = await priceService.getPriceFeedIds();
111102
const BATCH_SIZE = 10;
112103
for (let i = 0; i < feedIds.length; i += BATCH_SIZE) {
@@ -138,7 +129,7 @@ yargs(hideBin(process.argv))
138129
digest: number[];
139130
} = JSON.parse(
140131
execSync(
141-
`sui move build --dump-bytecode-as-base64 --path ${__dirname}/${argv.path} 2> /dev/null`,
132+
`iota move build --dump-bytecode-as-base64 --path ${__dirname}/${argv.path} 2> /dev/null`,
142133
{
143134
encoding: "utf-8",
144135
}
@@ -158,26 +149,26 @@ yargs(hideBin(process.argv))
158149
chain: {
159150
type: "string",
160151
demandOption: true,
161-
desc: "Chain to deploy the code to. Can be sui_mainnet or sui_testnet",
152+
desc: "Chain to deploy the code to. Can be iota_mainnet or iota_testnet",
162153
},
163154
path: OPTIONS.path,
164155
})
165156
.usage(
166-
"$0 deploy --private-key <private-key> --chain [sui_mainnet|sui_testnet] --path <path-to-contracts>"
157+
"$0 deploy --private-key <private-key> --chain [iota_mainnet|iota_testnet] --path <path-to-contracts>"
167158
);
168159
},
169160
async (argv) => {
170161
const walletPrivateKey = argv["private-key"];
171-
const chain = DefaultStore.chains[argv.chain] as SuiChain;
162+
const chain = DefaultStore.chains[argv.chain] as IotaChain;
172163
const keypair = Ed25519Keypair.fromSecretKey(
173-
Buffer.from(walletPrivateKey, "hex")
164+
new Uint8Array(Buffer.from(walletPrivateKey, "hex"))
174165
);
175166
const result = await publishPackage(
176167
keypair,
177168
chain.getProvider(),
178169
argv.path
179170
);
180-
const deploymentType = chain.isMainnet() ? "stable" : "beta";
171+
const deploymentType = "stable";
181172
const config = getDefaultDeploymentConfig(deploymentType);
182173
await initPyth(
183174
keypair,
@@ -206,7 +197,7 @@ yargs(hideBin(process.argv))
206197
},
207198
async (argv) => {
208199
const contract = getContract(argv.contract);
209-
const priceService = getPriceService(contract, argv.endpoint);
200+
const priceService = new PriceServiceConnection(argv.endpoint);
210201
const feedIds = argv["feed-id"] as string[];
211202
const vaas = await priceService.getLatestVaas(feedIds);
212203
const digest = await contract.executeUpdatePriceFeedWithFeeds(
@@ -239,7 +230,7 @@ yargs(hideBin(process.argv))
239230
async (argv) => {
240231
const contract = getContract(argv.contract);
241232
const keypair = Ed25519Keypair.fromSecretKey(
242-
Buffer.from(argv["private-key"], "hex")
233+
new Uint8Array(Buffer.from(argv["private-key"], "hex"))
243234
);
244235

245236
const pythContractsPath = resolve(`${__dirname}/${argv.path}`);

target_chains/sui/cli-iota/src/pyth_deploy.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import { Transaction } from "@mysten/sui/transactions";
1+
import { Transaction } from "@iota/iota-sdk/transactions";
22

3-
import { MIST_PER_SUI, normalizeSuiObjectId, fromB64 } from "@mysten/sui/utils";
3+
import {
4+
NANOS_PER_IOTA,
5+
normalizeIotaObjectId,
6+
fromB64,
7+
} from "@iota/iota-sdk/utils";
48

5-
import { Ed25519Keypair } from "@mysten/sui/dist/cjs/keypairs/ed25519";
9+
import { Ed25519Keypair } from "@iota/iota-sdk/keypairs/ed25519";
610
import { execSync } from "child_process";
711
import { DataSource } from "@pythnetwork/xc-admin-common";
8-
import { SuiClient } from "@mysten/sui/client";
12+
import { IotaClient } from "@iota/iota-sdk/client";
13+
import { bcs } from "@iota/iota-sdk/bcs";
914

1015
export async function publishPackage(
1116
keypair: Ed25519Keypair,
12-
provider: SuiClient,
17+
provider: IotaClient,
1318
packagePath: string
1419
): Promise<{ packageId: string; upgradeCapId: string; deployerCapId: string }> {
1520
// Build contracts
@@ -18,7 +23,7 @@ export async function publishPackage(
1823
dependencies: string[];
1924
} = JSON.parse(
2025
execSync(
21-
`sui move build --dump-bytecode-as-base64 --path ${__dirname}/${packagePath} 2> /dev/null`,
26+
`iota move build --dump-bytecode-as-base64 --path ${__dirname}/${packagePath} 2> /dev/null`,
2227
{
2328
encoding: "utf-8",
2429
}
@@ -31,17 +36,17 @@ export async function publishPackage(
3136
// const transactionBlock = new TransactionBlock();
3237
const txb = new Transaction();
3338

34-
txb.setGasBudget(MIST_PER_SUI / 2n); // 0.5 SUI
39+
txb.setGasBudget(NANOS_PER_IOTA / 2n); // 0.5 SUI
3540

3641
const [upgradeCap] = txb.publish({
3742
modules: buildOutput.modules.map((m: string) => Array.from(fromB64(m))),
3843
dependencies: buildOutput.dependencies.map((d: string) =>
39-
normalizeSuiObjectId(d)
44+
normalizeIotaObjectId(d)
4045
),
4146
});
4247

4348
// Transfer upgrade capability to deployer
44-
txb.transferObjects([upgradeCap], txb.pure.address(keypair.toSuiAddress()));
49+
txb.transferObjects([upgradeCap], txb.pure.address(keypair.toIotaAddress()));
4550

4651
// Execute transactions
4752
const result = await provider.signAndExecuteTransaction({
@@ -97,7 +102,7 @@ export async function publishPackage(
97102

98103
export async function initPyth(
99104
keypair: Ed25519Keypair,
100-
provider: SuiClient,
105+
provider: IotaClient,
101106
pythPackageId: string,
102107
deployerCapId: string,
103108
upgradeCapId: string,
@@ -109,19 +114,31 @@ export async function initPyth(
109114
const tx = new Transaction();
110115

111116
const baseUpdateFee = tx.pure.u64(1);
112-
const dataSourceEmitterAddresses = tx.pure.arguments(
113-
config.dataSources.map((dataSource) => [
114-
...Buffer.from(dataSource.emitterAddress, "hex"),
115-
])
117+
const dataSourceEmitterAddresses = tx.pure(
118+
bcs
119+
.vector(bcs.vector(bcs.u8()))
120+
.serialize(
121+
config.dataSources.map((dataSource) => [
122+
...Buffer.from(dataSource.emitterAddress, "hex"),
123+
])
124+
)
116125
);
117-
const dataSourceEmitterChainIds = tx.pure.arguments(
118-
config.dataSources.map((dataSource) => dataSource.emitterChain)
126+
const dataSourceEmitterChainIds = tx.pure(
127+
bcs
128+
.vector(bcs.u64())
129+
.serialize(
130+
config.dataSources.map((dataSource) => dataSource.emitterChain)
131+
)
119132
);
120-
const governanceEmitterAddress = tx.pure.arguments([
121-
...Buffer.from(config.governanceDataSource.emitterAddress, "hex"),
122-
]);
123-
const governanceEmitterChainId = tx.pure.arguments(
124-
config.governanceDataSource.emitterChain
133+
const governanceEmitterAddress = tx.pure(
134+
bcs
135+
.vector(bcs.u8())
136+
.serialize([
137+
...Buffer.from(config.governanceDataSource.emitterAddress, "hex"),
138+
])
139+
);
140+
const governanceEmitterChainId = tx.pure(
141+
bcs.u64().serialize(config.governanceDataSource.emitterChain)
125142
);
126143
const stalePriceThreshold = tx.pure.u64(60);
127144
tx.moveCall({
@@ -138,7 +155,7 @@ export async function initPyth(
138155
],
139156
});
140157

141-
tx.setGasBudget(MIST_PER_SUI / 10n); // 0.1 sui
158+
tx.setGasBudget(NANOS_PER_IOTA / 10n); // 0.1 sui
142159

143160
let result = await provider.signAndExecuteTransaction({
144161
signer: keypair,

target_chains/sui/cli-iota/src/upgrade_pyth.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { Transaction } from "@mysten/sui/transactions";
2-
import { fromB64, MIST_PER_SUI, normalizeSuiObjectId } from "@mysten/sui/utils";
3-
import { SuiClient } from "@mysten/sui/client";
4-
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
1+
import { Transaction } from "@iota/iota-sdk/transactions";
2+
import {
3+
fromB64,
4+
NANOS_PER_IOTA,
5+
normalizeIotaObjectId,
6+
} from "@iota/iota-sdk/utils";
7+
import { IotaClient } from "@iota/iota-sdk/client";
8+
import { Ed25519Keypair } from "@iota/iota-sdk/keypairs/ed25519";
59

610
import { execSync } from "child_process";
7-
import { SuiPriceFeedContract } from "@pythnetwork/contract-manager";
11+
import { IotaPriceFeedContract } from "@pythnetwork/contract-manager";
812

913
export function buildForBytecodeAndDigest(packagePath: string) {
1014
const buildOutput: {
@@ -20,19 +24,19 @@ export function buildForBytecodeAndDigest(packagePath: string) {
2024
return {
2125
modules: buildOutput.modules.map((m: string) => Array.from(fromB64(m))),
2226
dependencies: buildOutput.dependencies.map((d: string) =>
23-
normalizeSuiObjectId(d)
27+
normalizeIotaObjectId(d)
2428
),
2529
digest: Buffer.from(buildOutput.digest),
2630
};
2731
}
2832

2933
export async function upgradePyth(
3034
keypair: Ed25519Keypair,
31-
provider: SuiClient,
35+
provider: IotaClient,
3236
modules: number[][],
3337
dependencies: string[],
3438
signedVaa: Buffer,
35-
contract: SuiPriceFeedContract
39+
contract: IotaPriceFeedContract
3640
) {
3741
const pythPackage = await contract.getPackageId(contract.stateId);
3842

@@ -64,7 +68,7 @@ export async function upgradePyth(
6468
arguments: [tx.object(contract.stateId), upgradeReceipt],
6569
});
6670

67-
tx.setGasBudget(MIST_PER_SUI / 4n); // 0.25 SUI
71+
tx.setGasBudget(NANOS_PER_IOTA / 4n); // 0.25 SUI
6872

6973
return provider.signAndExecuteTransaction({
7074
signer: keypair,
@@ -78,9 +82,9 @@ export async function upgradePyth(
7882

7983
export async function migratePyth(
8084
keypair: Ed25519Keypair,
81-
provider: SuiClient,
85+
provider: IotaClient,
8286
signedUpgradeVaa: Buffer,
83-
contract: SuiPriceFeedContract,
87+
contract: IotaPriceFeedContract,
8488
pythPackageOld: string
8589
) {
8690
const pythPackage = await contract.getPackageId(contract.stateId);
@@ -98,7 +102,7 @@ export async function migratePyth(
98102
arguments: [tx.object(contract.stateId), verificationReceipt as any],
99103
});
100104

101-
tx.setGasBudget(MIST_PER_SUI / 10n); //0.1 SUI
105+
tx.setGasBudget(NANOS_PER_IOTA / 10n); //0.1 SUI
102106

103107
return provider.signAndExecuteTransaction({
104108
signer: keypair,

0 commit comments

Comments
 (0)