-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration.spec.ts
277 lines (259 loc) · 8.21 KB
/
integration.spec.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import * as dotenv from 'dotenv';
import path from 'path';
import { readFileSync } from 'fs';
import { getEnvVariable } from '../src/utils';
import { deploySC, WalletClient, ISCData } from '@massalabs/massa-sc-deployer';
import {
Args,
fromMAS,
MAX_GAS_DEPLOYMENT,
CHAIN_ID,
IAccount,
DefaultProviderUrls,
Client,
ClientFactory,
EOperationStatus,
ArrayTypes,
} from '@massalabs/massa-web3';
import { fileURLToPath } from 'url';
/* eslint-disable no-console */
// Obtain the current file name and directory paths
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(path.dirname(__filename));
// Load .env file content into process.env
dotenv.config();
// Get environment variables
const secretKey = getEnvVariable('WALLET_SECRET_KEY');
// Define deployment parameters
const chainId = CHAIN_ID.BuildNet; // Choose the chain ID corresponding to the network you want to deploy to
const maxGas = MAX_GAS_DEPLOYMENT; // Gas for deployment Default is the maximum gas allowed for deployment
const fees = 100000000n; // Fees to be paid for deployment. Default is 0
const waitFirstEvent = true;
let deployerAccount: IAccount;
let nft: string;
let onERC1155Received: string;
let onERC1155ReceivedFail: string;
let testnetClient: Client;
async function onERC1155BatchReceivedTestNormal() {
if (!deployerAccount?.address) throw new Error('Invalid deployer account');
console.log('should transfer tokens and call onERC1155Received');
const data = 'transfer data';
const from = deployerAccount.address;
const to = onERC1155Received;
const ids = [100n, 111n];
const values = [1n, 1n];
const operationId = await testnetClient.smartContracts().callSmartContract({
targetAddress: nft,
functionName: 'safeBatchTransferFrom',
parameter: new Args()
.addString(from)
.addString(to)
.addArray(ids, ArrayTypes.U256)
.addArray(values, ArrayTypes.U256)
.addString(data)
.serialize(),
maxGas: BigInt(2100000000),
coins: fromMAS(0.5),
fee: 100000000n,
});
await testnetClient
.smartContracts()
.awaitRequiredOperationStatus(operationId, EOperationStatus.FINAL_SUCCESS);
const events = await testnetClient
.smartContracts()
.getFilteredScOutputEvents({
emitter_address: null,
start: null,
end: null,
original_caller_address: null,
original_operation_id: operationId,
is_final: null,
});
const dataString = data
.split('')
.map((c) => c.charCodeAt(0))
.join(',');
if (
events[events.length - 1].data !==
`ERC1155BatchReceived:${from},${from},${ids.join(';')},${values.join(
';',
)},${dataString}`
) {
throw new Error('Invalid event data');
}
}
async function onERC1155BatchReceivedTestFail() {
if (!deployerAccount?.address) throw new Error('Invalid deployer account');
console.log('should reverts if wrong return value');
const data = 'transfer data';
const from = deployerAccount.address;
const to = onERC1155ReceivedFail;
const ids = [100n, 111n];
const values = [1n, 1n];
const operationId = await testnetClient.smartContracts().callSmartContract({
targetAddress: nft,
functionName: 'safeBatchTransferFrom',
parameter: new Args()
.addString(from)
.addString(to)
.addArray(ids, ArrayTypes.U256)
.addArray(values, ArrayTypes.U256)
.addString(data)
.serialize(),
maxGas: BigInt(2100000000),
coins: fromMAS(0.5),
fee: 100000000n,
});
await testnetClient
.smartContracts()
.awaitRequiredOperationStatus(operationId, EOperationStatus.FINAL_ERROR);
}
async function onERC1155ReceivedTestNormal() {
if (!deployerAccount?.address) throw new Error('Invalid deployer account');
console.log('should transfer tokens and call onERC1155Received');
const data = 'transfer data';
const from = deployerAccount.address;
const to = onERC1155Received;
const id = 100n;
const value = 1n;
const operationId = await testnetClient.smartContracts().callSmartContract({
targetAddress: nft,
functionName: 'safeTransferFrom',
parameter: new Args()
.addString(from)
.addString(to)
.addU256(id)
.addU256(value)
.addString(data)
.serialize(),
maxGas: BigInt(2100000000),
coins: fromMAS(0.5),
fee: 100000000n,
});
await testnetClient
.smartContracts()
.awaitRequiredOperationStatus(operationId, EOperationStatus.FINAL_SUCCESS);
const events = await testnetClient
.smartContracts()
.getFilteredScOutputEvents({
emitter_address: null,
start: null,
end: null,
original_caller_address: null,
original_operation_id: operationId,
is_final: null,
});
const dataString = data
.split('')
.map((c) => c.charCodeAt(0))
.join(',');
if (
events[events.length - 1].data !==
`ERC1155Received:${from},${from},${id},${value},${dataString}`
) {
throw new Error('Invalid event data');
}
}
async function onERC1155ReceivedTestFail() {
if (!deployerAccount?.address) throw new Error('Invalid deployer account');
console.log('should reverts if wrong return value');
const data = 'transfer data';
const from = deployerAccount.address;
const to = onERC1155ReceivedFail;
const id = 100n;
const value = 1n;
const operationId = await testnetClient.smartContracts().callSmartContract({
targetAddress: nft,
functionName: 'safeTransferFrom',
parameter: new Args()
.addString(from)
.addString(to)
.addU256(id)
.addU256(value)
.addString(data)
.serialize(),
maxGas: BigInt(2100000000),
coins: fromMAS(0.5),
fee: 100000000n,
});
await testnetClient
.smartContracts()
.awaitRequiredOperationStatus(operationId, EOperationStatus.FINAL_ERROR);
}
async function beforeAll() {
// Create an account using the private sdkeyc
deployerAccount = await WalletClient.getAccountFromSecretKey(secretKey);
const res1 = await deploySC(
DefaultProviderUrls.BUILDNET, // JSON RPC URL
deployerAccount, // account deploying the smart contract(s)
[
{
data: readFileSync(path.join(__dirname, 'build', 'NFT-exemple.wasm')), // smart contract bytecode
coins: fromMAS(0.1), // coins for deployment
args: new Args()
.addString('ipfs://QmW77ZQQ7Jm9q8WuLbH8YZg2K7T9Qnjbzm7jYVQQrJY5Yd')
.addArray([100n, 111n], ArrayTypes.U256)
.addArray([100n, 100n], ArrayTypes.U256), // arguments for deployment
} as ISCData,
],
chainId,
fees,
maxGas,
waitFirstEvent,
);
const res2 = await deploySC(
DefaultProviderUrls.BUILDNET, // JSON RPC URL
deployerAccount, // account deploying the smart contract(s)
[
{
data: readFileSync(
path.join(__dirname, 'build', 'OnERC1155Received.wasm'),
), // smart contract bytecode
coins: fromMAS(0.1), // coins for deployment
args: new Args(), // arguments for deployment
} as ISCData,
],
chainId,
fees,
maxGas,
waitFirstEvent,
);
const res3 = await deploySC(
DefaultProviderUrls.BUILDNET, // JSON RPC URL
deployerAccount, // account deploying the smart contract(s)
[
{
data: readFileSync(
path.join(__dirname, 'build', 'OnERC1155ReceivedFail.wasm'),
), // smart contract bytecode
coins: fromMAS(0.1), // coins for deployment
args: new Args(), // arguments for deployment
} as ISCData,
],
chainId,
fees,
maxGas,
waitFirstEvent,
);
if (!res2.events || !res1.events || !res3.events)
throw new Error('Invalid events');
nft = res1?.events[res1?.events.length - 1].data.split(': ')[1];
onERC1155Received = res2?.events[res2?.events.length - 1].data.split(': ')[1];
onERC1155ReceivedFail =
res3?.events[res3?.events.length - 1].data.split(': ')[1];
testnetClient = await ClientFactory.createDefaultClient(
DefaultProviderUrls.BUILDNET,
chainId,
true, // retry failed requests
deployerAccount, // optional parameter
);
}
(async () => {
await beforeAll();
console.log('onERC1155Received');
await onERC1155ReceivedTestNormal();
await onERC1155ReceivedTestFail();
console.log('onERC1155BatchReceived');
await onERC1155BatchReceivedTestNormal();
await onERC1155BatchReceivedTestFail();
})();