clients/js: add terra2 support

This commit is contained in:
Evan Gray 2022-06-17 17:56:19 +00:00 committed by Evan Gray
parent df3219e0f7
commit 2144d68a09
5 changed files with 101 additions and 152 deletions

View File

@ -272,7 +272,7 @@ yargs(hideBin(process.argv))
} else if (isEVMChain(chain)) { } else if (isEVMChain(chain)) {
await execute_governance_evm(parsed_vaa.payload, buf, network, chain); await execute_governance_evm(parsed_vaa.payload, buf, network, chain);
} else if (isTerraChain(chain)) { } else if (isTerraChain(chain)) {
await execute_governance_terra(parsed_vaa.payload, buf, network); await execute_governance_terra(parsed_vaa.payload, buf, network, chain);
} else if (chain === "solana") { } else if (chain === "solana") {
await execute_governance_solana(parsed_vaa, buf, network); await execute_governance_solana(parsed_vaa, buf, network);
} else if (chain === "algorand") { } else if (chain === "algorand") {

View File

@ -91,8 +91,9 @@ const MAINNET = {
key: undefined, key: undefined,
}, },
terra2: { terra2: {
rpc: undefined, rpc: "https://phoenix-lcd.terra.dev",
key: undefined, chain_id: "phoenix-1",
key: get_env_var("TERRA_MNEMONIC"),
}, },
ropsten: { ropsten: {
rpc: `https://ropsten.infura.io/v3/${get_env_var("INFURA_KEY")}`, rpc: `https://ropsten.infura.io/v3/${get_env_var("INFURA_KEY")}`,
@ -175,8 +176,9 @@ const TESTNET = {
key: get_env_var("ETH_KEY"), key: get_env_var("ETH_KEY"),
}, },
terra2: { terra2: {
rpc: undefined, rpc: "https://pisco-lcd.terra.dev",
key: undefined, chain_id: "pisco-1",
key: get_env_var("TERRA_MNEMONIC"),
}, },
ropsten: { ropsten: {
rpc: `https://ropsten.infura.io/v3/${get_env_var("INFURA_KEY")}`, rpc: `https://ropsten.infura.io/v3/${get_env_var("INFURA_KEY")}`,
@ -259,8 +261,9 @@ const DEVNET = {
key: undefined, key: undefined,
}, },
terra2: { terra2: {
rpc: undefined, rpc: "http://localhost:1318",
key: undefined, chain_id: "phoenix-1",
key: "notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius",
}, },
ropsten: { ropsten: {
rpc: undefined, rpc: undefined,

View File

@ -9,9 +9,9 @@
"version": "1.0.0", "version": "1.0.0",
"dependencies": { "dependencies": {
"@celo-tools/celo-ethers-wrapper": "^0.1.0", "@celo-tools/celo-ethers-wrapper": "^0.1.0",
"@certusone/wormhole-sdk": "^0.4.1", "@certusone/wormhole-sdk": "^0.4.3",
"@solana/web3.js": "^1.22.0", "@solana/web3.js": "^1.22.0",
"@terra-money/terra.js": "^1.8.9", "@terra-money/terra.js": "^3.1.3",
"axios": "^0.24.0", "axios": "^0.24.0",
"binary-parser": "^2.0.2", "binary-parser": "^2.0.2",
"bn.js": "^5.2.0", "bn.js": "^5.2.0",
@ -542,9 +542,9 @@
} }
}, },
"node_modules/@certusone/wormhole-sdk": { "node_modules/@certusone/wormhole-sdk": {
"version": "0.4.1", "version": "0.4.3",
"resolved": "https://registry.npmjs.org/@certusone/wormhole-sdk/-/wormhole-sdk-0.4.1.tgz", "resolved": "https://registry.npmjs.org/@certusone/wormhole-sdk/-/wormhole-sdk-0.4.3.tgz",
"integrity": "sha512-kScSlYtdOY+xlJNMBSkze16umYIw+/yI2TRQ1qY27Gnzqwes4Fl1z+WJEioO3pCH62SYLCYlme16xjL50Y7b+A==", "integrity": "sha512-2rjH30gzbSWsMBcUOHT1ql9/1x5R2/WQwXFATU/9Vg3qcTKJn8z/kbvU/doHAHuX4OYeoFj3QXQ3qDuwkJl3ig==",
"dependencies": { "dependencies": {
"@improbable-eng/grpc-web": "^0.14.0", "@improbable-eng/grpc-web": "^0.14.0",
"@solana/spl-token": "^0.1.8", "@solana/spl-token": "^0.1.8",
@ -558,38 +558,6 @@
"rxjs": "^7.3.0" "rxjs": "^7.3.0"
} }
}, },
"node_modules/@certusone/wormhole-sdk/node_modules/@terra-money/terra.js": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/@terra-money/terra.js/-/terra.js-3.1.3.tgz",
"integrity": "sha512-80HU5WXrqf3hZTo2GRhIycH17A+SkLD6afbu8MynMDGGD/A861F3zVv6NQ/Z7PWQGHxgddZEK/A0UQ2uK/xfHg==",
"dependencies": {
"@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7",
"@terra-money/terra.proto": "~2.0.0",
"axios": "^0.26.1",
"bech32": "^2.0.0",
"bip32": "^2.0.6",
"bip39": "^3.0.3",
"bufferutil": "^4.0.3",
"decimal.js": "^10.2.1",
"jscrypto": "^1.0.1",
"readable-stream": "^3.6.0",
"secp256k1": "^4.0.2",
"tmp": "^0.2.1",
"utf-8-validate": "^5.0.5",
"ws": "^7.5.5"
},
"engines": {
"node": ">=14"
}
},
"node_modules/@certusone/wormhole-sdk/node_modules/@terra-money/terra.js/node_modules/axios": {
"version": "0.26.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
"integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
"dependencies": {
"follow-redirects": "^1.14.8"
}
},
"node_modules/@certusone/wormhole-sdk/node_modules/bech32": { "node_modules/@certusone/wormhole-sdk/node_modules/bech32": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz", "resolved": "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz",
@ -1608,11 +1576,13 @@
} }
}, },
"node_modules/@terra-money/terra.js": { "node_modules/@terra-money/terra.js": {
"version": "1.8.9", "version": "3.1.3",
"resolved": "https://registry.npmjs.org/@terra-money/terra.js/-/terra.js-1.8.9.tgz", "resolved": "https://registry.npmjs.org/@terra-money/terra.js/-/terra.js-3.1.3.tgz",
"integrity": "sha512-vOBzn6qEWmWDsDngABr1YEwrLXfZjOSCcw/41twSluhspa25zNjLhFPsUexeAozOENbylwPkvEPEbzeFUCbIDg==", "integrity": "sha512-80HU5WXrqf3hZTo2GRhIycH17A+SkLD6afbu8MynMDGGD/A861F3zVv6NQ/Z7PWQGHxgddZEK/A0UQ2uK/xfHg==",
"dependencies": { "dependencies": {
"axios": "^0.21.1", "@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7",
"@terra-money/terra.proto": "~2.0.0",
"axios": "^0.26.1",
"bech32": "^2.0.0", "bech32": "^2.0.0",
"bip32": "^2.0.6", "bip32": "^2.0.6",
"bip39": "^3.0.3", "bip39": "^3.0.3",
@ -1623,18 +1593,18 @@
"secp256k1": "^4.0.2", "secp256k1": "^4.0.2",
"tmp": "^0.2.1", "tmp": "^0.2.1",
"utf-8-validate": "^5.0.5", "utf-8-validate": "^5.0.5",
"ws": "^7.4.2" "ws": "^7.5.5"
}, },
"engines": { "engines": {
"node": ">=12" "node": ">=14"
} }
}, },
"node_modules/@terra-money/terra.js/node_modules/axios": { "node_modules/@terra-money/terra.js/node_modules/axios": {
"version": "0.21.1", "version": "0.26.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
"dependencies": { "dependencies": {
"follow-redirects": "^1.10.0" "follow-redirects": "^1.14.8"
} }
}, },
"node_modules/@terra-money/terra.js/node_modules/bech32": { "node_modules/@terra-money/terra.js/node_modules/bech32": {
@ -10963,9 +10933,9 @@
"requires": {} "requires": {}
}, },
"@certusone/wormhole-sdk": { "@certusone/wormhole-sdk": {
"version": "0.4.1", "version": "0.4.3",
"resolved": "https://registry.npmjs.org/@certusone/wormhole-sdk/-/wormhole-sdk-0.4.1.tgz", "resolved": "https://registry.npmjs.org/@certusone/wormhole-sdk/-/wormhole-sdk-0.4.3.tgz",
"integrity": "sha512-kScSlYtdOY+xlJNMBSkze16umYIw+/yI2TRQ1qY27Gnzqwes4Fl1z+WJEioO3pCH62SYLCYlme16xjL50Y7b+A==", "integrity": "sha512-2rjH30gzbSWsMBcUOHT1ql9/1x5R2/WQwXFATU/9Vg3qcTKJn8z/kbvU/doHAHuX4OYeoFj3QXQ3qDuwkJl3ig==",
"requires": { "requires": {
"@improbable-eng/grpc-web": "^0.14.0", "@improbable-eng/grpc-web": "^0.14.0",
"@solana/spl-token": "^0.1.8", "@solana/spl-token": "^0.1.8",
@ -10979,37 +10949,6 @@
"rxjs": "^7.3.0" "rxjs": "^7.3.0"
}, },
"dependencies": { "dependencies": {
"@terra-money/terra.js": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/@terra-money/terra.js/-/terra.js-3.1.3.tgz",
"integrity": "sha512-80HU5WXrqf3hZTo2GRhIycH17A+SkLD6afbu8MynMDGGD/A861F3zVv6NQ/Z7PWQGHxgddZEK/A0UQ2uK/xfHg==",
"requires": {
"@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7",
"@terra-money/terra.proto": "~2.0.0",
"axios": "^0.26.1",
"bech32": "^2.0.0",
"bip32": "^2.0.6",
"bip39": "^3.0.3",
"bufferutil": "^4.0.3",
"decimal.js": "^10.2.1",
"jscrypto": "^1.0.1",
"readable-stream": "^3.6.0",
"secp256k1": "^4.0.2",
"tmp": "^0.2.1",
"utf-8-validate": "^5.0.5",
"ws": "^7.5.5"
},
"dependencies": {
"axios": {
"version": "0.26.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
"integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
"requires": {
"follow-redirects": "^1.14.8"
}
}
}
},
"bech32": { "bech32": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz", "resolved": "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz",
@ -11634,11 +11573,13 @@
} }
}, },
"@terra-money/terra.js": { "@terra-money/terra.js": {
"version": "1.8.9", "version": "3.1.3",
"resolved": "https://registry.npmjs.org/@terra-money/terra.js/-/terra.js-1.8.9.tgz", "resolved": "https://registry.npmjs.org/@terra-money/terra.js/-/terra.js-3.1.3.tgz",
"integrity": "sha512-vOBzn6qEWmWDsDngABr1YEwrLXfZjOSCcw/41twSluhspa25zNjLhFPsUexeAozOENbylwPkvEPEbzeFUCbIDg==", "integrity": "sha512-80HU5WXrqf3hZTo2GRhIycH17A+SkLD6afbu8MynMDGGD/A861F3zVv6NQ/Z7PWQGHxgddZEK/A0UQ2uK/xfHg==",
"requires": { "requires": {
"axios": "^0.21.1", "@terra-money/legacy.proto": "npm:@terra-money/terra.proto@^0.1.7",
"@terra-money/terra.proto": "~2.0.0",
"axios": "^0.26.1",
"bech32": "^2.0.0", "bech32": "^2.0.0",
"bip32": "^2.0.6", "bip32": "^2.0.6",
"bip39": "^3.0.3", "bip39": "^3.0.3",
@ -11649,15 +11590,15 @@
"secp256k1": "^4.0.2", "secp256k1": "^4.0.2",
"tmp": "^0.2.1", "tmp": "^0.2.1",
"utf-8-validate": "^5.0.5", "utf-8-validate": "^5.0.5",
"ws": "^7.4.2" "ws": "^7.5.5"
}, },
"dependencies": { "dependencies": {
"axios": { "axios": {
"version": "0.21.1", "version": "0.26.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
"requires": { "requires": {
"follow-redirects": "^1.10.0" "follow-redirects": "^1.14.8"
} }
}, },
"bech32": { "bech32": {

View File

@ -3,9 +3,9 @@
"version": "1.0.0", "version": "1.0.0",
"dependencies": { "dependencies": {
"@celo-tools/celo-ethers-wrapper": "^0.1.0", "@celo-tools/celo-ethers-wrapper": "^0.1.0",
"@certusone/wormhole-sdk": "^0.4.1", "@certusone/wormhole-sdk": "^0.4.3",
"@solana/web3.js": "^1.22.0", "@solana/web3.js": "^1.22.0",
"@terra-money/terra.js": "^1.8.9", "@terra-money/terra.js": "^3.1.3",
"axios": "^0.24.0", "axios": "^0.24.0",
"binary-parser": "^2.0.2", "binary-parser": "^2.0.2",
"bn.js": "^5.2.0", "bn.js": "^5.2.0",

View File

@ -1,98 +1,103 @@
import { LCDClient, MnemonicKey, MsgExecuteContract } from "@terra-money/terra.js"; import {
LCDClient,
MnemonicKey,
MsgExecuteContract,
} from "@terra-money/terra.js";
import { fromUint8Array } from "js-base64"; import { fromUint8Array } from "js-base64";
import { impossible, Payload } from "./vaa"; import { impossible, Payload } from "./vaa";
import { NETWORKS } from "./networks" import { NETWORKS } from "./networks";
import { CONTRACTS } from "@certusone/wormhole-sdk" import { CONTRACTS, TerraChainName } from "@certusone/wormhole-sdk";
export async function execute_governance_terra( export async function execute_governance_terra(
payload: Payload, payload: Payload,
vaa: Buffer, vaa: Buffer,
network: "MAINNET" | "TESTNET" | "DEVNET" network: "MAINNET" | "TESTNET" | "DEVNET",
chain: TerraChainName
) { ) {
let n = NETWORKS[network][chain];
let n = NETWORKS[network]['terra'] let contracts = CONTRACTS[network][chain];
let contracts = CONTRACTS[network]['terra']
const terra = new LCDClient({ const terra = new LCDClient({
URL: n.rpc, URL: n.rpc,
chainID: n.chain_id, chainID: n.chain_id,
}) isClassic: chain === "terra",
});
const wallet = terra.wallet(new MnemonicKey({ const wallet = terra.wallet(
mnemonic: n.key new MnemonicKey({
})) mnemonic: n.key,
})
);
let target_contract: string let target_contract: string;
let execute_msg: object let execute_msg: object;
switch (payload.module) { switch (payload.module) {
case "Core": case "Core":
target_contract = contracts.core target_contract = contracts.core;
// sigh... // sigh...
execute_msg = { execute_msg = {
submit_v_a_a: { submit_v_a_a: {
vaa: fromUint8Array(vaa) vaa: fromUint8Array(vaa),
}, },
} };
switch (payload.type) { switch (payload.type) {
case "GuardianSetUpgrade": case "GuardianSetUpgrade":
console.log("Submitting new guardian set") console.log("Submitting new guardian set");
break break;
case "ContractUpgrade": case "ContractUpgrade":
console.log("Upgrading core contract") console.log("Upgrading core contract");
break break;
default: default:
impossible(payload) impossible(payload);
} }
break break;
case "NFTBridge": case "NFTBridge":
if (contracts.nft_bridge === undefined) { if (contracts.nft_bridge === undefined) {
// NOTE: this code can safely be removed once the terra NFT bridge is // NOTE: this code can safely be removed once the terra NFT bridge is
// released, but it's fine for it to stay, as the condition will just be // released, but it's fine for it to stay, as the condition will just be
// skipped once 'contracts.nft_bridge' is defined // skipped once 'contracts.nft_bridge' is defined
throw new Error("NFT bridge not supported yet for terra") throw new Error("NFT bridge not supported yet for terra");
} }
target_contract = contracts.nft_bridge target_contract = contracts.nft_bridge;
execute_msg = { execute_msg = {
submit_vaa: { submit_vaa: {
data: fromUint8Array(vaa) data: fromUint8Array(vaa),
}, },
} };
switch (payload.type) { switch (payload.type) {
case "ContractUpgrade": case "ContractUpgrade":
console.log("Upgrading contract") console.log("Upgrading contract");
break break;
case "RegisterChain": case "RegisterChain":
console.log("Registering chain") console.log("Registering chain");
break break;
default: default:
impossible(payload) impossible(payload);
} }
break break;
case "TokenBridge": case "TokenBridge":
target_contract = contracts.token_bridge target_contract = contracts.token_bridge;
execute_msg = { execute_msg = {
submit_vaa: { submit_vaa: {
data: fromUint8Array(vaa) data: fromUint8Array(vaa),
}, },
} };
switch (payload.type) { switch (payload.type) {
case "ContractUpgrade": case "ContractUpgrade":
console.log("Upgrading contract") console.log("Upgrading contract");
break break;
case "RegisterChain": case "RegisterChain":
console.log("Registering chain") console.log("Registering chain");
break break;
default: default:
impossible(payload) impossible(payload);
execute_msg = impossible(payload) execute_msg = impossible(payload);
} }
break break;
default: default:
target_contract = impossible(payload) target_contract = impossible(payload);
execute_msg = impossible(payload) execute_msg = impossible(payload);
} }
const transaction = new MsgExecuteContract( const transaction = new MsgExecuteContract(
@ -100,16 +105,16 @@ export async function execute_governance_terra(
target_contract, target_contract,
execute_msg, execute_msg,
{ uluna: 1000 } { uluna: 1000 }
) );
wallet wallet
.createAndSignTx({ .createAndSignTx({
msgs: [transaction], msgs: [transaction],
memo: '', memo: "",
})
.then(tx => terra.tx.broadcast(tx))
.then(result => {
console.log(result)
console.log(`TX hash: ${result.txhash}`)
}) })
.then((tx) => terra.tx.broadcast(tx))
.then((result) => {
console.log(result);
console.log(`TX hash: ${result.txhash}`);
});
} }