wormhole/clients/js/terra.ts

160 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-06-17 10:56:19 -07:00
import {
2022-09-29 11:21:18 -07:00
Coin,
Fee,
2022-06-17 10:56:19 -07:00
LCDClient,
MnemonicKey,
MsgExecuteContract,
} from "@terra-money/terra.js";
import { fromUint8Array } from "js-base64";
import { impossible, Payload } from "./vaa";
2022-06-17 10:56:19 -07:00
import { NETWORKS } from "./networks";
2022-09-29 11:21:18 -07:00
import axios from "axios";
import { CONTRACTS, TerraChainName } from "@certusone/wormhole-sdk/lib/cjs/utils/consts";
export async function execute_terra(
payload: Payload,
vaa: Buffer,
2022-06-17 10:56:19 -07:00
network: "MAINNET" | "TESTNET" | "DEVNET",
chain: TerraChainName
) {
2022-06-17 10:56:19 -07:00
let n = NETWORKS[network][chain];
let contracts = CONTRACTS[network][chain];
const terra = new LCDClient({
URL: n.rpc,
chainID: n.chain_id,
2022-06-17 10:56:19 -07:00
isClassic: chain === "terra",
});
2022-06-17 10:56:19 -07:00
const wallet = terra.wallet(
new MnemonicKey({
mnemonic: n.key,
})
);
2022-06-17 10:56:19 -07:00
let target_contract: string;
let execute_msg: object;
switch (payload.module) {
case "Core":
2022-06-17 10:56:19 -07:00
target_contract = contracts.core;
// sigh...
execute_msg = {
submit_v_a_a: {
2022-06-17 10:56:19 -07:00
vaa: fromUint8Array(vaa),
},
2022-06-17 10:56:19 -07:00
};
switch (payload.type) {
case "GuardianSetUpgrade":
2022-06-17 10:56:19 -07:00
console.log("Submitting new guardian set");
break;
case "ContractUpgrade":
2022-06-17 10:56:19 -07:00
console.log("Upgrading core contract");
break;
default:
2022-06-17 10:56:19 -07:00
impossible(payload);
}
2022-06-17 10:56:19 -07:00
break;
case "NFTBridge":
if (contracts.nft_bridge === undefined) {
// 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
// skipped once 'contracts.nft_bridge' is defined
2022-06-17 10:56:19 -07:00
throw new Error("NFT bridge not supported yet for terra");
}
2022-06-17 10:56:19 -07:00
target_contract = contracts.nft_bridge;
execute_msg = {
submit_vaa: {
2022-06-17 10:56:19 -07:00
data: fromUint8Array(vaa),
},
2022-06-17 10:56:19 -07:00
};
switch (payload.type) {
case "ContractUpgrade":
2022-06-17 10:56:19 -07:00
console.log("Upgrading contract");
break;
case "RegisterChain":
2022-06-17 10:56:19 -07:00
console.log("Registering chain");
break;
case "Transfer":
console.log("Completing transfer");
break;
default:
2022-06-17 10:56:19 -07:00
impossible(payload);
}
2022-06-17 10:56:19 -07:00
break;
case "TokenBridge":
2022-06-17 10:56:19 -07:00
target_contract = contracts.token_bridge;
execute_msg = {
submit_vaa: {
2022-06-17 10:56:19 -07:00
data: fromUint8Array(vaa),
},
2022-06-17 10:56:19 -07:00
};
switch (payload.type) {
case "ContractUpgrade":
2022-06-17 10:56:19 -07:00
console.log("Upgrading contract");
break;
case "RegisterChain":
2022-06-17 10:56:19 -07:00
console.log("Registering chain");
break;
case "Transfer":
console.log("Completing transfer");
break;
case "AttestMeta":
console.log("Creating wrapped token");
break;
case "TransferWithPayload":
2022-09-29 11:21:18 -07:00
throw Error("Can't complete payload 3 transfer from CLI");
default:
2022-09-29 11:21:18 -07:00
impossible(payload);
break;
}
2022-06-17 10:56:19 -07:00
break;
default:
2022-06-17 10:56:19 -07:00
target_contract = impossible(payload);
execute_msg = impossible(payload);
}
const transaction = new MsgExecuteContract(
wallet.key.accAddress,
target_contract,
execute_msg,
{ uluna: 1000 }
2022-06-17 10:56:19 -07:00
);
2022-09-29 11:21:18 -07:00
const feeDenoms = ["uluna"];
const gasPrices = await axios
.get("https://fcd.terra.dev/v1/txs/gas_prices")
.then((result) => result.data);
const feeEstimate = await terra.tx.estimateFee(
[
{
sequenceNumber: await wallet.sequence(),
publicKey: wallet.key.publicKey,
},
],
{
msgs: [transaction],
memo: "",
feeDenoms,
gasPrices,
}
);
wallet
.createAndSignTx({
msgs: [transaction],
2022-06-17 10:56:19 -07:00
memo: "",
2022-09-29 11:21:18 -07:00
fee: new Fee(
feeEstimate.gas_limit,
feeEstimate.amount.add(new Coin("uluna", 12))
),
})
2022-06-17 10:56:19 -07:00
.then((tx) => terra.tx.broadcast(tx))
.then((result) => {
console.log(result);
console.log(`TX hash: ${result.txhash}`);
});
}