Skip to content

Commit

Permalink
feat(contractFactory): lib expanded with contract factory
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Mar 10, 2022
1 parent 1ef9d2f commit 2c9c3d1
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
31 changes: 30 additions & 1 deletion __tests__/contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isBN } from 'bn.js';

import { Account, Contract, Provider, defaultProvider, ec, stark } from '../src';
import { Account, Contract, ContractFactory, Provider, defaultProvider, ec, stark } from '../src';
import { getSelectorFromName } from '../src/utils/hash';
import { BigNumberish, toBN } from '../src/utils/number';
import { compileCalldata } from '../src/utils/stark';
Expand Down Expand Up @@ -266,3 +266,32 @@ describe('class Contract {}', () => {
});
});
});

describe('class ContractFactory {}', () => {
let erc20Address: string;
beforeAll(async () => {
const { code, transaction_hash, address } = await defaultProvider.deployContract({
contract: compiledErc20,
});
expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTransaction(transaction_hash);
erc20Address = address;
});
test('deployment of new contract', async () => {
const factory = new ContractFactory(compiledErc20);
const erc20 = await factory.deploy();
expect(erc20 instanceof Contract);
});
test('get the transaction of the deployed contract', async () => {
const factory = new ContractFactory(compiledErc20);
await factory.deploy();
const tx = await factory.getDeployTransaction();
expect(tx).toHaveProperty('status');
expect(tx).toHaveProperty('transaction');
});
test('attach new contract', async () => {
const factory = new ContractFactory(compiledErc20);
const erc20 = factory.attach(erc20Address);
expect(erc20 instanceof Contract);
});
});
84 changes: 84 additions & 0 deletions src/contract/contractFactory.ts
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);
}
}
1 change: 1 addition & 0 deletions src/contract/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './default';
export * from './interface';
export * from './contractFactory';
2 changes: 1 addition & 1 deletion src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type FunctionAbi = {
name: string;
outputs: AbiEntry[];
stateMutability?: 'view';
type: 'function';
type: 'function' | 'constructor';
};

export type StructAbi = {
Expand Down

0 comments on commit 2c9c3d1

Please sign in to comment.