Skip to content

Commit 2749ed7

Browse files
committed
feat(contract_manager): add support for iota
1 parent 6b85129 commit 2749ed7

File tree

12 files changed

+764
-279
lines changed

12 files changed

+764
-279
lines changed

contract_manager/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@cosmjs/cosmwasm-stargate": "^0.32.3",
2727
"@cosmjs/stargate": "^0.32.3",
2828
"@injectivelabs/networks": "^1.14.6",
29+
"@iota/iota-sdk": "^0.5.0",
2930
"@mysten/sui": "^1.3.0",
3031
"@pythnetwork/client": "catalog:",
3132
"@pythnetwork/cosmwasm-deploy-tools": "workspace:*",
@@ -36,6 +37,7 @@
3637
"@pythnetwork/pyth-sdk-solidity": "workspace:^",
3738
"@pythnetwork/pyth-starknet-js": "^0.2.1",
3839
"@pythnetwork/pyth-sui-js": "workspace:*",
40+
"@pythnetwork/pyth-iota-js": "workspace:*",
3941
"@pythnetwork/pyth-ton": "workspace:*",
4042
"@pythnetwork/pyth-ton-js": "workspace:*",
4143
"@pythnetwork/solana-utils": "workspace:^",

contract_manager/scripts/sync_wormhole_guardian_set.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
CosmWasmPriceFeedContract,
66
DefaultStore,
77
EvmPriceFeedContract,
8+
IotaWormholeContract,
89
SuiWormholeContract,
910
toPrivateKey,
1011
} from "../src";
@@ -32,6 +33,7 @@ async function main() {
3233
for (const contract of Object.values(DefaultStore.wormhole_contracts)) {
3334
if (
3435
contract instanceof SuiWormholeContract ||
36+
contract instanceof IotaWormholeContract ||
3537
contract instanceof AptosWormholeContract
3638
) {
3739
if (chains && !chains.includes(contract.getChain().getId())) {

contract_manager/src/chains.ts

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import {
2020
InjectiveExecutor,
2121
} from "@pythnetwork/cosmwasm-deploy-tools";
2222
import { Network } from "@injectivelabs/networks";
23+
import { IotaClient } from "@iota/iota-sdk/client";
2324
import { SuiClient } from "@mysten/sui/client";
24-
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
25+
import { Ed25519Keypair as IotaEd25519Keypair } from "@iota/iota-sdk/keypairs/ed25519";
26+
import { Ed25519Keypair as SuiEd25519Keypair } from "@mysten/sui/keypairs/ed25519";
2527
import { TokenId } from "./token";
2628
import { BN, Provider, Wallet, WalletUnlocked } from "fuels";
2729
import { FUEL_ETH_ASSET_ID } from "@pythnetwork/pyth-fuel-js";
@@ -38,6 +40,8 @@ import { keyPairFromSeed } from "@ton/crypto";
3840
import { PythContract } from "@pythnetwork/pyth-ton-js";
3941
import * as nearAPI from "near-api-js";
4042
import * as bs58 from "bs58";
43+
import { MIST_PER_SUI } from "@mysten/sui/utils";
44+
import { NANOS_PER_IOTA } from "@iota/iota-sdk/utils";
4145

4246
/**
4347
* Returns the chain rpc url with any environment variables replaced or throws an error if any are missing
@@ -333,8 +337,8 @@ export class SuiChain extends Chain {
333337
}
334338

335339
async getAccountAddress(privateKey: PrivateKey): Promise<string> {
336-
const keypair = Ed25519Keypair.fromSecretKey(
337-
Buffer.from(privateKey, "hex")
340+
const keypair = SuiEd25519Keypair.fromSecretKey(
341+
new Uint8Array(Buffer.from(privateKey, "hex"))
338342
);
339343
return keypair.toSuiAddress();
340344
}
@@ -344,7 +348,73 @@ export class SuiChain extends Chain {
344348
const balance = await provider.getBalance({
345349
owner: await this.getAccountAddress(privateKey),
346350
});
347-
return Number(balance.totalBalance) / 10 ** 9;
351+
return Number(balance.totalBalance) / Number(MIST_PER_SUI);
352+
}
353+
}
354+
355+
export class IotaChain extends Chain {
356+
static type = "IotaChain";
357+
358+
constructor(
359+
id: string,
360+
mainnet: boolean,
361+
wormholeChainName: string,
362+
nativeToken: TokenId | undefined,
363+
public rpcUrl: string
364+
) {
365+
super(id, mainnet, wormholeChainName, nativeToken);
366+
}
367+
368+
static fromJson(parsed: ChainConfig): IotaChain {
369+
if (parsed.type !== IotaChain.type) throw new Error("Invalid type");
370+
return new IotaChain(
371+
parsed.id,
372+
parsed.mainnet,
373+
parsed.wormholeChainName,
374+
parsed.nativeToken,
375+
parsed.rpcUrl
376+
);
377+
}
378+
379+
toJson(): KeyValueConfig {
380+
return {
381+
id: this.id,
382+
wormholeChainName: this.wormholeChainName,
383+
mainnet: this.mainnet,
384+
rpcUrl: this.rpcUrl,
385+
type: IotaChain.type,
386+
};
387+
}
388+
389+
getType(): string {
390+
return IotaChain.type;
391+
}
392+
393+
/**
394+
* Returns the payload for a governance contract upgrade instruction for contracts deployed on this chain
395+
* @param digest hex string of the 32 byte digest for the new package without the 0x prefix
396+
*/
397+
generateGovernanceUpgradePayload(digest: string): Buffer {
398+
return new UpgradeContract256Bit(this.wormholeChainName, digest).encode();
399+
}
400+
401+
getProvider(): IotaClient {
402+
return new IotaClient({ url: this.rpcUrl });
403+
}
404+
405+
async getAccountAddress(privateKey: PrivateKey): Promise<string> {
406+
const keypair = IotaEd25519Keypair.fromSecretKey(
407+
new Uint8Array(Buffer.from(privateKey, "hex"))
408+
);
409+
return keypair.toIotaAddress();
410+
}
411+
412+
async getAccountBalance(privateKey: PrivateKey): Promise<number> {
413+
const provider = this.getProvider();
414+
const balance = await provider.getBalance({
415+
owner: await this.getAccountAddress(privateKey),
416+
});
417+
return Number(balance.totalBalance) / Number(NANOS_PER_IOTA);
348418
}
349419
}
350420

@@ -912,7 +982,9 @@ export class NearChain extends Chain {
912982

913983
async getAccountAddress(privateKey: PrivateKey): Promise<string> {
914984
return Buffer.from(
915-
Ed25519Keypair.fromSecretKey(Buffer.from(privateKey, "hex"))
985+
SuiEd25519Keypair.fromSecretKey(
986+
new Uint8Array(Buffer.from(privateKey, "hex"))
987+
)
916988
.getPublicKey()
917989
.toRawBytes()
918990
).toString("hex");
@@ -931,7 +1003,9 @@ export class NearChain extends Chain {
9311003
): Promise<nearAPI.Account> {
9321004
const keyStore = new nearAPI.keyStores.InMemoryKeyStore();
9331005
if (typeof senderPrivateKey !== "undefined") {
934-
const key = bs58.encode(Buffer.from(senderPrivateKey, "hex"));
1006+
const key = bs58.encode(
1007+
new Uint8Array(Buffer.from(senderPrivateKey, "hex"))
1008+
);
9351009
const keyPair = nearAPI.KeyPair.fromString(key);
9361010
const address = await this.getAccountAddress(senderPrivateKey);
9371011
await keyStore.setKey(this.networkId, address, keyPair);

contract_manager/src/contracts/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from "./cosmwasm";
33
export * from "./evm";
44
export * from "./fuel";
55
export * from "./sui";
6+
export * from "./iota";
67
export * from "./wormhole";
78
export * from "./evm_abis";
89
export * from "./ton";

0 commit comments

Comments
 (0)