git.delta.rocks / unique-network / refs/commits / 1d1847e4f8dc

difftreelog

Combine transfer and transferFromm tests

Max Andreev2022-12-16parent: #6d28dd8.patch.diff
in: master

1 file changed

modifiedtests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth
208 }208 }
209 });209 });
210 210
211 itEth('Can perform transferFrom()', async ({helper}) => {211 [
212 'transferFrom',
213 'transferFromCross',
214 ].map(testCase =>
215 itEth(`Can perform ${testCase}`, async ({helper}) => {
212 const owner = await helper.eth.createAccountWithBalance(donor);216 const isCross = testCase === 'transferFromCross';
217 const owner = await helper.eth.createAccountWithBalance(donor);
213 const spender = await helper.eth.createAccountWithBalance(donor);218 const ownerCross = helper.ethCrossAccount.fromAddress(owner);
219 const spender = await helper.eth.createAccountWithBalance(donor);
214 const receiver = helper.eth.createAccount();220 const receiverEth = helper.eth.createAccount();
215 const collection = await helper.rft.mintCollection(alice);221 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
222 const [receiverSub] = await helper.arrange.createAccounts([1n], donor);
223 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);
224
225 const collection = await helper.rft.mintCollection(alice);
216 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});226 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});
217227
218 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);228 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);
219 const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);229 const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);
220230
221 await contract.methods.approve(spender, 100).send();231 await contract.methods.approve(spender, 100).send({from: owner});
232
233 // 1. Can transfer from
234 // 1.1 Plain ethereum or cross address:
235 {
236 const result = await contract.methods[testCase](
237 isCross ? ownerCross : owner,
238 isCross ? receiverCrossEth : receiverEth,
239 49,
240 ).send({from: spender});
222241
223 {242 // Check events:
224 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});
225 let event = result.events.Transfer;243 const transferEvent = result.events.Transfer;
226 expect(event.address).to.be.equal(tokenAddress);244 expect(transferEvent.address).to.be.equal(tokenAddress);
227 expect(event.returnValues.from).to.be.equal(owner);245 expect(transferEvent.returnValues.from).to.be.equal(owner);
228 expect(event.returnValues.to).to.be.equal(receiver);246 expect(transferEvent.returnValues.to).to.be.equal(receiverEth);
229 expect(event.returnValues.value).to.be.equal('49');247 expect(transferEvent.returnValues.value).to.be.equal('49');
230248
231 event = result.events.Approval;249 const approvalEvent = result.events.Approval;
232 expect(event.address).to.be.equal(tokenAddress);250 expect(approvalEvent.address).to.be.equal(tokenAddress);
233 expect(event.returnValues.owner).to.be.equal(owner);251 expect(approvalEvent.returnValues.owner).to.be.equal(owner);
234 expect(event.returnValues.spender).to.be.equal(spender);252 expect(approvalEvent.returnValues.spender).to.be.equal(spender);
235 expect(event.returnValues.value).to.be.equal('51');253 expect(approvalEvent.returnValues.value).to.be.equal('51');
236 }
237254
238 {255 // Check balances:
239 const balance = await contract.methods.balanceOf(receiver).call();256 const receiverBalance = await contract.methods.balanceOf(receiverEth).call();
240 expect(+balance).to.equal(49);257 const ownerBalance = await contract.methods.balanceOf(owner).call();
241 }
242258
243 {259 expect(+receiverBalance).to.equal(49);
244 const balance = await contract.methods.balanceOf(owner).call();
245 expect(+balance).to.equal(151);
246 }260 expect(+ownerBalance).to.equal(151);
247 });
248
249 itEth('Can perform transferFromCross()', async ({helper}) => {
250 const owner = await helper.eth.createAccountWithBalance(donor);
251 const ownerCross = helper.ethCrossAccount.fromAddress(owner);261 }
252 const spender = await helper.eth.createAccountWithBalance(donor);
253 const receiver = helper.eth.createAccount();
254 const receiverSub = (await helper.arrange.createAccounts([1n], donor))[0];
255 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiver);
256 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);
257 262
258 const collection = await helper.rft.mintCollection(alice);263 // 1.2 Cross substrate address:
264 if (testCase === 'transferFromCross') {
265 const result = await contract.methods.transferFromCross(ownerCross, receiverCrossSub, 51).send({from: spender});
259 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});266 // Check events:
267 const transferEvent = result.events.Transfer;
268 expect(transferEvent.address).to.be.equal(tokenAddress);
269 expect(transferEvent.returnValues.from).to.be.equal(owner);
270 expect(transferEvent.returnValues.to).to.be.equal(helper.address.substrateToEth(receiverSub.address));
271 expect(transferEvent.returnValues.value).to.be.equal('51');
260272
261 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);273 const approvalEvent = result.events.Approval;
274 expect(approvalEvent.address).to.be.equal(tokenAddress);
275 expect(approvalEvent.returnValues.owner).to.be.equal(owner);
262 const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);276 expect(approvalEvent.returnValues.spender).to.be.equal(spender);
277 expect(approvalEvent.returnValues.value).to.be.equal('0');
263278
264 await contract.methods.approve(spender, 100).send({from: owner});279 // Check balances:
265
266 {280 const receiverBalance = await collection.getTokenBalance(tokenId, {Substrate: receiverSub.address});
267 const result = await contract.methods.transferFromCross(ownerCross, receiverCrossEth, 49).send({from: spender});
268 let event = result.events.Transfer;281 const ownerBalance = await contract.methods.balanceOf(owner).call();
269 expect(event.address).to.be.equal(tokenAddress);
270 expect(event.returnValues.from).to.be.equal(owner);282 expect(receiverBalance).to.equal(51n);
271 expect(event.returnValues.to).to.be.equal(receiver);283 expect(+ownerBalance).to.equal(100);
272 expect(event.returnValues.value).to.be.equal('49');284 }
285 }));
273286
274 event = result.events.Approval;287 [
288 'transfer',
289 'transferCross',
290 ].map(testCase =>
275 expect(event.address).to.be.equal(tokenAddress);291 itEth.only(`Can perform ${testCase}()`, async ({helper}) => {
292 const isCross = testCase === 'transferCross';
276 expect(event.returnValues.owner).to.be.equal(owner);293 const owner = await helper.eth.createAccountWithBalance(donor);
294 const receiverEth = helper.eth.createAccount();
277 expect(event.returnValues.spender).to.be.equal(spender);295 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
296 const [receiverSub] = await helper.arrange.createAccounts([1n], donor);
278 expect(event.returnValues.value).to.be.equal('51');297 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);
298 const collection = await helper.rft.mintCollection(alice);
279 }299 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});
280300
281 {301 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);
282 const balance = await contract.methods.balanceOf(receiver).call();
283 expect(+balance).to.equal(49);302 const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);
284 }
285303
286 {304 // 1. Can transfer to plain ethereum or cross-ethereum account:
305 {
287 const balance = await contract.methods.balanceOf(owner).call();306 const result = await contract.methods[testCase](isCross ? receiverCrossEth : receiverEth, 50).send({from: owner});
307 // Check events
308 const transferEvent = result.events.Transfer;
309 expect(transferEvent.address).to.be.equal(tokenAddress);
310 expect(transferEvent.returnValues.from).to.be.equal(owner);
311 expect(transferEvent.returnValues.to).to.be.equal(receiverEth);
312 expect(transferEvent.returnValues.value).to.be.equal('50');
313 // Check balances:
314 const ownerBalance = await contract.methods.balanceOf(owner).call();
288 expect(+balance).to.equal(151);315 const receiverBalance = await contract.methods.balanceOf(receiverEth).call();
316 expect(+ownerBalance).to.equal(150);
317 expect(+receiverBalance).to.equal(50);
289 }318 }
290 319
291 {320 // 2. Can transfer to cross-substrate account:
321 if(isCross) {
292 const result = await contract.methods.transferFromCross(ownerCross, receiverCrossSub, 51).send({from: spender});322 const result = await contract.methods.transferCross(receiverCrossSub, 50).send({from: owner});
293 let event = result.events.Transfer;323 // Check events:
324 const event = result.events.Transfer;
294 expect(event.address).to.be.equal(tokenAddress);325 expect(event.address).to.be.equal(tokenAddress);
295 expect(event.returnValues.from).to.be.equal(owner);326 expect(event.returnValues.from).to.be.equal(owner);
296 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(receiverSub.address));327 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(receiverSub.address));
297 expect(event.returnValues.value).to.be.equal('51');328 expect(event.returnValues.value).to.be.equal('50');
329 // Check balances:
330 const ownerBalance = await contract.methods.balanceOf(owner).call();
331 const receiverBalance = await collection.getTokenBalance(tokenId, {Substrate: receiverSub.address});
332 expect(+ownerBalance).to.equal(100);
333 expect(receiverBalance).to.equal(50n);
334 }
335 }));
298336
299 event = result.events.Approval;
300 expect(event.address).to.be.equal(tokenAddress);
301 expect(event.returnValues.owner).to.be.equal(owner);
302 expect(event.returnValues.spender).to.be.equal(spender);
303 expect(event.returnValues.value).to.be.equal('0');
304 }
305
306 {
307 const balance = await collection.getTokenBalance(tokenId, {Substrate: receiverSub.address});
308 expect(balance).to.equal(51n);
309 }
310
311 {
312 const balance = await contract.methods.balanceOf(owner).call();
313 expect(+balance).to.equal(100);
314 }
315 });
316
317 itEth('Can perform transfer()', async ({helper}) => {
318 const owner = await helper.eth.createAccountWithBalance(donor);
319 const receiver = helper.eth.createAccount();
320 const collection = await helper.rft.mintCollection(alice);
321 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});
322
323 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);
324 const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);
325
326 {
327 const result = await contract.methods.transfer(receiver, 50).send({from: owner});
328 const event = result.events.Transfer;
329 expect(event.address).to.be.equal(tokenAddress);
330 expect(event.returnValues.from).to.be.equal(owner);
331 expect(event.returnValues.to).to.be.equal(receiver);
332 expect(event.returnValues.value).to.be.equal('50');
333 }
334
335 {
336 const balance = await contract.methods.balanceOf(owner).call();
337 expect(+balance).to.equal(150);
338 }
339
340 {
341 const balance = await contract.methods.balanceOf(receiver).call();
342 expect(+balance).to.equal(50);
343 }
344 });
345
346 [337 [
347 'transfer',338 'transfer',
348 // 'transferCross', // TODO339 'transferCross',
349 ].map(testCase => 340 ].map(testCase =>
350 itEth(`Cannot ${testCase}() non-owned token`, async ({helper}) => {341 itEth(`Cannot ${testCase}() non-owned token`, async ({helper}) => {
342 const isCross = testCase === 'transferCross';
351 const owner = await helper.eth.createAccountWithBalance(donor);343 const owner = await helper.eth.createAccountWithBalance(donor);
352 const receiver = await helper.eth.createAccountWithBalance(donor);344 const ownerCross = helper.ethCrossAccount.fromAddress(owner);
345 const receiverEth = await helper.eth.createAccountWithBalance(donor);
346 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
353 const collection = await helper.rft.mintCollection(alice);347 const collection = await helper.rft.mintCollection(alice);
354 const rftOwner = await collection.mintToken(alice, 10n, {Ethereum: owner});348 const rftOwner = await collection.mintToken(alice, 10n, {Ethereum: owner});
355 const rftReceiver = await collection.mintToken(alice, 10n, {Ethereum: receiver});349 const rftReceiver = await collection.mintToken(alice, 10n, {Ethereum: receiverEth});
356 const tokenIdNonExist = 9999999;350 const tokenIdNonExist = 9999999;
357 351
358 const tokenAddress1 = helper.ethAddress.fromTokenId(collection.collectionId, rftOwner.tokenId);352 const tokenAddress1 = helper.ethAddress.fromTokenId(collection.collectionId, rftOwner.tokenId);
363 const tokenEvmNonExist = helper.ethNativeContract.rftToken(tokenAddressNonExist, owner);357 const tokenEvmNonExist = helper.ethNativeContract.rftToken(tokenAddressNonExist, owner);
364 358
365 // 1. Can transfer zero amount (EIP-20):359 // 1. Can transfer zero amount (EIP-20):
366 await tokenEvmOwner.methods[testCase](receiver, 0).send({from: owner});360 await tokenEvmOwner.methods[testCase](isCross ? receiverCrossEth : receiverEth, 0).send({from: owner});
367 // 2. Cannot transfer non-owned token:361 // 2. Cannot transfer non-owned token:
368 await expect(tokenEvmReceiver.methods[testCase](owner, 0).send({from: owner})).to.be.rejected;362 await expect(tokenEvmReceiver.methods[testCase](isCross ? ownerCross : owner, 0).send({from: owner})).to.be.rejected;
369 await expect(tokenEvmReceiver.methods[testCase](owner, 5).send({from: owner})).to.be.rejected;363 await expect(tokenEvmReceiver.methods[testCase](isCross ? ownerCross : owner, 5).send({from: owner})).to.be.rejected;
370 // 3. Cannot transfer non-existing token:364 // 3. Cannot transfer non-existing token:
371 await expect(tokenEvmNonExist.methods[testCase](owner, 0).send({from: owner})).to.be.rejected;365 await expect(tokenEvmNonExist.methods[testCase](isCross ? ownerCross : owner, 0).send({from: owner})).to.be.rejected;
372 await expect(tokenEvmNonExist.methods[testCase](owner, 5).send({from: owner})).to.be.rejected;366 await expect(tokenEvmNonExist.methods[testCase](isCross ? ownerCross : owner, 5).send({from: owner})).to.be.rejected;
373367
374 // 4. Storage is not corrupted:368 // 4. Storage is not corrupted:
375 expect(await rftOwner.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);369 expect(await rftOwner.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);
376 expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: receiver.toLowerCase()}]);370 expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: receiverEth.toLowerCase()}]);
377 expect(await helper.rft.getTokenTop10Owners(collection.collectionId, tokenIdNonExist)).to.deep.eq([]); // TODO371 expect(await helper.rft.getTokenTop10Owners(collection.collectionId, tokenIdNonExist)).to.deep.eq([]);
378372
379 // 4.1 Tokens can be transferred:373 // 4.1 Tokens can be transferred:
380 await tokenEvmOwner.methods[testCase](receiver, 10).send({from: owner});374 await tokenEvmOwner.methods[testCase](isCross ? receiverCrossEth : receiverEth, 10).send({from: owner});
381 await tokenEvmReceiver.methods[testCase](owner, 10).send({from: receiver});375 await tokenEvmReceiver.methods[testCase](isCross ? ownerCross : owner, 10).send({from: receiverEth});
382 expect(await rftOwner.getTop10Owners()).to.deep.eq([{Ethereum: receiver.toLowerCase()}]);376 expect(await rftOwner.getTop10Owners()).to.deep.eq([{Ethereum: receiverEth.toLowerCase()}]);
383 expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);377 expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);
384 }));378 }));
385379
386 itEth('Can perform transferCross()', async ({helper}) => {
387 const owner = await helper.eth.createAccountWithBalance(donor);
388 const receiver = helper.eth.createAccount();
389 const receiverSub = (await helper.arrange.createAccounts([1n], donor))[0];
390 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiver);
391 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);
392 const collection = await helper.rft.mintCollection(alice);
393 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});
394
395 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);
396 const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);
397
398 {
399 const result = await contract.methods.transferCross(receiverCrossEth, 50).send({from: owner});
400 const event = result.events.Transfer;
401 expect(event.address).to.be.equal(tokenAddress);
402 expect(event.returnValues.from).to.be.equal(owner);
403 expect(event.returnValues.to).to.be.equal(receiver);
404 expect(event.returnValues.value).to.be.equal('50');
405 }
406
407 {
408 const balance = await contract.methods.balanceOf(owner).call();
409 expect(+balance).to.equal(150);
410 }
411
412 {
413 const balance = await contract.methods.balanceOf(receiver).call();
414 expect(+balance).to.equal(50);
415 }
416
417 {
418 const result = await contract.methods.transferCross(receiverCrossSub, 50).send({from: owner});
419 const event = result.events.Transfer;
420 expect(event.address).to.be.equal(tokenAddress);
421 expect(event.returnValues.from).to.be.equal(owner);
422 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(receiverSub.address));
423 expect(event.returnValues.value).to.be.equal('50');
424 }
425
426 {
427 const balance = await contract.methods.balanceOf(owner).call();
428 expect(+balance).to.equal(100);
429 }
430
431 {
432 const balance = await collection.getTokenBalance(tokenId, {Substrate: receiverSub.address});
433 expect(balance).to.equal(50n);
434 }
435 });
436 itEth('Can perform repartition()', async ({helper}) => {380 itEth('Can perform repartition()', async ({helper}) => {
437 const owner = await helper.eth.createAccountWithBalance(donor);381 const owner = await helper.eth.createAccountWithBalance(donor);
438 const receiver = await helper.eth.createAccountWithBalance(donor);382 const receiver = await helper.eth.createAccountWithBalance(donor);