/
send-and-spend-funds-from-predicates.test.ts
236 lines (186 loc) · 6.81 KB
/
send-and-spend-funds-from-predicates.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { seedTestWallet } from '@fuel-ts/account/test-utils';
import { safeExec } from '@fuel-ts/errors/test-utils';
import type { Provider } from 'fuels';
import { WalletUnlocked, Predicate, getRandomB256 } from 'fuels';
import {
DocSnippetProjectsEnum,
getDocsSnippetsForcProject,
} from '../../../test/fixtures/forc-projects';
import { getTestWallet } from '../../utils';
/**
* @group node
*/
describe(__filename, () => {
let walletWithFunds: WalletUnlocked;
let provider: Provider;
let baseAssetId: string;
const { abiContents: abi, binHexlified: bin } = getDocsSnippetsForcProject(
DocSnippetProjectsEnum.SIMPLE_PREDICATE
);
beforeAll(async () => {
walletWithFunds = await getTestWallet();
provider = walletWithFunds.provider;
const inputAddress = '0xfc05c23a8f7f66222377170ddcbfea9c543dff0dd2d2ba4d0478a4521423a9d4';
const predicate = new Predicate({
bytecode: bin,
provider,
abi,
inputData: [inputAddress],
});
baseAssetId = provider.getBaseAssetId();
await seedTestWallet(predicate, [[100_000, baseAssetId]]);
});
it('should successfully use predicate to spend assets', async () => {
// #region send-and-spend-funds-from-predicates-2
const inputAddress = '0xfc05c23a8f7f66222377170ddcbfea9c543dff0dd2d2ba4d0478a4521423a9d4';
const predicate = new Predicate({
bytecode: bin,
provider,
abi,
inputData: [inputAddress],
});
// #endregion send-and-spend-funds-from-predicates-2
// #region send-and-spend-funds-from-predicates-3
const amountToPredicate = 1000;
const amountToReceiver = 200;
const tx = await walletWithFunds.transfer(predicate.address, amountToPredicate, baseAssetId, {
gasLimit: 1000,
});
let { isStatusSuccess } = await tx.waitForResult();
expect(isStatusSuccess).toBeTruthy();
// #endregion send-and-spend-funds-from-predicates-3
// #region send-and-spend-funds-from-predicates-5
const receiverWallet = WalletUnlocked.generate({
provider,
});
const receiverInitialBalance = await receiverWallet.getBalance();
const tx2 = await predicate.transfer(
receiverWallet.address.toB256(),
amountToReceiver,
baseAssetId
);
({ isStatusSuccess } = await tx2.waitForResult());
expect(isStatusSuccess).toBeTruthy();
const receiverFinalBalance = await receiverWallet.getBalance();
expect(receiverFinalBalance.gt(receiverInitialBalance)).toBeTruthy();
({ isStatusSuccess } = await tx2.waitForResult());
expect(isStatusSuccess).toBeTruthy();
// #endregion send-and-spend-funds-from-predicates-5
});
it('should fail when trying to spend predicates entire amount', async () => {
const predicate = new Predicate({
bytecode: bin,
provider,
abi,
inputData: ['0xfc05c23a8f7f66222377170ddcbfea9c543dff0dd2d2ba4d0478a4521423a9d4'],
});
const amountToPredicate = 100;
const tx = await walletWithFunds.transfer(predicate.address, amountToPredicate, baseAssetId, {
gasLimit: 1_000,
});
await tx.waitForResult();
const receiverWallet = WalletUnlocked.generate({
provider,
});
const { error } = await safeExec(async () =>
predicate.transfer(receiverWallet.address, await predicate.getBalance(), baseAssetId)
);
// #region send-and-spend-funds-from-predicates-6
const errorMsg = 'not enough coins to fit the target';
// #endregion send-and-spend-funds-from-predicates-6
expect((<Error>error).message).toMatch(errorMsg);
});
it('should fail when set wrong input data for predicate', async () => {
const predicateOwner = WalletUnlocked.generate({
provider,
});
const predicate = new Predicate({
bytecode: bin,
abi,
provider: predicateOwner.provider,
inputData: [getRandomB256()],
});
const amountToPredicate = 10000;
const tx = await walletWithFunds.transfer(predicate.address, amountToPredicate, baseAssetId, {
gasLimit: 1000,
});
await tx.waitForResult();
const receiverWallet = WalletUnlocked.generate({
provider,
});
const amountToWallet = 150;
const { error } = await safeExec(() =>
predicate.transfer(receiverWallet.address, amountToWallet, baseAssetId)
);
// #region send-and-spend-funds-from-predicates-7
const errorMsg = 'PredicateVerificationFailed';
// #endregion send-and-spend-funds-from-predicates-7
expect((<Error>error).message).toMatch(errorMsg);
});
it('should ensure predicate createTransfer works as expected', async () => {
const inputAddress = '0xfc05c23a8f7f66222377170ddcbfea9c543dff0dd2d2ba4d0478a4521423a9d4';
const predicate = new Predicate({
bytecode: bin,
abi,
provider,
inputData: [inputAddress],
});
const amountToPredicate = 10_000;
const tx = await walletWithFunds.transfer(predicate.address, amountToPredicate, baseAssetId, {
gasLimit: 1_000,
});
await tx.waitForResult();
const receiverWallet = WalletUnlocked.generate({
provider,
});
const amountToReceiver = 200;
// #region send-and-spend-funds-from-predicates-8
const transactionRequest = await predicate.createTransfer(
receiverWallet.address,
amountToReceiver,
baseAssetId,
{
gasLimit: 1000,
}
);
const chainId = provider.getChainId();
const txId = transactionRequest.getTransactionId(chainId);
const res = await predicate.sendTransaction(transactionRequest);
await res.waitForResult();
// #endregion send-and-spend-funds-from-predicates-8
const txIdFromExecutedTx = res.id;
expect(txId).toEqual(txIdFromExecutedTx);
});
it('should be able to pre-stage a transaction, get TX ID, and then send the transaction', async () => {
const inputAddress = '0xfc05c23a8f7f66222377170ddcbfea9c543dff0dd2d2ba4d0478a4521423a9d4';
const predicate = new Predicate({
bytecode: bin,
abi,
provider,
inputData: [inputAddress],
});
const amountToPredicate = 10_000;
const tx = await walletWithFunds.transfer(predicate.address, amountToPredicate, baseAssetId, {
gasLimit: 1_000,
});
await tx.waitForResult();
const receiverWallet = WalletUnlocked.generate({
provider,
});
// #region predicates-prestage-transaction
// Prepare the transaction
const preparedTx = await predicate.createTransfer(
receiverWallet.address,
amountToPredicate,
baseAssetId
);
// Get the transaction ID before sending the transaction
const txId = preparedTx.getTransactionId(provider.getChainId());
// Send the transaction
const res = await predicate.sendTransaction(preparedTx);
await res.waitForResult();
// #endregion predicates-prestage-transaction
const txIdFromExecutedTx = res.id;
expect(txId).toEqual(txIdFromExecutedTx);
});
});