migrate.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. await wallet
  68. .createAndSignTx({
  69. msgs: [
  70. new MsgMigrateContract(
  71. wallet.key.accAddress,
  72. "terra10pyejy66429refv3g35g2t7am0was7ya7kz2a4",
  73. codeIds["token_bridge.wasm"],
  74. {
  75. "action": ""
  76. },
  77. { uluna: 1000 }
  78. ),
  79. ],
  80. memo: "",
  81. })
  82. .then((tx) => terra.tx.broadcast(tx))
  83. .then((rs) => console.log(rs));
  84. // Set the Admin to the contract.
  85. await wallet
  86. .createAndSignTx({
  87. msgs: [
  88. new MsgUpdateContractAdmin(
  89. wallet.key.accAddress,
  90. "terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5",
  91. "terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5"
  92. ),
  93. ],
  94. memo: "",
  95. })
  96. .then((tx) => terra.tx.broadcast(tx))
  97. .then((rs) => console.log(rs));
  98. // Deploy a new CodeID.
  99. try {
  100. const tx = await wallet.createAndSignTx({
  101. msgs: [store_code],
  102. memo: "",
  103. fee: new StdFee(hardcodedGas["wormhole.wasm"], {
  104. uluna: "100000",
  105. }),
  106. });
  107. const rs = await terra.tx.broadcast(tx);
  108. const ci = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  109. codeIds[file] = parseInt(ci);
  110. } catch (e) {
  111. console.log("Failed to Execute");
  112. }
  113. // Perform a Guardian Set Upgrade to check the following
  114. // flow with six guardians rather than the default one.
  115. const guardianUpgradeVAA = '01000000000100f8547caf1d1263e6b4742aef05691a9e2a7aa082bb2f1deb3850e43b801a87044cf786924d8adff5553f31b41149f94a32b568321390450f12c31aa15c2f941101000000010000000100010000000000000000000000000000000000000000000000000000000000000004000000000227cc370000000000000000000000000000000000000000000000000000000000436f72650200000000000106befa429d57cd18b7f8a4d91a2da9ab4af05d0fbe4ba0c2db9a26208b3bb1a50b01b16941c10d76db4ba0c2db9a26208b3bb1a50b01b16941c10d76db4ba0c2db9a26208b3bb1a50b01b16941c10d76db4ba0c2db9a26208b3bb1a50b01b16941c10d76db4ba0c2db9a26208b3bb1a50b01b16941c10d76db';
  116. await wallet
  117. .createAndSignTx({
  118. msgs: [
  119. new MsgExecuteContract(
  120. wallet.key.accAddress,
  121. "terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5",
  122. {
  123. submit_v_a_a: {
  124. vaa: Buffer.from(guardianUpgradeVAA, "hex").toString(
  125. "base64"
  126. ),
  127. },
  128. },
  129. { uluna: 1000 }
  130. ),
  131. ],
  132. memo: "",
  133. })
  134. .then((tx) => terra.tx.broadcast(tx))
  135. .then((rs) => console.log(rs));
  136. // Upgrace VAA with 5 signatures to test qurom threshold.
  137. const upgradeVAA = '0100000001050058f5e6a55261e137b12405eb5acf3e4670101c3b7561c6694d7116b6afec85b153f90992fb5e0d6d5a79506f524324fb21894ef655367cc37a572b07a9bfe43301011dba8dca119605dcd30efaf7c4f6980afdf5d58f9625648b652288505abe19be11eabe7424e69d3dae682a84c58208237a975c5ed7757613f546763e14db621200021dba8dca119605dcd30efaf7c4f6980afdf5d58f9625648b652288505abe19be11eabe7424e69d3dae682a84c58208237a975c5ed7757613f546763e14db621200031dba8dca119605dcd30efaf7c4f6980afdf5d58f9625648b652288505abe19be11eabe7424e69d3dae682a84c58208237a975c5ed7757613f546763e14db621200041dba8dca119605dcd30efaf7c4f6980afdf5d58f9625648b652288505abe19be11eabe7424e69d3dae682a84c58208237a975c5ed7757613f546763e14db6212000000000100000001000100000000000000000000000000000000000000000000000000000000000000040000000000a653200000000000000000000000000000000000000000000000000000000000436f72650100030000000000000000000000000000000000000000000000000000000000000005';
  138. // Perform a decentralised update with a signed VAA.
  139. await wallet
  140. .createAndSignTx({
  141. msgs: [
  142. new MsgExecuteContract(
  143. wallet.key.accAddress,
  144. "terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5",
  145. {
  146. submit_v_a_a: {
  147. vaa: Buffer.from(upgradeVAA, "hex").toString(
  148. "base64"
  149. ),
  150. },
  151. },
  152. { uluna: 1000 }
  153. ),
  154. ],
  155. memo: "",
  156. })
  157. .then((tx) => terra.tx.broadcast(tx))
  158. .then((rs) => console.log(rs));
  159. // Set the Admin of the Token Bridge to itself.
  160. await wallet
  161. .createAndSignTx({
  162. msgs: [
  163. new MsgUpdateContractAdmin(
  164. wallet.key.accAddress,
  165. "terra10pyejy66429refv3g35g2t7am0was7ya7kz2a4",
  166. "terra10pyejy66429refv3g35g2t7am0was7ya7kz2a4"
  167. ),
  168. ],
  169. memo: "",
  170. })
  171. .then((tx) => terra.tx.broadcast(tx))
  172. .then((rs) => console.log(rs));
  173. // Upgrade VAA for the Token Bridge.
  174. const upgradeTokenVAA = '01000000010500088c284fe2adf0976511290902cbb1dd29239dcd9cb343936c8e76825777db0912eecb7d1be70ddc8b15091834bc0626ea52cc82a202c71f1dc2ff6acffa111b0101b9c36107b2fa1ad413ec6a71aca58d4cd44dea28b692c242805ff0c6df7ce0cb5648f92f5a17a1e1cd2e6df89abb236716d9556a03e6ec5d2ad463cd326d1b830102b9c36107b2fa1ad413ec6a71aca58d4cd44dea28b692c242805ff0c6df7ce0cb5648f92f5a17a1e1cd2e6df89abb236716d9556a03e6ec5d2ad463cd326d1b830103b9c36107b2fa1ad413ec6a71aca58d4cd44dea28b692c242805ff0c6df7ce0cb5648f92f5a17a1e1cd2e6df89abb236716d9556a03e6ec5d2ad463cd326d1b830104b9c36107b2fa1ad413ec6a71aca58d4cd44dea28b692c242805ff0c6df7ce0cb5648f92f5a17a1e1cd2e6df89abb236716d9556a03e6ec5d2ad463cd326d1b8301000000010000000100010000000000000000000000000000000000000000000000000000000000000004000000000441f94100000000000000000000000000000000000000000000546f6b656e4272696467650200030000000000000000000000000000000000000000000000000000000000000005';
  175. // Perform a decentralised update with a signed VAA.
  176. await wallet
  177. .createAndSignTx({
  178. msgs: [
  179. new MsgExecuteContract(
  180. wallet.key.accAddress,
  181. "terra10pyejy66429refv3g35g2t7am0was7ya7kz2a4",
  182. {
  183. submit_vaa: {
  184. data: Buffer.from(upgradeTokenVAA, "hex").toString(
  185. "base64"
  186. ),
  187. },
  188. },
  189. { uluna: 1000 }
  190. ),
  191. ],
  192. memo: "",
  193. })
  194. .then((tx) => terra.tx.broadcast(tx))
  195. .then((rs) => console.log(rs));
  196. }
  197. main();