difftreelog
fixed deploy marketplace contract
in: master
1 file changed
tests/src/eth/marketplace/marketplaceNew.test.tsdiffbeforeafterboth1import {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 // To transfer item to matcher it first needs to be transfered to EVM account of bob32 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});33 // Fees will be paid from EVM account, so we should have some balance here34 await transferBalanceExpectSuccess(api, seller, evmToAddress(subToEth(seller.address)), 10n ** 18n);35 await transferBalanceExpectSuccess(api, alice, evmToAddress(subToEth(alice.address)), 10n ** 18n);3637 // Token is owned by seller initially38 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});3940 // Ask41 {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 // Token is transferred to matcher47 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});4849 // Buy50 {51 const sellerBalanceBeforePurchase = await getBalanceSingle(api, seller.address);52 // There is two functions named 'buy', so we should provide full signature53 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 // Token is transferred to evm account of alice58 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});596061 // Transfer token to substrate side of alice62 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});6364 // Token is transferred to substrate account of alice, seller received funds65 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});66 });67//selling for custom tokens excluded from release 68/* itWeb3('With custom ERC20', async ({api, web3}) => {69 const matcherOwner = await createEthAccountWithBalance(api, web3);70 const matcherJSON = JSON.parse((await readFile(`${__dirname}/MarketPlace.json`)).toString());71 const matcherContract = new web3.eth.Contract(matcherJSON.abi, undefined, {72 from: matcherOwner,73 ...GAS_ARGS,74 });75 const matcher = await matcherContract.deploy({data: (matcherJSON.bytecode).toString()}).send({from: matcherOwner});7677 const alice = privateKey('//Alice');78 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});79 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});8081 const fungibleId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});82 const evmFungible = new web3.eth.Contract(fungibleAbi as any, collectionIdToAddress(fungibleId), {from: matcherOwner, ...GAS_ARGS});83 await createFungibleItemExpectSuccess(alice, fungibleId, {Value: PRICE}, {Ethereum: subToEth(alice.address)});8485 const seller = privateKey('//Bob');8687 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);8889 // To transfer item to matcher it first needs to be transfered to EVM account of bob90 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});91 // Fees will be paid from EVM account, so we should have some balance here92 await transferBalanceExpectSuccess(api, seller, evmToAddress(subToEth(seller.address)), 10n ** 18n);93 await transferBalanceExpectSuccess(api, alice, evmToAddress(subToEth(alice.address)), 10n ** 18n);9495 // Token is owned by seller initially96 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});9798 // Ask99 {100 await executeEthTxOnSub(api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));101 await executeEthTxOnSub(api, seller, matcher, m => m.addAsk(PRICE, evmFungible.options.address, evmCollection.options.address, tokenId, 1));102 }103104 // Token is transferred to matcher105 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});106107 // Buy108 {109 const sellerBalanceBeforePurchase = BigInt(await evmFungible.methods.balanceOf(subToEth(seller.address)).call());110 const buyerBalanceBeforePurchase = BigInt(await evmFungible.methods.balanceOf(subToEth(alice.address)).call());111112 await executeEthTxOnSub(api, alice, evmFungible, m => m.approve(matcher.options.address, PRICE));113 // There is two functions named 'buy', so we should provide full signature114 await executeEthTxOnSub(api, alice, matcher, m =>115 m['buy(address,uint256,address,uint256)'](evmCollection.options.address, tokenId, evmFungible.options.address, PRICE));116117 // Approved price is removed from buyer balance, and added to seller118 expect(BigInt(await evmFungible.methods.balanceOf(subToEth(seller.address)).call()) - sellerBalanceBeforePurchase === PRICE);119 expect(buyerBalanceBeforePurchase - BigInt(await evmFungible.methods.balanceOf(subToEth(alice.address)).call()) === PRICE);120 }121122 // Token is transferred to evm account of alice123 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});124125126 // Transfer token to substrate side of alice127 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});128129 // Token is transferred to substrate account of alice130 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});131 }); */132133 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 // To transfer item to matcher it first needs to be transfered to EVM account of bob154 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});155 // Fees will be paid from EVM account, so we should have some balance here156 await transferBalanceExpectSuccess(api, seller, evmToAddress(subToEth(seller.address)), 10n ** 18n);157 await transferBalanceExpectSuccess(api, alice, evmToAddress(subToEth(alice.address)), 10n ** 18n);158159 // Token is owned by seller initially160 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});161162 // Ask163 {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 // Token is transferred to matcher169 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});170171 // Give buyer KSM172 await matcher.methods.depositKSM(PRICE, subToEth(alice.address)).send({from: escrow});173174 // Buy175 {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 // Price is removed from buyer balance, and added to seller182 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 // Token is transferred to evm account of alice187 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});188189 // Transfer token to substrate side of alice190 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});191192 // Token is transferred to substrate account of alice, seller received funds193 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});194 });195});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 // To transfer item to matcher it first needs to be transfered to EVM account of bob32 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});33 // Fees will be paid from EVM account, so we should have some balance here34 await transferBalanceExpectSuccess(api, seller, evmToAddress(subToEth(seller.address)), 10n ** 18n);35 await transferBalanceExpectSuccess(api, alice, evmToAddress(subToEth(alice.address)), 10n ** 18n);3637 // Token is owned by seller initially38 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});3940 // Ask41 {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 // Token is transferred to matcher47 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});4849 // Buy50 {51 const sellerBalanceBeforePurchase = await getBalanceSingle(api, seller.address);52 // There is two functions named 'buy', so we should provide full signature53 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 // Token is transferred to evm account of alice58 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});596061 // Transfer token to substrate side of alice62 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});6364 // Token is transferred to substrate account of alice, seller received funds65 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});66 });67//selling for custom tokens excluded from release 68/* itWeb3('With custom ERC20', async ({api, web3}) => {69 const matcherOwner = await createEthAccountWithBalance(api, web3);70 const matcherJSON = JSON.parse((await readFile(`${__dirname}/MarketPlace.json`)).toString());71 const matcherContract = new web3.eth.Contract(matcherJSON.abi, undefined, {72 from: matcherOwner,73 ...GAS_ARGS,74 });75 const matcher = await matcherContract.deploy({data: (matcherJSON.bytecode).toString()}).send({from: matcherOwner});7677 const alice = privateKey('//Alice');78 const collectionId = await createCollectionExpectSuccess({mode: {type: 'NFT'}});79 const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collectionId), {from: matcherOwner});8081 const fungibleId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});82 const evmFungible = new web3.eth.Contract(fungibleAbi as any, collectionIdToAddress(fungibleId), {from: matcherOwner, ...GAS_ARGS});83 await createFungibleItemExpectSuccess(alice, fungibleId, {Value: PRICE}, {Ethereum: subToEth(alice.address)});8485 const seller = privateKey('//Bob');8687 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', seller.address);8889 // To transfer item to matcher it first needs to be transfered to EVM account of bob90 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});91 // Fees will be paid from EVM account, so we should have some balance here92 await transferBalanceExpectSuccess(api, seller, evmToAddress(subToEth(seller.address)), 10n ** 18n);93 await transferBalanceExpectSuccess(api, alice, evmToAddress(subToEth(alice.address)), 10n ** 18n);9495 // Token is owned by seller initially96 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});9798 // Ask99 {100 await executeEthTxOnSub(api, seller, evmCollection, m => m.approve(matcher.options.address, tokenId));101 await executeEthTxOnSub(api, seller, matcher, m => m.addAsk(PRICE, evmFungible.options.address, evmCollection.options.address, tokenId, 1));102 }103104 // Token is transferred to matcher105 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});106107 // Buy108 {109 const sellerBalanceBeforePurchase = BigInt(await evmFungible.methods.balanceOf(subToEth(seller.address)).call());110 const buyerBalanceBeforePurchase = BigInt(await evmFungible.methods.balanceOf(subToEth(alice.address)).call());111112 await executeEthTxOnSub(api, alice, evmFungible, m => m.approve(matcher.options.address, PRICE));113 // There is two functions named 'buy', so we should provide full signature114 await executeEthTxOnSub(api, alice, matcher, m =>115 m['buy(address,uint256,address,uint256)'](evmCollection.options.address, tokenId, evmFungible.options.address, PRICE));116117 // Approved price is removed from buyer balance, and added to seller118 expect(BigInt(await evmFungible.methods.balanceOf(subToEth(seller.address)).call()) - sellerBalanceBeforePurchase === PRICE);119 expect(buyerBalanceBeforePurchase - BigInt(await evmFungible.methods.balanceOf(subToEth(alice.address)).call()) === PRICE);120 }121122 // Token is transferred to evm account of alice123 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});124125126 // Transfer token to substrate side of alice127 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});128129 // Token is transferred to substrate account of alice130 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});131 }); */132133 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 // To transfer item to matcher it first needs to be transfered to EVM account of bob155 await transferExpectSuccess(collectionId, tokenId, seller, {Ethereum: subToEth(seller.address)});156 // Fees will be paid from EVM account, so we should have some balance here157 await transferBalanceExpectSuccess(api, seller, evmToAddress(subToEth(seller.address)), 10n ** 18n);158 await transferBalanceExpectSuccess(api, alice, evmToAddress(subToEth(alice.address)), 10n ** 18n);159160 // Token is owned by seller initially161 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(seller.address)});162163 // Ask164 {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 // Token is transferred to matcher170 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: matcher.options.address.toLowerCase()});171172 // Give buyer KSM173 await matcher.methods.depositKSM(PRICE, subToEth(alice.address)).send({from: escrow});174175 // Buy176 {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 // Price is removed from buyer balance, and added to seller183 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 // Token is transferred to evm account of alice188 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Ethereum: subToEthLowercase(alice.address)});189190 // Transfer token to substrate side of alice191 await transferFromExpectSuccess(collectionId, tokenId, alice, {Ethereum: subToEth(alice.address)}, {Substrate: alice.address});192193 // Token is transferred to substrate account of alice, seller received funds194 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal({Substrate: alice.address});195 });196});