deploy_evm_contract.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import yargs from "yargs";
  2. import { hideBin } from "yargs/helpers";
  3. import { EvmChain } from "../src/chains";
  4. import { DefaultStore } from "../src/store";
  5. import { readFileSync } from "fs";
  6. import { toPrivateKey } from "../src";
  7. import { COMMON_DEPLOY_OPTIONS } from "./common";
  8. const parser = yargs(hideBin(process.argv))
  9. .scriptName("deploy_evm_contract.ts")
  10. .usage(
  11. "Usage: $0 --std-output <path/to/std-output.json> --private-key <private-key> --chain <chain> [--deploy-args <arg> [... <args>]]"
  12. )
  13. .options({
  14. "std-output": {
  15. type: "string",
  16. demandOption: true,
  17. desc: "Path to the standard JSON output of the contract (build artifact)",
  18. },
  19. "private-key": COMMON_DEPLOY_OPTIONS["private-key"],
  20. chain: COMMON_DEPLOY_OPTIONS["chain"],
  21. "deploy-args": {
  22. type: "array",
  23. desc: "Arguments to pass to the contract constructor. Each argument must begin with 0x if it's a hex string",
  24. },
  25. });
  26. async function main() {
  27. const argv = await parser.argv;
  28. const chain = DefaultStore.chains[argv.chain];
  29. if (!chain) {
  30. throw new Error(`Chain ${argv.contract} not found`);
  31. }
  32. if (chain instanceof EvmChain) {
  33. const artifact = JSON.parse(readFileSync(argv["std-output"], "utf8"));
  34. const address = await chain.deploy(
  35. toPrivateKey(argv["private-key"]),
  36. artifact["abi"],
  37. artifact["bytecode"],
  38. argv["deploy-args"] || []
  39. );
  40. console.log(`Deployed contract at ${address}`);
  41. } else {
  42. throw new Error("Chain is not an EVM chain");
  43. }
  44. }
  45. main();