git.delta.rocks / unique-network / refs/commits / cfff3fbcb7c4

difftreelog

feat test with substrate address

Grigoriy Simonov2023-06-02parent: #1bda5f2.patch.diff
in: master

1 file changed

modifiedtests/src/eth/marketplace-v2/marketplace.test.tsdiffbeforeafterboth
19import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util';19import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util';
20import {makeNames} from '../../util';20import {makeNames} from '../../util';
21import {expect} from 'chai';21import {expect} from 'chai';
22import Web3 from 'web3';
2223
23const {dirname} = makeNames(import.meta.url);24const {dirname} = makeNames(import.meta.url);
2425
75 );76 );
76 }77 }
78
79 function substrateAddressToHex(sub: Uint8Array| string, web3: Web3) {
80 if (typeof sub === 'string')
81 return web3.utils.padLeft(web3.utils.toHex(web3.utils.toBN(sub)), 64);
82 else if (sub instanceof Uint8Array)
83 return web3.utils.padLeft(web3.utils.bytesToHex(Array.from(sub)), 64);
84 }
7785
78 itEth('Deploy', async ({helper}) => {86 itEth('Deploy', async ({helper}) => {
79 const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);87 const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);
8088
81 await deployMarket(helper, marketOwner);89 await deployMarket(helper, marketOwner);
82 });90 });
8391
84 itEth('Put + Buy', async ({helper}) => {92 itEth('Put + Buy [eth]', async ({helper}) => {
85 const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);93 const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);
86 const sellerCross = await helper.ethCrossAccount.createAccountWithBalance(donor, 600n);94 const market = await deployMarket(helper, marketOwner);
95
87 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');96 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');
88 const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner);97 const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner);
98
99 const sellerCross = await helper.ethCrossAccount.createAccountWithBalance(donor, 600n);
89 const result = await contract.methods.mint(sellerCross.eth).send();100 const result = await collection.methods.mintCross(sellerCross, []).send();
90 const tokenId = result.events.Transfer.returnValues.tokenId;101 const tokenId = result.events.Transfer.returnValues.tokenId;
91
92 const market = await deployMarket(helper, marketOwner);
93
94 await contract.methods.approve(market.options.address, tokenId).send({from: sellerCross.eth});102 await collection.methods.approve(market.options.address, tokenId).send({from: sellerCross.eth});
103
95 const putResult = await market.methods.put(collectionId, tokenId, 1, 1, sellerCross).send({from: sellerCross.eth});104 const putResult = await market.methods.put(collectionId, tokenId, 1, 1, sellerCross).send({from: sellerCross.eth});
96 expect(putResult.events.TokenIsUpForSale).is.not.undefined;105 expect(putResult.events.TokenIsUpForSale).is.not.undefined;
106 let ownerCross = await collection.methods.ownerOfCross(tokenId).call();
107 expect(ownerCross.eth).to.be.eq(sellerCross.eth);
108 expect(ownerCross.sub).to.be.eq(sellerCross.sub);
97109
98 const buyerCross = await helper.ethCrossAccount.createAccountWithBalance(donor, 600n);110 const buyerCross = await helper.ethCrossAccount.createAccountWithBalance(donor, 600n);
99 const buyResult = await market.methods.buy(collectionId, tokenId, 1, buyerCross).send({from: buyerCross.eth, value: 1});111 const buyResult = await market.methods.buy(collectionId, tokenId, 1, buyerCross).send({from: buyerCross.eth, value: 1});
100 expect(buyResult.events.TokenIsPurchased).is.not.undefined;112 expect(buyResult.events.TokenIsPurchased).is.not.undefined;
113 ownerCross = await collection.methods.ownerOfCross(tokenId).call();
114 expect(ownerCross.eth).to.be.eq(buyerCross.eth);
115 expect(ownerCross.sub).to.be.eq(buyerCross.sub);
101 });116 });
117
118 itEth('Put + Buy [sub]', async ({helper}) => {
119 const PRICE = 1n;
120 const web3 = helper.getWeb3();
121 const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);
122 const market = await deployMarket(helper, marketOwner);
123
124 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');
125 const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner);
126
127 const [seller] = await helper.arrange.createAccounts([600n], donor);
128 const sellerMirror = helper.address.substrateToEth(seller.address);
129 const sellerCross = helper.ethCrossAccount.fromKeyringPair(seller);
130 const result = await collection.methods.mintCross(sellerCross, []).send();
131 const tokenId = result.events.Transfer.returnValues.tokenId;
132 await helper.nft.approveToken(seller, collectionId, tokenId, {Ethereum: market.options.address}, 1n);
133
134 await helper.eth.sendEVM(seller, market.options.address, market.methods.put(collectionId, tokenId, PRICE, 1, sellerCross).encodeABI(), '0');
135 let ownerCross = await collection.methods.ownerOfCross(tokenId).call();
136 expect(ownerCross.eth).to.be.eq(sellerCross.eth);
137 expect(substrateAddressToHex(ownerCross.sub, web3)).to.be.eq(substrateAddressToHex(sellerCross.sub, web3));
138
139 const [buyer] = await helper.arrange.createAccounts([600n], donor);
140 const buyerMirror = helper.address.substrateToEth(buyer.address);
141 const buyerCross = helper.ethCrossAccount.fromKeyringPair(buyer);
142 await helper.eth.transferBalanceFromSubstrate(donor, buyerMirror, 1n);
143 //TODO: change balance check to helper.balance.getSubstrate when implementation of sendMoney will be fixed in contract
144 const sellerBalance = BigInt(await web3.eth.getBalance(sellerMirror));
145 await helper.eth.sendEVM(buyer, market.options.address, market.methods.buy(collectionId, tokenId, 1, buyerCross).encodeABI(), PRICE.toString());
146 const sellerBalanceAfterBuy = BigInt(await web3.eth.getBalance(sellerMirror));
147 ownerCross = await collection.methods.ownerOfCross(tokenId).call();
148 expect(ownerCross.eth).to.be.eq(buyerCross.eth);
149 expect(substrateAddressToHex(ownerCross.sub, web3)).to.be.eq(substrateAddressToHex(buyerCross.sub, web3));
150 expect(sellerBalance + PRICE).to.be.equal(sellerBalanceAfterBuy);
151 });
102});152});
103153