1import {readFile} from 'fs/promises';2import {getBalanceSingle, transferBalanceExpectSuccess} from '../../substrate/get-balance';3import privateKey from '../../substrate/privateKey';4import {createCollectionExpectSuccess, createFungibleItemExpectSuccess, createItemExpectSuccess, getTokenOwner, transferExpectSuccess, transferFromExpectSuccess} from '../../util/helpers';5import {collectionIdToAddress, createEthAccountWithBalance, executeEthTxOnSub, GAS_ARGS, itWeb3, subToEth, subToEthLowercase} from '../util/helpers';6import {evmToAddress} from '@polkadot/util-crypto';7import nonFungibleAbi from '../nonFungibleAbi.json';8import fungibleAbi from '../fungibleAbi.json';9import {expect} from 'chai';1011const PRICE = 2000n;1213describe('Matcher contract usage', () => {14 itWeb3('With UNQ', async ({api, web3}) => {15 const matcherOwner = await createEthAccountWithBalance(api, web3);16 const matcherJSON = JSON.parse((await readFile(`${__dirname}/MarketPlace.json`)).toString());17 const matcherContract = new web3.eth.Contract(matcherJSON.abi, undefined, {18 from: matcherOwner,19 ...GAS_ARGS,20 });21 const matcher = await matcherContract.deploy({data: (matcherJSON.bytecode).toString()}).send({from: matcherOwner});2223 const alice = privateKey('//Alice');24 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});25 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});2627 const seller = privateKey('//Bob');2829 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);3031 32 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});33 34 await transferBalanceExpectSuccess(api, seller, evmToAddress(subToEth(seller.address)), 10n ** 18n);35 await transferBalanceExpectSuccess(api, alice, evmToAddress(subToEth(alice.address)), 10n ** 18n);3637 38 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});3940 41 {42 await executeEthTxOnSub(api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));43 await executeEthTxOnSub(api, seller, matcher, m => m.addAsk(PRICE, '0x0000000000000000000000000000000000000001', evmCollection.options.address, tokenId, 1));44 }4546 47 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});4849 50 {51 const sellerBalanceBeforePurchase = await getBalanceSingle(api, seller.address);52 53 await executeEthTxOnSub(api, alice, matcher, m => m['buy(address,uint256)'](evmCollection.options.address, tokenId), {value: PRICE});54 expect(await getBalanceSingle(api, seller.address) - sellerBalanceBeforePurchase === PRICE);55 }5657 58 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});596061 62 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});6364 65 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});66 });676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 itWeb3('With escrow', async ({api, web3}) => {134 const matcherOwner = await createEthAccountWithBalance(api, web3);135 const escrow = await createEthAccountWithBalance(api, web3);136 const matcherJSON = JSON.parse((await readFile(`${__dirname}/MarketPlace.json`)).toString());137 const matcherContract = new web3.eth.Contract(matcherJSON.abi, undefined, {138 from: matcherOwner,139 ...GAS_ARGS,140 });141 const matcher = await matcherContract.deploy({data: (matcherJSON.bytecode).toString()}).send({from: matcherOwner});142143 const ksmToken = 11;144145 const alice = privateKey('//Alice');146 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});147 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});148149 const seller = privateKey('//Bob');150151 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);152153 154 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});155 156 await transferBalanceExpectSuccess(api, seller, evmToAddress(subToEth(seller.address)), 10n ** 18n);157 await transferBalanceExpectSuccess(api, alice, evmToAddress(subToEth(alice.address)), 10n ** 18n);158159 160 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});161162 163 {164 await executeEthTxOnSub(api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));165 await executeEthTxOnSub(api, seller, matcher, m => m.addAsk(PRICE, ksmToken, evmCollection.options.address, tokenId, 1));166 }167168 169 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});170171 172 await matcher.methods.depositKSM(PRICE, subToEth(alice.address)).send({from: escrow});173174 175 {176 expect(await matcher.methods.escrowBalance(ksmToken, subToEth(seller.address)).call()).to.be.equal('0');177 expect(await matcher.methods.escrowBalance(ksmToken, subToEth(alice.address)).call()).to.be.equal(PRICE.toString());178179 await executeEthTxOnSub(api, alice, matcher, m => m.buyKSM(evmCollection.options.address, tokenId, subToEth(alice.address)));180181 182 expect(await matcher.methods.escrowBalance(ksmToken, subToEth(alice.address)).call()).to.be.equal('0');183 expect(await matcher.methods.escrowBalance(ksmToken, subToEth(seller.address)).call()).to.be.equal(PRICE.toString());184 }185186 187 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});188189 190 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});191192 193 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});194 });195});