difftreelog
Merge pull request #744 from UniqueNetwork/tests/eth-helpers
in: master
Tests/eth helpers
5 files changed
tests/src/eth/allowlist.test.tsdiffbeforeafterboth92 });92 });939394 itEth('Collection allowlist can be added and removed by [cross] address', async ({helper}) => {94 itEth('Collection allowlist can be added and removed by [cross] address', async ({helper}) => {95 const owner = await helper.eth.createAccountWithBalance(donor);95 const owner = (await helper.eth.createAccountWithBalance(donor)).toLowerCase();96 const user = donor;96 const [userSub] = await helper.arrange.createAccounts([10n], donor);97 97 const userEth = await helper.eth.createAccountWithBalance(donor);98 98 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');99 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');99 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);100 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);100 const userCross = helper.ethCrossAccount.fromKeyringPair(user);101 const userCrossSub = helper.ethCrossAccount.fromKeyringPair(userSub);101 102 const userCrossEth = helper.ethCrossAccount.fromAddress(userEth);103 const ownerCrossEth = helper.ethCrossAccount.fromAddress(owner);104 105 // Can addToCollectionAllowListCross:102 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;106 expect(await helper.collection.allowed(collectionId, {Substrate: userSub.address})).to.be.false;103 await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});107 await collectionEvm.methods.addToCollectionAllowListCross(userCrossSub).send({from: owner});108 await collectionEvm.methods.addToCollectionAllowListCross(userCrossEth).send({from: owner});109 await collectionEvm.methods.addToCollectionAllowListCross(ownerCrossEth).send({from: owner});104 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.true;110 expect(await helper.collection.allowed(collectionId, {Substrate: userSub.address})).to.be.true;111 expect(await helper.collection.allowed(collectionId, {Ethereum: userEth})).to.be.true;105 expect(await collectionEvm.methods.allowlistedCross(userCross).call({from: owner})).to.be.true;112 expect(await collectionEvm.methods.allowlistedCross(userCrossSub).call({from: owner})).to.be.true;106 113 expect(await collectionEvm.methods.allowlistedCross(userCrossEth).call({from: owner})).to.be.true;114115 await collectionEvm.methods.mint(userEth).send(); // token #1116 await collectionEvm.methods.mint(userEth).send(); // token #2117 await collectionEvm.methods.setCollectionAccess(1).send();118 119 // allowlisted account can transfer and transferCross:120 await collectionEvm.methods.transfer(owner, 1).send({from: userEth});121 await collectionEvm.methods.transferCross(userCrossSub, 2).send({from: userEth});122 expect(await helper.nft.getTokenOwner(collectionId, 1)).to.deep.eq({Ethereum: owner});123 expect(await helper.nft.getTokenOwner(collectionId, 2)).to.deep.eq({Substrate: userSub.address});124 125 // can removeFromCollectionAllowListCross:107 await collectionEvm.methods.removeFromCollectionAllowListCross(userCross).send({from: owner});126 await collectionEvm.methods.removeFromCollectionAllowListCross(userCrossSub).send({from: owner});127 await collectionEvm.methods.removeFromCollectionAllowListCross(userCrossEth).send({from: owner});108 expect(await helper.collection.allowed(collectionId, {Substrate: user.address})).to.be.false;128 expect(await helper.collection.allowed(collectionId, {Substrate: userSub.address})).to.be.false;129 expect(await helper.collection.allowed(collectionId, {Ethereum: userEth})).to.be.false;109 expect(await collectionEvm.methods.allowlistedCross(userCross).call({from: owner})).to.be.false;130 expect(await collectionEvm.methods.allowlistedCross(userCrossSub).call({from: owner})).to.be.false;131 expect(await collectionEvm.methods.allowlistedCross(userCrossEth).call({from: owner})).to.be.false;132133 // cannot transfer anymore134 await collectionEvm.methods.mint(userEth).send();135 await expect(collectionEvm.methods.transfer(owner, 2).send({from: userEth})).to.be.rejectedWith(/Transaction has been reverted/);110 });136 });111137112 // Soft-deprecated138 // Soft-deprecatedtests/src/eth/collectionAdmin.test.tsdiffbeforeafterboth39 });39 });40 });40 });4142 // Soft-deprecated43 itEth('Add admin by owner', async ({helper}) => {44 const owner = await helper.eth.createAccountWithBalance(donor);45 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');46 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);4748 const newAdmin = helper.eth.createAccount();4950 await collectionEvm.methods.addCollectionAdmin(newAdmin).send();51 const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);52 expect(adminList[0].asEthereum.toString().toLocaleLowerCase())53 .to.be.eq(newAdmin.toLocaleLowerCase());54 });554156 itEth('Add cross account admin by owner', async ({helper, privateKey}) => {42 itEth('can add account admin by owner', async ({helper, privateKey}) => {43 // arrange57 const owner = await helper.eth.createAccountWithBalance(donor);44 const owner = await helper.eth.createAccountWithBalance(donor);58 45 const adminSub = await privateKey('//admin2');46 const adminEth = helper.eth.createAccount().toLowerCase();4748 const adminDeprecated = helper.eth.createAccount().toLowerCase();49 const adminCrossSub = helper.ethCrossAccount.fromKeyringPair(adminSub);50 const adminCrossEth = helper.ethCrossAccount.fromAddress(adminEth);51 59 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');52 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');60 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);53 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);61 5455 // Soft-deprecated: can addCollectionAdmin 62 const newAdmin = await privateKey('//Bob');56 await collectionEvm.methods.addCollectionAdmin(adminDeprecated).send();57 // Can addCollectionAdminCross for substrate and ethereum address63 const newAdminCross = helper.ethCrossAccount.fromKeyringPair(newAdmin);58 await collectionEvm.methods.addCollectionAdminCross(adminCrossSub).send();64 await collectionEvm.methods.addCollectionAdminCross(newAdminCross).send();59 await collectionEvm.methods.addCollectionAdminCross(adminCrossEth).send();656061 // 1. Expect api.rpc.unique.adminlist returns admins:66 const adminList = await helper.collection.getAdmins(collectionId);62 const adminListRpc = await helper.collection.getAdmins(collectionId);63 expect(adminListRpc).to.has.length(3);67 expect(adminList).to.be.like([{Substrate: newAdmin.address}]);64 expect(adminListRpc).to.be.deep.contain.members([{Substrate: adminSub.address}, {Ethereum: adminEth}, {Ethereum: adminDeprecated}]);6566 // 2. Expect methods.collectionAdmins == api.rpc.unique.adminlist67 let adminListEth = await collectionEvm.methods.collectionAdmins().call();68 adminListEth = adminListEth.map((element: IEthCrossAccountId) => {69 return helper.address.convertCrossAccountFromEthCrossAccount(element);70 });71 expect(adminListRpc).to.be.like(adminListEth);68 });72 });697370 itEth('Check adminlist', async ({helper, privateKey}) => {74 itEth('cross account admin can mint', async ({helper}) => {75 // arrange: create collection and accounts71 const owner = await helper.eth.createAccountWithBalance(donor);76 const owner = await helper.eth.createAccountWithBalance(donor);72 73 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');77 const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', 'a', 'b', 'uri');74 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);78 const adminEth = (await helper.eth.createAccountWithBalance(donor)).toLowerCase();7576 const admin1 = helper.eth.createAccount();79 const adminCrossEth = helper.ethCrossAccount.fromAddress(adminEth);77 const admin2 = await privateKey('admin');80 const [adminSub] = await helper.arrange.createAccounts([100n], donor);78 const admin2Cross = helper.ethCrossAccount.fromKeyringPair(admin2);81 const adminCrossSub = helper.ethCrossAccount.fromKeyringPair(adminSub);79 82 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);80 // Soft-deprecated83 84 // cannot mint while not admin81 await collectionEvm.methods.addCollectionAdmin(admin1).send();85 await expect(collectionEvm.methods.mint(owner).send({from: adminEth})).to.be.rejected;86 await expect(helper.nft.mintToken(adminSub, {collectionId, owner: {Ethereum: owner}})).to.be.rejectedWith(/common.PublicMintingNotAllowed/);87 88 // admin (sub and eth) can mint token:82 await collectionEvm.methods.addCollectionAdminCross(admin2Cross).send();89 await collectionEvm.methods.addCollectionAdminCross(adminCrossEth).send();8384 const adminListRpc = await helper.collection.getAdmins(collectionId);90 await collectionEvm.methods.addCollectionAdminCross(adminCrossSub).send();85 let adminListEth = await collectionEvm.methods.collectionAdmins().call();91 await collectionEvm.methods.mint(owner).send({from: adminEth});86 adminListEth = adminListEth.map((element: IEthCrossAccountId) => {92 await helper.nft.mintToken(adminSub, {collectionId, owner: {Ethereum: owner}});87 return helper.address.convertCrossAccountFromEthCrossAcoount(element);9388 });89 expect(adminListRpc).to.be.like(adminListEth);94 expect(await helper.collection.getLastTokenId(collectionId)).to.eq(2);90 });95 });919692 // Soft-deprecated93 itEth('Verify owner or admin', async ({helper}) => {97 itEth('cannot add invalid cross account admin', async ({helper}) => {94 const owner = await helper.eth.createAccountWithBalance(donor);98 const owner = await helper.eth.createAccountWithBalance(donor);99 const [admin] = await helper.arrange.createAccounts([100n, 100n], donor);10095 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');101 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');9697 const newAdmin = helper.eth.createAccount();98 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);102 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);99 103100 expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.false;104 const adminCross = {101 await collectionEvm.methods.addCollectionAdmin(newAdmin).send();105 eth: helper.address.substrateToEth(admin.address),106 sub: admin.addressRaw,107 };102 expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.true;108 await expect(collectionEvm.methods.addCollectionAdminCross(adminCross).send()).to.be.rejected;103 });109 });104110105 itEth('Verify owner or admin cross', async ({helper, privateKey}) => {111 itEth('can verify owner with methods.isOwnerOrAdmin[Cross]', async ({helper, privateKey}) => {106 const owner = await helper.eth.createAccountWithBalance(donor);112 const owner = await helper.eth.createAccountWithBalance(donor);107 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');113 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');108114115 const adminDeprecated = helper.eth.createAccount();109 const newAdmin = await privateKey('admin');116 const admin1Cross = helper.ethCrossAccount.fromKeyringPair(await privateKey('admin'));110 const newAdminCross = helper.ethCrossAccount.fromKeyringPair(newAdmin);117 const admin2Cross = helper.ethCrossAccount.fromAddress(helper.address.substrateToEth((await privateKey('admin3')).address));111 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);118 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);112 119 120 // Soft-deprecated:121 expect(await collectionEvm.methods.isOwnerOrAdmin(adminDeprecated).call()).to.be.false;113 expect(await collectionEvm.methods.isOwnerOrAdminCross(newAdminCross).call()).to.be.false;122 expect(await collectionEvm.methods.isOwnerOrAdminCross(admin1Cross).call()).to.be.false;123 expect(await collectionEvm.methods.isOwnerOrAdminCross(admin2Cross).call()).to.be.false;124125 await collectionEvm.methods.addCollectionAdmin(adminDeprecated).send();114 await collectionEvm.methods.addCollectionAdminCross(newAdminCross).send();126 await collectionEvm.methods.addCollectionAdminCross(admin1Cross).send();127 await collectionEvm.methods.addCollectionAdminCross(admin2Cross).send();128129 // Soft-deprecated: isOwnerOrAdmin returns true130 expect(await collectionEvm.methods.isOwnerOrAdmin(adminDeprecated).call()).to.be.true;131 // Expect isOwnerOrAdminCross return true115 expect(await collectionEvm.methods.isOwnerOrAdminCross(newAdminCross).call()).to.be.true;132 expect(await collectionEvm.methods.isOwnerOrAdminCross(admin1Cross).call()).to.be.true;133 expect(await collectionEvm.methods.isOwnerOrAdminCross(admin2Cross).call()).to.be.true;116 });134 });117135118 // Soft-deprecated136 // Soft-deprecated154 const owner = await helper.eth.createAccountWithBalance(donor);172 const owner = await helper.eth.createAccountWithBalance(donor);155 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');173 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');156174157 const [admin] = await helper.arrange.createAccounts([10n], donor);175 const [admin, notAdmin] = await helper.arrange.createAccounts([10n, 10n], donor);158 const adminCross = helper.ethCrossAccount.fromKeyringPair(admin);176 const adminCross = helper.ethCrossAccount.fromKeyringPair(admin);159 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);177 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);160 await collectionEvm.methods.addCollectionAdminCross(adminCross).send();178 await collectionEvm.methods.addCollectionAdminCross(adminCross).send();161179162 const [notAdmin] = await helper.arrange.createAccounts([10n], donor);163 const notAdminCross = helper.ethCrossAccount.fromKeyringPair(notAdmin);180 const notAdminCross = helper.ethCrossAccount.fromKeyringPair(notAdmin);164 await expect(collectionEvm.methods.addCollectionAdminCross(notAdminCross).call({from: adminCross.eth}))181 await expect(collectionEvm.methods.addCollectionAdminCross(notAdminCross).call({from: adminCross.eth}))165 .to.be.rejectedWith('NoPermission');182 .to.be.rejectedWith('NoPermission');tests/src/eth/fungible.test.tsdiffbeforeafterboth314 }314 }315 });315 });316316317 itEth('Can perform transferFromCross()', async ({helper, privateKey}) => {317 itEth('Can perform transferFromCross()', async ({helper}) => {318 const sender = await helper.eth.createAccountWithBalance(donor, 100n);318 const sender = await helper.eth.createAccountWithBalance(donor, 100n);319319320 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);320 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);508 expect(event.returnValues.value).to.be.equal('51');508 expect(event.returnValues.value).to.be.equal('51');509 });509 });510510511 itEth('Events emitted for transferFromCross()', async ({helper, privateKey}) => {511 itEth('Events emitted for transferFromCross()', async ({helper}) => {512 const sender = await helper.eth.createAccountWithBalance(donor, 100n);512 const sender = await helper.eth.createAccountWithBalance(donor, 100n);513513514 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);514 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);tests/src/eth/nonFungible.test.tsdiffbeforeafterboth17import {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util';17import {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util';18import {IKeyringPair} from '@polkadot/types/types';18import {IKeyringPair} from '@polkadot/types/types';19import {Contract} from 'web3-eth-contract';19import {Contract} from 'web3-eth-contract';20import exp from 'constants';2120222123describe('NFT: Information getting', () => {22describe('NFT: Information getting', () => {280 });279 });281280282 itEth('Can perform approveCross()', async ({helper}) => {281 itEth('Can perform approveCross()', async ({helper}) => {282 // arrange: create accounts283 const owner = await helper.eth.createAccountWithBalance(donor, 100n);284 const ownerCross = helper.ethCrossAccount.fromAddress(owner);285 const receiverSub = charlie;286 const recieverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);287 const receiverEth = await helper.eth.createAccountWithBalance(donor, 100n);288 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);289290 // arrange: create collection and tokens:283 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});291 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});284285 const owner = await helper.eth.createAccountWithBalance(donor, 100n);292 const token1 = await collection.mintToken(minter, {Ethereum: owner});286 const receiver = charlie;287288 const token = await collection.mintToken(minter, {Ethereum: owner});293 const token2 = await collection.mintToken(minter, {Ethereum: owner});289294290 const address = helper.ethAddress.fromCollectionId(collection.collectionId);295 const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');291 const contract = helper.ethNativeContract.collection(address, 'nft');296292297 // Can approveCross substrate and ethereum address:293 {294 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);298 const resultSub = await collectionEvm.methods.approveCross(recieverCrossSub, token1.tokenId).send({from: owner});295 const result = await contract.methods.approveCross(recieverCross, token.tokenId).send({from: owner});299 const resultEth = await collectionEvm.methods.approveCross(receiverCrossEth, token2.tokenId).send({from: owner});296 const event = result.events.Approval;300 const eventSub = resultSub.events.Approval;301 const eventEth = resultEth.events.Approval;297 expect(event).to.be.like({302 expect(eventSub).to.be.like({298 address: helper.ethAddress.fromCollectionId(collection.collectionId),303 address: helper.ethAddress.fromCollectionId(collection.collectionId),299 event: 'Approval',304 event: 'Approval',300 returnValues: {305 returnValues: {301 owner,306 owner,302 approved: helper.address.substrateToEth(receiver.address),307 approved: helper.address.substrateToEth(receiverSub.address),303 tokenId: token.tokenId.toString(),308 tokenId: token1.tokenId.toString(),304 },309 },305 });310 });306 }311 expect(eventEth).to.be.like({312 address: helper.ethAddress.fromCollectionId(collection.collectionId),313 event: 'Approval',314 returnValues: {315 owner,316 approved: receiverEth,317 tokenId: token2.tokenId.toString(),318 },319 });320321 // Substrate address can transferFrom approved tokens:322 await helper.nft.transferTokenFrom(receiverSub, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiverSub.address});323 expect(await helper.nft.getTokenOwner(collection.collectionId, token1.tokenId)).to.deep.eq({Substrate: receiverSub.address});324 // Ethereum address can transferFromCross approved tokens:325 await collectionEvm.methods.transferFromCross(ownerCross, receiverCrossEth, token2.tokenId).send({from: receiverEth});326 expect(await helper.nft.getTokenOwner(collection.collectionId, token2.tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()});307 });327 });308328309 itEth('Can perform transferFrom()', async ({helper}) => {329 itEth('Can perform transferFrom()', async ({helper}) => {340 }360 }341 });361 });342362343 itEth('Can perform transferFromCross()', async ({helper, privateKey}) => {363 itEth('Can perform transferFromCross()', async ({helper}) => {344 const minter = await privateKey('//Alice');345 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});364 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});346365347 const owner = await privateKey('//Bob');366 const [owner, receiver] = await helper.arrange.createAccounts([100n, 100n], donor);348 const spender = await helper.eth.createAccountWithBalance(donor);367 const spender = await helper.eth.createAccountWithBalance(donor);349 const receiver = await privateKey('//Charlie');350368351 const token = await collection.mintToken(minter, {Substrate: owner.address});369 const token = await collection.mintToken(minter, {Substrate: owner.address});352370501 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));519 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));502 });520 });503521504 itEth('Can perform transferFromCross()', async ({helper, privateKey}) => {522 itEth('Can perform transferFromCross()', async ({helper}) => {505 const collectionMinter = alice;523 const collectionMinter = alice;506 const owner = bob;524 const owner = bob;507 const receiver = charlie;525 const receiver = charlie;tests/src/util/playgrounds/unique.tsdiffbeforeafterboth2429 * @param ethCrossAccount etherium cross account2429 * @param ethCrossAccount etherium cross account2430 * @returns substrate cross account id2430 * @returns substrate cross account id2431 */2431 */2432 convertCrossAccountFromEthCrossAcoount(ethCrossAccount: IEthCrossAccountId): ICrossAccountId {2432 convertCrossAccountFromEthCrossAccount(ethCrossAccount: IEthCrossAccountId): ICrossAccountId {2433 if (ethCrossAccount.sub === '0') {2433 if (ethCrossAccount.sub === '0') {2434 return {Ethereum: ethCrossAccount.eth.toLocaleLowerCase()};2434 return {Ethereum: ethCrossAccount.eth.toLocaleLowerCase()};2435 }2435 }