-
Notifications
You must be signed in to change notification settings - Fork 695
/
contractFactory.ts
92 lines (78 loc) · 2.78 KB
/
contractFactory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { AccountInterface } from '../account';
import { Abi, ArgsOrCalldataWithOptions, CompiledContract } from '../types';
import assert from '../utils/assert';
import { CallData } from '../utils/calldata';
import { Contract, getCalldata, splitArgsAndOptions } from './default';
export class ContractFactory {
abi: Abi;
compiledContract: CompiledContract;
classHash: string;
account: AccountInterface;
private CallData: CallData;
constructor(
compiledContract: CompiledContract,
classHash: string,
account: AccountInterface,
abi: Abi = compiledContract.abi // abi can be different from the deployed contract ie for proxy contracts
) {
this.abi = abi;
this.compiledContract = compiledContract;
this.account = account;
this.classHash = classHash;
this.CallData = new CallData(abi);
}
/**
* Deploys contract and returns new instance of the Contract
*
* @param args - Array of the constructor arguments for deployment
* @param options (optional) Object - parseRequest, parseResponse, addressSalt
* @returns deployed Contract
*/
public async deploy(...args: ArgsOrCalldataWithOptions): Promise<Contract> {
const { args: param, options = { parseRequest: true } } = splitArgsAndOptions(args);
const constructorCalldata = getCalldata(param, () => {
if (options.parseRequest) {
this.CallData.validate('DEPLOY', 'constructor', param);
return this.CallData.compile('constructor', param);
}
// eslint-disable-next-line no-console
console.warn('Call skipped parsing but provided rawArgs, possible malfunction request');
return param;
});
const {
deploy: { contract_address, transaction_hash },
} = await this.account.declareAndDeploy({
contract: this.compiledContract,
constructorCalldata,
salt: options.addressSalt,
});
assert(Boolean(contract_address), 'Deployment of the contract failed');
const contractInstance = new Contract(
this.compiledContract.abi,
contract_address!,
this.account
);
contractInstance.deployTransactionHash = transaction_hash;
return contractInstance;
}
/**
* Attaches to new Account
*
* @param account - new Provider or Account to attach to
* @returns ContractFactory
*/
connect(account: AccountInterface): ContractFactory {
this.account = account;
return this;
}
/**
* Attaches current abi and account to the new address
*
* @param address - Contract address
* @returns Contract
*/
attach(address: string): Contract {
return new Contract(this.abi, address, this.account);
}
// 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
}