-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
contract.ts
52 lines (44 loc) · 1.72 KB
/
contract.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
import type { FunctionFragment, JsonAbi, JsonFlatAbi } from '@fuel-ts/abi-coder';
import { Interface } from '@fuel-ts/abi-coder';
import { Address } from '@fuel-ts/address';
import type { AbstractAddress, AbstractContract } from '@fuel-ts/interfaces';
import type { Provider } from '@fuel-ts/providers';
import { Wallet } from '@fuel-ts/wallet';
import type { InvokeFunctions } from '../types';
import { FunctionInvocationScope } from './functions/invocation-scope';
import { MultiCallInvocationScope } from './functions/multicall-scope';
export default class Contract implements AbstractContract {
id!: AbstractAddress;
provider!: Provider | null;
interface!: Interface;
wallet!: Wallet | null;
functions: InvokeFunctions = {};
constructor(
id: string | AbstractAddress,
abi: JsonAbi | JsonFlatAbi | Interface,
walletOrProvider: Wallet | Provider | null = null
) {
this.interface = abi instanceof Interface ? abi : new Interface(abi);
this.id = Address.fromAddressOrString(id);
if (walletOrProvider instanceof Wallet) {
this.provider = walletOrProvider.provider;
this.wallet = walletOrProvider;
} else {
this.provider = walletOrProvider;
this.wallet = null;
}
Object.keys(this.interface.functions).forEach((name) => {
const fragment = this.interface.getFunction(name);
Object.defineProperty(this.functions, fragment.name, {
value: this.buildFunction(fragment),
writable: false,
});
});
}
buildFunction(func: FunctionFragment) {
return (...args: Array<unknown>) => new FunctionInvocationScope(this, func, args);
}
multiCall(calls: Array<FunctionInvocationScope>) {
return new MultiCallInvocationScope(this, calls);
}
}