diff --git a/package-lock.json b/package-lock.json index 0a918030c..7e9c7a214 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5665,9 +5665,9 @@ "dev": true }, "node_modules/follow-redirects": { - "version": "1.14.7", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.7.tgz", - "integrity": "sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ==", + "version": "1.14.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", + "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==", "funding": [ { "type": "individual", @@ -21044,9 +21044,9 @@ "dev": true }, "follow-redirects": { - "version": "1.14.7", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.7.tgz", - "integrity": "sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ==" + "version": "1.14.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", + "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==" }, "form-data": { "version": "3.0.1", diff --git a/src/contract/contractFactory.ts b/src/contract/contractFactory.ts index 6997397dc..77891641e 100644 --- a/src/contract/contractFactory.ts +++ b/src/contract/contractFactory.ts @@ -2,28 +2,23 @@ import assert from 'minimalistic-assert'; import { Account } from '../account'; import { Provider, defaultProvider } from '../provider'; -import { Abi, CompiledContract, GetTransactionResponse, RawCalldata } from '../types'; +import { Abi, CompiledContract, RawCalldata } from '../types'; import { BigNumberish } from '../utils/number'; import { Contract } from './default'; export class ContractFactory { - address!: string | null; + abi: Abi; - abi!: Abi; - - transaction_hash!: string | null; - - code!: string | null; + compiledContract: CompiledContract; providerOrAccount: Provider | Account; - compiledContract: CompiledContract; - constructor( compiledContract: CompiledContract, - providerOrAccount: Provider | Account = defaultProvider + providerOrAccount: Provider | Account = defaultProvider, + abi: Abi = compiledContract.abi // abi can be different from the deployed contract ie for proxy contracts ) { - this.abi = compiledContract.abi; + this.abi = abi; this.compiledContract = compiledContract; this.providerOrAccount = providerOrAccount; } @@ -44,41 +39,40 @@ export class ContractFactory { constructorCalldata, addressSalt, }); - assert(code === 'TRANSACTION_RECEIVED', 'Deployment of the contract failed'); - this.address = address as string; - this.transaction_hash = transaction_hash; - this.code = code; - return new Contract(this.compiledContract.abi, address as string, this.providerOrAccount); - } + assert( + code === 'TRANSACTION_RECEIVED' && Boolean(address), + 'Deployment of the contract failed' + ); - /** - * Attaches current abi and provider or account to the new address - * - * @param address - Contract address - * @returns Contract - */ - attach(address: string): Contract { - return ContractFactory.getContract(this.abi, address, this.providerOrAccount); + const contractInstance = new Contract( + this.compiledContract.abi, + address!, + this.providerOrAccount + ); + contractInstance.deployTransactionHash = transaction_hash; + + return contractInstance; } /** - * Fetch the transaction of the deployment + * Attaches to new Provider or Account * - * @returns Transaction + * @param providerOrAccount - new Provider or Account to attach to */ - public async getDeployTransaction(): Promise { - if (this.transaction_hash) { - return this.providerOrAccount.getTransaction(this.transaction_hash); - } - throw Error('Deployment not initialized yet'); + connect(providerOrAccount: Provider | Account): ContractFactory { + this.providerOrAccount = providerOrAccount; + return this; } /** - * Instances contract + * Attaches current abi and provider or account to the new address * + * @param address - Contract address * @returns Contract */ - static getContract(abi: Abi, address: string, providerOrAccount?: Provider | Account): Contract { - return new Contract(abi, address, providerOrAccount); + attach(address: string): Contract { + return new Contract(this.abi, address, this.providerOrAccount); } + + // ethers.js' getDeployTransaction cant be supported as it requires the account or signer to return a signed transaction which is not possible with the current implementation } diff --git a/src/contract/default.ts b/src/contract/default.ts index e2bc1f8c4..53909d2be 100644 --- a/src/contract/default.ts +++ b/src/contract/default.ts @@ -85,6 +85,8 @@ export class Contract implements ContractInterface { providerOrAccount: Provider | Account; + deployTransactionHash?: string; + protected readonly abi: Abi; protected readonly structs: { [name: string]: StructAbi }; diff --git a/src/contract/interface.ts b/src/contract/interface.ts index f1d32b94c..06502177c 100644 --- a/src/contract/interface.ts +++ b/src/contract/interface.ts @@ -13,6 +13,8 @@ export abstract class ContractInterface { public abstract providerOrAccount: Provider | Account; + public abstract deployTransactionHash?: string; + readonly functions!: { [name: string]: AsyncContractFunction }; readonly callStatic!: { [name: string]: AsyncContractFunction };