git.delta.rocks / unique-network / refs/commits / 22fb1ca81266

difftreelog

refactor use playgtounds in collectionSponsoring eth test

Daniel Shiposha2022-09-30parent: #8afb146.patch.diff
in: master

1 file changed

modifiedtests/src/eth/collectionSponsoring.test.tsdiffbeforeafterboth
1import {addToAllowListExpectSuccess, confirmSponsorshipExpectSuccess, createCollectionExpectSuccess, enablePublicMintingExpectSuccess, getDetailedCollectionInfo, setCollectionSponsorExpectSuccess, UNIQUE} from '../util/helpers';1import {collectionIdToAddress, normalizeEvents} from './util/helpers';
2import {itWeb3, createEthAccount, collectionIdToAddress, GAS_ARGS, normalizeEvents, createEthAccountWithBalance, evmCollectionHelpers, getCollectionAddressFromResult, evmCollection, ethBalanceViaSub} from './util/helpers';
3import nonFungibleAbi from './nonFungibleAbi.json';
4import {expect} from 'chai';
5import {evmToAddress} from '@polkadot/util-crypto';2import {evmToAddress} from '@polkadot/util-crypto';
3import {itEth, expect} from '../eth/util/playgrounds';
64
7describe('evm collection sponsoring', () => {5describe('evm collection sponsoring', () => {
8 itWeb3('sponsors mint transactions', async ({web3, privateKeyWrapper}) => {6 itEth('sponsors mint transactions', async ({helper, privateKey}) => {
9 const alice = privateKeyWrapper('//Alice');7 const alice = privateKey('//Alice');
108
11 const collection = await createCollectionExpectSuccess();9 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}});
12 await setCollectionSponsorExpectSuccess(collection, alice.address);10 await collection.setSponsor(alice, alice.address);
13 await confirmSponsorshipExpectSuccess(collection);11 await collection.confirmSponsorship(alice);
1412
15 const minter = createEthAccount(web3);13 const minter = helper.eth.createAccount();
16 expect(await web3.eth.getBalance(minter)).to.equal('0');14 expect(await helper.balance.getEthereum(minter)).to.equal(0n);
1715
18 const address = collectionIdToAddress(collection);16 const address = helper.ethAddress.fromCollectionId(collection.collectionId);
19 const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collection), {from: minter, ...GAS_ARGS});17 const contract = helper.ethNativeContract.collection(address, 'nft', minter);
2018
21 await enablePublicMintingExpectSuccess(alice, collection);19 await collection.addToAllowList(alice, {Ethereum: minter});
22 await addToAllowListExpectSuccess(alice, collection, {Ethereum: minter});
2320
24 const nextTokenId = await contract.methods.nextTokenId().call();21 const nextTokenId = await contract.methods.nextTokenId().call();
25 expect(nextTokenId).to.equal('1');22 expect(nextTokenId).to.equal('1');
59 // expect(bigIntToSub(api, BigInt(sponsorTuple[1]))).to.be.eq(sponsor.address);56 // expect(bigIntToSub(api, BigInt(sponsorTuple[1]))).to.be.eq(sponsor.address);
60 // });57 // });
6158
62 itWeb3('Remove sponsor', async ({api, web3, privateKeyWrapper}) => {59 itEth('Remove sponsor', async ({helper, privateKey}) => {
63 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);60 const alice = privateKey('//Alice');
64 const collectionHelpers = evmCollectionHelpers(web3, owner);
65 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * UNIQUE)});
66 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);
67 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
68 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);
6961
62 const owner = await helper.eth.createAccountWithBalance(alice);
63 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);
64
65 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();
66 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);
67 const sponsor = await helper.eth.createAccountWithBalance(alice);
68 const collectionEvm = await helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);
69
70 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;70 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;
71 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});71 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});
72 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;72 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;
80 expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');80 expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');
81 });81 });
8282
83 itWeb3('Sponsoring collection from evm address via access list', async ({api, web3, privateKeyWrapper}) => {83 itEth('Sponsoring collection from evm address via access list', async ({helper, privateKey}) => {
84 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);84 const alice = privateKey('//Alice');
85
86 const owner = await helper.eth.createAccountWithBalance(alice);
85 const collectionHelpers = evmCollectionHelpers(web3, owner);87 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);
88
86 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * UNIQUE)});89 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();
90 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);
87 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);91 const collectionId = helper.ethAddress.extractCollectionId(collectionIdAddress);
92 const collection = helper.nft.getCollectionObject(collectionId);
88 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);93 const sponsor = await helper.eth.createAccountWithBalance(alice);
89 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);94 const collectionEvm = await helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);
95
90 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});96 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});
91 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;97 let collectionData = (await collection.getData())!;
92 const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;98 const ss58Format = helper.chain.getChainProperties().ss58Format;
93 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;99 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
94 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
95 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');100 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
96101
97 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});102 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});
98 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;103 collectionData = (await collection.getData())!;
99 expect(collectionSub.sponsorship.isConfirmed).to.be.true;104 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
100 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
101105
102 const user = createEthAccount(web3);106 const user = helper.eth.createAccount();
103 const nextTokenId = await collectionEvm.methods.nextTokenId().call();107 const nextTokenId = await collectionEvm.methods.nextTokenId().call();
104 expect(nextTokenId).to.be.equal('1');108 expect(nextTokenId).to.be.equal('1');
105109
106 const oldPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();110 const oldPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();
107 expect(oldPermissions.mintMode).to.be.false;111 expect(oldPermissions.mintMode).to.be.false;
108 expect(oldPermissions.access).to.be.equal('Normal');112 expect(oldPermissions.access).to.be.equal('Normal');
109113
110 await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});114 await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});
111 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});115 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});
112 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});116 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});
113117
114 const newPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();118 const newPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();
115 expect(newPermissions.mintMode).to.be.true;119 expect(newPermissions.mintMode).to.be.true;
116 expect(newPermissions.access).to.be.equal('AllowList');120 expect(newPermissions.access).to.be.equal('AllowList');
117121
118 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);122 const ownerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));
119 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);123 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));
120124
121 {125 {
122 const nextTokenId = await collectionEvm.methods.nextTokenId().call();126 const nextTokenId = await collectionEvm.methods.nextTokenId().call();
140 },144 },
141 ]);145 ]);
142146
143 const ownerBalanceAfter = await ethBalanceViaSub(api, owner);147 const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));
144 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);148 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));
145149
146 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');150 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');
147 expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);151 expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);
205 // }209 // }
206 // });210 // });
207211
208 itWeb3('Check that transaction via EVM spend money from sponsor address', async ({api, web3, privateKeyWrapper}) => {212 itEth('Check that transaction via EVM spend money from sponsor address', async ({helper, privateKey}) => {
209 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);213 const alice = privateKey('//Alice');
214
215 const owner = await helper.eth.createAccountWithBalance(alice);
210 const collectionHelpers = evmCollectionHelpers(web3, owner);216 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);
217
211 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * UNIQUE)});218 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();
219 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);
212 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);220 const collectionId = helper.ethAddress.extractCollectionId(collectionIdAddress);
221 const collection = helper.nft.getCollectionObject(collectionId);
213 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);222 const sponsor = await helper.eth.createAccountWithBalance(alice);
214 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);223 const collectionEvm = await helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);
224
215 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();225 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();
216 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;226 let collectionData = (await collection.getData())!;
217 const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;227 const ss58Format = helper.chain.getChainProperties().ss58Format;
218 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;228 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
219 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
220 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');229 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
230
221 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);231 const sponsorCollection = await helper.ethNativeContract.collection(collectionIdAddress, 'nft', sponsor);
222 await sponsorCollection.methods.confirmCollectionSponsorship().send();232 await sponsorCollection.methods.confirmCollectionSponsorship().send();
223 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;233 collectionData = (await collection.getData())!;
224 expect(collectionSub.sponsorship.isConfirmed).to.be.true;234 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
225 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
226235
227 const user = createEthAccount(web3);236 const user = helper.eth.createAccount();
228 await collectionEvm.methods.addCollectionAdmin(user).send();237 await collectionEvm.methods.addCollectionAdmin(user).send();
229 238
230 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);239 const ownerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));
231 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);240 const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));
232 241
233 242 const userCollectionEvm = await helper.ethNativeContract.collection(collectionIdAddress, 'nft', user);
234 const userCollectionEvm = evmCollection(web3, user, collectionIdAddress);
235 const nextTokenId = await userCollectionEvm.methods.nextTokenId().call();243 const nextTokenId = await userCollectionEvm.methods.nextTokenId().call();
236 expect(nextTokenId).to.be.equal('1');244 expect(nextTokenId).to.be.equal('1');
237 result = await userCollectionEvm.methods.mintWithTokenURI(245 result = await userCollectionEvm.methods.mintWithTokenURI(
256 ]);264 ]);
257 expect(await userCollectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');265 expect(await userCollectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');
258 266
259 const ownerBalanceAfter = await ethBalanceViaSub(api, owner);267 const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));
260 expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);268 expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);
261 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);269 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));
262 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;270 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;
263 });271 });
264});272});