Skip to content

Commit

Permalink
fix(contract-factory): move more towards ethers.js api
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Mar 10, 2022
1 parent 2c9c3d1 commit 4caa6ce
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 41 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 29 additions & 35 deletions src/contract/contractFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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<GetTransactionResponse> {
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
}
2 changes: 2 additions & 0 deletions src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export class Contract implements ContractInterface {

providerOrAccount: Provider | Account;

deployTransactionHash?: string;

protected readonly abi: Abi;

protected readonly structs: { [name: string]: StructAbi };
Expand Down
2 changes: 2 additions & 0 deletions src/contract/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit 4caa6ce

Please sign in to comment.