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 3132 33 34 35 36 37 38 39 4041 const {collectionId, address} = await getCollectionFromSubstrate(api, owner);42 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});4344 45 const nftTokenId = await contract.methods.nextTokenId().call();46 await contract.methods.mint(47 owner,48 nftTokenId,49 ).send({from: owner});5051 52 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 64 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 86 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 94 const nftTokenIdA = await contractA.methods.nextTokenId().call();95 await contractA.methods.mint(96 owner,97 nftTokenIdA,98 ).send({from: owner});99100 101 const nftTokenIdB = await contractB.methods.nextTokenId().call();102 await contractB.methods.mint(103 owner,104 nftTokenIdB,105 ).send({from: owner});106107 108 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 132 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 141 const nftTokenId = await contract.methods.nextTokenId().call();142 await contract.methods.mint(143 owner,144 nftTokenId,145 ).send({from: owner});146147 148 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 162 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 170 const tokenId = await contract.methods.nextTokenId().call();171 await contract.methods.mint(172 malignant,173 tokenId,174 ).send({from: owner});175 176 177 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 195 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 203 const nftTokenIdB = await contractB.methods.nextTokenId().call();204 await contractB.methods.mint(205 malignant,206 nftTokenIdB,207 ).send({from: owner});208209 210 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 227 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 235 const nftTokenIdB = await contractB.methods.nextTokenId().call();236 await contractB.methods.mint(237 owner,238 nftTokenIdB,239 ).send({from: owner});240241 242 await expect(contractB.methods243 .transfer(nftTokenAddressA, nftTokenIdB)244 .call()).to.be.rejectedWith('SourceCollectionIsNotAllowedToNest');245 });246});