difftreelog
Minor improvment
in: master
1 file 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 {usingPlaygrounds} from './../../util/playgrounds/index';18import {IKeyringPair} from '@polkadot/types/types';19import {readFile} from 'fs/promises';20import {collectionIdToAddress, contractHelpers, GAS_ARGS, SponsoringMode} from '../util/helpers';21import nonFungibleAbi from '../nonFungibleAbi.json';2223import {itEth, expect} from '../util/playgrounds';2425const PRICE = 2000n;2627describe('Matcher contract usage', () => {28 let donor: IKeyringPair;29 let alice: IKeyringPair;30 let aliceMirror: string;31 let aliceDoubleMirror: string;32 let seller: IKeyringPair;33 let sellerMirror: string;3435 before(async () => {36 await usingPlaygrounds(async (_helper, privateKey) => {37 donor = privateKey('//Alice');38 }); 39 });4041 beforeEach(async () => {42 await usingPlaygrounds(async (helper, privateKey) => {43 [alice] = await helper.arrange.createAccounts([10000n], donor);44 aliceMirror = helper.address.substrateToEth(alice.address).toLowerCase();45 aliceDoubleMirror = helper.address.ethToSubstrate(aliceMirror);46 seller = privateKey(`//Seller/${Date.now()}`);47 sellerMirror = helper.address.substrateToEth(seller.address).toLowerCase();4849 await helper.balance.transferToSubstrate(donor, aliceDoubleMirror, 10_000_000_000_000_000_000n);50 });51 });5253 itEth('With UNQ', async ({helper}) => {54 const web3 = helper.web3!;5556 const sponsor = await helper.eth.createAccountWithBalance(donor);57 const matcherOwner = await helper.eth.createAccountWithBalance(donor);58 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {59 from: matcherOwner,60 ...GAS_ARGS,61 });62 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments:[matcherOwner]}).send({from: matcherOwner});63 const helpers = contractHelpers(web3, matcherOwner);64 await helpers.methods.setSponsoringMode(matcher.options.address, SponsoringMode.Allowlisted).send({from: matcherOwner});65 await helpers.methods.setSponsoringRateLimit(matcher.options.address, 1).send({from: matcherOwner});66 67 await helpers.methods.setSponsor(matcher.options.address, sponsor).send({from: matcherOwner});68 await helpers.methods.confirmSponsorship(matcher.options.address).send({from: sponsor});6970 const collection = await helper.nft.mintCollection(alice, {limits: {sponsorApproveTimeout: 1}, pendingSponsor: alice.address});71 await collection.confirmSponsorship(alice);72 await collection.addToAllowList(alice, {Substrate: aliceDoubleMirror});73 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collection.collectionId), {from: matcherOwner});74 await helper.eth.transferBalanceFromSubstrate(donor, aliceMirror);7576 await helpers.methods.toggleAllowed(matcher.options.address, aliceMirror, true).send({from: matcherOwner});77 await helpers.methods.toggleAllowed(matcher.options.address, sellerMirror, true).send({from: matcherOwner});7879 const token = await collection.mintToken(alice, {Ethereum: sellerMirror});8081 // Token is owned by seller initially82 expect(await token.getOwner()).to.be.deep.equal({Ethereum: sellerMirror});8384 // Ask85 {86 await helper.eth.sendEVM(seller, evmCollection.options.address, evmCollection.methods.approve(matcher.options.address, token.tokenId).encodeABI(), '0');87 await helper.eth.sendEVM(seller, matcher.options.address, matcher.methods.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, token.tokenId).encodeABI(), '0');88 }8990 // Token is transferred to matcher91 expect(await token.getOwner()).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});9293 // Buy94 {95 const sellerBalanceBeforePurchase = await helper.balance.getSubstrate(seller.address);96 await helper.eth.sendEVM(alice, matcher.options.address, matcher.methods.buy(evmCollection.options.address, token.tokenId).encodeABI(), PRICE.toString());97 expect(await helper.balance.getSubstrate(seller.address) - sellerBalanceBeforePurchase === PRICE);98 }99100 // Token is transferred to evm account of alice101 expect(await token.getOwner()).to.be.deep.equal({Ethereum: aliceMirror});102103 // Transfer token to substrate side of alice104 await token.transferFrom(alice, {Ethereum: aliceMirror}, {Substrate: alice.address});105106 // Token is transferred to substrate account of alice, seller received funds107 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});108 });109110 itEth('With escrow', async ({helper}) => {111 const web3 = helper.web3!;112113 const sponsor = await helper.eth.createAccountWithBalance(donor);114 const matcherOwner = await helper.eth.createAccountWithBalance(donor);115 const escrow = await helper.eth.createAccountWithBalance(donor);116 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {117 from: matcherOwner,118 ...GAS_ARGS,119 });120 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments: [matcherOwner]}).send({from: matcherOwner, gas: 10000000});121 await matcher.methods.setEscrow(escrow).send({from: matcherOwner});122 const helpers = contractHelpers(web3, matcherOwner);123 await helpers.methods.setSponsoringMode(matcher.options.address, SponsoringMode.Allowlisted).send({from: matcherOwner});124 await helpers.methods.setSponsoringRateLimit(matcher.options.address, 1).send({from: matcherOwner});125 126 await helpers.methods.setSponsor(matcher.options.address, sponsor).send({from: matcherOwner});127 await helpers.methods.confirmSponsorship(matcher.options.address).send({from: sponsor});128129 const collection = await helper.nft.mintCollection(alice, {limits: {sponsorApproveTimeout: 1}, pendingSponsor: alice.address});130 await collection.confirmSponsorship(alice);131 await collection.addToAllowList(alice, {Substrate: aliceDoubleMirror});132 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collection.collectionId), {from: matcherOwner});133 await helper.eth.transferBalanceFromSubstrate(donor, aliceMirror);134135136 await helpers.methods.toggleAllowed(matcher.options.address, aliceMirror, true).send({from: matcherOwner});137138 await helpers.methods.toggleAllowed(matcher.options.address, sellerMirror, true).send({from: matcherOwner});139140 const token = await collection.mintToken(alice, {Ethereum: sellerMirror});141142 // Token is owned by seller initially143 expect(await token.getOwner()).to.be.deep.equal({Ethereum: sellerMirror});144145 // Ask146 {147 await helper.eth.sendEVM(seller, evmCollection.options.address, evmCollection.methods.approve(matcher.options.address, token.tokenId).encodeABI(), '0');148 await helper.eth.sendEVM(seller, matcher.options.address, matcher.methods.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, token.tokenId).encodeABI(), '0');149 }150151 // Token is transferred to matcher152 expect(await token.getOwner()).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});153154 // Give buyer KSM155 await matcher.methods.depositKSM(PRICE, aliceMirror).send({from: escrow});156157 // Buy158 {159 expect(await matcher.methods.balanceKSM(sellerMirror).call()).to.be.equal('0');160 expect(await matcher.methods.balanceKSM(aliceMirror).call()).to.be.equal(PRICE.toString());161162 await helper.eth.sendEVM(alice, matcher.options.address, matcher.methods.buyKSM(evmCollection.options.address, token.tokenId, aliceMirror, aliceMirror).encodeABI(), '0');163164 // Price is removed from buyer balance, and added to seller165 expect(await matcher.methods.balanceKSM(aliceMirror).call()).to.be.equal('0');166 expect(await matcher.methods.balanceKSM(sellerMirror).call()).to.be.equal(PRICE.toString());167 }168169 // Token is transferred to evm account of alice170 expect(await token.getOwner()).to.be.deep.equal({Ethereum: aliceMirror});171172 // Transfer token to substrate side of alice173 await token.transferFrom(alice, {Ethereum: aliceMirror}, {Substrate: alice.address});174175 // Token is transferred to substrate account of alice, seller received funds176 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});177 });178179 itEth('Sell tokens from substrate user via EVM contract', async ({helper, privateKey}) => {180 const web3 = helper.web3!;181182 const matcherOwner = await helper.eth.createAccountWithBalance(donor);183 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {184 from: matcherOwner,185 ...GAS_ARGS,186 });187 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments:[matcherOwner]}).send({from: matcherOwner});188 await helper.eth.transferBalanceFromSubstrate(donor, matcher.options.address);189190 const collection = await helper.nft.mintCollection(alice, {limits: {sponsorApproveTimeout: 1}});191 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collection.collectionId), {from: matcherOwner});192193 await helper.balance.transferToSubstrate(donor, seller.address, 100_000_000_000_000_000_000n);194 195 const token = await collection.mintToken(alice, {Ethereum: sellerMirror});196197 // Token is owned by seller initially198 expect(await token.getOwner()).to.be.deep.equal({Ethereum: sellerMirror});199200 // Ask201 {202 await helper.eth.sendEVM(seller, evmCollection.options.address, evmCollection.methods.approve(matcher.options.address, token.tokenId).encodeABI(), '0');203 await helper.eth.sendEVM(seller, matcher.options.address, matcher.methods.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, token.tokenId).encodeABI(), '0');204 }205206 // Token is transferred to matcher207 expect(await token.getOwner()).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});208209 // Buy210 {211 const sellerBalanceBeforePurchase = await helper.balance.getSubstrate(seller.address);212 await helper.eth.sendEVM(alice, matcher.options.address, matcher.methods.buy(evmCollection.options.address, token.tokenId).encodeABI(), PRICE.toString());213 expect(await helper.balance.getSubstrate(seller.address) - sellerBalanceBeforePurchase === PRICE);214 }215216 // Token is transferred to evm account of alice217 expect(await token.getOwner()).to.be.deep.equal({Ethereum: aliceMirror});218219 // Transfer token to substrate side of alice220 await token.transferFrom(alice, {Ethereum: aliceMirror}, {Substrate: alice.address});221222 // Token is transferred to substrate account of alice, seller received funds223 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});224 });225});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 {usingPlaygrounds} from './../../util/playgrounds/index';18import {IKeyringPair} from '@polkadot/types/types';19import {readFile} from 'fs/promises';20import {collectionIdToAddress, contractHelpers, GAS_ARGS, SponsoringMode} from '../util/helpers';21import nonFungibleAbi from '../nonFungibleAbi.json';2223import {itEth, expect} from '../util/playgrounds';2425const PRICE = 2000n;2627describe('Matcher contract usage', () => {28 let donor: IKeyringPair;29 let alice: IKeyringPair;30 let aliceMirror: string;31 let aliceDoubleMirror: string;32 let seller: IKeyringPair;33 let sellerMirror: string;3435 before(async () => {36 await usingPlaygrounds(async (_helper, privateKey) => {37 donor = privateKey('//Alice');38 }); 39 });4041 beforeEach(async () => {42 await usingPlaygrounds(async (helper, privateKey) => {43 [alice] = await helper.arrange.createAccounts([10000n], donor);44 aliceMirror = helper.address.substrateToEth(alice.address).toLowerCase();45 aliceDoubleMirror = helper.address.ethToSubstrate(aliceMirror);46 seller = privateKey(`//Seller/${Date.now()}`);47 sellerMirror = helper.address.substrateToEth(seller.address).toLowerCase();4849 await helper.balance.transferToSubstrate(donor, aliceDoubleMirror, 10_000_000_000_000_000_000n);50 });51 });5253 itEth('With UNQ', async ({helper}) => {54 const web3 = helper.getWeb3();55 const matcherOwner = await helper.eth.createAccountWithBalance(donor);56 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {57 from: matcherOwner,58 ...GAS_ARGS,59 });60 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments:[matcherOwner]}).send({from: matcherOwner});6162 const sponsor = await helper.eth.createAccountWithBalance(donor);63 const helpers = contractHelpers(web3, matcherOwner);64 await helpers.methods.setSponsoringMode(matcher.options.address, SponsoringMode.Allowlisted).send({from: matcherOwner});65 await helpers.methods.setSponsoringRateLimit(matcher.options.address, 1).send({from: matcherOwner});66 67 await helpers.methods.setSponsor(matcher.options.address, sponsor).send({from: matcherOwner});68 await helpers.methods.confirmSponsorship(matcher.options.address).send({from: sponsor});6970 const collection = await helper.nft.mintCollection(alice, {limits: {sponsorApproveTimeout: 1}, pendingSponsor: alice.address});71 await collection.confirmSponsorship(alice);72 await collection.addToAllowList(alice, {Substrate: aliceDoubleMirror});73 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collection.collectionId), {from: matcherOwner});74 await helper.eth.transferBalanceFromSubstrate(donor, aliceMirror);7576 await helpers.methods.toggleAllowed(matcher.options.address, aliceMirror, true).send({from: matcherOwner});77 await helpers.methods.toggleAllowed(matcher.options.address, sellerMirror, true).send({from: matcherOwner});7879 const token = await collection.mintToken(alice, {Ethereum: sellerMirror});8081 // Token is owned by seller initially82 expect(await token.getOwner()).to.be.deep.equal({Ethereum: sellerMirror});8384 // Ask85 {86 await helper.eth.sendEVM(seller, evmCollection.options.address, evmCollection.methods.approve(matcher.options.address, token.tokenId).encodeABI(), '0');87 await helper.eth.sendEVM(seller, matcher.options.address, matcher.methods.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, token.tokenId).encodeABI(), '0');88 }8990 // Token is transferred to matcher91 expect(await token.getOwner()).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});9293 // Buy94 {95 const sellerBalanceBeforePurchase = await helper.balance.getSubstrate(seller.address);96 await helper.eth.sendEVM(alice, matcher.options.address, matcher.methods.buy(evmCollection.options.address, token.tokenId).encodeABI(), PRICE.toString());97 expect(await helper.balance.getSubstrate(seller.address) - sellerBalanceBeforePurchase === PRICE);98 }99100 // Token is transferred to evm account of alice101 expect(await token.getOwner()).to.be.deep.equal({Ethereum: aliceMirror});102103 // Transfer token to substrate side of alice104 await token.transferFrom(alice, {Ethereum: aliceMirror}, {Substrate: alice.address});105106 // Token is transferred to substrate account of alice, seller received funds107 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});108 });109110 itEth('With escrow', async ({helper}) => {111 const web3 = helper.getWeb3();112 const matcherOwner = await helper.eth.createAccountWithBalance(donor);113 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {114 from: matcherOwner,115 ...GAS_ARGS,116 });117 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments: [matcherOwner]}).send({from: matcherOwner, gas: 10000000});118119 const sponsor = await helper.eth.createAccountWithBalance(donor);120 const escrow = await helper.eth.createAccountWithBalance(donor);121 await matcher.methods.setEscrow(escrow).send({from: matcherOwner});122 const helpers = contractHelpers(web3, matcherOwner);123 await helpers.methods.setSponsoringMode(matcher.options.address, SponsoringMode.Allowlisted).send({from: matcherOwner});124 await helpers.methods.setSponsoringRateLimit(matcher.options.address, 1).send({from: matcherOwner});125 126 await helpers.methods.setSponsor(matcher.options.address, sponsor).send({from: matcherOwner});127 await helpers.methods.confirmSponsorship(matcher.options.address).send({from: sponsor});128129 const collection = await helper.nft.mintCollection(alice, {limits: {sponsorApproveTimeout: 1}, pendingSponsor: alice.address});130 await collection.confirmSponsorship(alice);131 await collection.addToAllowList(alice, {Substrate: aliceDoubleMirror});132 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collection.collectionId), {from: matcherOwner});133 await helper.eth.transferBalanceFromSubstrate(donor, aliceMirror);134135136 await helpers.methods.toggleAllowed(matcher.options.address, aliceMirror, true).send({from: matcherOwner});137138 await helpers.methods.toggleAllowed(matcher.options.address, sellerMirror, true).send({from: matcherOwner});139140 const token = await collection.mintToken(alice, {Ethereum: sellerMirror});141142 // Token is owned by seller initially143 expect(await token.getOwner()).to.be.deep.equal({Ethereum: sellerMirror});144145 // Ask146 {147 await helper.eth.sendEVM(seller, evmCollection.options.address, evmCollection.methods.approve(matcher.options.address, token.tokenId).encodeABI(), '0');148 await helper.eth.sendEVM(seller, matcher.options.address, matcher.methods.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, token.tokenId).encodeABI(), '0');149 }150151 // Token is transferred to matcher152 expect(await token.getOwner()).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});153154 // Give buyer KSM155 await matcher.methods.depositKSM(PRICE, aliceMirror).send({from: escrow});156157 // Buy158 {159 expect(await matcher.methods.balanceKSM(sellerMirror).call()).to.be.equal('0');160 expect(await matcher.methods.balanceKSM(aliceMirror).call()).to.be.equal(PRICE.toString());161162 await helper.eth.sendEVM(alice, matcher.options.address, matcher.methods.buyKSM(evmCollection.options.address, token.tokenId, aliceMirror, aliceMirror).encodeABI(), '0');163164 // Price is removed from buyer balance, and added to seller165 expect(await matcher.methods.balanceKSM(aliceMirror).call()).to.be.equal('0');166 expect(await matcher.methods.balanceKSM(sellerMirror).call()).to.be.equal(PRICE.toString());167 }168169 // Token is transferred to evm account of alice170 expect(await token.getOwner()).to.be.deep.equal({Ethereum: aliceMirror});171172 // Transfer token to substrate side of alice173 await token.transferFrom(alice, {Ethereum: aliceMirror}, {Substrate: alice.address});174175 // Token is transferred to substrate account of alice, seller received funds176 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});177 });178179 itEth('Sell tokens from substrate user via EVM contract', async ({helper}) => {180 const web3 = helper.getWeb3();181 const matcherOwner = await helper.eth.createAccountWithBalance(donor);182 const matcherContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/MarketPlace.abi`)).toString()), undefined, {183 from: matcherOwner,184 ...GAS_ARGS,185 });186 const matcher = await matcherContract.deploy({data: (await readFile(`${__dirname}/MarketPlace.bin`)).toString(), arguments:[matcherOwner]}).send({from: matcherOwner});187188 await helper.eth.transferBalanceFromSubstrate(donor, matcher.options.address);189190 const collection = await helper.nft.mintCollection(alice, {limits: {sponsorApproveTimeout: 1}});191 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collection.collectionId), {from: matcherOwner});192193 await helper.balance.transferToSubstrate(donor, seller.address, 100_000_000_000_000_000_000n);194 195 const token = await collection.mintToken(alice, {Ethereum: sellerMirror});196197 // Token is owned by seller initially198 expect(await token.getOwner()).to.be.deep.equal({Ethereum: sellerMirror});199200 // Ask201 {202 await helper.eth.sendEVM(seller, evmCollection.options.address, evmCollection.methods.approve(matcher.options.address, token.tokenId).encodeABI(), '0');203 await helper.eth.sendEVM(seller, matcher.options.address, matcher.methods.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, token.tokenId).encodeABI(), '0');204 }205206 // Token is transferred to matcher207 expect(await token.getOwner()).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});208209 // Buy210 {211 const sellerBalanceBeforePurchase = await helper.balance.getSubstrate(seller.address);212 await helper.eth.sendEVM(alice, matcher.options.address, matcher.methods.buy(evmCollection.options.address, token.tokenId).encodeABI(), PRICE.toString());213 expect(await helper.balance.getSubstrate(seller.address) - sellerBalanceBeforePurchase === PRICE);214 }215216 // Token is transferred to evm account of alice217 expect(await token.getOwner()).to.be.deep.equal({Ethereum: aliceMirror});218219 // Transfer token to substrate side of alice220 await token.transferFrom(alice, {Ethereum: aliceMirror}, {Substrate: alice.address});221222 // Token is transferred to substrate account of alice, seller received funds223 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});224 });225});