git.delta.rocks / unique-network / refs/commits / c7b709a1e360

difftreelog

test(eth-nesting) fix and refactor

Fahrrader2022-06-09parent: #5a779df.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, setCollectionLimitsExpectSuccess} from '../../util/helpers';7import nonFungibleAbi from '../nonFungibleAbi.json';89let alice: IKeyringPair;1011const getCollectionFromSubstrate = async (api: ApiPromise, ethAddress: string): Promise<{ collectionId: number, address: string }> => {12  const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});13  await setCollectionLimitsExpectSuccess(alice, collectionId, {nestingRule: 'Owner'});14  await submitTransactionAsync(alice, api.tx.unique.addCollectionAdmin(collectionId, {Ethereum: ethAddress}));15  return {collectionId, address: collectionIdToAddress(collectionId)};16};1718describe('Integration Test: Nesting', () => {19  before(async () => {20    await usingApi(async (api, privateKeyWrapper) => {21      alice = privateKeyWrapper('//Alice');22    });23  });2425  itWeb3('NFT: allows an Owner to nest/unnest their token', async ({api, web3}) => {26    const owner = await createEthAccountWithBalance(api, web3);27    // const receiver = await createEthAccountWithBalance(api, web3);2829    // const helper = evmCollectionHelper(web3, owner);30    // const collectionName = 'CollectionEVM';31    // const description = 'Some description';32    // const tokenPrefix = 'token prefix';33  34    // const collection = await helper.methods35    //   .create721Collection(collectionName, description, tokenPrefix)36    //   .send();3738    const {collectionId, address} = await getCollectionFromSubstrate(api, owner);3940    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});4142    const nftTokenId = await contract.methods.nextTokenId().call();43    await contract.methods.mint(44      owner,45      nftTokenId,46    ).send({from: owner});4748    const firstTargetNftTokenId = await contract.methods.nextTokenId().call();49    await contract.methods.mint(50      owner,51      firstTargetNftTokenId,52    ).send({from: owner});5354    const targetNftTokenAddress = tokenIdToAddress(collectionId, firstTargetNftTokenId);5556    await contract.methods.transfer(targetNftTokenAddress, nftTokenId).send({from: owner});57    expect(await contract.methods.ownerOf(nftTokenId).call()).to.be.equal(targetNftTokenAddress);58    59    const secondTargetNftTokenId = await contract.methods.nextTokenId().call();60    await contract.methods.mint(61      owner,62      secondTargetNftTokenId,63    ).send({from: owner});64    const nextNftTokenAddress = tokenIdToAddress(collectionId, secondTargetNftTokenId);65      66    await contract.methods.transfer(nextNftTokenAddress, nftTokenId).send({from: owner});67    expect(await contract.methods.ownerOf(nftTokenId).call()).to.be.equal(nextNftTokenAddress);68  });6970  itWeb3('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({api, web3}) => {71    const owner = await createEthAccountWithBalance(api, web3);7273    const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);74    const {collectionId: collectionIdB, address: addressCollectionB} = await getCollectionFromSubstrate(api, owner);757677    const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});78    const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: owner, ...GAS_ARGS});7980    const nftTokenIdA1 = await contractA.methods.nextTokenId().call();81    await contractA.methods.mint(82      owner,83      nftTokenIdA1,84    ).send({from: owner});85    const nftTokenAddressA1 = tokenIdToAddress(collectionIdA, nftTokenIdA1);8687    const nftTokenIdA2 = await contractA.methods.nextTokenId().call();88    await contractA.methods.mint(89      owner,90      nftTokenIdA2,91    ).send({from: owner});9293    const nftTokenIdB1 = await contractB.methods.nextTokenId().call();94    await contractB.methods.mint(95      owner,96      nftTokenIdB1,97    ).send({from: owner});9899    await setCollectionLimitsExpectSuccess(alice, collectionIdA, {nestingRule: {OwnerRestricted:[collectionIdA, collectionIdB]}});100101    await contractA.methods.transfer(nftTokenAddressA1, nftTokenIdA2).send({from: owner});102    expect(await contractA.methods.ownerOf(nftTokenIdA2).call()).to.be.equal(nftTokenAddressA1);103104    await contractB.methods.transfer(nftTokenAddressA1, nftTokenIdB1).send({from: owner});105    expect(await contractB.methods.ownerOf(nftTokenIdB1).call()).to.be.equal(nftTokenAddressA1);106  });107});108109describe('Negative Test: Nesting', async() => {110  before(async () => {111    await usingApi(async (api, privateKeyWrapper) => {112      alice = privateKeyWrapper('//Alice');113    });114  });115116  itWeb3('NFT: disallows to nest token if nesting is disabled', async ({api, web3}) => {117    const owner = await createEthAccountWithBalance(api, web3);118119    const {collectionId, address} = await getCollectionFromSubstrate(api, owner);120    await setCollectionLimitsExpectSuccess(alice, collectionId, {nestingRule: 'Disabled'});121122    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});123124    const nftTokenId = await contract.methods.nextTokenId().call();125    await contract.methods.mint(126      owner,127      nftTokenId,128    ).send({from: owner});129130    const targetNftTokenId = await contract.methods.nextTokenId().call();131    await contract.methods.mint(132      owner,133      targetNftTokenId,134    ).send({from: owner});135136    const targetNftTokenAddress = tokenIdToAddress(collectionId, targetNftTokenId);137138    await expect(contract.methods139      .transfer(targetNftTokenAddress, nftTokenId)140      .call()).to.be.rejectedWith('NestingIsDisabled');141  });142  143  itWeb3('NFT: disallows a non-Owner to nest someone else\'s token', async ({api, web3}) => {144    const owner = await createEthAccountWithBalance(api, web3);145    const receiver = await createEthAccountWithBalance(api, web3);146147    const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);148    const {collectionId: collectionIdB, address: addressCollectionB} = await getCollectionFromSubstrate(api, receiver);149150    const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});151    const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: receiver, ...GAS_ARGS});152153    const nftTokenIdA = await contractA.methods.nextTokenId().call();154    await contractA.methods.mint(155      owner,156      nftTokenIdA,157    ).send({from: owner});158    159    const nftTokenIdB = await contractB.methods.nextTokenId().call();160    await contractB.methods.mint(161      receiver,162      nftTokenIdB,163    ).send({from: receiver});164      165    const nftTokenAddressB = tokenIdToAddress(collectionIdB, nftTokenIdB);166167    await expect(contractA.methods168      .transfer(nftTokenAddressB, nftTokenIdA)169      .call()).to.be.rejectedWith('OnlyOwnerAllowedToNest');170  });171  172  itWeb3('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({api, web3}) => {173    const owner = 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 setCollectionLimitsExpectSuccess(alice, collectionIdA, {nestingRule: {OwnerRestricted:[collectionIdA]}});182183    const nftTokenIdA = await contractA.methods.nextTokenId().call();184    await contractA.methods.mint(185      owner,186      nftTokenIdA,187    ).send({from: owner});188    189    const nftTokenIdB = await contractB.methods.nextTokenId().call();190    await contractB.methods.mint(191      owner,192      nftTokenIdB,193    ).send({from: owner});194      195    const nftTokenAddressA = tokenIdToAddress(collectionIdA, nftTokenIdA);196197    await expect(contractB.methods198      .transfer(nftTokenAddressA, nftTokenIdB)199      .call()).to.be.rejectedWith('SourceCollectionIsNotAllowedToNest');200  });201  202  itWeb3('NFT: disallows to nest token in an unlisted collection', async ({api, web3}) => {203    const owner = await createEthAccountWithBalance(api, web3);204205    const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);206207    const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});208209    await setCollectionLimitsExpectSuccess(alice, collectionIdA, {nestingRule: {OwnerRestricted:[]}});210211    const nftTokenIdA = await contractA.methods.nextTokenId().call();212    await contractA.methods.mint(213      owner,214      nftTokenIdA,215    ).send({from: owner});216    217    const nftTokenAddressA = tokenIdToAddress(collectionIdA, nftTokenIdA);218219    await expect(contractA.methods220      .transfer(nftTokenAddressA, nftTokenIdA)221      .call()).to.be.rejectedWith('SourceCollectionIsNotAllowedToNest');222  });223});
after · 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 receiver = await createEthAccountWithBalance(api, web3);3132    // const helper = evmCollectionHelper(web3, owner);33    // const collectionName = 'CollectionEVM';34    // const description = 'Some description';35    // const tokenPrefix = 'token prefix';36  37    // const collection = await helper.methods38    //   .create721Collection(collectionName, description, tokenPrefix)39    //   .send();4041    const {collectionId, address} = await getCollectionFromSubstrate(api, owner);42    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});4344    // Create a token to be nested45    const nftTokenId = await contract.methods.nextTokenId().call();46    await contract.methods.mint(47      owner,48      nftTokenId,49    ).send({from: owner});5051    // Nest into a token52    const firstTargetNftTokenId = await contract.methods.nextTokenId().call();53    await contract.methods.mint(54      owner,55      firstTargetNftTokenId,56    ).send({from: owner});5758    const targetNftTokenAddress = tokenIdToAddress(collectionId, firstTargetNftTokenId);5960    await contract.methods.transfer(targetNftTokenAddress, nftTokenId).send({from: owner});61    expect(await contract.methods.ownerOf(nftTokenId).call()).to.be.equal(targetNftTokenAddress);6263    // Re-nest into another64    const secondTargetNftTokenId = await contract.methods.nextTokenId().call();65    await contract.methods.mint(66      owner,67      secondTargetNftTokenId,68    ).send({from: owner});69    const nextNftTokenAddress = tokenIdToAddress(collectionId, secondTargetNftTokenId);7071    await contract.methods.transfer(nextNftTokenAddress, nftTokenId).send({from: owner});72    expect(await contract.methods.ownerOf(nftTokenId).call()).to.be.equal(nextNftTokenAddress);73  });7475  itWeb3('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({api, web3}) => {76    const owner = await createEthAccountWithBalance(api, web3);7778    const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);79    const {collectionId: collectionIdB, address: addressCollectionB} = await getCollectionFromSubstrate(api, owner);80    await setCollectionPermissionsExpectSuccess(alice, collectionIdA, {nesting: {OwnerRestricted:[collectionIdA, collectionIdB]}});8182    const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});83    const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: owner, ...GAS_ARGS});8485    // Create a token to nest into86    const targetNftTokenId = await contractA.methods.nextTokenId().call();87    await contractA.methods.mint(88      owner,89      targetNftTokenId,90    ).send({from: owner});91    const nftTokenAddressA1 = tokenIdToAddress(collectionIdA, targetNftTokenId);9293    // Create a token for nesting in the same collection as the target94    const nftTokenIdA = await contractA.methods.nextTokenId().call();95    await contractA.methods.mint(96      owner,97      nftTokenIdA,98    ).send({from: owner});99100    // Create a token for nesting in a different collection101    const nftTokenIdB = await contractB.methods.nextTokenId().call();102    await contractB.methods.mint(103      owner,104      nftTokenIdB,105    ).send({from: owner});106107    // Nest108    await contractA.methods.transfer(nftTokenAddressA1, nftTokenIdA).send({from: owner});109    expect(await contractA.methods.ownerOf(nftTokenIdA).call()).to.be.equal(nftTokenAddressA1);110111    await contractB.methods.transfer(nftTokenAddressA1, nftTokenIdB).send({from: owner});112    expect(await contractB.methods.ownerOf(nftTokenIdB).call()).to.be.equal(nftTokenAddressA1);113  });114});115116describe('Negative Test: EVM Nesting', async() => {117  before(async () => {118    await usingApi(async (api, privateKeyWrapper) => {119      alice = privateKeyWrapper('//Alice');120    });121  });122123  itWeb3('NFT: disallows to nest token if nesting is disabled', async ({api, web3}) => {124    const owner = await createEthAccountWithBalance(api, web3);125126    const {collectionId, address} = await getCollectionFromSubstrate(api, owner);127    await setCollectionPermissionsExpectSuccess(alice, collectionId, {nesting: 'Disabled'});128129    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});130131    // Create a token to nest into132    const targetNftTokenId = await contract.methods.nextTokenId().call();133    await contract.methods.mint(134      owner,135      targetNftTokenId,136    ).send({from: owner});137138    const targetNftTokenAddress = tokenIdToAddress(collectionId, targetNftTokenId);139140    // Create a token to nest141    const nftTokenId = await contract.methods.nextTokenId().call();142    await contract.methods.mint(143      owner,144      nftTokenId,145    ).send({from: owner});146147    // Try to nest148    await expect(contract.methods149      .transfer(targetNftTokenAddress, nftTokenId)150      .call()).to.be.rejectedWith('NestingIsDisabled');151  });152  153  itWeb3('NFT: disallows a non-Owner to nest someone else\'s token', async ({api, web3}) => {154    const owner = await createEthAccountWithBalance(api, web3);155    const malignant = await createEthAccountWithBalance(api, web3);156157    const {collectionId, address: collectionAdress} = await getCollectionFromSubstrate(api, owner);158159    const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionAdress, {from: owner, ...GAS_ARGS});160161    // Mint a token162    const targetTokenId = await contract.methods.nextTokenId().call();163    await contract.methods.mint(164      owner,165      targetTokenId,166    ).send({from: owner});167    const targetTokenAddress = tokenIdToAddress(collectionId, targetTokenId);168    169    // Mint a token belonging to a different account170    const tokenId = await contract.methods.nextTokenId().call();171    await contract.methods.mint(172      malignant,173      tokenId,174    ).send({from: owner});175      176    // Try to nest one token in another as a non-owner account177    await expect(contract.methods178      .transfer(targetTokenAddress, tokenId)179      .call({from: malignant})).to.be.rejectedWith('OnlyOwnerAllowedToNest');180  });181  182  itWeb3('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({api, web3}) => {183    const owner = await createEthAccountWithBalance(api, web3);184    const malignant = await createEthAccountWithBalance(api, web3);185186    const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);187    const {collectionId: collectionIdB, address: addressCollectionB} = await getCollectionFromSubstrate(api, owner);188189    const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});190    const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: owner, ...GAS_ARGS});191192    await setCollectionPermissionsExpectSuccess(alice, collectionIdA, {nesting: {OwnerRestricted:[collectionIdA, collectionIdB]}});193194    // Create a token in one collection195    const nftTokenIdA = await contractA.methods.nextTokenId().call();196    await contractA.methods.mint(197      owner,198      nftTokenIdA,199    ).send({from: owner});200    const nftTokenAddressA = tokenIdToAddress(collectionIdA, nftTokenIdA);201202    // Create a token in another collection belonging to someone else203    const nftTokenIdB = await contractB.methods.nextTokenId().call();204    await contractB.methods.mint(205      malignant,206      nftTokenIdB,207    ).send({from: owner});208209    // Try to drag someone else's token into the other collection and nest210    await expect(contractB.methods211      .transfer(nftTokenAddressA, nftTokenIdB)212      .call({from: malignant})).to.be.rejectedWith('OnlyOwnerAllowedToNest');213  });214  215  itWeb3('NFT: disallows to nest token in an unlisted collection', async ({api, web3}) => {216    const owner = await createEthAccountWithBalance(api, web3);217218    const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);219    const {address: addressCollectionB} = await getCollectionFromSubstrate(api, owner);220221    const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});222    const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: owner, ...GAS_ARGS});223224    await setCollectionPermissionsExpectSuccess(alice, collectionIdA, {nesting: {OwnerRestricted:[collectionIdA]}});225226    // Create a token in one collection227    const nftTokenIdA = await contractA.methods.nextTokenId().call();228    await contractA.methods.mint(229      owner,230      nftTokenIdA,231    ).send({from: owner});232    const nftTokenAddressA = tokenIdToAddress(collectionIdA, nftTokenIdA);233234    // Create a token in another collection235    const nftTokenIdB = await contractB.methods.nextTokenId().call();236    await contractB.methods.mint(237      owner,238      nftTokenIdB,239    ).send({from: owner});240241    // Try to nest into a token in the other collection, disallowed in the first242    await expect(contractB.methods243      .transfer(nftTokenAddressA, nftTokenIdB)244      .call()).to.be.rejectedWith('SourceCollectionIsNotAllowedToNest');245  });246});