/
multicalls.test.ts
111 lines (92 loc) · 3.42 KB
/
multicalls.test.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import type { Contract, Provider } from 'fuels';
import { BN, ContractFactory } from 'fuels';
import {
DocSnippetProjectsEnum,
getDocsSnippetsForcProject,
} from '../../../test/fixtures/forc-projects';
import { getTestWallet } from '../../utils';
/**
* @group node
*/
describe(__filename, () => {
let echoContract: Contract;
let counterContract: Contract;
let contextContract: Contract;
let provider: Provider;
let baseAssetId: string;
beforeAll(async () => {
const wallet = await getTestWallet();
provider = wallet.provider;
baseAssetId = provider.getBaseAssetId();
const counterArtifacts = getDocsSnippetsForcProject(DocSnippetProjectsEnum.COUNTER);
const echoArtifacts = getDocsSnippetsForcProject(DocSnippetProjectsEnum.ECHO_VALUES);
const contextArtifacts = getDocsSnippetsForcProject(DocSnippetProjectsEnum.RETURN_CONTEXT);
const factory1 = new ContractFactory(
echoArtifacts.binHexlified,
echoArtifacts.abiContents,
wallet
);
const factory2 = new ContractFactory(
counterArtifacts.binHexlified,
counterArtifacts.abiContents,
wallet
);
const factory3 = new ContractFactory(
contextArtifacts.binHexlified,
contextArtifacts.abiContents,
wallet
);
echoContract = await factory1.deployContract();
counterContract = await factory2.deployContract({
storageSlots: counterArtifacts.storageSlots,
});
contextContract = await factory3.deployContract();
});
it('should successfully submit multiple calls from the same contract function', async () => {
// #region multicall-1
const { value: results } = await counterContract
.multiCall([
counterContract.functions.get_count(),
counterContract.functions.increment_count(2),
counterContract.functions.increment_count(4),
])
.call();
const initialValue = new BN(results[0]).toNumber();
const incrementedValue1 = new BN(results[1]).toNumber();
const incrementedValue2 = new BN(results[2]).toNumber();
expect(incrementedValue1).toEqual(initialValue + 2);
expect(incrementedValue2).toEqual(incrementedValue1 + 4);
// #endregion multicall-1
});
it('should successfully submit multiple calls from different contracts functions', async () => {
// #region multicall-2
const chain = echoContract.multiCall([
echoContract.functions.echo_u8(17),
counterContract.functions.get_count(),
counterContract.functions.increment_count(5),
]);
const { value: results } = await chain.call();
const echoedValue = results[0];
const initialCounterValue = new BN(results[1]).toNumber();
const counterIncrementedValue = new BN(results[2]).toNumber();
expect(echoedValue).toEqual(17);
expect(counterIncrementedValue).toEqual(initialCounterValue + 5);
// #endregion multicall-2
});
it('should successfully submit multiple calls from different contracts functions', async () => {
// #region multicall-3
const { value: results } = await contextContract
.multiCall([
echoContract.functions.echo_u8(10),
contextContract.functions.return_context_amount().callParams({
forward: [100, baseAssetId],
}),
])
.call();
const echoedValue = results[0];
const fowardedValue = new BN(results[1]).toNumber();
expect(echoedValue).toEqual(10);
expect(fowardedValue).toEqual(100);
// #endregion multicall-3
});
});