-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathpredicate-with-configurable.test.ts
159 lines (103 loc) · 4.84 KB
/
predicate-with-configurable.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { generateTestWallet } from '@fuel-ts/wallet/test-utils';
import { readFileSync } from 'fs';
import type { Account, CoinQuantityLike } from 'fuels';
import { getRandomB256, WalletUnlocked, Predicate, BN, NativeAssetId, Provider } from 'fuels';
import { join } from 'path';
import abi from '../test-projects/predicate-with-configurable/out/debug/predicate-with-configurable-abi.json';
const bytecode = readFileSync(
join(
__dirname,
'../test-projects/predicate-with-configurable/out/debug/predicate-with-configurable.bin'
)
);
const defaultValues = {
FEE: 10,
ADDRESS: '0x38966262edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96',
};
let wallet: WalletUnlocked;
const fundPredicate = async (predicate: Predicate<[number]>, amount: number) => {
const tx = await wallet.transfer(predicate.address, amount);
await tx.waitForResult();
};
const assertAccountBalance = async (account: Account, valueToAssert: number) => {
const balance = await account.getBalance(NativeAssetId);
expect(new BN(balance).toNumber()).toEqual(valueToAssert);
};
describe('Predicate With Configurable', () => {
beforeAll(async () => {
const provider = new Provider('http://127.0.0.1:4000/graphql');
const quantities: CoinQuantityLike[] = [
{
amount: 1_000_000,
assetId: NativeAssetId,
},
];
wallet = await generateTestWallet(provider, quantities);
});
it('should assert when input values are set to default configurable constants values', async () => {
const predicate = new Predicate(bytecode, abi, wallet.provider);
const amountToTransfer = 200;
// transfer funds to predicate
await fundPredicate(predicate, 500);
// create destination wallet
const destination = WalletUnlocked.generate();
await assertAccountBalance(destination, 0);
// set predicate input data to be the same as default configurable value
predicate.setData(defaultValues.FEE, defaultValues.ADDRESS);
const tx = await predicate.transfer(destination.address, amountToTransfer);
await tx.waitForResult();
await assertAccountBalance(destination, amountToTransfer);
});
it('should assert when input and configurable values are set equal (FEE)', async () => {
const configurableConstants = { FEE: 35 };
expect(configurableConstants.FEE).not.toEqual(defaultValues.FEE);
const predicate = new Predicate(bytecode, abi, wallet.provider, configurableConstants);
const amountToTransfer = 300;
const destination = WalletUnlocked.generate();
await assertAccountBalance(destination, 0);
// transfer funds to predicate
await fundPredicate(predicate, 500);
predicate.setData(configurableConstants.FEE, defaultValues.ADDRESS);
// executing predicate transfer
const tx = await predicate.transfer(destination.address, amountToTransfer);
await tx.waitForResult();
await assertAccountBalance(destination, amountToTransfer);
});
it('should assert when input and configurable values are set equal (ADDRESS)', async () => {
const configurableConstants = { ADDRESS: getRandomB256() };
expect(configurableConstants.ADDRESS).not.toEqual(defaultValues.ADDRESS);
const predicate = new Predicate(bytecode, abi, wallet.provider, configurableConstants);
const amountToTransfer = 300;
const destination = WalletUnlocked.generate();
await assertAccountBalance(destination, 0);
// transfer funds to predicate
await fundPredicate(predicate, 500);
predicate.setData(defaultValues.FEE, configurableConstants.ADDRESS);
// executing predicate transfer
const tx = await predicate.transfer(destination.address, amountToTransfer);
await tx.waitForResult();
await assertAccountBalance(destination, amountToTransfer);
});
it('should assert when input and configurable values are set equal (BOTH)', async () => {
const configurableConstants = {
FEE: 90,
ADDRESS: getRandomB256(),
};
expect(configurableConstants.FEE).not.toEqual(defaultValues.FEE);
expect(configurableConstants.ADDRESS).not.toEqual(defaultValues.ADDRESS);
const predicate = new Predicate(bytecode, abi, wallet.provider, configurableConstants);
const amountToTransfer = 300;
const destination = WalletUnlocked.generate();
await assertAccountBalance(destination, 0);
await fundPredicate(predicate, 500);
predicate.setData(configurableConstants.FEE, configurableConstants.ADDRESS);
const tx = await predicate.transfer(destination.address, amountToTransfer);
await tx.waitForResult();
await assertAccountBalance(destination, amountToTransfer);
});
it('should throws when no input data is given', async () => {
const predicate = new Predicate(bytecode, abi, wallet.provider);
const destination = WalletUnlocked.generate();
await expect(predicate.transfer(destination.address, 300)).rejects.toThrowError();
});
});