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

difftreelog

feat balanceOfCross for fingible

Trubnikov Sergey2023-04-18parent: #ab8fc13.patch.diff
in: master

8 files changed

modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -327,6 +327,15 @@
 	fn collection_helper_address(&self) -> Result<Address> {
 		Ok(T::ContractAddress::get())
 	}
+
+	/// @notice Balance of account
+	/// @param owner An cross address for whom to query the balance
+	/// @return The number of fingibles owned by `owner`, possibly zero
+	fn balance_of_cross(&self, owner: CrossAddress) -> Result<U256> {
+		self.consume_store_reads(1)?;
+		let balance = <Balance<T>>::get((self.id, owner.into_sub_cross_account::<T>()?));
+		Ok(balance.into())
+	}
 }
 
 #[solidity_interface(
modifiedpallets/fungible/src/stubs/UniqueFungible.rawdiffbeforeafterboth

binary blob — no preview

modifiedpallets/fungible/src/stubs/UniqueFungible.soldiffbeforeafterboth
--- a/pallets/fungible/src/stubs/UniqueFungible.sol
+++ b/pallets/fungible/src/stubs/UniqueFungible.sol
@@ -511,7 +511,7 @@
 	bytes value;
 }
 
-/// @dev the ERC-165 identifier for this interface is 0x85d7dea6
+/// @dev the ERC-165 identifier for this interface is 0x69d14d3e
 contract ERC20UniqueExtensions is Dummy, ERC165 {
 	/// @dev Function to check the amount of tokens that an owner allowed to a spender.
 	/// @param owner crossAddress The address which owns the funds.
@@ -630,6 +630,18 @@
 		dummy;
 		return 0x0000000000000000000000000000000000000000;
 	}
+
+	/// @notice Balance of account
+	/// @param owner An cross address for whom to query the balance
+	/// @return The number of fingibles owned by `owner`, possibly zero
+	/// @dev EVM selector for this function is: 0xec069398,
+	///  or in textual repr: balanceOfCross((address,uint256))
+	function balanceOfCross(CrossAddress memory owner) public view returns (uint256) {
+		require(false, stub_error);
+		owner;
+		dummy;
+		return 0;
+	}
 }
 
 struct AmountForAddress {
modifiedtests/src/eth/abi/fungible.jsondiffbeforeafterboth
--- a/tests/src/eth/abi/fungible.json
+++ b/tests/src/eth/abi/fungible.json
@@ -181,6 +181,23 @@
           { "internalType": "uint256", "name": "sub", "type": "uint256" }
         ],
         "internalType": "struct CrossAddress",
+        "name": "owner",
+        "type": "tuple"
+      }
+    ],
+    "name": "balanceOfCross",
+    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "inputs": [
+      {
+        "components": [
+          { "internalType": "address", "name": "eth", "type": "address" },
+          { "internalType": "uint256", "name": "sub", "type": "uint256" }
+        ],
+        "internalType": "struct CrossAddress",
         "name": "from",
         "type": "tuple"
       },
modifiedtests/src/eth/api/UniqueFungible.soldiffbeforeafterboth
--- a/tests/src/eth/api/UniqueFungible.sol
+++ b/tests/src/eth/api/UniqueFungible.sol
@@ -353,7 +353,7 @@
 	bytes value;
 }
 
-/// @dev the ERC-165 identifier for this interface is 0x85d7dea6
+/// @dev the ERC-165 identifier for this interface is 0x69d14d3e
 interface ERC20UniqueExtensions is Dummy, ERC165 {
 	/// @dev Function to check the amount of tokens that an owner allowed to a spender.
 	/// @param owner crossAddress The address which owns the funds.
@@ -416,6 +416,13 @@
 	/// @dev EVM selector for this function is: 0x1896cce6,
 	///  or in textual repr: collectionHelperAddress()
 	function collectionHelperAddress() external view returns (address);
+
+	/// @notice Balance of account
+	/// @param owner An cross address for whom to query the balance
+	/// @return The number of fingibles owned by `owner`, possibly zero
+	/// @dev EVM selector for this function is: 0xec069398,
+	///  or in textual repr: balanceOfCross((address,uint256))
+	function balanceOfCross(CrossAddress memory owner) external view returns (uint256);
 }
 
 struct AmountForAddress {
modifiedtests/src/eth/fungible.test.tsdiffbeforeafterboth
--- a/tests/src/eth/fungible.test.ts
+++ b/tests/src/eth/fungible.test.ts
@@ -427,6 +427,22 @@
     const toBalanceAfter = await collection.getBalance({Ethereum: receiver});
     expect(toBalanceAfter - toBalanceBefore).to.be.eq(51n);
   });
+
+  itEth('Check balanceOfCross()', async ({helper}) => {
+    const collection = await helper.ft.mintCollection(alice, {});
+    const owner = await helper.ethCrossAccount.createAccountWithBalance(donor);
+    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+    const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner.eth);
+
+    expect(BigInt(await collectionEvm.methods.balanceOfCross(owner).call({from: owner.eth})) === 0n).to.be.true;
+
+    let sum = 0n;
+    for (let i = 1n; i < 100n; i++) {
+      await collection.mint(alice, 100n, {Ethereum: owner.eth});
+      sum += 100n;
+      expect(BigInt(await collectionEvm.methods.balanceOfCross(owner).call({from: owner.eth})) === sum).to.be.true;
+    }
+  });
 });
 
 describe('Fungible: Fees', () => {
modifiedtests/src/eth/reFungible.test.tsdiffbeforeafterboth
--- a/tests/src/eth/reFungible.test.ts
+++ b/tests/src/eth/reFungible.test.ts
@@ -585,7 +585,7 @@
     expect(event.returnValues.tokenId).to.equal(tokenId.toString());
   });
 
-  itEth('Check crossBalanceOf()', async ({helper}) => {
+  itEth('Check balanceOfCross()', async ({helper}) => {
     const collection = await helper.rft.mintCollection(minter, {});
     const owner = await helper.ethCrossAccount.createAccountWithBalance(donor);
     const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
modifiedtests/src/eth/tokens/minting.test.tsdiffbeforeafterboth
before · tests/src/eth/tokens/minting.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 {Pallets} from '../../util';19import {expect, itEth, usingEthPlaygrounds} from '../util';202122describe.only('Minting tokens', () => {23  let donor: IKeyringPair;24  let alice: IKeyringPair;2526  before(async function() {27    await usingEthPlaygrounds(async (helper, privateKey) => {28      donor = await privateKey({url: import.meta.url});29      [alice] = await helper.arrange.createAccounts([30n, 20n], donor);30    });31  });3233  [34    {mode: 'nft' as const, requiredPallets: []},35    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},36    {mode: 'ft' as const, requiredPallets: []},37  ].map(testCase => {38    itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mint() for Substrate collection`, testCase.requiredPallets, async ({helper}) => {39      const owner = await helper.eth.createAccountWithBalance(donor);40      const receiver = helper.eth.createAccount();41      const mintingParams = testCase.mode === 'ft' ? [receiver, 100] : [receiver];4243      const collection = await helper[testCase.mode].mintCollection(alice);44      await collection.addAdmin(alice, {Ethereum: owner});4546      const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);47      const contract = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);4849      const result = await contract.methods.mint(...mintingParams).send({from: owner});5051      // Check events:52      const event = result.events.Transfer;53      expect(event.address).to.equal(collectionAddress);54      expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');55      expect(event.returnValues.to).to.equal(receiver);56      if (testCase.mode === 'ft')57        expect(event.returnValues.value).to.equal('100');5859      // Check token exist:60      if(testCase.mode === 'ft') {61        expect(await helper.ft.getBalance(collection.collectionId, {Ethereum: receiver})).to.eq(100n);62      } else {63        const tokenId = event.returnValues.tokenId;64        expect(tokenId).to.be.equal('1');65        expect(await helper.collection.getLastTokenId(collection.collectionId)).to.eq(1);66        expect(await contract.methods.ownerOfCross(tokenId).call()).to.be.like([receiver, '0']);67      }68    });69  });7071  [72    {mode: 'nft' as const, requiredPallets: []},73    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},74    {mode: 'ft' as const, requiredPallets: []},75  ].map(testCase => {76    itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mint() for Ethereum collection`, testCase.requiredPallets, async ({helper}) => {77      const owner = await helper.eth.createAccountWithBalance(donor);78      const receiver = helper.eth.createAccount();79      const mintingParams = testCase.mode === 'ft' ? [receiver, 100] : [receiver];8081      const {collection, collectionAddress, collectionId} = await helper.eth.createCollection(testCase.mode, owner, 'Name', 'Desc', 'Prefix');8283      const result = await collection.methods.mint(...mintingParams).send({from: owner});8485      // Check events:86      const event = result.events.Transfer;87      expect(event.address).to.equal(collectionAddress);88      expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');89      expect(event.returnValues.to).to.equal(receiver);90      if (testCase.mode === 'ft')91        expect(event.returnValues.value).to.equal('100');9293      // Check token exist:94      if(testCase.mode === 'ft') {95        expect(await helper.ft.getBalance(collectionId, {Ethereum: receiver})).to.eq(100n);96      } else {97        const tokenId = event.returnValues.tokenId;98        expect(tokenId).to.be.equal('1');99        expect(await helper.collection.getLastTokenId(collectionId)).to.eq(1);100        expect(await collection.methods.ownerOfCross(tokenId).call()).to.be.like([receiver, '0']);101      }102    });103  });104105  [106    {mode: 'nft' as const, requiredPallets: []},107    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},108    {mode: 'ft' as const, requiredPallets: []},109  ].map(testCase => {110    itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mint() for Ethereum collection`, testCase.requiredPallets, async ({helper}) => {111      const owner = await helper.eth.createAccountWithBalance(donor);112      const receiver = helper.eth.createAccount();113      const mintingParams = testCase.mode === 'ft' ? [receiver, 100] : [receiver];114115      const {collection, collectionAddress, collectionId} = await helper.eth.createCollection(testCase.mode, owner, 'Name', 'Desc', 'Prefix');116117      const result = await collection.methods.mint(...mintingParams).send({from: owner});118119      // Check events:120      const event = result.events.Transfer;121      expect(event.address).to.equal(collectionAddress);122      expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');123      expect(event.returnValues.to).to.equal(receiver);124      if (testCase.mode === 'ft')125        expect(event.returnValues.value).to.equal('100');126127      // Check token exist:128      if(testCase.mode === 'ft') {129        expect(await helper.ft.getBalance(collectionId, {Ethereum: receiver})).to.eq(100n);130      } else {131        const tokenId = event.returnValues.tokenId;132        expect(tokenId).to.be.equal('1');133        expect(await helper.collection.getLastTokenId(collectionId)).to.eq(1);134        expect(await collection.methods.ownerOfCross(tokenId).call()).to.be.like([receiver, '0']);135      }136    });137  });138139  [140    {mode: 'nft' as const, requiredPallets: []},141    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},142  ].map(testCase => {143    itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mintWithTokenURI() for Ethereum collection`, testCase.requiredPallets, async ({helper}) => {144      const owner = await helper.eth.createAccountWithBalance(donor);145      const receiver = helper.eth.createAccount();146147      const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', '6', '6', '');148      const contract = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);149150      const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();151      const tokenId = result.events.Transfer.returnValues.tokenId;152      expect(tokenId).to.be.equal('1');153154      const event = result.events.Transfer;155      expect(event.address).to.be.equal(collectionAddress);156      expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');157      expect(event.returnValues.to).to.be.equal(receiver);158159      expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');160      expect(await contract.methods.ownerOfCross(tokenId).call()).to.be.like([receiver, '0']);161      // TODO: this wont work right now, need release 919000 first162      // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send();163      // const tokenUri = await contract.methods.tokenURI(nextTokenId).call();164      // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);165    });166  });167});