deploy-pyth-bridge.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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://lcd.terra.dev",
  63. chainID: "columbus-5",
  64. name: "mainnet",
  65. },
  66. wormholeContract: "terra1dq03ugtd40zu9hcgdzrsq6z2z4hwhc9tqk2uy5",
  67. pythEmitterAddress:
  68. "6bb14509a612f01fbbc4cffeebd4bbfb492a86df717ebe92eb6df432a3f00a25",
  69. },
  70. testnet: {
  71. terraHost: {
  72. URL: "https://bombay-lcd.terra.dev",
  73. chainID: "bombay-12",
  74. name: "testnet",
  75. },
  76. wormholeContract: "terra1pd65m0q9tl3v8znnz5f5ltsfegyzah7g42cx5v",
  77. pythEmitterAddress:
  78. "f346195ac02f37d60d4db8ffa6ef74cb1be3550047543a4a9ee9acf4d78697b0",
  79. },
  80. };
  81. const terraHost = CONFIG[argv.network].terraHost;
  82. const wormholeContract = CONFIG[argv.network].wormholeContract;
  83. const pythEmitterAddress = CONFIG[argv.network].pythEmitterAddress;
  84. const lcd = new LCDClient(terraHost);
  85. const feeDenoms = ["uluna"];
  86. const gasPrices = await axios
  87. .get(TERRA_GAS_PRICES_URL)
  88. .then((result) => result.data);
  89. const wallet = lcd.wallet(
  90. new MnemonicKey({
  91. mnemonic: argv.mnemonic,
  92. })
  93. );
  94. /* Deploy artifacts */
  95. var codeId;
  96. if (argv.codeId !== undefined) {
  97. codeId = argv.codeId;
  98. } else {
  99. if (argv.artifact === undefined) {
  100. console.error(
  101. "Artifact is not provided. Please at least provide artifact or code id"
  102. );
  103. process.exit(1);
  104. }
  105. const contract_bytes = readFileSync(artifact);
  106. console.log(`Storing WASM: ${artifact} (${contract_bytes.length} bytes)`);
  107. const store_code = new MsgStoreCode(
  108. wallet.key.accAddress,
  109. contract_bytes.toString("base64")
  110. );
  111. const feeEstimate = await lcd.tx.estimateFee(
  112. wallet.key.accAddress,
  113. [store_code],
  114. {
  115. feeDenoms,
  116. gasPrices,
  117. }
  118. );
  119. console.log("Deploy fee: ", feeEstimate.amount.toString());
  120. const tx = await wallet.createAndSignTx({
  121. msgs: [store_code],
  122. feeDenoms,
  123. gasPrices,
  124. fee: feeEstimate,
  125. });
  126. const rs = await lcd.tx.broadcast(tx);
  127. try {
  128. const ci = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  129. codeId = parseInt(ci);
  130. } catch (e) {
  131. console.error(
  132. "Encountered an error in parsing deploy code result. Printing raw log"
  133. );
  134. console.error(rs.raw_log);
  135. throw e;
  136. }
  137. console.log("Code ID: ", codeId);
  138. if (argv.instantiate || argv.migrate) {
  139. console.log("Sleeping for 10 seconds for store transaction to finalize.");
  140. await sleep(10000);
  141. }
  142. }
  143. if (argv.instantiate) {
  144. console.log("Instantiating a contract");
  145. async function instantiate(codeId, inst_msg) {
  146. var address;
  147. await wallet
  148. .createAndSignTx({
  149. msgs: [
  150. new MsgInstantiateContract(
  151. wallet.key.accAddress,
  152. wallet.key.accAddress,
  153. codeId,
  154. inst_msg
  155. ),
  156. ],
  157. })
  158. .then((tx) => lcd.tx.broadcast(tx))
  159. .then((rs) => {
  160. try {
  161. address = /"contract_address","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  162. } catch (e) {
  163. console.error(
  164. "Encountered an error in parsing instantiation result. Printing raw log"
  165. );
  166. console.error(rs.raw_log);
  167. throw e;
  168. }
  169. });
  170. console.log(
  171. `Instantiated Pyth at ${address} (${convert_terra_address_to_hex(
  172. address
  173. )})`
  174. );
  175. return address;
  176. }
  177. const pythChain = 1;
  178. const contractAddress = await instantiate(codeId, {
  179. wormhole_contract: wormholeContract,
  180. pyth_emitter: Buffer.from(pythEmitterAddress, "hex").toString("base64"),
  181. pyth_emitter_chain: pythChain,
  182. });
  183. console.log(`Deployed Pyth contract at ${contractAddress}`);
  184. }
  185. if (argv.migrate) {
  186. if (argv.contract === "") {
  187. console.error(
  188. "Contract address is not provided. Provide it using --contract"
  189. );
  190. process.exit(1);
  191. }
  192. console.log(`Migrating contract ${argv.contract} to ${codeId}`);
  193. const tx = await wallet.createAndSignTx({
  194. msgs: [
  195. new MsgMigrateContract(
  196. wallet.key.accAddress,
  197. argv.contract,
  198. codeId,
  199. {
  200. action: "",
  201. },
  202. { uluna: 1000 }
  203. ),
  204. ],
  205. feeDenoms,
  206. gasPrices,
  207. });
  208. const rs = await lcd.tx.broadcast(tx);
  209. var resultCodeId;
  210. try {
  211. resultCodeId = /"code_id","value":"([^"]+)/gm.exec(rs.raw_log)[1];
  212. assert.equal(codeId, resultCodeId);
  213. } catch (e) {
  214. console.error(
  215. "Encountered an error in parsing migration result. Printing raw log"
  216. );
  217. console.error(rs.raw_log);
  218. throw e;
  219. }
  220. console.log(
  221. `Contract ${argv.contract} code_id successfully updated to ${resultCodeId}`
  222. );
  223. }
  224. // Terra addresses are "human-readable", but for cross-chain registrations, we
  225. // want the "canonical" version
  226. function convert_terra_address_to_hex(human_addr) {
  227. return "0x" + toHex(zeroPad(Bech32.decode(human_addr).data, 32));
  228. }
  229. function sleep(ms) {
  230. return new Promise((resolve) => setTimeout(resolve, ms));
  231. }