difftreelog
test(eth-nesting) using EVM completely instead of Substrate
in: master
1 file changed
tests/src/eth/nesting/nest.test.tsdiffbeforeafterboth1import {ApiPromise} from '@polkadot/api';1import {ApiPromise} from '@polkadot/api';2import {IKeyringPair} from '@polkadot/types/types';2import {Contract} from 'web3-eth-contract';3import {expect} from 'chai';3import {expect} from 'chai';4import Web3 from 'web3';4import {collectionIdToAddress, createEthAccountWithBalance, GAS_ARGS, itWeb3, tokenIdToAddress} from '../../eth/util/helpers';5import {createEthAccountWithBalance, evmCollectionHelpers, GAS_ARGS, getCollectionAddressFromResult, itWeb3, tokenIdToAddress} from '../../eth/util/helpers';5import usingApi, {submitTransactionAsync} from '../../substrate/substrate-api';6import {createCollectionExpectSuccess, setCollectionPermissionsExpectSuccess} from '../../util/helpers';7import nonFungibleAbi from '../nonFungibleAbi.json';6import nonFungibleAbi from '../nonFungibleAbi.json';89let alice: IKeyringPair;10711const getCollectionFromSubstrate = async (8const createNestingCollection = async (12 api: ApiPromise, 9 api: ApiPromise,10 web3: Web3,13 ethAddress: string,11 owner: string,14): Promise<{ collectionId: number, address: 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();15 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});18 const {collectionIdAddress: collectionAddress, collectionId} = await getCollectionAddressFromResult(api, result);1916 await setCollectionPermissionsExpectSuccess(alice, collectionId, {nesting: 'Owner'});20 const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionAddress, {from: owner, ...GAS_ARGS});17 await submitTransactionAsync(alice, api.tx.unique.addCollectionAdmin(collectionId, {Ethereum: ethAddress}));21 await contract.methods.addCollectionAdmin(owner).send();22 await contract.methods.setCollectionNesting(true).send({from: owner});2318 return {collectionId, address: collectionIdToAddress(collectionId)};24 return {collectionId, collectionAddress, contract};19};25};202621describe('Integration Test: EVM Nesting', () => {27describe('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}) => {28 itWeb3('NFT: allows an Owner to nest/unnest their token', async ({api, web3, privateKeyWrapper}) => {29 const owner = await createEthAccountWithBalance(api, web3);29 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);30 const {collectionId, address} = await getCollectionFromSubstrate(api, owner);30 const {collectionId, contract} = await createNestingCollection(api, web3, owner);31 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});323133 // Create a token to be nested32 // Create a token to be nested34 const nftTokenId = await contract.methods.nextTokenId().call();33 const nftTokenId = await contract.methods.nextTokenId().call();61 expect(await contract.methods.ownerOf(nftTokenId).call()).to.be.equal(nextNftTokenAddress);60 expect(await contract.methods.ownerOf(nftTokenId).call()).to.be.equal(nextNftTokenAddress);62 });61 });636264 itWeb3('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({api, web3}) => {63 itWeb3('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({api, web3, privateKeyWrapper}) => {65 const owner = await createEthAccountWithBalance(api, web3);64 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);666567 const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);66 const {collectionId: collectionIdA, collectionAddress: collectionAddressA, contract: contractA} = await createNestingCollection(api, web3, owner);68 const {collectionId: collectionIdB, address: addressCollectionB} = await getCollectionFromSubstrate(api, owner);67 const {collectionAddress: collectionAddressB, contract: contractB} = await createNestingCollection(api, web3, 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});68 await contractA.methods.setCollectionNesting(true, [collectionAddressA, collectionAddressB]).send({from: owner});72 const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: owner, ...GAS_ARGS});736974 // Create a token to nest into70 // Create a token to nest into75 const targetNftTokenId = await contractA.methods.nextTokenId().call();71 const targetNftTokenId = await contractA.methods.nextTokenId().call();103});99});104100105describe('Negative Test: EVM Nesting', async() => {101describe('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}) => {102 itWeb3('NFT: disallows to nest token if nesting is disabled', async ({api, web3, privateKeyWrapper}) => {113 const owner = await createEthAccountWithBalance(api, web3);103 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);114104115 const {collectionId, address} = await getCollectionFromSubstrate(api, owner);105 const {collectionId, contract} = await createNestingCollection(api, web3, owner);116 await setCollectionPermissionsExpectSuccess(alice, collectionId, {nesting: 'Disabled'});117118 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});106 await contract.methods.setCollectionNesting(false).send({from: owner});119107120 // Create a token to nest into108 // Create a token to nest into121 const targetNftTokenId = await contract.methods.nextTokenId().call();109 const targetNftTokenId = await contract.methods.nextTokenId().call();136 // Try to nest124 // Try to nest137 await expect(contract.methods125 await expect(contract.methods138 .transfer(targetNftTokenAddress, nftTokenId)126 .transfer(targetNftTokenAddress, nftTokenId)139 .call()).to.be.rejectedWith('NestingIsDisabled');127 .call({from: owner})).to.be.rejectedWith('UserIsNotAllowedToNest');140 });128 });141 129 142 itWeb3('NFT: disallows a non-Owner to nest someone else\'s token', async ({api, web3}) => {130 itWeb3('NFT: disallows a non-Owner to nest someone else\'s token', async ({api, web3, privateKeyWrapper}) => {143 const owner = await createEthAccountWithBalance(api, web3);131 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);144 const malignant = await createEthAccountWithBalance(api, web3);132 const malignant = await createEthAccountWithBalance(api, web3, privateKeyWrapper);145133146 const {collectionId, address: collectionAdress} = await getCollectionFromSubstrate(api, owner);134 const {collectionId, contract} = await createNestingCollection(api, web3, owner);147148 const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionAdress, {from: owner, ...GAS_ARGS});149135150 // Mint a token136 // Mint a token151 const targetTokenId = await contract.methods.nextTokenId().call();137 const targetTokenId = await contract.methods.nextTokenId().call();165 // Try to nest one token in another as a non-owner account151 // Try to nest one token in another as a non-owner account166 await expect(contract.methods152 await expect(contract.methods167 .transfer(targetTokenAddress, tokenId)153 .transfer(targetTokenAddress, tokenId)168 .call({from: malignant})).to.be.rejectedWith('OnlyOwnerAllowedToNest');154 .call({from: malignant})).to.be.rejectedWith('UserIsNotAllowedToNest');169 });155 });170 156 171 itWeb3('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({api, web3}) => {157 itWeb3('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({api, web3, privateKeyWrapper}) => {172 const owner = await createEthAccountWithBalance(api, web3);158 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);173 const malignant = await createEthAccountWithBalance(api, web3);159 const malignant = await createEthAccountWithBalance(api, web3, privateKeyWrapper);174160175 const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);161 const {collectionId: collectionIdA, collectionAddress: collectionAddressA, contract: contractA} = await createNestingCollection(api, web3, owner);176 const {collectionId: collectionIdB, address: addressCollectionB} = await getCollectionFromSubstrate(api, owner);162 const {collectionAddress: collectionAddressB, contract: contractB} = await createNestingCollection(api, web3, owner);177163178 const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});164 await contractA.methods.setCollectionNesting(true, [collectionAddressA, collectionAddressB]).send({from: owner});179 const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: owner, ...GAS_ARGS});180181 await setCollectionPermissionsExpectSuccess(alice, collectionIdA, {nesting: {OwnerRestricted:[collectionIdA, collectionIdB]}});182165183 // Create a token in one collection166 // Create a token in one collection184 const nftTokenIdA = await contractA.methods.nextTokenId().call();167 const nftTokenIdA = await contractA.methods.nextTokenId().call();198 // Try to drag someone else's token into the other collection and nest181 // Try to drag someone else's token into the other collection and nest199 await expect(contractB.methods182 await expect(contractB.methods200 .transfer(nftTokenAddressA, nftTokenIdB)183 .transfer(nftTokenAddressA, nftTokenIdB)201 .call({from: malignant})).to.be.rejectedWith('OnlyOwnerAllowedToNest');184 .call({from: malignant})).to.be.rejectedWith('UserIsNotAllowedToNest');202 });185 });203 186 204 itWeb3('NFT: disallows to nest token in an unlisted collection', async ({api, web3}) => {187 itWeb3('NFT: disallows to nest token in an unlisted collection', async ({api, web3, privateKeyWrapper}) => {205 const owner = await createEthAccountWithBalance(api, web3);188 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);206189207 const {collectionId: collectionIdA, address: addressCollectionA} = await getCollectionFromSubstrate(api, owner);190 const {collectionId: collectionIdA, collectionAddress: collectionAddressA, contract: contractA} = await createNestingCollection(api, web3, owner);208 const {address: addressCollectionB} = await getCollectionFromSubstrate(api, owner);191 const {contract: contractB} = await createNestingCollection(api, web3, owner);209192210 const contractA = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionA, {from: owner, ...GAS_ARGS});193 await contractA.methods.setCollectionNesting(true, [collectionAddressA]).send({from: owner});211 const contractB = new web3.eth.Contract(nonFungibleAbi as any, addressCollectionB, {from: owner, ...GAS_ARGS});212213 await setCollectionPermissionsExpectSuccess(alice, collectionIdA, {nesting: {OwnerRestricted:[collectionIdA]}});214194215 // Create a token in one collection195 // Create a token in one collection216 const nftTokenIdA = await contractA.methods.nextTokenId().call();196 const nftTokenIdA = await contractA.methods.nextTokenId().call();