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
1import {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';
8
9let alice: IKeyringPair;
107
11const 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.methods
16 .createNonfungibleCollection('A', 'B', 'C')
17 .send();
15 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});18 const {collectionIdAddress: collectionAddress, collectionId} = await getCollectionAddressFromResult(api, result);
19
16 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});
23
18 return {collectionId, address: collectionIdToAddress(collectionId)};24 return {collectionId, collectionAddress, contract};
19};25};
2026
21describe('Integration Test: EVM Nesting', () => {27describe('Integration Test: EVM Nesting', () => {
22 before(async () => {
23 await usingApi(async (_api, privateKeyWrapper) => {
24 alice = privateKeyWrapper('//Alice');
25 });
26 });
27
28 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});
3231
33 // Create a token to be nested32 // Create a token to be nested
34 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 });
6362
64 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);
6665
67 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]}});
70
71 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});
7369
74 // Create a token to nest into70 // Create a token to nest into
75 const targetNftTokenId = await contractA.methods.nextTokenId().call();71 const targetNftTokenId = await contractA.methods.nextTokenId().call();
103});99});
104100
105describe('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 });
111
112 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);
114104
115 const {collectionId, address} = await getCollectionFromSubstrate(api, owner);105 const {collectionId, contract} = await createNestingCollection(api, web3, owner);
116 await setCollectionPermissionsExpectSuccess(alice, collectionId, {nesting: 'Disabled'});
117
118 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});106 await contract.methods.setCollectionNesting(false).send({from: owner});
119107
120 // Create a token to nest into108 // Create a token to nest into
121 const targetNftTokenId = await contract.methods.nextTokenId().call();109 const targetNftTokenId = await contract.methods.nextTokenId().call();
136 // Try to nest124 // Try to nest
137 await expect(contract.methods125 await expect(contract.methods
138 .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);
145133
146 const {collectionId, address: collectionAdress} = await getCollectionFromSubstrate(api, owner);134 const {collectionId, contract} = await createNestingCollection(api, web3, owner);
147
148 const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionAdress, {from: owner, ...GAS_ARGS});
149135
150 // Mint a token136 // Mint a token
151 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 account
166 await expect(contract.methods152 await expect(contract.methods
167 .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);
174160
175 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);
177163
178 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});
180
181 await setCollectionPermissionsExpectSuccess(alice, collectionIdA, {nesting: {OwnerRestricted:[collectionIdA, collectionIdB]}});
182165
183 // Create a token in one collection166 // Create a token in one collection
184 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 nest
199 await expect(contractB.methods182 await expect(contractB.methods
200 .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);
206189
207 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);
209192
210 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});
212
213 await setCollectionPermissionsExpectSuccess(alice, collectionIdA, {nesting: {OwnerRestricted:[collectionIdA]}});
214194
215 // Create a token in one collection195 // Create a token in one collection
216 const nftTokenIdA = await contractA.methods.nextTokenId().call();196 const nftTokenIdA = await contractA.methods.nextTokenId().call();