deploy-pyth-bridge.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { LCDClient, MnemonicKey } from "@terra-money/terra.js";
  2. import {
  3. MsgInstantiateContract,
  4. MsgMigrateContract,
  5. MsgStoreCode,
  6. } from "@terra-money/terra.js";
  7. import { readFileSync } from "fs";
  8. import { Bech32, toHex } from "@cosmjs/encoding";
  9. import { zeroPad } from "ethers/lib/utils.js";
  10. import axios from "axios";
  11. import yargs from "yargs";
  12. import { hideBin } from "yargs/helpers";
  13. import assert from "assert";
  14. export const TERRA_GAS_PRICES_URL = "https://fcd.terra.dev/v1/txs/gas_prices";
  15. const argv = yargs(hideBin(process.argv))
  16. .option("network", {
  17. description: "Which network to deploy to",
  18. choices: ["mainnet", "testnet"],
  19. required: true,
  20. })
  21. .option("artifact", {
  22. description: "Path to Pyth artifact",
  23. type: "string",
  24. required: false,
  25. })
  26. .option("mnemonic", {
  27. description: "Mnemonic (private key)",
  28. type: "string",
  29. required: true,
  30. })
  31. .option("instantiate", {
  32. description: "Instantiate contract if set (default: disabled)",
  33. type: "boolean",
  34. default: false,
  35. required: false,
  36. })
  37. .option("migrate", {
  38. description: "Migrate an existing contract if set (default: disabled)",
  39. type: "boolean",
  40. default: false,
  41. required: false,
  42. })
  43. .option("contract", {
  44. description: "Contract address, used only for migration",
  45. type: "string",
  46. required: false,
  47. default: "",
  48. })
  49. .option("code-id", {
  50. description:
  51. "Code Id, if provided this will be used for migrate/instantiate and no code will be uploaded",
  52. type: "number",
  53. requred: false,
  54. })
  55. .help()
  56. .alias("help", "h").argv;
  57. const artifact = argv.artifact;
  58. /* Set up terra client & wallet. It won't fail because inputs are validated with yargs */
  59. const CONFIG = {
  60. mainnet: {
  61. terraHost: {
  62. URL: "https://phoenix-lcd.terra.dev",
  63. chainID: "phoenix-1",
  64. name: "mainnet",
  65. },
  66. wormholeContract:
  67. "terra12mrnzvhx3rpej6843uge2yyfppfyd3u9c3uq223q8sl48huz9juqffcnh",
  68. pythEmitterAddress:
  69. "6bb14509a612f01fbbc4cffeebd4bbfb492a86df717ebe92eb6df432a3f00a25",
  70. },
  71. testnet: {
  72. terraHost: {
  73. URL: "https://pisco-lcd.terra.dev",
  74. chainID: "pisco-1",
  75. name: "testnet",
  76. },
  77. wormholeContract:
  78. "terra19nv3xr5lrmmr7egvrk2kqgw4kcn43xrtd5g0mpgwwvhetusk4k7s66jyv0",
  79. pythEmitterAddress:
  80. "f346195ac02f37d60d4db8ffa6ef74cb1be3550047543a4a9ee9acf4d78697b0",
  81. },
  82. };
  83. const terraHost = CONFIG[argv.network].terraHost;
  84. const wormholeContract = CONFIG[argv.network].wormholeContract;
  85. const pythEmitterAddress = CONFIG[argv.network].pythEmitterAddress;
  86. const lcd = new LCDClient(terraHost);
  87. const feeDenoms = ["uluna"];
  88. const gasPrices = await axios
  89. .get(TERRA_GAS_PRICES_URL)
  90. .then((result) => result.data);
  91. const wallet = lcd.wallet(
  92. new MnemonicKey({
  93. mnemonic: argv.mnemonic,
  94. })
  95. );
  96. /* Deploy artifacts */
  97. var codeId;
  98. if (argv.codeId !== undefined) {
  99. codeId = argv.codeId;
  100. } else {
  101. if (argv.artifact === undefined) {
  102. console.error(
  103. "Artifact is not provided. Please at least provide artifact or code id"
  104. );
  105. process.exit(1);
  106. }
  107. const contract_bytes = readFileSync(artifact);
  108. console.log(`Storing WASM: ${artifact} (${contract_bytes.length} bytes)`);
  109. const store_code = new MsgStoreCode(
  110. wallet.key.accAddress,
  111. contract_bytes.toString("base64")
  112. );
  113. /*
  114. const feeEstimate = await lcd.tx.estimateFee([wallet.key.accAddress], {
  115. msgs: [store_code],
  116. feeDenoms,
  117. // gasPrices,
  118. });
  119. */
  120. console.log("Deploy fee: ", feeEstimate.amount.toString());
  121. const tx = await wallet.createAndSignTx({
  122. msgs: [store_code],
  123. feeDenoms,
  124. // gasPrices,
  125. // fee: feeEstimate,
  126. });
  127. const rs = await lcd.tx.broadcast(tx);
  128. try {
  129. const ci = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  130. codeId = parseInt(ci);
  131. } catch (e) {
  132. console.error(
  133. "Encountered an error in parsing deploy code result. Printing raw log"
  134. );
  135. console.error(rs.raw_log);
  136. throw e;
  137. }
  138. console.log("Code ID: ", codeId);
  139. if (argv.instantiate || argv.migrate) {
  140. console.log("Sleeping for 10 seconds for store transaction to finalize.");
  141. await sleep(10000);
  142. }
  143. }
  144. if (argv.instantiate) {
  145. console.log("Instantiating a contract");
  146. async function instantiate(codeId, inst_msg) {
  147. var address;
  148. await wallet
  149. .createAndSignTx({
  150. msgs: [
  151. new MsgInstantiateContract(
  152. wallet.key.accAddress,
  153. wallet.key.accAddress,
  154. codeId,
  155. inst_msg
  156. ),
  157. ],
  158. })
  159. .then((tx) => lcd.tx.broadcast(tx))
  160. .then((rs) => {
  161. try {
  162. address = /"contract_address","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  163. } catch (e) {
  164. console.error(
  165. "Encountered an error in parsing instantiation result. Printing raw log"
  166. );
  167. console.error(rs.raw_log);
  168. throw e;
  169. }
  170. });
  171. console.log(
  172. `Instantiated Pyth at ${address} (${convert_terra_address_to_hex(
  173. address
  174. )})`
  175. );
  176. return address;
  177. }
  178. const pythChain = 1;
  179. const contractAddress = await instantiate(codeId, {
  180. wormhole_contract: wormholeContract,
  181. pyth_emitter: Buffer.from(pythEmitterAddress, "hex").toString("base64"),
  182. pyth_emitter_chain: pythChain,
  183. });
  184. console.log(`Deployed Pyth contract at ${contractAddress}`);
  185. }
  186. if (argv.migrate) {
  187. if (argv.contract === "") {
  188. console.error(
  189. "Contract address is not provided. Provide it using --contract"
  190. );
  191. process.exit(1);
  192. }
  193. console.log(`Migrating contract ${argv.contract} to ${codeId}`);
  194. const tx = await wallet.createAndSignTx({
  195. msgs: [
  196. new MsgMigrateContract(
  197. wallet.key.accAddress,
  198. argv.contract,
  199. codeId,
  200. {
  201. action: "",
  202. },
  203. { uluna: 1000 }
  204. ),
  205. ],
  206. feeDenoms,
  207. // gasPrices,
  208. });
  209. const rs = await lcd.tx.broadcast(tx);
  210. var resultCodeId;
  211. try {
  212. resultCodeId = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  213. assert.equal(codeId, resultCodeId);
  214. } catch (e) {
  215. console.error(
  216. "Encountered an error in parsing migration result. Printing raw log"
  217. );
  218. console.error(rs.raw_log);
  219. throw e;
  220. }
  221. console.log(
  222. `Contract ${argv.contract} code_id successfully updated to ${resultCodeId}`
  223. );
  224. }
  225. // Terra addresses are "human-readable", but for cross-chain registrations, we
  226. // want the "canonical" version
  227. function convert_terra_address_to_hex(human_addr) {
  228. return "0x" + toHex(zeroPad(Bech32.decode(human_addr).data, 32));
  229. }
  230. function sleep(ms) {
  231. return new Promise((resolve) => setTimeout(resolve, ms));
  232. }