migrate.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { Wallet, LCDClient, MnemonicKey } from "@terra-money/terra.js";
  2. import {
  3. StdFee,
  4. MsgExecuteContract,
  5. MsgInstantiateContract,
  6. MsgMigrateContract,
  7. MsgStoreCode,
  8. MsgUpdateContractAdmin,
  9. } from "@terra-money/terra.js";
  10. import { readFileSync, readdirSync } from "fs";
  11. async function main() {
  12. const terra = new LCDClient({
  13. URL: "http://localhost:1317",
  14. chainID: "localterra",
  15. });
  16. const wallet = terra.wallet(
  17. new MnemonicKey({
  18. mnemonic:
  19. "notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius",
  20. })
  21. );
  22. const hardcodedGas = {
  23. "wormhole.wasm": 5000000,
  24. };
  25. // Deploy Wormhole alone.
  26. const file = "wormhole.wasm";
  27. const contract_bytes = readFileSync(`../artifacts/${file}`);
  28. console.log(`Storing WASM: ${file} (${contract_bytes.length} bytes)`);
  29. // Get new code id.
  30. const store_code = new MsgStoreCode(
  31. wallet.key.accAddress,
  32. contract_bytes.toString("base64")
  33. );
  34. const codeIds = {};
  35. try {
  36. const tx = await wallet.createAndSignTx({
  37. msgs: [store_code],
  38. memo: "",
  39. fee: new StdFee(hardcodedGas["wormhole.wasm"], {
  40. uluna: "100000",
  41. }),
  42. });
  43. const rs = await terra.tx.broadcast(tx);
  44. const ci = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  45. codeIds[file] = parseInt(ci);
  46. } catch (e) {
  47. console.log("Failed to Execute");
  48. }
  49. // Perform a Centralised update.
  50. await wallet
  51. .createAndSignTx({
  52. msgs: [
  53. new MsgMigrateContract(
  54. wallet.key.accAddress,
  55. "terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5",
  56. codeIds["wormhole.wasm"],
  57. {
  58. "action": ""
  59. },
  60. { uluna: 1000 }
  61. ),
  62. ],
  63. memo: "",
  64. })
  65. .then((tx) => terra.tx.broadcast(tx))
  66. .then((rs) => console.log(rs));
  67. // Set the Admin to the contract.
  68. await wallet
  69. .createAndSignTx({
  70. msgs: [
  71. new MsgUpdateContractAdmin(
  72. wallet.key.accAddress,
  73. "terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5",
  74. "terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5"
  75. ),
  76. ],
  77. memo: "",
  78. })
  79. .then((tx) => terra.tx.broadcast(tx))
  80. .then((rs) => console.log(rs));
  81. // Deploy a new CodeID.
  82. try {
  83. const tx = await wallet.createAndSignTx({
  84. msgs: [store_code],
  85. memo: "",
  86. fee: new StdFee(hardcodedGas["wormhole.wasm"], {
  87. uluna: "100000",
  88. }),
  89. });
  90. const rs = await terra.tx.broadcast(tx);
  91. const ci = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  92. codeIds[file] = parseInt(ci);
  93. } catch (e) {
  94. console.log("Failed to Execute");
  95. }
  96. const upgradeVAA = '010000000001008928c70a029a924d334a24587e9d2ddbcfa7250d7ba61200e86b16966ef2bbd675fb759aa7a47c6392482ef073e9a6d7c4980dc53ed6f90fc84331486e284912000000000100000001000100000000000000000000000000000000000000000000000000000000000000040000000004e78c580000000000000000000000000000000000000000000000000000000000436f72650100030000000000000000000000000000000000000000000000000000000000000005';
  97. // Perform a decentralised update with a signed VAA.
  98. await wallet
  99. .createAndSignTx({
  100. msgs: [
  101. new MsgExecuteContract(
  102. wallet.key.accAddress,
  103. "terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5",
  104. {
  105. submit_v_a_a: {
  106. vaa: Buffer.from(upgradeVAA, "hex").toString(
  107. "base64"
  108. ),
  109. },
  110. },
  111. { uluna: 1000 }
  112. ),
  113. ],
  114. memo: "",
  115. })
  116. .then((tx) => terra.tx.broadcast(tx))
  117. .then((rs) => console.log(rs));
  118. }
  119. main();