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 2829 30 31 32 33 34 35 36 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});