git.delta.rocks / unique-network / refs/commits / 7f793b95dbd6

difftreelog

test(eth-nesting) using EVM completely instead of Substrate

Fahrrader2022-06-14parent: #ee82c34.patch.diff
in: master

1 file changed

modifiedtests/src/eth/nesting/nest.test.tsdiffbeforeafterboth
before · tests/src/eth/nesting/nest.test.ts
1import {ApiPromise} from '@polkadot/api';2import {IKeyringPair} from '@polkadot/types/types';3import {expect} from 'chai';4import {collectionIdToAddress, createEthAccountWithBalance, GAS_ARGS, itWeb3, tokenIdToAddress} from '../../eth/util/helpers';5import usingApi, {submitTransactionAsync} from '../../substrate/substrate-api';6import {createCollectionExpectSuccess, setCollectionPermissionsExpectSuccess} from '../../util/helpers';7import nonFungibleAbi from '../nonFungibleAbi.json';89let alice: IKeyringPair;1011const getCollectionFromSubstrate = async (12  api: ApiPromise, 13  ethAddress: string,14): Promise<{ collectionId: number, address: string }> => {15  const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});16  await setCollectionPermissionsExpectSuccess(alice, collectionId, {nesting: 'Owner'});17  await submitTransactionAsync(alice, api.tx.unique.addCollectionAdmin(collectionId, {Ethereum: ethAddress}));18  return {collectionId, address: collectionIdToAddress(collectionId)};19};2021describe('Integration Test: EVM Nesting', () => {22  before(async () => {23    await usingApi(async (_api, privateKeyWrapper) => {24      alice = privateKeyWrapper('//Alice');25    });26  });2728  itWeb3('NFT: allows an Owner to nest/unnest their token', async ({api, web3}) => {29    const owner = await createEthAccountWithBalance(api, web3);30    const {collectionId, address} = await getCollectionFromSubstrate(api, owner);31    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});3233    // Create a token to be nested34    const nftTokenId = await contract.methods.nextTokenId().call();35    await contract.methods.mint(36      owner,37      nftTokenId,38    ).send({from: owner});3940    // Nest into a token41    const firstTargetNftTokenId = await contract.methods.nextTokenId().call();42    await contract.methods.mint(43      owner,44      firstTargetNftTokenId,45    ).send({from: owner});4647    const targetNftTokenAddress = tokenIdToAddress(collectionId, firstTargetNftTokenId);4849    await contract.methods.transfer(targetNftTokenAddress, nftTokenId).send({from: owner});50    expect(await contract.methods.ownerOf(nftTokenId).call()).to.be.equal(targetNftTokenAddress);5152    // Re-nest into another53    const secondTargetNftTokenId = await contract.methods.nextTokenId().call();54    await contract.methods.mint(55      owner,56      secondTargetNftTokenId,57    ).send({from: owner});58    const nextNftTokenAddress = tokenIdToAddress(collectionId, secondTargetNftTokenId);5960    await contract.methods.transfer(nextNftTokenAddress, nftTokenId).send({from: owner});61    expect(await contract.methods.ownerOf(nftTokenId).call()).to.be.equal(nextNftTokenAddress);62  });6364  itWeb3('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({api, web3}) => {65    const owner = await createEthAccountWithBalance(api, web3);6667    const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);68    const {collectionId: collectionIdB, address: addressCollectionB} = await getCollectionFromSubstrate(api, owner);69    await setCollectionPermissionsExpectSuccess(alice, collectionIdA, {nesting: {OwnerRestricted:[collectionIdA, collectionIdB]}});7071    const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});72    const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: owner, ...GAS_ARGS});7374    // Create a token to nest into75    const targetNftTokenId = await contractA.methods.nextTokenId().call();76    await contractA.methods.mint(77      owner,78      targetNftTokenId,79    ).send({from: owner});80    const nftTokenAddressA1 = tokenIdToAddress(collectionIdA, targetNftTokenId);8182    // Create a token for nesting in the same collection as the target83    const nftTokenIdA = await contractA.methods.nextTokenId().call();84    await contractA.methods.mint(85      owner,86      nftTokenIdA,87    ).send({from: owner});8889    // Create a token for nesting in a different collection90    const nftTokenIdB = await contractB.methods.nextTokenId().call();91    await contractB.methods.mint(92      owner,93      nftTokenIdB,94    ).send({from: owner});9596    // Nest97    await contractA.methods.transfer(nftTokenAddressA1, nftTokenIdA).send({from: owner});98    expect(await contractA.methods.ownerOf(nftTokenIdA).call()).to.be.equal(nftTokenAddressA1);99100    await contractB.methods.transfer(nftTokenAddressA1, nftTokenIdB).send({from: owner});101    expect(await contractB.methods.ownerOf(nftTokenIdB).call()).to.be.equal(nftTokenAddressA1);102  });103});104105describe('Negative Test: EVM Nesting', async() => {106  before(async () => {107    await usingApi(async (api, privateKeyWrapper) => {108      alice = privateKeyWrapper('//Alice');109    });110  });111112  itWeb3('NFT: disallows to nest token if nesting is disabled', async ({api, web3}) => {113    const owner = await createEthAccountWithBalance(api, web3);114115    const {collectionId, address} = await getCollectionFromSubstrate(api, owner);116    await setCollectionPermissionsExpectSuccess(alice, collectionId, {nesting: 'Disabled'});117118    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});119120    // Create a token to nest into121    const targetNftTokenId = await contract.methods.nextTokenId().call();122    await contract.methods.mint(123      owner,124      targetNftTokenId,125    ).send({from: owner});126127    const targetNftTokenAddress = tokenIdToAddress(collectionId, targetNftTokenId);128129    // Create a token to nest130    const nftTokenId = await contract.methods.nextTokenId().call();131    await contract.methods.mint(132      owner,133      nftTokenId,134    ).send({from: owner});135136    // Try to nest137    await expect(contract.methods138      .transfer(targetNftTokenAddress, nftTokenId)139      .call()).to.be.rejectedWith('NestingIsDisabled');140  });141  142  itWeb3('NFT: disallows a non-Owner to nest someone else\'s token', async ({api, web3}) => {143    const owner = await createEthAccountWithBalance(api, web3);144    const malignant = await createEthAccountWithBalance(api, web3);145146    const {collectionId, address: collectionAdress} = await getCollectionFromSubstrate(api, owner);147148    const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionAdress, {from: owner, ...GAS_ARGS});149150    // Mint a token151    const targetTokenId = await contract.methods.nextTokenId().call();152    await contract.methods.mint(153      owner,154      targetTokenId,155    ).send({from: owner});156    const targetTokenAddress = tokenIdToAddress(collectionId, targetTokenId);157    158    // Mint a token belonging to a different account159    const tokenId = await contract.methods.nextTokenId().call();160    await contract.methods.mint(161      malignant,162      tokenId,163    ).send({from: owner});164      165    // Try to nest one token in another as a non-owner account166    await expect(contract.methods167      .transfer(targetTokenAddress, tokenId)168      .call({from: malignant})).to.be.rejectedWith('OnlyOwnerAllowedToNest');169  });170  171  itWeb3('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({api, web3}) => {172    const owner = await createEthAccountWithBalance(api, web3);173    const malignant = await createEthAccountWithBalance(api, web3);174175    const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);176    const {collectionId: collectionIdB, address: addressCollectionB} = await getCollectionFromSubstrate(api, owner);177178    const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});179    const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: owner, ...GAS_ARGS});180181    await setCollectionPermissionsExpectSuccess(alice, collectionIdA, {nesting: {OwnerRestricted:[collectionIdA, collectionIdB]}});182183    // Create a token in one collection184    const nftTokenIdA = await contractA.methods.nextTokenId().call();185    await contractA.methods.mint(186      owner,187      nftTokenIdA,188    ).send({from: owner});189    const nftTokenAddressA = tokenIdToAddress(collectionIdA, nftTokenIdA);190191    // Create a token in another collection belonging to someone else192    const nftTokenIdB = await contractB.methods.nextTokenId().call();193    await contractB.methods.mint(194      malignant,195      nftTokenIdB,196    ).send({from: owner});197198    // Try to drag someone else's token into the other collection and nest199    await expect(contractB.methods200      .transfer(nftTokenAddressA, nftTokenIdB)201      .call({from: malignant})).to.be.rejectedWith('OnlyOwnerAllowedToNest');202  });203  204  itWeb3('NFT: disallows to nest token in an unlisted collection', async ({api, web3}) => {205    const owner = await createEthAccountWithBalance(api, web3);206207    const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);208    const {address: addressCollectionB} = await getCollectionFromSubstrate(api, owner);209210    const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});211    const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: owner, ...GAS_ARGS});212213    await setCollectionPermissionsExpectSuccess(alice, collectionIdA, {nesting: {OwnerRestricted:[collectionIdA]}});214215    // Create a token in one collection216    const nftTokenIdA = await contractA.methods.nextTokenId().call();217    await contractA.methods.mint(218      owner,219      nftTokenIdA,220    ).send({from: owner});221    const nftTokenAddressA = tokenIdToAddress(collectionIdA, nftTokenIdA);222223    // Create a token in another collection224    const nftTokenIdB = await contractB.methods.nextTokenId().call();225    await contractB.methods.mint(226      owner,227      nftTokenIdB,228    ).send({from: owner});229230    // Try to nest into a token in the other collection, disallowed in the first231    await expect(contractB.methods232      .transfer(nftTokenAddressA, nftTokenIdB)233      .call()).to.be.rejectedWith('SourceCollectionIsNotAllowedToNest');234  });235});
after · tests/src/eth/nesting/nest.test.ts
1import {ApiPromise} from '@polkadot/api';2import {Contract} from 'web3-eth-contract';3import {expect} from 'chai';4import Web3 from 'web3';5import {createEthAccountWithBalance, evmCollectionHelpers, GAS_ARGS, getCollectionAddressFromResult, itWeb3, tokenIdToAddress} from '../../eth/util/helpers';6import nonFungibleAbi from '../nonFungibleAbi.json';78const createNestingCollection = async (9  api: ApiPromise,10  web3: Web3,11  owner: string,12): Promise<{ collectionId: number, collectionAddress: string, contract: Contract }> => {13  const collectionHelper = evmCollectionHelpers(web3, owner);14        15  const result = await collectionHelper.methods16    .createNonfungibleCollection('A', 'B', 'C')17    .send();18  const {collectionIdAddress: collectionAddress, collectionId} = await getCollectionAddressFromResult(api, result);1920  const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionAddress, {from: owner, ...GAS_ARGS});21  await contract.methods.addCollectionAdmin(owner).send();22  await contract.methods.setCollectionNesting(true).send({from: owner});2324  return {collectionId, collectionAddress, contract};25};2627describe('Integration Test: EVM Nesting', () => {28  itWeb3('NFT: allows an Owner to nest/unnest their token', async ({api, web3, privateKeyWrapper}) => {29    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);30    const {collectionId, contract} = await createNestingCollection(api, web3, owner);3132    // Create a token to be nested33    const nftTokenId = await contract.methods.nextTokenId().call();34    await contract.methods.mint(35      owner,36      nftTokenId,37    ).send({from: owner});3839    // Nest into a token40    const firstTargetNftTokenId = await contract.methods.nextTokenId().call();41    await contract.methods.mint(42      owner,43      firstTargetNftTokenId,44    ).send({from: owner});4546    const targetNftTokenAddress = tokenIdToAddress(collectionId, firstTargetNftTokenId);4748    await contract.methods.transfer(targetNftTokenAddress, nftTokenId).send({from: owner});49    expect(await contract.methods.ownerOf(nftTokenId).call()).to.be.equal(targetNftTokenAddress);5051    // Re-nest into another52    const secondTargetNftTokenId = await contract.methods.nextTokenId().call();53    await contract.methods.mint(54      owner,55      secondTargetNftTokenId,56    ).send({from: owner});57    const nextNftTokenAddress = tokenIdToAddress(collectionId, secondTargetNftTokenId);5859    await contract.methods.transfer(nextNftTokenAddress, nftTokenId).send({from: owner});60    expect(await contract.methods.ownerOf(nftTokenId).call()).to.be.equal(nextNftTokenAddress);61  });6263  itWeb3('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({api, web3, privateKeyWrapper}) => {64    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);6566    const {collectionId: collectionIdA, collectionAddress: collectionAddressA, contract: contractA} = await createNestingCollection(api, web3, owner);67    const {collectionAddress: collectionAddressB, contract: contractB} = await createNestingCollection(api, web3, owner);68    await contractA.methods.setCollectionNesting(true, [collectionAddressA, collectionAddressB]).send({from: owner});6970    // Create a token to nest into71    const targetNftTokenId = await contractA.methods.nextTokenId().call();72    await contractA.methods.mint(73      owner,74      targetNftTokenId,75    ).send({from: owner});76    const nftTokenAddressA1 = tokenIdToAddress(collectionIdA, targetNftTokenId);7778    // Create a token for nesting in the same collection as the target79    const nftTokenIdA = await contractA.methods.nextTokenId().call();80    await contractA.methods.mint(81      owner,82      nftTokenIdA,83    ).send({from: owner});8485    // Create a token for nesting in a different collection86    const nftTokenIdB = await contractB.methods.nextTokenId().call();87    await contractB.methods.mint(88      owner,89      nftTokenIdB,90    ).send({from: owner});9192    // Nest93    await contractA.methods.transfer(nftTokenAddressA1, nftTokenIdA).send({from: owner});94    expect(await contractA.methods.ownerOf(nftTokenIdA).call()).to.be.equal(nftTokenAddressA1);9596    await contractB.methods.transfer(nftTokenAddressA1, nftTokenIdB).send({from: owner});97    expect(await contractB.methods.ownerOf(nftTokenIdB).call()).to.be.equal(nftTokenAddressA1);98  });99});100101describe('Negative Test: EVM Nesting', async() => {102  itWeb3('NFT: disallows to nest token if nesting is disabled', async ({api, web3, privateKeyWrapper}) => {103    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);104105    const {collectionId, contract} = await createNestingCollection(api, web3, owner);106    await contract.methods.setCollectionNesting(false).send({from: owner});107108    // Create a token to nest into109    const targetNftTokenId = await contract.methods.nextTokenId().call();110    await contract.methods.mint(111      owner,112      targetNftTokenId,113    ).send({from: owner});114115    const targetNftTokenAddress = tokenIdToAddress(collectionId, targetNftTokenId);116117    // Create a token to nest118    const nftTokenId = await contract.methods.nextTokenId().call();119    await contract.methods.mint(120      owner,121      nftTokenId,122    ).send({from: owner});123124    // Try to nest125    await expect(contract.methods126      .transfer(targetNftTokenAddress, nftTokenId)127      .call({from: owner})).to.be.rejectedWith('UserIsNotAllowedToNest');128  });129  130  itWeb3('NFT: disallows a non-Owner to nest someone else\'s token', async ({api, web3, privateKeyWrapper}) => {131    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);132    const malignant = await createEthAccountWithBalance(api, web3, privateKeyWrapper);133134    const {collectionId, contract} = await createNestingCollection(api, web3, owner);135136    // Mint a token137    const targetTokenId = await contract.methods.nextTokenId().call();138    await contract.methods.mint(139      owner,140      targetTokenId,141    ).send({from: owner});142    const targetTokenAddress = tokenIdToAddress(collectionId, targetTokenId);143    144    // Mint a token belonging to a different account145    const tokenId = await contract.methods.nextTokenId().call();146    await contract.methods.mint(147      malignant,148      tokenId,149    ).send({from: owner});150      151    // Try to nest one token in another as a non-owner account152    await expect(contract.methods153      .transfer(targetTokenAddress, tokenId)154      .call({from: malignant})).to.be.rejectedWith('UserIsNotAllowedToNest');155  });156  157  itWeb3('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({api, web3, privateKeyWrapper}) => {158    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);159    const malignant = await createEthAccountWithBalance(api, web3, privateKeyWrapper);160161    const {collectionId: collectionIdA, collectionAddress: collectionAddressA, contract: contractA} = await createNestingCollection(api, web3, owner);162    const {collectionAddress: collectionAddressB, contract: contractB} = await createNestingCollection(api, web3, owner);163164    await contractA.methods.setCollectionNesting(true, [collectionAddressA, collectionAddressB]).send({from: owner});165166    // Create a token in one collection167    const nftTokenIdA = await contractA.methods.nextTokenId().call();168    await contractA.methods.mint(169      owner,170      nftTokenIdA,171    ).send({from: owner});172    const nftTokenAddressA = tokenIdToAddress(collectionIdA, nftTokenIdA);173174    // Create a token in another collection belonging to someone else175    const nftTokenIdB = await contractB.methods.nextTokenId().call();176    await contractB.methods.mint(177      malignant,178      nftTokenIdB,179    ).send({from: owner});180181    // Try to drag someone else's token into the other collection and nest182    await expect(contractB.methods183      .transfer(nftTokenAddressA, nftTokenIdB)184      .call({from: malignant})).to.be.rejectedWith('UserIsNotAllowedToNest');185  });186  187  itWeb3('NFT: disallows to nest token in an unlisted collection', async ({api, web3, privateKeyWrapper}) => {188    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);189190    const {collectionId: collectionIdA, collectionAddress: collectionAddressA, contract: contractA} = await createNestingCollection(api, web3, owner);191    const {contract: contractB} = await createNestingCollection(api, web3, owner);192193    await contractA.methods.setCollectionNesting(true, [collectionAddressA]).send({from: owner});194195    // Create a token in one collection196    const nftTokenIdA = await contractA.methods.nextTokenId().call();197    await contractA.methods.mint(198      owner,199      nftTokenIdA,200    ).send({from: owner});201    const nftTokenAddressA = tokenIdToAddress(collectionIdA, nftTokenIdA);202203    // Create a token in another collection204    const nftTokenIdB = await contractB.methods.nextTokenId().call();205    await contractB.methods.mint(206      owner,207      nftTokenIdB,208    ).send({from: owner});209210    // Try to nest into a token in the other collection, disallowed in the first211    await expect(contractB.methods212      .transfer(nftTokenAddressA, nftTokenIdB)213      .call()).to.be.rejectedWith('SourceCollectionIsNotAllowedToNest');214  });215});