deploy.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { Wallet, 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. // TODO: Workaround /tx/estimate_fee errors.
  10. const gas_prices = {
  11. uluna: "0.15",
  12. usdr: "0.1018",
  13. uusd: "0.15",
  14. ukrw: "178.05",
  15. umnt: "431.6259",
  16. ueur: "0.125",
  17. ucny: "0.97",
  18. ujpy: "16",
  19. ugbp: "0.11",
  20. uinr: "11",
  21. ucad: "0.19",
  22. uchf: "0.13",
  23. uaud: "0.19",
  24. usgd: "0.2",
  25. };
  26. async function main() {
  27. const terra = new LCDClient({
  28. URL: "http://localhost:1317",
  29. chainID: "localterra",
  30. });
  31. const wallet = terra.wallet(
  32. new MnemonicKey({
  33. mnemonic:
  34. "notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius",
  35. })
  36. );
  37. await wallet.sequence();
  38. // Deploy WASM blobs.
  39. // Read a list of files from directory containing compiled contracts.
  40. const artifacts = readdirSync("../artifacts/");
  41. // Sort them to get a determinstic list for consecutive code ids.
  42. artifacts.sort();
  43. artifacts.reverse();
  44. const hardcodedGas = {
  45. "cw20_base.wasm": 4000000,
  46. "cw20_wrapped.wasm": 4000000,
  47. "wormhole.wasm": 5000000,
  48. "token_bridge.wasm": 6000000,
  49. };
  50. // Deploy all found WASM files and assign Code IDs.
  51. const codeIds = {};
  52. for (const artifact in artifacts) {
  53. if (
  54. artifacts.hasOwnProperty(artifact) &&
  55. artifacts[artifact].includes(".wasm")
  56. ) {
  57. const file = artifacts[artifact];
  58. const contract_bytes = readFileSync(`../artifacts/${file}`);
  59. console.log(`Storing WASM: ${file} (${contract_bytes.length} bytes)`);
  60. const store_code = new MsgStoreCode(
  61. wallet.key.accAddress,
  62. contract_bytes.toString("base64")
  63. );
  64. try {
  65. const tx = await wallet.createAndSignTx({
  66. msgs: [store_code],
  67. memo: "",
  68. fee: new StdFee(hardcodedGas[artifacts[artifact]], {
  69. uluna: "100000",
  70. }),
  71. });
  72. const rs = await terra.tx.broadcast(tx);
  73. const ci = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  74. codeIds[file] = parseInt(ci);
  75. } catch (e) {
  76. console.log("Failed to Execute");
  77. }
  78. }
  79. }
  80. console.log(codeIds);
  81. // Governance constants defined by the Wormhole spec.
  82. const govChain = 1;
  83. const govAddress =
  84. "0000000000000000000000000000000000000000000000000000000000000004";
  85. const addresses = {};
  86. // Instantiate Wormhole
  87. console.log("Instantiating Wormhole");
  88. await wallet
  89. .createAndSignTx({
  90. msgs: [
  91. new MsgInstantiateContract(
  92. wallet.key.accAddress,
  93. wallet.key.accAddress,
  94. codeIds["wormhole.wasm"],
  95. {
  96. gov_chain: govChain,
  97. gov_address: Buffer.from(govAddress, "hex").toString("base64"),
  98. guardian_set_expirity: 86400,
  99. initial_guardian_set: {
  100. addresses: [
  101. {
  102. bytes: Buffer.from(
  103. "beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe",
  104. "hex"
  105. ).toString("base64"),
  106. },
  107. ],
  108. expiration_time: 0,
  109. },
  110. }
  111. ),
  112. ],
  113. memo: "",
  114. })
  115. .then((tx) => terra.tx.broadcast(tx))
  116. .then((rs) => {
  117. const address = /"contract_address","value":"([^"]+)/gm.exec(
  118. rs.raw_log
  119. )[1];
  120. addresses["wormhole.wasm"] = address;
  121. });
  122. console.log("Instantiating Token Bridge");
  123. await wallet
  124. .createAndSignTx({
  125. msgs: [
  126. new MsgInstantiateContract(
  127. wallet.key.accAddress,
  128. wallet.key.accAddress,
  129. codeIds["token_bridge.wasm"],
  130. {
  131. owner: wallet.key.accAddress,
  132. gov_chain: govChain,
  133. gov_address: Buffer.from(govAddress, "hex").toString("base64"),
  134. wormhole_contract: addresses["wormhole.wasm"],
  135. wrapped_asset_code_id: 2,
  136. }
  137. ),
  138. ],
  139. memo: "",
  140. })
  141. .then((tx) => terra.tx.broadcast(tx))
  142. .then((rs) => {
  143. const address = /"contract_address","value":"([^"]+)/gm.exec(
  144. rs.raw_log
  145. )[1];
  146. addresses["token_bridge.wasm"] = address;
  147. });
  148. await wallet
  149. .createAndSignTx({
  150. msgs: [
  151. new MsgInstantiateContract(
  152. wallet.key.accAddress,
  153. undefined,
  154. codeIds["cw20_base.wasm"],
  155. {
  156. name: "MOCK",
  157. symbol: "MCK",
  158. decimals: 6,
  159. initial_balances: [
  160. {
  161. address: wallet.key.accAddress,
  162. amount: "100000000",
  163. },
  164. ],
  165. mint: null,
  166. }
  167. ),
  168. ],
  169. memo: "",
  170. })
  171. .then((tx) => terra.tx.broadcast(tx))
  172. .then((rs) => {
  173. const address = /"contract_address","value":"([^"]+)/gm.exec(
  174. rs.raw_log
  175. )[1];
  176. addresses["mock.wasm"] = address;
  177. });
  178. console.log(addresses);
  179. const registrations = [
  180. "01000000000100c9f4230109e378f7efc0605fb40f0e1869f2d82fda5b1dfad8a5a2dafee85e033d155c18641165a77a2db6a7afbf2745b458616cb59347e89ae0c7aa3e7cc2d400000000010000000100010000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000546f6b656e4272696467650100000001c69a1b1a65dd336bf1df6a77afb501fc25db7fc0938cb08595a9ef473265cb4f",
  181. "01000000000100e2e1975d14734206e7a23d90db48a6b5b6696df72675443293c6057dcb936bf224b5df67d32967adeb220d4fe3cb28be515be5608c74aab6adb31099a478db5c01000000010000000100010000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000546f6b656e42726964676501000000020000000000000000000000000290fb167208af455bb137780163b7b7a9a10c16",
  182. ];
  183. for (const registration in registrations) {
  184. if (registrations.hasOwnProperty(registration)) {
  185. console.log('Registering');
  186. await wallet
  187. .createAndSignTx({
  188. msgs: [
  189. new MsgExecuteContract(
  190. wallet.key.accAddress,
  191. addresses["token_bridge.wasm"],
  192. {
  193. submit_vaa: {
  194. data: Buffer.from(registrations[registration], "hex").toString('base64'),
  195. },
  196. },
  197. { uluna: 1000 }
  198. ),
  199. ],
  200. memo: "",
  201. fee: new StdFee(2000000, {
  202. uluna: "100000",
  203. }),
  204. })
  205. .then((tx) => terra.tx.broadcast(tx))
  206. .then((rs) => console.log(rs));
  207. }
  208. }
  209. }
  210. main();