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

difftreelog

Update market + substrate sponsoring example

Andy Smith2023-06-27parent: #8196877.patch.diff
in: master

2 files changed

modifiedtests/src/eth/marketplace-v2/Market.soldiffbeforeafterboth
--- a/tests/src/eth/marketplace-v2/Market.sol
+++ b/tests/src/eth/marketplace-v2/Market.sol
@@ -87,6 +87,18 @@
         }
     }
 
+    /**
+     * Fallback that allows this contract to receive native token.
+     * We need this for self-sponsoring
+     */
+    fallback() external payable {}
+
+    /**
+     * Receive also allows this contract to receive native token.
+     * We need this for self-sponsoring
+     */
+    receive() external payable {}
+
     function getErc721(uint32 collectionId) private view returns (IERC721) {
         address collectionAddress = collectionHelpers.collectionAddress(
             collectionId
modifiedtests/src/eth/marketplace-v2/marketplace.test.tsdiffbeforeafterboth
2323
24const {dirname} = makeNames(import.meta.url);24const {dirname} = makeNames(import.meta.url);
25
26const MARKET_FEE = 1;
2527
26describe('Market V2 Contract', () => {28describe('Market V2 Contract', () => {
27 let donor: IKeyringPair;29 let donor: IKeyringPair;
88 },90 },
89 ],91 ],
90 15000000,92 15000000,
91 [1, 0],93 [MARKET_FEE, 0],
92 );94 );
93 }95 }
9496
111 const marketOwner = await helper.eth.createAccountWithBalance(donor, 60000n);113 const marketOwner = await helper.eth.createAccountWithBalance(donor, 60000n);
112 const market = await deployMarket(helper, marketOwner);114 const market = await deployMarket(helper, marketOwner);
113 const contractHelpers = helper.ethNativeContract.contractHelpers(marketOwner);115 const contractHelpers = helper.ethNativeContract.contractHelpers(marketOwner);
114 await contractHelpers.methods.selfSponsoredEnable(market.options.address).send({from: marketOwner});116
115 await helper.eth.transferBalanceFromSubstrate(donor, market.options.address, 10n);117 // Set external sponsoring
116
117 // TODO: this should work too, instead of selfSponsoring!
118 await contractHelpers.methods.setSponsor(market.options.address, marketOwner).send({from: marketOwner});118 await contractHelpers.methods.setSponsor(market.options.address, marketOwner).send({from: marketOwner});
119 await contractHelpers.methods.confirmSponsorship(market.options.address).send({from: marketOwner});119 await contractHelpers.methods.confirmSponsorship(market.options.address).send({from: marketOwner});
120120
121 // Configure sponsoring
121 await contractHelpers.methods.setSponsoringMode(market.options.address, SponsoringMode.Generous).send({from: marketOwner});122 await contractHelpers.methods.setSponsoringMode(market.options.address, SponsoringMode.Generous).send({from: marketOwner});
122 await contractHelpers.methods.setSponsoringRateLimit(market.options.address, 0).send({from: marketOwner});123 await contractHelpers.methods.setSponsoringRateLimit(market.options.address, 0).send({from: marketOwner});
123124
124 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');125 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');
125 const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner, true);126 const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner, true);
127
128 // Set collection sponsoring
126 await collection.methods.setCollectionSponsor(marketOwner).send({from: marketOwner});129 await collection.methods.setCollectionSponsor(marketOwner).send({from: marketOwner});
127 await collection.methods.confirmCollectionSponsorship().send({from: marketOwner});130 await collection.methods.confirmCollectionSponsorship().send({from: marketOwner});
128131
153156
154 // Buyer has only 10 UNQ157 // Buyer has only 10 UNQ
155 const buyerBalance = await helper.balance.getEthereum(buyerCross.eth);158 const buyerBalance = await helper.balance.getEthereum(buyerCross.eth);
156 expect(buyerBalance).to.be.eq(10n * ONE_TOKEN)159 expect(buyerBalance).to.be.eq(10n * ONE_TOKEN);
157160
158 const buyResult = await market.methods.buy(collectionId, tokenId, 1, buyerCross).send({from: buyerCross.eth, value: PRICE.toString(), gasLimit: 1_000_000});161 const buyResult = await market.methods.buy(collectionId, tokenId, 1, buyerCross).send({from: buyerCross.eth, value: PRICE.toString(), gasLimit: 1_000_000});
159 expect(buyResult.events.TokenIsPurchased).is.not.undefined;162 expect(buyResult.events.TokenIsPurchased).is.not.undefined;
168 });171 });
169172
170 itEth('Put + Buy [sub]', async ({helper}) => {173 itEth('Put + Buy [sub]', async ({helper}) => {
174 const ONE_TOKEN = helper.balance.getOneTokenNominal();
171 const PRICE = 1n;175 const PRICE = 2n * ONE_TOKEN; // 2 UNQ
172 const web3 = helper.getWeb3();176 const web3 = helper.getWeb3();
173 const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);177 const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);
174 const market = await deployMarket(helper, marketOwner);178 const market = await deployMarket(helper, marketOwner);
179 const contractHelpers = helper.ethNativeContract.contractHelpers(marketOwner);
180
181 // Set self sponsoring from contract balance
182 await contractHelpers.methods.selfSponsoredEnable(market.options.address).send({from: marketOwner});
183 await helper.eth.transferBalanceFromSubstrate(donor, market.options.address, 10n);
184
185 // Configure sponsoring
186 await contractHelpers.methods.setSponsoringMode(market.options.address, SponsoringMode.Generous).send({from: marketOwner});
187 await contractHelpers.methods.setSponsoringRateLimit(market.options.address, 0).send({from: marketOwner});
175188
176 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');189 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');
177 const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner);190 const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner, true);
191
192 // Set collection sponsoring
193 await collection.methods.setCollectionSponsor(marketOwner).send({from: marketOwner});
194 await collection.methods.confirmCollectionSponsorship().send({from: marketOwner});
178195
179 const [seller] = await helper.arrange.createAccounts([600n], donor);196 const seller = helper.util.fromSeed(`//Market-seller-${(new Date()).getTime()}`);
180 const sellerCross = helper.ethCrossAccount.fromKeyringPair(seller);197 const sellerCross = helper.ethCrossAccount.fromKeyringPair(seller);
198
199 // Seller has no funds at all, his transactions are sponsored
200 {
201 const sellerBalance = await helper.balance.getSubstrate(seller.address);
202 expect(sellerBalance).to.be.eq(0n);
203 }
204
181 const result = await collection.methods.mintCross(sellerCross, []).send();205 const result = await collection.methods.mintCross(sellerCross, []).send();
182 const tokenId = result.events.Transfer.returnValues.tokenId;206 const tokenId = result.events.Transfer.returnValues.tokenId;
183 await helper.nft.approveToken(seller, collectionId, tokenId, {Ethereum: market.options.address}, 1n);207 await helper.nft.approveToken(seller, collectionId, tokenId, {Ethereum: market.options.address});
184208
185 await helper.eth.sendEVM(seller, market.options.address, market.methods.put(collectionId, tokenId, PRICE, 1, sellerCross).encodeABI(), '0');209 await helper.eth.sendEVM(seller, market.options.address, market.methods.put(collectionId, tokenId, PRICE, 1, sellerCross).encodeABI(), '0');
210 // Seller balance is still zero
211 {
212 const sellerBalance = await helper.balance.getSubstrate(seller.address);
213 expect(sellerBalance).to.be.eq(0n);
214 }
186 let ownerCross = await collection.methods.ownerOfCross(tokenId).call();215 let ownerCross = await collection.methods.ownerOfCross(tokenId).call();
187 expect(ownerCross.eth).to.be.eq(sellerCross.eth);216 expect(ownerCross.eth).to.be.eq(sellerCross.eth);
188 expect(substrateAddressToHex(ownerCross.sub, web3)).to.be.eq(substrateAddressToHex(sellerCross.sub, web3));217 expect(substrateAddressToHex(ownerCross.sub, web3)).to.be.eq(substrateAddressToHex(sellerCross.sub, web3));
189218
190 const [buyer] = await helper.arrange.createAccounts([600n], donor);219 const [buyer] = await helper.arrange.createAccounts([600n], donor);
220 // Buyer has only expected balance
221 {
222 const buyerBalance = await helper.balance.getSubstrate(buyer.address);
223 expect(buyerBalance).to.be.eq(600n * ONE_TOKEN);
224 }
191 const buyerMirror = helper.address.substrateToEth(buyer.address);225 const buyerMirror = helper.address.substrateToEth(buyer.address);
192 const buyerCross = helper.ethCrossAccount.fromKeyringPair(buyer);226 const buyerCross = helper.ethCrossAccount.fromKeyringPair(buyer);
193 await helper.eth.transferBalanceFromSubstrate(donor, buyerMirror, 1n);227 await helper.eth.transferBalanceFromSubstrate(donor, buyerMirror, PRICE, false);
228
194 const sellerBalance = BigInt(await helper.balance.getSubstrate(seller.address));229 const buyerBalanceBefore = await helper.balance.getSubstrate(buyer.address);
195 await helper.eth.sendEVM(buyer, market.options.address, market.methods.buy(collectionId, tokenId, 1, buyerCross).encodeABI(), PRICE.toString());230 await helper.eth.sendEVM(buyer, market.options.address, market.methods.buy(collectionId, tokenId, 1, buyerCross).encodeABI(), PRICE.toString());
231 const buyerBalanceAfter = await helper.balance.getSubstrate(buyer.address);
232 // Buyer balance not changed: transaction is sponsored
233 expect(buyerBalanceBefore).to.be.eq(buyerBalanceAfter);
234
196 const sellerBalanceAfterBuy = BigInt(await helper.balance.getSubstrate(seller.address));235 const sellerBalanceAfterBuy = BigInt(await helper.balance.getSubstrate(seller.address));
197 ownerCross = await collection.methods.ownerOfCross(tokenId).call();236 ownerCross = await collection.methods.ownerOfCross(tokenId).call();
198 expect(ownerCross.eth).to.be.eq(buyerCross.eth);237 expect(ownerCross.eth).to.be.eq(buyerCross.eth);
199 expect(substrateAddressToHex(ownerCross.sub, web3)).to.be.eq(substrateAddressToHex(buyerCross.sub, web3));238 expect(substrateAddressToHex(ownerCross.sub, web3)).to.be.eq(substrateAddressToHex(buyerCross.sub, web3));
239
240 // Seller got only PRICE - MARKET_FEE
200 expect(sellerBalance + PRICE).to.be.equal(sellerBalanceAfterBuy);241 expect(sellerBalanceAfterBuy).to.be.eq(PRICE * BigInt(100 - MARKET_FEE) / 100n);
201 });242 });
202});243});
203244