Skip to content
Closed
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
8,020 changes: 7,078 additions & 942 deletions target-chains/cosmwasm/tools/package-lock.json

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions target-chains/cosmwasm/tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@
"name": "tools",
"version": "1.0.0",
"description": "",
"main": "deploy.js",
"main": "deploy.ts",
"type": "module",
"scripts": {
"deploy-pyth": "node deploy-pyth-bridge.js"
"build": "tsc",
"deploy-pyth": "ts-node deploy-pyth-bridge.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@cosmjs/encoding": "^0.26.2",
"@injectivelabs/networks": "^1.0.55",
"@injectivelabs/sdk-ts": "^1.0.327",
"@injectivelabs/utils": "^1.0.47",
"@terra-money/terra.js": "^3.1.3",
"dotenv": "^16.0.0",
"ethers": "^5.4.4",
"yargs": "^17.0.1"
},
"devDependencies": {
"@types/yargs": "^17.0.18",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"eslint": "^8.27.0",
"eslint-config-prettier": "^8.5.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { LCDClient, MnemonicKey } from "@terra-money/terra.js";
import {
MsgInstantiateContract,
MsgMigrateContract,
MsgStoreCode,
} from "@terra-money/terra.js";
import { readFileSync } from "fs";
import { Bech32, toHex } from "@cosmjs/encoding";
import { zeroPad } from "ethers/lib/utils.js";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import assert from "assert";
import { TerraDeployer } from "./terra";
import { InjectiveDeployer } from "./injective";
import { Network } from "@injectivelabs/networks";
import { PrivateKey } from "@injectivelabs/sdk-ts";

const argv = yargs(hideBin(process.argv))
.option("network", {
Expand Down Expand Up @@ -52,14 +47,14 @@ const argv = yargs(hideBin(process.argv))
requred: false,
})
.help()
.alias("help", "h").argv;
.alias("help", "h")
.parseSync();

const artifact = argv.artifact;

/* Set up terra client & wallet. It won't fail because inputs are validated with yargs */
const artifact = argv.artifact!;

const CONFIG = {
mainnet: {
terra_mainnet: {
type: "terra",
terraHost: {
URL: "https://phoenix-lcd.terra.dev",
chainID: "phoenix-1",
Expand Down Expand Up @@ -101,7 +96,8 @@ const CONFIG = {
},
},
},
testnet: {
terra_testnet: {
type: "terra",
terraHost: {
URL: "https://pisco-lcd.terra.dev",
chainID: "pisco-1",
Expand Down Expand Up @@ -143,23 +139,74 @@ const CONFIG = {
},
},
},
injective_testnet: {
type: "injective",
injectiveHost: {
network: Network.Testnet,
},
pythConfig: {
wormhole_contract: "inj1xx3aupmgv3ce537c0yce8zzd3sz567syuyedpg",
data_sources: [
{
emitter: Buffer.from(
"f346195ac02f37d60d4db8ffa6ef74cb1be3550047543a4a9ee9acf4d78697b0",
"hex"
).toString("base64"),
chain_id: 1,
},
{
emitter: Buffer.from(
"a27839d641b07743c0cb5f68c51f8cd31d2c0762bec00dc6fcd25433ef1ab5b6",
"hex"
).toString("base64"),
chain_id: 26,
},
],
governance_source: {
emitter: Buffer.from(
"63278d271099bfd491951b3e648f08b1c71631e4a53674ad43e8f9f98068c385",
"hex"
).toString("base64"),
chain_id: 1,
},
governance_source_index: 0,
governance_sequence_number: 0,
chain_id: 19,
valid_time_period_secs: 60,
fee: {
amount: "1",
// FIXME ??
denom: "inj",
},
},
},
};

const terraHost = CONFIG[argv.network].terraHost;
const pythConfig = CONFIG[argv.network].pyth_config;
const lcd = new LCDClient(terraHost);
// @ts-ignore
var pythConfig = CONFIG[argv.network].pyth_config;
// @ts-ignore
var deployer: Deployer;

const feeDenoms = ["uluna"];

const wallet = lcd.wallet(
new MnemonicKey({
mnemonic: argv.mnemonic,
})
);
// @ts-ignore
const config: any = CONFIG[argv.network];
if (config.type == "terra") {
const lcd = new LCDClient(config.terraHost);
const wallet = lcd.wallet(
new MnemonicKey({
mnemonic: argv.mnemonic,
})
);
deployer = new TerraDeployer(wallet);
} else if (config.type == "injective") {
deployer = new InjectiveDeployer(
config.injectiveHost.network,
PrivateKey.fromMnemonic(argv.mnemonic)
);
}

/* Deploy artifacts */

var codeId;
var codeId: number;

if (argv.codeId !== undefined) {
codeId = argv.codeId;
Expand All @@ -171,32 +218,7 @@ if (argv.codeId !== undefined) {
process.exit(1);
}

const contract_bytes = readFileSync(artifact);
console.log(`Storing WASM: ${artifact} (${contract_bytes.length} bytes)`);

const store_code = new MsgStoreCode(
wallet.key.accAddress,
contract_bytes.toString("base64")
);

const tx = await wallet.createAndSignTx({
msgs: [store_code],
feeDenoms,
});

const rs = await lcd.tx.broadcast(tx);

try {
const ci = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
codeId = parseInt(ci);
} catch (e) {
console.error(
"Encountered an error in parsing deploy code result. Printing raw log"
);
console.error(rs.raw_log);
throw e;
}

codeId = await deployer.deployArtifact(artifact);
console.log("Code ID: ", codeId);

if (argv.instantiate || argv.migrate) {
Expand All @@ -207,44 +229,11 @@ if (argv.codeId !== undefined) {

if (argv.instantiate) {
console.log("Instantiating a contract");

async function instantiate(codeId, inst_msg, label) {
var address;
await wallet
.createAndSignTx({
msgs: [
new MsgInstantiateContract(
wallet.key.accAddress,
wallet.key.accAddress,
codeId,
inst_msg,
undefined,
label
),
],
})
.then((tx) => lcd.tx.broadcast(tx))
.then((rs) => {
try {
address = /"contract_address","value":"([^"]+)/gm.exec(rs.raw_log)[1];
} catch (e) {
console.error(
"Encountered an error in parsing instantiation result. Printing raw log"
);
console.error(rs.raw_log);
throw e;
}
});
console.log(
`Instantiated Pyth at ${address} (${convert_terra_address_to_hex(
address
)})`
);
return address;
}

const contractAddress = await instantiate(codeId, pythConfig, "pyth");

const contractAddress = await deployer.instantiate(
codeId,
pythConfig,
"pyth"
);
console.log(`Deployed Pyth contract at ${contractAddress}`);
}

Expand All @@ -258,45 +247,13 @@ if (argv.migrate) {

console.log(`Migrating contract ${argv.contract} to ${codeId}`);

const tx = await wallet.createAndSignTx({
msgs: [
new MsgMigrateContract(
wallet.key.accAddress,
argv.contract,
codeId,
{
action: "",
},
{ uluna: 1000 }
),
],
feeDenoms,
});

const rs = await lcd.tx.broadcast(tx);
var resultCodeId;
try {
resultCodeId = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
assert.equal(codeId, resultCodeId);
} catch (e) {
console.error(
"Encountered an error in parsing migration result. Printing raw log"
);
console.error(rs.raw_log);
throw e;
}
const resultCodeId = deployer.migrate(argv.contract, codeId);

console.log(
`Contract ${argv.contract} code_id successfully updated to ${resultCodeId}`
);
}

// Terra addresses are "human-readable", but for cross-chain registrations, we
// want the "canonical" version
function convert_terra_address_to_hex(human_addr) {
return "0x" + toHex(zeroPad(Bech32.decode(human_addr).data, 32));
}

function sleep(ms) {
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ await wallet.sequence();

/* Deploy artifacts */

const codeIds = {};
const codeIds: Record<string, number> = {};
for (const file of artifacts) {
const contract_bytes = readFileSync(`../artifacts/${file}`);
console.log(`Storing WASM: ${file} (${contract_bytes.length} bytes)`);
Expand All @@ -86,7 +86,8 @@ for (const file of artifacts) {
});

const rs = await terra.tx.broadcast(tx);
const ci = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
// @ts-ignore
const ci: string = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
codeIds[file] = parseInt(ci);
} catch (e) {
console.log(`${e}`);
Expand All @@ -107,8 +108,12 @@ const govChain = 1;
const govAddress =
"0000000000000000000000000000000000000000000000000000000000000004";

async function instantiate(contract, inst_msg, label) {
var address;
async function instantiate(
contract: string,
inst_msg: string | object,
label: string
): Promise<string> {
var address: string = "";
await wallet
.createAndSignTx({
msgs: [
Expand All @@ -125,6 +130,7 @@ async function instantiate(contract, inst_msg, label) {
})
.then((tx) => terra.tx.broadcast(tx))
.then((rs) => {
// @ts-ignore
address = /"_contract_address","value":"([^"]+)/gm.exec(rs.raw_log)[1];
});
console.log(
Expand All @@ -138,7 +144,7 @@ async function instantiate(contract, inst_msg, label) {
// Instantiate contracts. NOTE: Only append at the end, the ordering must be
// deterministic for the addresses to work

const addresses = {};
const addresses: Record<string, string> = {};

addresses["wormhole.wasm"] = await instantiate(
"wormhole.wasm",
Expand Down Expand Up @@ -199,6 +205,6 @@ addresses["pyth_cosmwasm.wasm"] = await instantiate(

// Terra addresses are "human-readable", but for cross-chain registrations, we
// want the "canonical" version
function convert_terra_address_to_hex(human_addr) {
function convert_terra_address_to_hex(human_addr: string) {
return "0x" + toHex(zeroPad(Bech32.decode(human_addr).data, 32));
}
11 changes: 11 additions & 0 deletions target-chains/cosmwasm/tools/src/deployer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export abstract class Deployer {
abstract deployArtifact(artifact: string): Promise<number>;

abstract instantiate(
codeId: number,
inst_msg: string | object,
label: string
): Promise<string>;

abstract migrate(contract: string, codeId: number): Promise<void>;
}
Loading