deploy-pyth-bridge.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. const feeEstimate = await lcd.tx.estimateFee([wallet.key.accAddress], {
  114. msgs: [store_code],
  115. feeDenoms,
  116. gasPrices,
  117. });
  118. console.log("Deploy fee: ", feeEstimate.amount.toString());
  119. const tx = await wallet.createAndSignTx({
  120. msgs: [store_code],
  121. feeDenoms,
  122. gasPrices,
  123. fee: feeEstimate,
  124. });
  125. const rs = await lcd.tx.broadcast(tx);
  126. try {
  127. const ci = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  128. codeId = parseInt(ci);
  129. } catch (e) {
  130. console.error(
  131. "Encountered an error in parsing deploy code result. Printing raw log"
  132. );
  133. console.error(rs.raw_log);
  134. throw e;
  135. }
  136. console.log("Code ID: ", codeId);
  137. if (argv.instantiate || argv.migrate) {
  138. console.log("Sleeping for 10 seconds for store transaction to finalize.");
  139. await sleep(10000);
  140. }
  141. }
  142. if (argv.instantiate) {
  143. console.log("Instantiating a contract");
  144. async function instantiate(codeId, inst_msg) {
  145. var address;
  146. await wallet
  147. .createAndSignTx({
  148. msgs: [
  149. new MsgInstantiateContract(
  150. wallet.key.accAddress,
  151. wallet.key.accAddress,
  152. codeId,
  153. inst_msg
  154. ),
  155. ],
  156. })
  157. .then((tx) => lcd.tx.broadcast(tx))
  158. .then((rs) => {
  159. try {
  160. address = /"contract_address","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  161. } catch (e) {
  162. console.error(
  163. "Encountered an error in parsing instantiation result. Printing raw log"
  164. );
  165. console.error(rs.raw_log);
  166. throw e;
  167. }
  168. });
  169. console.log(
  170. `Instantiated Pyth at ${address} (${convert_terra_address_to_hex(
  171. address
  172. )})`
  173. );
  174. return address;
  175. }
  176. const pythChain = 1;
  177. const contractAddress = await instantiate(codeId, {
  178. wormhole_contract: wormholeContract,
  179. pyth_emitter: Buffer.from(pythEmitterAddress, "hex").toString("base64"),
  180. pyth_emitter_chain: pythChain,
  181. });
  182. console.log(`Deployed Pyth contract at ${contractAddress}`);
  183. }
  184. if (argv.migrate) {
  185. if (argv.contract === "") {
  186. console.error(
  187. "Contract address is not provided. Provide it using --contract"
  188. );
  189. process.exit(1);
  190. }
  191. console.log(`Migrating contract ${argv.contract} to ${codeId}`);
  192. const tx = await wallet.createAndSignTx({
  193. msgs: [
  194. new MsgMigrateContract(
  195. wallet.key.accAddress,
  196. argv.contract,
  197. codeId,
  198. {
  199. action: "",
  200. },
  201. { uluna: 1000 }
  202. ),
  203. ],
  204. feeDenoms,
  205. gasPrices,
  206. });
  207. const rs = await lcd.tx.broadcast(tx);
  208. var resultCodeId;
  209. try {
  210. resultCodeId = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  211. assert.equal(codeId, resultCodeId);
  212. } catch (e) {
  213. console.error(
  214. "Encountered an error in parsing migration result. Printing raw log"
  215. );
  216. console.error(rs.raw_log);
  217. throw e;
  218. }
  219. console.log(
  220. `Contract ${argv.contract} code_id successfully updated to ${resultCodeId}`
  221. );
  222. }
  223. // Terra addresses are "human-readable", but for cross-chain registrations, we
  224. // want the "canonical" version
  225. function convert_terra_address_to_hex(human_addr) {
  226. return "0x" + toHex(zeroPad(Bech32.decode(human_addr).data, 32));
  227. }
  228. function sleep(ms) {
  229. return new Promise((resolve) => setTimeout(resolve, ms));
  230. }