/
interacting-with-contracts.test.ts
67 lines (51 loc) · 2.41 KB
/
interacting-with-contracts.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
import type { Provider } from 'fuels';
import { Contract } from 'fuels';
import { DocSnippetProjectsEnum } from '../../../test/fixtures/forc-projects';
import { createAndDeployContractFromProject, getTestWallet } from '../../utils';
/**
* @group node
*/
describe(__filename, () => {
let counterContract: Contract;
let provider: Provider;
let baseAssetId: string;
beforeAll(async () => {
counterContract = await createAndDeployContractFromProject(DocSnippetProjectsEnum.COUNTER);
provider = counterContract.provider;
baseAssetId = provider.getBaseAssetId();
});
it('should successfully use "get" to read from the blockchain', async () => {
await counterContract.functions.increment_count(1).call();
const { id: contractId, interface: abi } = counterContract;
// #region interacting-with-contracts-1
const contract = new Contract(contractId, abi, provider);
const { value } = await contract.functions.get_count().get();
// #endregion interacting-with-contracts-1
expect(value.toNumber()).toBeGreaterThanOrEqual(1);
});
it('should successfully use "dryRun" to validate a TX without a wallet', async () => {
const { id: contractId, interface: abi } = counterContract;
// #region interacting-with-contracts-2
const contract = new Contract(contractId, abi, provider);
const { value } = await contract.functions.increment_count(1).dryRun();
// #endregion interacting-with-contracts-2
expect(value.toNumber()).toBeGreaterThanOrEqual(1);
});
it('should successfully use "simulate" to validate if wallet can pay for transaction', async () => {
const { id: contractId, interface: abi } = counterContract;
const fundedWallet = await getTestWallet([[10_000, baseAssetId]]);
// #region interacting-with-contracts-3
const contract = new Contract(contractId, abi, fundedWallet);
const { value } = await contract.functions.increment_count(10).simulate();
// #endregion interacting-with-contracts-3
expect(value.toNumber()).toBeGreaterThanOrEqual(10);
});
it('should successfully execute a contract call without a wallet', async () => {
const contract = counterContract;
// #region interacting-with-contracts-4
await contract.functions.increment_count(10).call();
// #endregion interacting-with-contracts-4
const { value } = await contract.functions.get_count().get();
expect(value.toNumber()).toBeGreaterThanOrEqual(10);
});
});