git.delta.rocks / unique-network / refs/commits / 1b2f0f9403a4

difftreelog

patch: Fix broken test. Add some new tests. Move tests.

Trubnikov Sergey2022-08-05parent: #3daa491.patch.diff
in: master

2 files changed

modifiedtests/src/eth/collectionSponsoring.test.tsdiffbeforeafterboth
1import {addToAllowListExpectSuccess, confirmSponsorshipExpectSuccess, createCollectionExpectSuccess, enablePublicMintingExpectSuccess, setCollectionSponsorExpectSuccess} from '../util/helpers';1import {addToAllowListExpectSuccess, confirmSponsorshipExpectSuccess, createCollectionExpectSuccess, enablePublicMintingExpectSuccess, getDetailedCollectionInfo, setCollectionSponsorExpectSuccess} from '../util/helpers';
2import {itWeb3, createEthAccount, collectionIdToAddress, GAS_ARGS, normalizeEvents} from './util/helpers';2import {itWeb3, createEthAccount, collectionIdToAddress, GAS_ARGS, normalizeEvents, createEthAccountWithBalance, evmCollectionHelpers, getCollectionAddressFromResult, evmCollection, ethBalanceViaSub} from './util/helpers';
3import nonFungibleAbi from './nonFungibleAbi.json';3import nonFungibleAbi from './nonFungibleAbi.json';
4import {expect} from 'chai';4import {expect} from 'chai';
5import { evmToAddress } from '@polkadot/util-crypto';
56
6describe('evm collection sponsoring', () => {7describe('evm collection sponsoring', () => {
7 itWeb3('sponsors mint transactions', async ({web3, privateKeyWrapper}) => {8 itWeb3('sponsors mint transactions', async ({web3, privateKeyWrapper}) => {
37 ]);38 ]);
38 });39 });
40
41 itWeb3('Sponsoring collection from evm address via access list', async ({api, web3, privateKeyWrapper}) => {
42 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
43 const collectionHelpers = evmCollectionHelpers(web3, owner);
44 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();
45 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
46 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
47 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);
48 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});
49 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;
50 const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;
51 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;
52 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
53 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
54
55 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});
56 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;
57 expect(collectionSub.sponsorship.isConfirmed).to.be.true;
58 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
59
60 const user = createEthAccount(web3);
61 const nextTokenId = await collectionEvm.methods.nextTokenId().call();
62 expect(nextTokenId).to.be.equal('1');
63
64 const oldPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();
65 expect(oldPermissions.mintMode).to.be.false;
66 expect(oldPermissions.access).to.be.equal('Normal');
67
68 await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});
69 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});
70 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});
71
72 const newPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();
73 expect(newPermissions.mintMode).to.be.true;
74 expect(newPermissions.access).to.be.equal('AllowList');
75
76 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);
77 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);
78
79 {
80 const nextTokenId = await collectionEvm.methods.nextTokenId().call();
81 expect(nextTokenId).to.be.equal('1');
82 const result = await collectionEvm.methods.mintWithTokenURI(
83 user,
84 nextTokenId,
85 'Test URI',
86 ).send({from: user});
87 const events = normalizeEvents(result.events);
88
89 expect(events).to.be.deep.equal([
90 {
91 address: collectionIdAddress,
92 event: 'Transfer',
93 args: {
94 from: '0x0000000000000000000000000000000000000000',
95 to: user,
96 tokenId: nextTokenId,
97 },
98 },
99 ]);
100
101 const ownerBalanceAfter = await ethBalanceViaSub(api, owner);
102 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);
103
104 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');
105 expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);
106 expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;
107 }
108 });
109
110 itWeb3('Check that transaction via EVM spend money from sponsor address', async ({api, web3, privateKeyWrapper}) => {
111 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
112 const collectionHelpers = evmCollectionHelpers(web3, owner);
113 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();
114 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
115 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
116 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);
117 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();
118 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;
119 const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;
120 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;
121 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
122 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
123 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);
124 await sponsorCollection.methods.confirmCollectionSponsorship().send();
125 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;
126 expect(collectionSub.sponsorship.isConfirmed).to.be.true;
127 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
128
129 const user = createEthAccount(web3);
130 await collectionEvm.methods.addCollectionAdmin(user).send();
131
132 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);
133 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);
134
135
136 const userCollectionEvm = evmCollection(web3, user, collectionIdAddress);
137 const nextTokenId = await userCollectionEvm.methods.nextTokenId().call();
138 expect(nextTokenId).to.be.equal('1');
139 result = await userCollectionEvm.methods.mintWithTokenURI(
140 user,
141 nextTokenId,
142 'Test URI',
143 ).send();
144
145 const events = normalizeEvents(result.events);
146 const address = collectionIdToAddress(collectionId);
147
148 expect(events).to.be.deep.equal([
149 {
150 address,
151 event: 'Transfer',
152 args: {
153 from: '0x0000000000000000000000000000000000000000',
154 to: user,
155 tokenId: nextTokenId,
156 },
157 },
158 ]);
159 expect(await userCollectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');
160
161 const ownerBalanceAfter = await ethBalanceViaSub(api, owner);
162 expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);
163 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);
164 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;
165 });
39});166});
40167
modifiedtests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth
23 itWeb3,23 itWeb3,
24 SponsoringMode,24 SponsoringMode,
25 createEthAccount,25 createEthAccount,
26 collectionIdToAddress,
27 GAS_ARGS,
28 normalizeEvents,
29 subToEth,
30 executeEthTxOnSub,
31 evmCollectionHelpers,
32 getCollectionAddressFromResult,
33 evmCollection,
34 ethBalanceViaSub,26 ethBalanceViaSub,
35} from './util/helpers';27} from './util/helpers';
36import {
37 addCollectionAdminExpectSuccess,
38 createCollectionExpectSuccess,
39 getDetailedCollectionInfo,
40 transferBalanceTo,
41} from '../util/helpers';
42import nonFungibleAbi from './nonFungibleAbi.json';
43import getBalance from '../substrate/get-balance';
44import {evmToAddress} from '@polkadot/util-crypto';28import {evmToAddress} from '@polkadot/util-crypto';
4529
46describe('Sponsoring EVM contracts', () => {30describe('Sponsoring EVM contracts', () => {
62 await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).send({from: notOwner})).to.rejected;46 await expect(helpers.methods.setSponsoringMode(notOwner, SponsoringMode.Allowlisted).send({from: notOwner})).to.rejected;
63 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;47 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
64 });48 });
49
50 itWeb3('Sponsor can be set by the address that deployed the contract', async ({api, web3, privateKeyWrapper}) => {
51 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
52 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
53 const flipper = await deployFlipper(web3, owner);
54 const helpers = contractHelpers(web3, owner);
55 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;
56 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;
57 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;
58 });
59
60 itWeb3('Sponsor can not be set by the address that did not deployed the contract', async ({api, web3, privateKeyWrapper}) => {
61 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
62 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
63 const notOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
64 const flipper = await deployFlipper(web3, owner);
65 const helpers = contractHelpers(web3, owner);
66 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;
67 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).call({from: notOwner})).to.be.rejectedWith('NoPermission');
68 expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;
69 });
6570
66 itWeb3('In generous mode, non-allowlisted user transaction will be sponsored', async ({api, web3, privateKeyWrapper}) => {71 itWeb3('Sponsorship can be confirmed by the address that pending as sponsor', async ({api, web3, privateKeyWrapper}) => {
67 const alice = privateKeyWrapper('//Alice');72 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
73 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
74 const flipper = await deployFlipper(web3, owner);
75 const helpers = contractHelpers(web3, owner);
76 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
77 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;
78 await expect(helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor})).to.be.not.rejected;
79 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
80 });
6881
82 itWeb3('Sponsorship can not be confirmed by the address that not pending as sponsor', async ({api, web3, privateKeyWrapper}) => {
69 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);83 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
84 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
85 const notSponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
86 const flipper = await deployFlipper(web3, owner);
87 const helpers = contractHelpers(web3, owner);
88 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
89 await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;
90 await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPermission');
91 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
92 });
93
94 itWeb3('Get confirmed sponsor', async ({api, web3, privateKeyWrapper}) => {
95 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
96 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
97 const flipper = await deployFlipper(web3, owner);
98 const helpers = contractHelpers(web3, owner);
99 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
100 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
101
102 const result = await helpers.methods.getSponsor(flipper.options.address).call();
103
104 expect(result[0]).to.be.eq(sponsor);
105 const sponsorSub = api.registry.createType('AccountId', '0x' + BigInt(result[1]).toString(16).padStart(64, '0')).toJSON();
106 expect(sponsorSub).to.be.eq(evmToAddress(sponsor));
107 });
108
109 itWeb3('In generous mode, non-allowlisted user transaction will be sponsored', async ({api, web3, privateKeyWrapper}) => {
110 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
111 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
70 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);112 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
71113
72 const flipper = await deployFlipper(web3, owner);114 const flipper = await deployFlipper(web3, owner);
73115
74 const helpers = contractHelpers(web3, owner);116 const helpers = contractHelpers(web3, owner);
75117
76 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;118 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
119 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
120 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
121 expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
122
77 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});123 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});
78 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});124 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
79 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
80125
81 await transferBalanceToEth(api, alice, flipper.options.address);126 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);
127 const callerBalanceBefore = await ethBalanceViaSub(api, caller);
82128
83 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);
84 expect(originalFlipperBalance).to.be.not.equal('0');
85
86 await flipper.methods.flip().send({from: caller});129 await flipper.methods.flip().send({from: caller});
87 expect(await flipper.methods.getValue().call()).to.be.true;130 expect(await flipper.methods.getValue().call()).to.be.true;
88131
89 // Balance should be taken from flipper instead of caller132 // Balance should be taken from sponsor instead of caller
90 const balanceAfter = await web3.eth.getBalance(flipper.options.address);133 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);
134 const callerBalanceAfter = await ethBalanceViaSub(api, caller);
135 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;
91 expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);136 expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);
92 });137 });
93138
94 itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({api, web3, privateKeyWrapper}) => {139 itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({api, web3, privateKeyWrapper}) => {
95 const alice = privateKeyWrapper('//Alice');
96
97 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);140 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
141 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
98 const caller = createEthAccount(web3);142 const caller = createEthAccount(web3);
99143
100 const flipper = await deployFlipper(web3, owner);144 const flipper = await deployFlipper(web3, owner);
103 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});147 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});
104 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});148 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});
105149
106 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
107 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});150 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
108 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});151 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
109 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
110152
111 await transferBalanceToEth(api, alice, flipper.options.address);153 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
154 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
112155
113 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);156 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);
114 expect(originalFlipperBalance).to.be.not.equal('0');157 expect(sponsorBalanceBefore).to.be.not.equal('0');
115158
116 await flipper.methods.flip().send({from: caller});159 await flipper.methods.flip().send({from: caller});
117 expect(await flipper.methods.getValue().call()).to.be.true;160 expect(await flipper.methods.getValue().call()).to.be.true;
118161
119 // Balance should be taken from flipper instead of caller162 // Balance should be taken from flipper instead of caller
120 const balanceAfter = await web3.eth.getBalance(flipper.options.address);163 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);
121 expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);164 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;
122 });165 });
123166
124 itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should not decrease (non-allowlisted)', async ({api, web3, privateKeyWrapper}) => {167 itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should not decrease (non-allowlisted)', async ({api, web3, privateKeyWrapper}) => {
131174
132 const helpers = contractHelpers(web3, owner);175 const helpers = contractHelpers(web3, owner);
133176
134 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
135 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});177 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
136 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});178 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
137 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
138179
139 await transferBalanceToEth(api, alice, flipper.options.address);180 await transferBalanceToEth(api, alice, flipper.options.address);
140181
150 });191 });
151192
152 itWeb3('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({api, web3, privateKeyWrapper}) => {193 itWeb3('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({api, web3, privateKeyWrapper}) => {
153 const alice = privateKeyWrapper('//Alice');
154
155 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);194 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
195 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
156 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);196 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
157 const originalCallerBalance = await web3.eth.getBalance(caller);
158197
159 const flipper = await deployFlipper(web3, owner);198 const flipper = await deployFlipper(web3, owner);
160199
161 const helpers = contractHelpers(web3, owner);200 const helpers = contractHelpers(web3, owner);
162 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});201 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});
163 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});202 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});
164203
165 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
166 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});204 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
167 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});205 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
168 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
169206
170 await transferBalanceToEth(api, alice, flipper.options.address);207 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
208 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
171209
172 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);210 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);
173 expect(originalFlipperBalance).to.be.not.equal('0');211 const callerBalanceBefore = await ethBalanceViaSub(api, caller);
174212
175 await flipper.methods.flip().send({from: caller});213 await flipper.methods.flip().send({from: caller});
176 expect(await flipper.methods.getValue().call()).to.be.true;214 expect(await flipper.methods.getValue().call()).to.be.true;
177215
178 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);216 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);
217 const callerBalanceAfter = await ethBalanceViaSub(api, caller);
218 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;
219 expect(callerBalanceAfter).to.be.equals(callerBalanceBefore);
179 });220 });
180221
181 itWeb3('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({api, web3, privateKeyWrapper}) => {222 itWeb3('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({api, web3, privateKeyWrapper}) => {
182 const alice = privateKeyWrapper('//Alice');
183
184 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);223 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
224 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
185 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);225 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
186 const originalCallerBalance = await web3.eth.getBalance(caller);226 const originalCallerBalance = await web3.eth.getBalance(caller);
187227
191 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});231 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});
192 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});232 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});
193233
194 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
195 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});234 await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
196 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});235 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});
197 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
198236
199 await transferBalanceToEth(api, alice, flipper.options.address);237 await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
238 await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
200239
201 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);240 const originalFlipperBalance = await web3.eth.getBalance(sponsor);
202 expect(originalFlipperBalance).to.be.not.equal('0');241 expect(originalFlipperBalance).to.be.not.equal('0');
203242
204 await flipper.methods.flip().send({from: caller});243 await flipper.methods.flip().send({from: caller});
205 expect(await flipper.methods.getValue().call()).to.be.true;244 expect(await flipper.methods.getValue().call()).to.be.true;
206 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);245 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);
207246
208 const newFlipperBalance = await web3.eth.getBalance(flipper.options.address);247 const newFlipperBalance = await web3.eth.getBalance(sponsor);
209 expect(newFlipperBalance).to.be.not.equals(originalFlipperBalance);248 expect(newFlipperBalance).to.be.not.equals(originalFlipperBalance);
210249
211 await flipper.methods.flip().send({from: caller});250 await flipper.methods.flip().send({from: caller});
212 expect(await web3.eth.getBalance(flipper.options.address)).to.be.equal(newFlipperBalance);251 expect(await web3.eth.getBalance(sponsor)).to.be.equal(newFlipperBalance);
213 expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);252 expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);
214 });253 });
215254
219 const flipper = await deployFlipper(web3, owner);258 const flipper = await deployFlipper(web3, owner);
220 const helpers = contractHelpers(web3, owner);259 const helpers = contractHelpers(web3, owner);
221 expect(await helpers.methods.getSponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');260 expect(await helpers.methods.getSponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');
222 });
223
224 itWeb3('Sponsoring collection from evm address via access list', async ({api, web3, privateKeyWrapper}) => {
225 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
226 const collectionHelpers = evmCollectionHelpers(web3, owner);
227 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();
228 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
229 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
230 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);
231 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});
232 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;
233 const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;
234 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;
235 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
236 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
237
238 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});
239 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;
240 expect(collectionSub.sponsorship.isConfirmed).to.be.true;
241 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
242
243 const user = createEthAccount(web3);
244 const nextTokenId = await collectionEvm.methods.nextTokenId().call();
245 expect(nextTokenId).to.be.equal('1');
246
247 const oldPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();
248 expect(oldPermissions.mintMode).to.be.false;
249 expect(oldPermissions.access).to.be.equal('Normal');
250
251 await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});
252 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});
253 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});
254
255 const newPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();
256 expect(newPermissions.mintMode).to.be.true;
257 expect(newPermissions.access).to.be.equal('AllowList');
258
259 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);
260 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);
261
262 {
263 const nextTokenId = await collectionEvm.methods.nextTokenId().call();
264 expect(nextTokenId).to.be.equal('1');
265 const result = await collectionEvm.methods.mintWithTokenURI(
266 user,
267 nextTokenId,
268 'Test URI',
269 ).send({from: user});
270 const events = normalizeEvents(result.events);
271
272 expect(events).to.be.deep.equal([
273 {
274 address: collectionIdAddress,
275 event: 'Transfer',
276 args: {
277 from: '0x0000000000000000000000000000000000000000',
278 to: user,
279 tokenId: nextTokenId,
280 },
281 },
282 ]);
283
284 const ownerBalanceAfter = await ethBalanceViaSub(api, owner);
285 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);
286
287 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');
288 expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);
289 expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;
290 }
291 });
292
293 itWeb3('Check that transaction via EVM spend money from sponsor address', async ({api, web3, privateKeyWrapper}) => {
294 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
295 const collectionHelpers = evmCollectionHelpers(web3, owner);
296 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();
297 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
298 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
299 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);
300 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();
301 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;
302 const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;
303 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;
304 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
305 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
306 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);
307 await sponsorCollection.methods.confirmCollectionSponsorship().send();
308 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;
309 expect(collectionSub.sponsorship.isConfirmed).to.be.true;
310 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));
311
312 const user = createEthAccount(web3);
313 await collectionEvm.methods.addCollectionAdmin(user).send();
314
315 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);
316 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);
317
318
319 const userCollectionEvm = evmCollection(web3, user, collectionIdAddress);
320 const nextTokenId = await userCollectionEvm.methods.nextTokenId().call();
321 expect(nextTokenId).to.be.equal('1');
322 result = await userCollectionEvm.methods.mintWithTokenURI(
323 user,
324 nextTokenId,
325 'Test URI',
326 ).send();
327
328 const events = normalizeEvents(result.events);
329 const address = collectionIdToAddress(collectionId);
330
331 expect(events).to.be.deep.equal([
332 {
333 address,
334 event: 'Transfer',
335 args: {
336 from: '0x0000000000000000000000000000000000000000',
337 to: user,
338 tokenId: nextTokenId,
339 },
340 },
341 ]);
342 expect(await userCollectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');
343
344 const ownerBalanceAfter = await ethBalanceViaSub(api, owner);
345 expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);
346 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);
347 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;
348 });261 });
349});262});
350263