From 19ee2b516005b2c35b846f19457ec9bbfa0c283b Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Sun, 4 Aug 2019 19:55:46 -0400 Subject: [PATCH] Using CLI --wait instead of custom Plugin flag. --- packages/cli/src.ts/bin/ethers-ens.ts | 90 +++++++-------------------- 1 file changed, 21 insertions(+), 69 deletions(-) diff --git a/packages/cli/src.ts/bin/ethers-ens.ts b/packages/cli/src.ts/bin/ethers-ens.ts index 1f26d5d809..79bd403d9d 100644 --- a/packages/cli/src.ts/bin/ethers-ens.ts +++ b/packages/cli/src.ts/bin/ethers-ens.ts @@ -74,15 +74,13 @@ abstract class EnsPlugin extends Plugin { ethers.utils.defineReadOnly(this, "_ethAddressCache", { }); } - async getEns(): Promise { - let network = await this.provider.getNetwork(); - return new ethers.Contract(network.ensAddress, ensAbi, this.accounts[0] || this.provider); + getEns(): ethers.Contract { + return new ethers.Contract(this.network.ensAddress, ensAbi, this.accounts[0] || this.provider); } async getResolver(nodehash: string): Promise { if (!this._ethAddressCache[nodehash]) { - let ens = await this.getEns(); - this._ethAddressCache[nodehash] = await ens.resolver(nodehash); + this._ethAddressCache[nodehash] = await this.getEns().resolver(nodehash); } return new ethers.Contract(this._ethAddressCache[nodehash], resolverAbi, this.accounts[0] || this.provider); } @@ -132,7 +130,7 @@ class LookupPlugin extends EnsPlugin { async run(): Promise { await super.run(); - let ens = await this.getEns(); + let ens = this.getEns(); for (let i = 0; i < this.names.length; i++) { let name = this.names[i]; @@ -203,7 +201,6 @@ cli.addPlugin("lookup", LookupPlugin); abstract class AccountPlugin extends EnsPlugin { name: string; nodehash: string; - _wait: boolean; static getHelp(): Help { return logger.throwError("subclasses must implemetn this", ethers.errors.UNSUPPORTED_OPERATION, { @@ -211,32 +208,6 @@ abstract class AccountPlugin extends EnsPlugin { }); } - static getOptionHelp(): Array { - return [ - { - name: "[ --wait ]", - help: "Wait for the transaction to be mined" - } - ]; - } - - async wait(tx: ethers.providers.TransactionResponse): Promise { - if (!this._wait) { return; } - try { - let receipt = await tx.wait(); - this.dump("Success:", { - BlockNumber: receipt.blockNumber, - BlockHash: receipt.blockHash, - GasUsed: ethers.utils.commify(receipt.gasUsed.toString()), - Fee: ethers.utils.formatEther(receipt.gasUsed.mul(tx.gasPrice)) - }); - } catch (error) { - this.dump("Failed:", { - Error: error.message - }); - } - } - async _setValue(key: string, value: string): Promise { ethers.utils.defineReadOnly(this, key, value); if (key === "name") { @@ -280,8 +251,7 @@ abstract class ControllerPlugin extends AccountPlugin { duration: number; static getOptionHelp(): Array { - let result = super.getOptionHelp(); - [ + return [ { name: "[ --duration DAYS ]", help: "Register duration (default: 365 days)" @@ -298,10 +268,7 @@ abstract class ControllerPlugin extends AccountPlugin { name: "[ --owner OWNER ]", help: "The target owner (default: current account)" } - ].forEach((help) => { - result.push(help); - }); - return result; + ]; } async _setValue(key: string, value: string): Promise { @@ -378,9 +345,7 @@ class CommitPlugin extends ControllerPlugin { Commitment: commitment }); - let tx = await ethController.commit(commitment); - - this.wait(tx); + await ethController.commit(commitment); } } cli.addPlugin("commit", CommitPlugin); @@ -408,11 +373,9 @@ class RevealPlugin extends ControllerPlugin { Fee: ethers.utils.formatEther(fee), }); - let tx = await ethController.register(this.label, this.owner, this.duration, this.salt, { + await ethController.register(this.label, this.owner, this.duration, this.salt, { value: fee.mul(11).div(10) }); - - this.wait(tx); } } cli.addPlugin("reveal", RevealPlugin); @@ -462,12 +425,12 @@ abstract class AddressAccountPlugin extends AccountPlugin { address: string; static getOptionHelp(): Array { - let options = super.getOptionHelp(); - options.push({ - name: "[ --address ADDRESS ]", - help: "Override the address" - }); - return options; + return [ + { + name: "[ --address ADDRESS ]", + help: "Override the address" + } + ]; } getDefaultAddress(): Promise { @@ -497,10 +460,8 @@ class SetOwnerPlugin extends AddressAccountPlugin { async run(): Promise { await super.run(); - const ens = await this.getEns(); - let tx = ens.setOwner(this.nodehash, this.address); - this.wait(tx); + this.getEns().setOwner(this.nodehash, this.address); } } cli.addPlugin("set-owner", SetOwnerPlugin); @@ -533,9 +494,7 @@ class SetSubnodePlugin extends AddressAccountPlugin { Node: this.node }); - const ens = await this.getEns(); - let tx = await ens.setSubnodeOwner(ethers.utils.namehash(this.node), ethers.utils.id(this.label), this.address); - this.wait(tx); + await this.getEns().setSubnodeOwner(ethers.utils.namehash(this.node), ethers.utils.id(this.label), this.address); } } cli.addPlugin("set-subnode", SetSubnodePlugin); @@ -560,10 +519,7 @@ class SetResolverPlugin extends AddressAccountPlugin { Resolver: this.address }); - const ens = await this.getEns(); - let tx = await ens.setResolver(this.nodehash, this.address); - - this.wait(tx); + await this.getEns().setResolver(this.nodehash, this.address); } } cli.addPlugin("set-resolver", SetResolverPlugin); @@ -586,8 +542,7 @@ class SetAddrPlugin extends AddressAccountPlugin { }); let resolver = await this.getResolver(this.nodehash); - let tx = await resolver.setAddr(this.nodehash, this.address); - this.wait(tx); + await resolver.setAddr(this.nodehash, this.address); } } cli.addPlugin("set-addr", SetAddrPlugin); @@ -610,8 +565,7 @@ abstract class TextAccountPlugin extends AccountPlugin { }); let resolver = await this.getResolver(this.nodehash); - let tx = await resolver.setText(this.nodehash, key, value); - this.wait(tx); + await resolver.setText(this.nodehash, key, value); } } @@ -727,8 +681,7 @@ class MigrateRegistrarPlugin extends AccountPlugin { }); let legacyRegistrar = await this.getEthLegacyRegistrar(); - let tx = await legacyRegistrar.transferRegistrars(ethers.utils.id(this.label)); - this.wait(tx); + await legacyRegistrar.transferRegistrars(ethers.utils.id(this.label)); } } cli.addPlugin("migrate-registrar", MigrateRegistrarPlugin); @@ -771,8 +724,7 @@ class TransferPlugin extends AccountPlugin { }); let registrar = await this.getEthRegistrar(); - let tx = await registrar.transferFrom(this.accounts[0].getAddress(), this.new_owner, ethers.utils.id(this.label)); - this.wait(tx); + await registrar.transferFrom(this.accounts[0].getAddress(), this.new_owner, ethers.utils.id(this.label)); } } cli.addPlugin("transfer", TransferPlugin);