deploy.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { LCDClient, MnemonicKey } from "@terra-money/terra.js";
  2. import {
  3. StdFee,
  4. MsgInstantiateContract,
  5. MsgExecuteContract,
  6. MsgStoreCode,
  7. } from "@terra-money/terra.js";
  8. import { readFileSync, readdirSync } from "fs";
  9. import { Bech32, toHex } from "@cosmjs/encoding";
  10. import { zeroPad } from "ethers/lib/utils.js";
  11. /*
  12. NOTE: Only append to this array: keeping the ordering is crucial, as the
  13. contracts must be imported in a deterministic order so their addresses remain
  14. deterministic.
  15. */
  16. const artifacts = [
  17. "wormhole.wasm",
  18. "pyth_bridge.wasm",
  19. ];
  20. /* Check that the artifact folder contains all the wasm files we expect and nothing else */
  21. const actual_artifacts = readdirSync("../artifacts/").filter((a) =>
  22. a.endsWith(".wasm")
  23. );
  24. const missing_artifacts = artifacts.filter(
  25. (a) => !actual_artifacts.includes(a)
  26. );
  27. if (missing_artifacts.length) {
  28. console.log(
  29. "Error during terra deployment. The following files are expected to be in the artifacts folder:"
  30. );
  31. missing_artifacts.forEach((file) => console.log(` - ${file}`));
  32. console.log(
  33. "Hint: the deploy script needs to run after the contracts have been built."
  34. );
  35. console.log(
  36. "External binary blobs need to be manually added in tools/Dockerfile."
  37. );
  38. process.exit(1);
  39. }
  40. const unexpected_artifacts = actual_artifacts.filter(
  41. (a) => !artifacts.includes(a)
  42. );
  43. if (unexpected_artifacts.length) {
  44. console.log(
  45. "Error during terra deployment. The following files are not expected to be in the artifacts folder:"
  46. );
  47. unexpected_artifacts.forEach((file) => console.log(` - ${file}`));
  48. console.log("Hint: you might need to modify tools/deploy.js");
  49. process.exit(1);
  50. }
  51. /* Set up terra client & wallet */
  52. const terra = new LCDClient({
  53. URL: "http://localhost:1317",
  54. chainID: "localterra",
  55. });
  56. const wallet = terra.wallet(
  57. new MnemonicKey({
  58. mnemonic:
  59. "notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius",
  60. })
  61. );
  62. await wallet.sequence();
  63. /* Deploy artifacts */
  64. const codeIds = {};
  65. for (const file of artifacts) {
  66. const contract_bytes = readFileSync(`../artifacts/${file}`);
  67. console.log(`Storing WASM: ${file} (${contract_bytes.length} bytes)`);
  68. const store_code = new MsgStoreCode(
  69. wallet.key.accAddress,
  70. contract_bytes.toString("base64")
  71. );
  72. try {
  73. const tx = await wallet.createAndSignTx({
  74. msgs: [store_code],
  75. memo: "",
  76. });
  77. const rs = await terra.tx.broadcast(tx);
  78. const ci = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  79. codeIds[file] = parseInt(ci);
  80. } catch (e) {
  81. console.log(`${e}`);
  82. }
  83. }
  84. console.log(codeIds);
  85. /* Instantiate contracts.
  86. *
  87. * We instantiate the core contracts here (i.e. wormhole itself and the bridge contracts).
  88. * The wrapped asset contracts don't need to be instantiated here, because those
  89. * will be instantiated by the on-chain bridge contracts on demand.
  90. * */
  91. // Governance constants defined by the Wormhole spec.
  92. const govChain = 1;
  93. const govAddress =
  94. "0000000000000000000000000000000000000000000000000000000000000004";
  95. async function instantiate(contract, inst_msg) {
  96. var address;
  97. await wallet
  98. .createAndSignTx({
  99. msgs: [
  100. new MsgInstantiateContract(
  101. wallet.key.accAddress,
  102. wallet.key.accAddress,
  103. codeIds[contract],
  104. inst_msg
  105. ),
  106. ],
  107. memo: "",
  108. })
  109. .then((tx) => terra.tx.broadcast(tx))
  110. .then((rs) => {
  111. address = /"contract_address","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  112. });
  113. console.log(`Instantiated ${contract} at ${address} (${convert_terra_address_to_hex(address)})`);
  114. return address;
  115. }
  116. // Instantiate contracts. NOTE: Only append at the end, the ordering must be
  117. // deterministic for the addresses to work
  118. const addresses = {};
  119. addresses["wormhole.wasm"] = await instantiate("wormhole.wasm", {
  120. gov_chain: govChain,
  121. gov_address: Buffer.from(govAddress, "hex").toString("base64"),
  122. guardian_set_expirity: 86400,
  123. initial_guardian_set: {
  124. addresses: [
  125. {
  126. bytes: Buffer.from(
  127. "beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe",
  128. "hex"
  129. ).toString("base64"),
  130. },
  131. ],
  132. expiration_time: 0,
  133. },
  134. });
  135. const pythEmitterAddress =
  136. "71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b";
  137. const pythChain = 1;
  138. addresses["pyth_bridge.wasm"] = await instantiate("pyth_bridge.wasm", {
  139. wormhole_contract: addresses["wormhole.wasm"],
  140. pyth_emitter: Buffer.from(pythEmitterAddress, "hex").toString(
  141. "base64"
  142. ),
  143. pyth_emitter_chain: pythChain,
  144. });
  145. // Terra addresses are "human-readable", but for cross-chain registrations, we
  146. // want the "canonical" version
  147. function convert_terra_address_to_hex(human_addr) {
  148. return "0x" + toHex(zeroPad(Bech32.decode(human_addr).data, 32));
  149. }