difftreelog
path: Fix other broken tests.
in: master
2 files changed
tests/src/eth/marketplace/marketplace.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {readFile} from 'fs/promises';18import {getBalanceSingle} from '../../substrate/get-balance';19import {20 addToAllowListExpectSuccess, 21 confirmSponsorshipExpectSuccess, 22 createCollectionExpectSuccess, 23 createItemExpectSuccess, 24 getTokenOwner,25 setCollectionLimitsExpectSuccess, 26 setCollectionSponsorExpectSuccess, 27 transferExpectSuccess, 28 transferFromExpectSuccess,29 transferBalanceTo,30} from '../../util/helpers';31import {collectionIdToAddress, contractHelpers, createEthAccountWithBalance, executeEthTxOnSub, GAS_ARGS, itWeb3, SponsoringMode, subToEth, subToEthLowercase, transferBalanceToEth} from '../util/helpers';32import {evmToAddress} from '@polkadot/util-crypto';33import nonFungibleAbi from '../nonFungibleAbi.json';3435import {expect} from 'chai';3637const PRICE = 2000n;3839describe('Matcher contract usage', () => {40 itWeb3('With UNQ', async ({api, web3, privateKeyWrapper}) => {41 const alice = privateKeyWrapper('//Alice');42 const matcherOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);43 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {44 from: matcherOwner,45 ...GAS_ARGS,46 });47 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments:[matcherOwner]}).send({from: matcherOwner});48 const helpers = contractHelpers(web3, matcherOwner);49 await helpers.methods.setSponsoringMode(matcher.options.address, SponsoringMode.Allowlisted).send({from: matcherOwner});50 await helpers.methods.setSponsoringRateLimit(matcher.options.address, 1).send({from: matcherOwner});51 await transferBalanceToEth(api, alice, matcher.options.address);5253 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});54 await setCollectionLimitsExpectSuccess(alice, collectionId, {sponsorApproveTimeout: 1});55 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});56 await setCollectionSponsorExpectSuccess(collectionId, alice.address);57 await transferBalanceToEth(api, alice, subToEth(alice.address));58 await confirmSponsorshipExpectSuccess(collectionId);5960 await helpers.methods.toggleAllowed(matcher.options.address, subToEth(alice.address), true).send({from: matcherOwner});61 await addToAllowListExpectSuccess(alice, collectionId, evmToAddress(subToEth(alice.address)));6263 const seller = privateKeyWrapper(`//Seller/${Date.now()}`);64 await helpers.methods.toggleAllowed(matcher.options.address, subToEth(seller.address), true).send({from: matcherOwner});6566 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);6768 // To transfer item to matcher it first needs to be transfered to EVM account of bob69 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});7071 // Token is owned by seller initially72 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});7374 // Ask75 {76 await executeEthTxOnSub(web3, api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));77 await executeEthTxOnSub(web3, api, seller, matcher, m => m.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, tokenId));78 }7980 // Token is transferred to matcher81 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});8283 // Buy84 {85 const sellerBalanceBeforePurchase = await getBalanceSingle(api, seller.address);86 await executeEthTxOnSub(web3, api, alice, matcher, m => m.buy(evmCollection.options.address, tokenId), {value: PRICE});87 expect(await getBalanceSingle(api, seller.address) - sellerBalanceBeforePurchase === PRICE);88 }8990 // Token is transferred to evm account of alice91 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});9293 // Transfer token to substrate side of alice94 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});9596 // Token is transferred to substrate account of alice, seller received funds97 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});98 });99100101 itWeb3('With escrow', async ({api, web3, privateKeyWrapper}) => {102 const alice = privateKeyWrapper('//Alice');103 const matcherOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);104 const escrow = await createEthAccountWithBalance(api, web3, privateKeyWrapper);105 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {106 from: matcherOwner,107 ...GAS_ARGS,108 });109 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments: [matcherOwner]}).send({from: matcherOwner, gas: 10000000});110 await matcher.methods.setEscrow(escrow).send({from: matcherOwner});111 const helpers = contractHelpers(web3, matcherOwner);112 await helpers.methods.setSponsoringMode(matcher.options.address, SponsoringMode.Allowlisted).send({from: matcherOwner});113 await helpers.methods.setSponsoringRateLimit(matcher.options.address, 1).send({from: matcherOwner});114 await transferBalanceToEth(api, alice, matcher.options.address);115116 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});117 await setCollectionLimitsExpectSuccess(alice, collectionId, {sponsorApproveTimeout: 1});118 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});119 await setCollectionSponsorExpectSuccess(collectionId, alice.address);120 await transferBalanceToEth(api, alice, subToEth(alice.address));121 await confirmSponsorshipExpectSuccess(collectionId);122123 await helpers.methods.toggleAllowed(matcher.options.address, subToEth(alice.address), true).send({from: matcherOwner});124 await addToAllowListExpectSuccess(alice, collectionId, evmToAddress(subToEth(alice.address)));125126 const seller = privateKeyWrapper(`//Seller/${Date.now()}`);127 await helpers.methods.toggleAllowed(matcher.options.address, subToEth(seller.address), true).send({from: matcherOwner});128129 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);130131 // To transfer item to matcher it first needs to be transfered to EVM account of bob132 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});133134 // Token is owned by seller initially135 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});136137 // Ask138 {139 await executeEthTxOnSub(web3, api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));140 await executeEthTxOnSub(web3, api, seller, matcher, m => m.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, tokenId));141 }142143 // Token is transferred to matcher144 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});145146 // Give buyer KSM147 await matcher.methods.depositKSM(PRICE, subToEth(alice.address)).send({from: escrow});148149 // Buy150 {151 expect(await matcher.methods.balanceKSM(subToEth(seller.address)).call()).to.be.equal('0');152 expect(await matcher.methods.balanceKSM(subToEth(alice.address)).call()).to.be.equal(PRICE.toString());153154 await executeEthTxOnSub(web3, api, alice, matcher, m => m.buyKSM(evmCollection.options.address, tokenId, subToEth(alice.address), subToEth(alice.address)));155156 // Price is removed from buyer balance, and added to seller157 expect(await matcher.methods.balanceKSM(subToEth(alice.address)).call()).to.be.equal('0');158 expect(await matcher.methods.balanceKSM(subToEth(seller.address)).call()).to.be.equal(PRICE.toString());159 }160161 // Token is transferred to evm account of alice162 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});163164 // Transfer token to substrate side of alice165 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});166167 // Token is transferred to substrate account of alice, seller received funds168 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});169 });170171172 itWeb3('Sell tokens from substrate user via EVM contract', async ({api, web3, privateKeyWrapper}) => {173 const alice = privateKeyWrapper('//Alice');174 const matcherOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);175 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {176 from: matcherOwner,177 ...GAS_ARGS,178 });179 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments:[matcherOwner]}).send({from: matcherOwner});180 await transferBalanceToEth(api, alice, matcher.options.address);181182 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});183 await setCollectionLimitsExpectSuccess(alice, collectionId, {sponsorApproveTimeout: 1});184 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});185186 const seller = privateKeyWrapper(`//Seller/${Date.now()}`);187 await transferBalanceTo(api, alice, seller.address);188 189 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);190191 // To transfer item to matcher it first needs to be transfered to EVM account of bob192 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});193194 // Token is owned by seller initially195 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});196197 // Ask198 {199 await executeEthTxOnSub(web3, api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));200 await executeEthTxOnSub(web3, api, seller, matcher, m => m.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, tokenId));201 }202203 // Token is transferred to matcher204 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});205206 // Buy207 {208 const sellerBalanceBeforePurchase = await getBalanceSingle(api, seller.address);209 await executeEthTxOnSub(web3, api, alice, matcher, m => m.buy(evmCollection.options.address, tokenId), {value: PRICE});210 expect(await getBalanceSingle(api, seller.address) - sellerBalanceBeforePurchase === PRICE);211 }212213 // Token is transferred to evm account of alice214 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});215216 // Transfer token to substrate side of alice217 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});218219 // Token is transferred to substrate account of alice, seller received funds220 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});221 });222});1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {readFile} from 'fs/promises';18import {getBalanceSingle} from '../../substrate/get-balance';19import {20 addToAllowListExpectSuccess, 21 confirmSponsorshipExpectSuccess, 22 createCollectionExpectSuccess, 23 createItemExpectSuccess, 24 getTokenOwner,25 setCollectionLimitsExpectSuccess, 26 setCollectionSponsorExpectSuccess, 27 transferExpectSuccess, 28 transferFromExpectSuccess,29 transferBalanceTo,30} from '../../util/helpers';31import {collectionIdToAddress, contractHelpers, createEthAccountWithBalance, executeEthTxOnSub, GAS_ARGS, itWeb3, SponsoringMode, subToEth, subToEthLowercase, transferBalanceToEth} from '../util/helpers';32import {evmToAddress} from '@polkadot/util-crypto';33import nonFungibleAbi from '../nonFungibleAbi.json';3435import {expect} from 'chai';3637const PRICE = 2000n;3839describe('Matcher contract usage', () => {40 itWeb3('With UNQ', async ({api, web3, privateKeyWrapper}) => {41 const alice = privateKeyWrapper('//Alice');42 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);43 const matcherOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);44 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {45 from: matcherOwner,46 ...GAS_ARGS,47 });48 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments:[matcherOwner]}).send({from: matcherOwner});49 const helpers = contractHelpers(web3, matcherOwner);50 await helpers.methods.setSponsoringMode(matcher.options.address, SponsoringMode.Allowlisted).send({from: matcherOwner});51 await helpers.methods.setSponsoringRateLimit(matcher.options.address, 1).send({from: matcherOwner});52 53 await helpers.methods.setSponsor(matcher.options.address, sponsor).send({from: matcherOwner});54 await helpers.methods.confirmSponsorship(matcher.options.address).send({from: sponsor});5556 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});57 await setCollectionLimitsExpectSuccess(alice, collectionId, {sponsorApproveTimeout: 1});58 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});59 await setCollectionSponsorExpectSuccess(collectionId, alice.address);60 await transferBalanceToEth(api, alice, subToEth(alice.address));61 await confirmSponsorshipExpectSuccess(collectionId);6263 await helpers.methods.toggleAllowed(matcher.options.address, subToEth(alice.address), true).send({from: matcherOwner});64 await addToAllowListExpectSuccess(alice, collectionId, evmToAddress(subToEth(alice.address)));6566 const seller = privateKeyWrapper(`//Seller/${Date.now()}`);67 await helpers.methods.toggleAllowed(matcher.options.address, subToEth(seller.address), true).send({from: matcherOwner});6869 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);7071 // To transfer item to matcher it first needs to be transfered to EVM account of bob72 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});7374 // Token is owned by seller initially75 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});7677 // Ask78 {79 await executeEthTxOnSub(web3, api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));80 await executeEthTxOnSub(web3, api, seller, matcher, m => m.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, tokenId));81 }8283 // Token is transferred to matcher84 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});8586 // Buy87 {88 const sellerBalanceBeforePurchase = await getBalanceSingle(api, seller.address);89 await executeEthTxOnSub(web3, api, alice, matcher, m => m.buy(evmCollection.options.address, tokenId), {value: PRICE});90 expect(await getBalanceSingle(api, seller.address) - sellerBalanceBeforePurchase === PRICE);91 }9293 // Token is transferred to evm account of alice94 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});9596 // Transfer token to substrate side of alice97 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});9899 // Token is transferred to substrate account of alice, seller received funds100 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});101 });102103104 itWeb3('With escrow', async ({api, web3, privateKeyWrapper}) => {105 const alice = privateKeyWrapper('//Alice');106 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);107 const matcherOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);108 const escrow = await createEthAccountWithBalance(api, web3, privateKeyWrapper);109 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {110 from: matcherOwner,111 ...GAS_ARGS,112 });113 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments: [matcherOwner]}).send({from: matcherOwner, gas: 10000000});114 await matcher.methods.setEscrow(escrow).send({from: matcherOwner});115 const helpers = contractHelpers(web3, matcherOwner);116 await helpers.methods.setSponsoringMode(matcher.options.address, SponsoringMode.Allowlisted).send({from: matcherOwner});117 await helpers.methods.setSponsoringRateLimit(matcher.options.address, 1).send({from: matcherOwner});118 119 await helpers.methods.setSponsor(matcher.options.address, sponsor).send({from: matcherOwner});120 await helpers.methods.confirmSponsorship(matcher.options.address).send({from: sponsor});121122 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});123 await setCollectionLimitsExpectSuccess(alice, collectionId, {sponsorApproveTimeout: 1});124 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});125 await setCollectionSponsorExpectSuccess(collectionId, alice.address);126 await transferBalanceToEth(api, alice, subToEth(alice.address));127 await confirmSponsorshipExpectSuccess(collectionId);128129 await helpers.methods.toggleAllowed(matcher.options.address, subToEth(alice.address), true).send({from: matcherOwner});130 await addToAllowListExpectSuccess(alice, collectionId, evmToAddress(subToEth(alice.address)));131132 const seller = privateKeyWrapper(`//Seller/${Date.now()}`);133 await helpers.methods.toggleAllowed(matcher.options.address, subToEth(seller.address), true).send({from: matcherOwner});134135 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);136137 // To transfer item to matcher it first needs to be transfered to EVM account of bob138 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});139140 // Token is owned by seller initially141 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});142143 // Ask144 {145 await executeEthTxOnSub(web3, api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));146 await executeEthTxOnSub(web3, api, seller, matcher, m => m.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, tokenId));147 }148149 // Token is transferred to matcher150 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});151152 // Give buyer KSM153 await matcher.methods.depositKSM(PRICE, subToEth(alice.address)).send({from: escrow});154155 // Buy156 {157 expect(await matcher.methods.balanceKSM(subToEth(seller.address)).call()).to.be.equal('0');158 expect(await matcher.methods.balanceKSM(subToEth(alice.address)).call()).to.be.equal(PRICE.toString());159160 await executeEthTxOnSub(web3, api, alice, matcher, m => m.buyKSM(evmCollection.options.address, tokenId, subToEth(alice.address), subToEth(alice.address)));161162 // Price is removed from buyer balance, and added to seller163 expect(await matcher.methods.balanceKSM(subToEth(alice.address)).call()).to.be.equal('0');164 expect(await matcher.methods.balanceKSM(subToEth(seller.address)).call()).to.be.equal(PRICE.toString());165 }166167 // Token is transferred to evm account of alice168 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});169170 // Transfer token to substrate side of alice171 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});172173 // Token is transferred to substrate account of alice, seller received funds174 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});175 });176177178 itWeb3('Sell tokens from substrate user via EVM contract', async ({api, web3, privateKeyWrapper}) => {179 const alice = privateKeyWrapper('//Alice');180 const matcherOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);181 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {182 from: matcherOwner,183 ...GAS_ARGS,184 });185 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments:[matcherOwner]}).send({from: matcherOwner});186 await transferBalanceToEth(api, alice, matcher.options.address);187188 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});189 await setCollectionLimitsExpectSuccess(alice, collectionId, {sponsorApproveTimeout: 1});190 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});191192 const seller = privateKeyWrapper(`//Seller/${Date.now()}`);193 await transferBalanceTo(api, alice, seller.address);194 195 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);196197 // To transfer item to matcher it first needs to be transfered to EVM account of bob198 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});199200 // Token is owned by seller initially201 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});202203 // Ask204 {205 await executeEthTxOnSub(web3, api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));206 await executeEthTxOnSub(web3, api, seller, matcher, m => m.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, tokenId));207 }208209 // Token is transferred to matcher210 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});211212 // Buy213 {214 const sellerBalanceBeforePurchase = await getBalanceSingle(api, seller.address);215 await executeEthTxOnSub(web3, api, alice, matcher, m => m.buy(evmCollection.options.address, tokenId), {value: PRICE});216 expect(await getBalanceSingle(api, seller.address) - sellerBalanceBeforePurchase === PRICE);217 }218219 // Token is transferred to evm account of alice220 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});221222 // Transfer token to substrate side of alice223 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});224225 // Token is transferred to substrate account of alice, seller received funds226 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});227 });228});tests/src/eth/sponsoring.test.tsdiffbeforeafterboth--- a/tests/src/eth/sponsoring.test.ts
+++ b/tests/src/eth/sponsoring.test.ts
@@ -15,13 +15,12 @@
// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
import {expect} from 'chai';
-import {contractHelpers, createEthAccount, createEthAccountWithBalance, deployCollector, deployFlipper, itWeb3, SponsoringMode, transferBalanceToEth} from './util/helpers';
+import {contractHelpers, createEthAccount, createEthAccountWithBalance, deployCollector, deployFlipper, itWeb3, SponsoringMode} from './util/helpers';
describe('EVM sponsoring', () => {
itWeb3('Fee is deducted from contract if sponsoring is enabled', async ({api, web3, privateKeyWrapper}) => {
- const alice = privateKeyWrapper('//Alice');
-
const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+ const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
const caller = createEthAccount(web3);
const originalCallerBalance = await web3.eth.getBalance(caller);
expect(originalCallerBalance).to.be.equal('0');
@@ -31,28 +30,31 @@
const helpers = contractHelpers(web3, owner);
await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});
await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});
+
+ await helpers.methods.setSponsor(flipper.options.address, sponsor).send({from: owner});
+ await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
-
- await transferBalanceToEth(api, alice, flipper.options.address);
- const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);
- expect(originalFlipperBalance).to.be.not.equal('0');
+ const originalSponsorBalance = await web3.eth.getBalance(sponsor);
+ expect(originalSponsorBalance).to.be.not.equal('0');
await flipper.methods.flip().send({from: caller});
expect(await flipper.methods.getValue().call()).to.be.true;
// Balance should be taken from flipper instead of caller
expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);
- expect(await web3.eth.getBalance(flipper.options.address)).to.be.not.equals(originalFlipperBalance);
+ expect(await web3.eth.getBalance(sponsor)).to.be.not.equals(originalSponsorBalance);
});
+
itWeb3('...but this doesn\'t applies to payable value', async ({api, web3, privateKeyWrapper}) => {
const alice = privateKeyWrapper('//Alice');
const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+ const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
const originalCallerBalance = await web3.eth.getBalance(caller);
expect(originalCallerBalance).to.be.not.equal('0');
@@ -68,16 +70,17 @@
await helpers.methods.setSponsoringRateLimit(collector.options.address, 0).send({from: owner});
expect(await helpers.methods.sponsoringEnabled(collector.options.address).call()).to.be.true;
- await transferBalanceToEth(api, alice, collector.options.address);
+ await helpers.methods.setSponsor(collector.options.address, sponsor).send({from: owner});
+ await helpers.methods.confirmSponsorship(collector.options.address).send({from: sponsor});
- const originalCollectorBalance = await web3.eth.getBalance(collector.options.address);
- expect(originalCollectorBalance).to.be.not.equal('0');
+ const originalSponsorBalance = await web3.eth.getBalance(sponsor);
+ expect(originalSponsorBalance).to.be.not.equal('0');
await collector.methods.giveMoney().send({from: caller, value: '10000'});
// Balance will be taken from both caller (value) and from collector (fee)
expect(await web3.eth.getBalance(caller)).to.be.equals((BigInt(originalCallerBalance) - 10000n).toString());
- expect(await web3.eth.getBalance(collector.options.address)).to.be.not.equals(originalCollectorBalance);
+ expect(await web3.eth.getBalance(sponsor)).to.be.not.equals(originalSponsorBalance);
expect(await collector.methods.getCollected().call()).to.be.equal('10000');
});
});