factory.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ContractFactory = void 0;
  4. const index_js_1 = require("../abi/index.js");
  5. const index_js_2 = require("../address/index.js");
  6. const index_js_3 = require("../utils/index.js");
  7. const contract_js_1 = require("./contract.js");
  8. // A = Arguments to the constructor
  9. // I = Interface of deployed contracts
  10. /**
  11. * A **ContractFactory** is used to deploy a Contract to the blockchain.
  12. */
  13. class ContractFactory {
  14. /**
  15. * The Contract Interface.
  16. */
  17. interface;
  18. /**
  19. * The Contract deployment bytecode. Often called the initcode.
  20. */
  21. bytecode;
  22. /**
  23. * The ContractRunner to deploy the Contract as.
  24. */
  25. runner;
  26. /**
  27. * Create a new **ContractFactory** with %%abi%% and %%bytecode%%,
  28. * optionally connected to %%runner%%.
  29. *
  30. * The %%bytecode%% may be the ``bytecode`` property within the
  31. * standard Solidity JSON output.
  32. */
  33. constructor(abi, bytecode, runner) {
  34. const iface = index_js_1.Interface.from(abi);
  35. // Dereference Solidity bytecode objects and allow a missing `0x`-prefix
  36. if (bytecode instanceof Uint8Array) {
  37. bytecode = (0, index_js_3.hexlify)((0, index_js_3.getBytes)(bytecode));
  38. }
  39. else {
  40. if (typeof (bytecode) === "object") {
  41. bytecode = bytecode.object;
  42. }
  43. if (!bytecode.startsWith("0x")) {
  44. bytecode = "0x" + bytecode;
  45. }
  46. bytecode = (0, index_js_3.hexlify)((0, index_js_3.getBytes)(bytecode));
  47. }
  48. (0, index_js_3.defineProperties)(this, {
  49. bytecode, interface: iface, runner: (runner || null)
  50. });
  51. }
  52. attach(target) {
  53. return new contract_js_1.BaseContract(target, this.interface, this.runner);
  54. }
  55. /**
  56. * Resolves to the transaction to deploy the contract, passing %%args%%
  57. * into the constructor.
  58. */
  59. async getDeployTransaction(...args) {
  60. let overrides = {};
  61. const fragment = this.interface.deploy;
  62. if (fragment.inputs.length + 1 === args.length) {
  63. overrides = await (0, contract_js_1.copyOverrides)(args.pop());
  64. }
  65. if (fragment.inputs.length !== args.length) {
  66. throw new Error("incorrect number of arguments to constructor");
  67. }
  68. const resolvedArgs = await (0, contract_js_1.resolveArgs)(this.runner, fragment.inputs, args);
  69. const data = (0, index_js_3.concat)([this.bytecode, this.interface.encodeDeploy(resolvedArgs)]);
  70. return Object.assign({}, overrides, { data });
  71. }
  72. /**
  73. * Resolves to the Contract deployed by passing %%args%% into the
  74. * constructor.
  75. *
  76. * This will resolve to the Contract before it has been deployed to the
  77. * network, so the [[BaseContract-waitForDeployment]] should be used before
  78. * sending any transactions to it.
  79. */
  80. async deploy(...args) {
  81. const tx = await this.getDeployTransaction(...args);
  82. (0, index_js_3.assert)(this.runner && typeof (this.runner.sendTransaction) === "function", "factory runner does not support sending transactions", "UNSUPPORTED_OPERATION", {
  83. operation: "sendTransaction"
  84. });
  85. const sentTx = await this.runner.sendTransaction(tx);
  86. const address = (0, index_js_2.getCreateAddress)(sentTx);
  87. return new contract_js_1.BaseContract(address, this.interface, this.runner, sentTx);
  88. }
  89. /**
  90. * Return a new **ContractFactory** with the same ABI and bytecode,
  91. * but connected to %%runner%%.
  92. */
  93. connect(runner) {
  94. return new ContractFactory(this.interface, this.bytecode, runner);
  95. }
  96. /**
  97. * Create a new **ContractFactory** from the standard Solidity JSON output.
  98. */
  99. static fromSolidity(output, runner) {
  100. (0, index_js_3.assertArgument)(output != null, "bad compiler output", "output", output);
  101. if (typeof (output) === "string") {
  102. output = JSON.parse(output);
  103. }
  104. const abi = output.abi;
  105. let bytecode = "";
  106. if (output.bytecode) {
  107. bytecode = output.bytecode;
  108. }
  109. else if (output.evm && output.evm.bytecode) {
  110. bytecode = output.evm.bytecode;
  111. }
  112. return new this(abi, bytecode, runner);
  113. }
  114. }
  115. exports.ContractFactory = ContractFactory;
  116. //# sourceMappingURL=factory.js.map