1234567891011121314151617import {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 72 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});7374 75 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});7677 78 {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 84 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});8586 87 {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 94 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});9596 97 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});9899 100 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 138 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});139140 141 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});142143 144 {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 150 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});151152 153 await matcher.methods.depositKSM(PRICE, subToEth(alice.address)).send({from: escrow});154155 156 {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 163 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 168 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});169170 171 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});172173 174 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 198 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});199200 201 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});202203 204 {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 210 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});211212 213 {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 220 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});221222 223 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});224225 226 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});227 });228});