git.delta.rocks / unique-network / refs/commits / 71175e37f6f9

difftreelog

feat update MarketV2 contract

Grigoriy Simonov2023-06-16parent: #e1f20e1.patch.diff
in: master

3 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
@@ -4,6 +4,7 @@
 import "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
 import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
 import { UniqueNFT, CrossAddress } from "@unique-nft/solidity-interfaces/contracts/UniqueNFT.sol";
+import { UniqueFungible, CrossAddress as CrossAddressF } from "@unique-nft/solidity-interfaces/contracts/UniqueFungible.sol";
 import "@unique-nft/solidity-interfaces/contracts/CollectionHelpers.sol";
 import "./royalty/UniqueRoyaltyHelper.sol";
 
@@ -20,6 +21,7 @@
     }
 
     uint32 public constant version = 0;
+    uint32 public constant buildVersion = 1;
     bytes4 private constant InterfaceId_ERC721 = 0x80ac58cd;
     bytes4 private constant InterfaceId_ERC165 = 0x5755c3f2;
     CollectionHelpers private constant collectionHelpers =
@@ -251,7 +253,7 @@
 
         IERC721 erc721 = getErc721(collectionId);
 
-        if (erc721.getApproved(tokenId) != selfAddress) {
+        if (erc721.getApproved(tokenId) != selfAddress || erc721.ownerOf(tokenId) != getAddressFromCrossAccount(order.seller)) {
           uint32 amount = order.amount;
           order.amount = 0;
           emit TokenRevoke(version, order, amount);
@@ -262,6 +264,27 @@
         }
     }
 
+    function getAddressFromCrossAccount(CrossAddress memory account) private pure returns (address) {
+        if (account.eth != address(0)) {
+            return account.eth;
+        } else {
+            return address(uint160(account.sub >> 96));
+        }
+    }
+
+    function revokeAdmin(uint32 collectionId, uint32 tokenId) public onlyAdmin {
+        Order memory order = orders[collectionId][tokenId];
+        if (order.price == 0) {
+          revert OrderNotFound();
+        }
+
+        uint32 amount = order.amount;
+        order.amount = 0;
+        emit TokenRevoke(version, order, amount);
+
+        delete orders[collectionId][tokenId];
+    }
+
     // ################################################################
     // Buy a token                                                    #
     // ################################################################
@@ -329,13 +352,14 @@
     }
 
     function sendMoney(CrossAddress memory to, uint256 money) private {
-      address payable eth;
-      if (to.eth != address(0)) {
-        eth = payable(to.eth);
-      } else {
-        eth = payable(address(uint160(to.sub >> 96)));
-      }
-      eth.transfer(money);
+      address collectionAddress = collectionHelpers.collectionAddress(0);
+
+      UniqueFungible fungible = UniqueFungible(collectionAddress);
+
+      CrossAddressF memory fromF = CrossAddressF(selfAddress, 0);
+      CrossAddressF memory toF = CrossAddressF(to.eth, to.sub);
+
+      fungible.transferFromCross(fromF, toF, money);
     }
 
     function sendRoyalties(address collection, uint tokenId, uint sellPrice) private returns (uint256, RoyaltyAmount[] memory) {
modifiedtests/src/eth/marketplace-v2/marketplace.test.tsdiffbeforeafterboth
before · tests/src/eth/marketplace-v2/marketplace.test.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {readFile} from 'fs/promises';19import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util';20import {makeNames} from '../../util';21import {expect} from 'chai';22import Web3 from 'web3';2324const {dirname} = makeNames(import.meta.url);2526describe('Market V2 Contract', () => {27  let donor: IKeyringPair;2829  before(async () => {30    await usingEthPlaygrounds(async (_helper, privateKey) => {31      donor = await privateKey({url: import.meta.url});32    });33  });3435  async function deployMarket(helper: EthUniqueHelper, marketOwner: string) {36    return await helper.ethContract.deployByCode(37      marketOwner,38      'Market',39      (await readFile(`${dirname}/Market.sol`)).toString(),40      [41        {42          solPath: '@unique-nft/solidity-interfaces/contracts/UniqueNFT.sol',43          fsPath: `${dirname}/../api/UniqueNFT.sol`,44        },45        {46          solPath: '@openzeppelin/contracts/utils/introspection/IERC165.sol',47          fsPath: `${dirname}/../../../node_modules/@openzeppelin/contracts/utils/introspection/IERC165.sol`,48        },49        {50          solPath: '@openzeppelin/contracts/utils/introspection/ERC165Checker.sol',51          fsPath: `${dirname}/../../../node_modules/@openzeppelin/contracts/utils/introspection/ERC165Checker.sol`,52        },53        {54          solPath: '@openzeppelin/contracts/token/ERC721/IERC721.sol',55          fsPath: `${dirname}/../../../node_modules/@openzeppelin/contracts/token/ERC721/IERC721.sol`,56        },57        {58          solPath: '@unique-nft/solidity-interfaces/contracts/CollectionHelpers.sol',59          fsPath: `${dirname}/../api/CollectionHelpers.sol`,60        },61        {62          solPath: 'royalty/UniqueRoyaltyHelper.sol',63          fsPath: `${dirname}/royalty/UniqueRoyaltyHelper.sol`,64        },65        {66          solPath: 'royalty/UniqueRoyalty.sol',67          fsPath: `${dirname}/royalty/UniqueRoyalty.sol`,68        },69        {70          solPath: 'royalty/LibPart.sol',71          fsPath: `${dirname}/royalty/LibPart.sol`,72        },73      ],74      15000000,75      [1, 0],76    );77  }7879  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  }8586  itEth('Deploy', async ({helper}) => {87    const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);8889    await deployMarket(helper, marketOwner);90  });9192  itEth('Put + Buy [eth]', async ({helper}) => {93    const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);94    const market = await deployMarket(helper, marketOwner);9596    const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');97    const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner);9899    const sellerCross = await helper.ethCrossAccount.createAccountWithBalance(donor, 600n);100    const result = await collection.methods.mintCross(sellerCross, []).send();101    const tokenId = result.events.Transfer.returnValues.tokenId;102    await collection.methods.approve(market.options.address, tokenId).send({from: sellerCross.eth});103104    const putResult = await market.methods.put(collectionId, tokenId, 1, 1, sellerCross).send({from: sellerCross.eth});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);109110    const buyerCross = await helper.ethCrossAccount.createAccountWithBalance(donor, 600n);111    const buyResult = await market.methods.buy(collectionId, tokenId, 1, buyerCross).send({from: buyerCross.eth, value: 1});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);116  });117118  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);123124    const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');125    const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner);126127    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);133134    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));138139    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 contract144    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  });152});
after · tests/src/eth/marketplace-v2/marketplace.test.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {readFile} from 'fs/promises';19import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util';20import {makeNames} from '../../util';21import {expect} from 'chai';22import Web3 from 'web3';2324const {dirname} = makeNames(import.meta.url);2526describe('Market V2 Contract', () => {27  let donor: IKeyringPair;2829  before(async () => {30    await usingEthPlaygrounds(async (_helper, privateKey) => {31      donor = await privateKey({url: import.meta.url});32    });33  });3435  async function deployMarket(helper: EthUniqueHelper, marketOwner: string) {36    return await helper.ethContract.deployByCode(37      marketOwner,38      'Market',39      (await readFile(`${dirname}/Market.sol`)).toString(),40      [41        {42          solPath: '@unique-nft/solidity-interfaces/contracts/UniqueNFT.sol',43          fsPath: `${dirname}/../api/UniqueNFT.sol`,44        },45        {46          solPath: '@unique-nft/solidity-interfaces/contracts/UniqueFungible.sol',47          fsPath: `${dirname}/../api/UniqueFungible.sol`,48        },49        {50          solPath: '@openzeppelin/contracts/utils/introspection/IERC165.sol',51          fsPath: `${dirname}/../../../node_modules/@openzeppelin/contracts/utils/introspection/IERC165.sol`,52        },53        {54          solPath: '@openzeppelin/contracts/utils/introspection/ERC165Checker.sol',55          fsPath: `${dirname}/../../../node_modules/@openzeppelin/contracts/utils/introspection/ERC165Checker.sol`,56        },57        {58          solPath: '@openzeppelin/contracts/token/ERC721/IERC721.sol',59          fsPath: `${dirname}/../../../node_modules/@openzeppelin/contracts/token/ERC721/IERC721.sol`,60        },61        {62          solPath: '@unique-nft/solidity-interfaces/contracts/CollectionHelpers.sol',63          fsPath: `${dirname}/../api/CollectionHelpers.sol`,64        },65        {66          solPath: 'royalty/UniqueRoyaltyHelper.sol',67          fsPath: `${dirname}/royalty/UniqueRoyaltyHelper.sol`,68        },69        {70          solPath: 'royalty/UniqueRoyalty.sol',71          fsPath: `${dirname}/royalty/UniqueRoyalty.sol`,72        },73        {74          solPath: 'royalty/LibPart.sol',75          fsPath: `${dirname}/royalty/LibPart.sol`,76        },77      ],78      15000000,79      [1, 0],80    );81  }8283  function substrateAddressToHex(sub: Uint8Array| string, web3: Web3) {84    if(typeof sub === 'string')85      return web3.utils.padLeft(web3.utils.toHex(web3.utils.toBN(sub)), 64);86    else if(sub instanceof Uint8Array)87      return web3.utils.padLeft(web3.utils.bytesToHex(Array.from(sub)), 64);88  }8990  itEth('Deploy', async ({helper}) => {91    const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);9293    await deployMarket(helper, marketOwner);94  });9596  itEth('Put + Buy [eth]', async ({helper}) => {97    const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);98    const market = await deployMarket(helper, marketOwner);99100    const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');101    const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner);102103    const sellerCross = await helper.ethCrossAccount.createAccountWithBalance(donor, 600n);104    const result = await collection.methods.mintCross(sellerCross, []).send();105    const tokenId = result.events.Transfer.returnValues.tokenId;106    await collection.methods.approve(market.options.address, tokenId).send({from: sellerCross.eth});107108    const putResult = await market.methods.put(collectionId, tokenId, 1, 1, sellerCross).send({from: sellerCross.eth});109    expect(putResult.events.TokenIsUpForSale).is.not.undefined;110    let ownerCross = await collection.methods.ownerOfCross(tokenId).call();111    expect(ownerCross.eth).to.be.eq(sellerCross.eth);112    expect(ownerCross.sub).to.be.eq(sellerCross.sub);113114    const buyerCross = await helper.ethCrossAccount.createAccountWithBalance(donor, 600n);115    const buyResult = await market.methods.buy(collectionId, tokenId, 1, buyerCross).send({from: buyerCross.eth, value: 1});116    expect(buyResult.events.TokenIsPurchased).is.not.undefined;117    ownerCross = await collection.methods.ownerOfCross(tokenId).call();118    expect(ownerCross.eth).to.be.eq(buyerCross.eth);119    expect(ownerCross.sub).to.be.eq(buyerCross.sub);120  });121122  itEth('Put + Buy [sub]', async ({helper}) => {123    const PRICE = 1n;124    const web3 = helper.getWeb3();125    const marketOwner = await helper.eth.createAccountWithBalance(donor, 600n);126    const market = await deployMarket(helper, marketOwner);127128    const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(marketOwner, 'Sponsor', 'absolutely anything', 'ROC');129    const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', marketOwner);130131    const [seller] = await helper.arrange.createAccounts([600n], donor);132    const sellerCross = helper.ethCrossAccount.fromKeyringPair(seller);133    const result = await collection.methods.mintCross(sellerCross, []).send();134    const tokenId = result.events.Transfer.returnValues.tokenId;135    await helper.nft.approveToken(seller, collectionId, tokenId, {Ethereum: market.options.address}, 1n);136137    await helper.eth.sendEVM(seller, market.options.address, market.methods.put(collectionId, tokenId, PRICE, 1, sellerCross).encodeABI(), '0');138    let ownerCross = await collection.methods.ownerOfCross(tokenId).call();139    expect(ownerCross.eth).to.be.eq(sellerCross.eth);140    expect(substrateAddressToHex(ownerCross.sub, web3)).to.be.eq(substrateAddressToHex(sellerCross.sub, web3));141142    const [buyer] = await helper.arrange.createAccounts([600n], donor);143    const buyerMirror = helper.address.substrateToEth(buyer.address);144    const buyerCross = helper.ethCrossAccount.fromKeyringPair(buyer);145    await helper.eth.transferBalanceFromSubstrate(donor, buyerMirror, 1n);146    const sellerBalance = BigInt(await helper.balance.getSubstrate(seller.address));147    await helper.eth.sendEVM(buyer, market.options.address, market.methods.buy(collectionId, tokenId, 1, buyerCross).encodeABI(), PRICE.toString());148    const sellerBalanceAfterBuy = BigInt(await helper.balance.getSubstrate(seller.address));149    ownerCross = await collection.methods.ownerOfCross(tokenId).call();150    expect(ownerCross.eth).to.be.eq(buyerCross.eth);151    expect(substrateAddressToHex(ownerCross.sub, web3)).to.be.eq(substrateAddressToHex(buyerCross.sub, web3));152    expect(sellerBalance + PRICE).to.be.equal(sellerBalanceAfterBuy);153  });154});
modifiedtests/src/eth/tokenProperties.test.tsdiffbeforeafterboth
--- a/tests/src/eth/tokenProperties.test.ts
+++ b/tests/src/eth/tokenProperties.test.ts
@@ -561,10 +561,10 @@
     itEth.ifWithPallets(`[${testCase.mode}] Can't be multiple set/read for non-existent token`, testCase.requiredPallets, async({helper}) => {
       const caller = await helper.eth.createAccountWithBalance(donor);
 
-      const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });
-      const permissions: ITokenPropertyPermission[] = properties.map(p => { return {key: p.key, permission: {tokenOwner: true,
+      const properties = Array(5).fill(0).map((_, i) => ({key: `key_${i}`, value: Buffer.from(`value_${i}`)}));
+      const permissions: ITokenPropertyPermission[] = properties.map(p => ({key: p.key, permission: {tokenOwner: true,
         collectionAdmin: true,
-        mutable: true}}; });
+        mutable: true}}));
 
       const collection = await helper[testCase.mode].mintCollection(alice, {
         tokenPrefix: 'ethp',