-
Notifications
You must be signed in to change notification settings - Fork 775
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contractFactory): lib expanded with contract factory
- Loading branch information
Showing
4 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import assert from 'minimalistic-assert'; | ||
|
||
import { Account } from '../account'; | ||
import { Provider, defaultProvider } from '../provider'; | ||
import { Abi, CompiledContract, GetTransactionResponse, RawCalldata } from '../types'; | ||
import { BigNumberish } from '../utils/number'; | ||
import { Contract } from './default'; | ||
|
||
export class ContractFactory { | ||
address!: string | null; | ||
|
||
abi!: Abi; | ||
|
||
transaction_hash!: string | null; | ||
|
||
code!: string | null; | ||
|
||
providerOrAccount: Provider | Account; | ||
|
||
compiledContract: CompiledContract; | ||
|
||
constructor( | ||
compiledContract: CompiledContract, | ||
providerOrAccount: Provider | Account = defaultProvider | ||
) { | ||
this.abi = compiledContract.abi; | ||
this.compiledContract = compiledContract; | ||
this.providerOrAccount = providerOrAccount; | ||
} | ||
|
||
/** | ||
* Deploys contract and returns new instance of the Contract | ||
* | ||
* @param constructorCalldata - Constructor Calldata | ||
* @param addressSalt (optional) - Address Salt for deployment | ||
* @returns deployed Contract | ||
*/ | ||
public async deploy( | ||
constructorCalldata?: RawCalldata, | ||
addressSalt?: BigNumberish | ||
): Promise<Contract> { | ||
const { address, code, transaction_hash } = await this.providerOrAccount.deployContract({ | ||
contract: this.compiledContract, | ||
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); | ||
} | ||
|
||
/** | ||
* 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); | ||
} | ||
|
||
/** | ||
* Fetch the transaction of the deployment | ||
* | ||
* @returns Transaction | ||
*/ | ||
public async getDeployTransaction(): Promise<GetTransactionResponse> { | ||
if (this.transaction_hash) { | ||
return this.providerOrAccount.getTransaction(this.transaction_hash); | ||
} | ||
throw Error('Deployment not initialized yet'); | ||
} | ||
|
||
/** | ||
* Instances contract | ||
* | ||
* @returns Contract | ||
*/ | ||
static getContract(abi: Abi, address: string, providerOrAccount?: Provider | Account): Contract { | ||
return new Contract(abi, address, providerOrAccount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './default'; | ||
export * from './interface'; | ||
export * from './contractFactory'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters