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(), arguments:[matcherOwner]}).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(), arguments:[escrow]}).send({from: matcherOwner});142143144 const ksmToken = 11;145146 const alice = privateKey('//Alice');147 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});148 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});149150 const seller = privateKey('//Bob');151152 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);153154 155 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});156 157 await transferBalanceExpectSuccess(api, seller, evmToAddress(subToEth(seller.address)), 10n ** 18n);158 await transferBalanceExpectSuccess(api, alice, evmToAddress(subToEth(alice.address)), 10n ** 18n);159160 161 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});162163 164 {165 await executeEthTxOnSub(api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));166 await executeEthTxOnSub(api, seller, matcher, m => m.addAsk(PRICE, ksmToken, evmCollection.options.address, tokenId, 1));167 }168169 170 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});171172 173 await matcher.methods.depositKSM(PRICE, subToEth(alice.address)).send({from: escrow});174175 176 {177 expect(await matcher.methods.escrowBalance(ksmToken, subToEth(seller.address)).call()).to.be.equal('0');178 expect(await matcher.methods.escrowBalance(ksmToken, subToEth(alice.address)).call()).to.be.equal(PRICE.toString());179180 await executeEthTxOnSub(api, alice, matcher, m => m.buyKSM(evmCollection.options.address, tokenId, subToEth(alice.address)));181182 183 expect(await matcher.methods.escrowBalance(ksmToken, subToEth(alice.address)).call()).to.be.equal('0');184 expect(await matcher.methods.escrowBalance(ksmToken, subToEth(seller.address)).call()).to.be.equal(PRICE.toString());185 }186187 188 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});189190 191 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});192193 194 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});195 });196});