From 9871bd2fd60599834b15223ca91a96808bda6a05 Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Tue, 18 Apr 2023 06:02:41 +0000 Subject: [PATCH] feat: balanceOfCross for RFT token --- --- a/pallets/refungible/src/erc_token.rs +++ b/pallets/refungible/src/erc_token.rs @@ -283,6 +283,16 @@ .map_err(dispatch_to_evm::)?; Ok(true) } + + /// @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 { + self.consume_store_reads(1)?; + let balance = >::get((self.id, self.1, owner.into_sub_cross_account::()?)); + Ok(balance.into()) + } + /// @dev Function that changes total amount of the tokens. /// Throws if `msg.sender` doesn't owns all of the tokens. /// @param amount New total amount of the tokens. --- a/pallets/refungible/src/stubs/UniqueRefungibleToken.sol +++ b/pallets/refungible/src/stubs/UniqueRefungibleToken.sol @@ -36,7 +36,7 @@ } } -/// @dev the ERC-165 identifier for this interface is 0x01d536fc +/// @dev the ERC-165 identifier for this interface is 0xedd3a564 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. @@ -97,6 +97,18 @@ return false; } + /// @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; + } + /// @dev Function that changes total amount of the tokens. /// Throws if `msg.sender` doesn't owns all of the tokens. /// @param amount New total amount of the tokens. --- a/runtime/common/ethereum/sponsoring/refungible.rs +++ b/runtime/common/ethereum/sponsoring/refungible.rs @@ -343,9 +343,11 @@ ERC165Call(_, _) => None, // Not sponsored - AllowanceCross { .. } | BurnFrom { .. } | BurnFromCross { .. } | Repartition { .. } => { - None - } + AllowanceCross { .. } + | BalanceOfCross { .. } + | BurnFrom { .. } + | BurnFromCross { .. } + | Repartition { .. } => None, TransferCross { .. } | TransferFromCross { .. } => { let RefungibleTokenHandle(handle, token_id) = token; --- a/tests/src/eth/abi/reFungibleToken.json +++ b/tests/src/eth/abi/reFungibleToken.json @@ -130,6 +130,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" }, --- a/tests/src/eth/api/UniqueRefungibleToken.sol +++ b/tests/src/eth/api/UniqueRefungibleToken.sol @@ -23,7 +23,7 @@ function parentTokenId() external view returns (uint256); } -/// @dev the ERC-165 identifier for this interface is 0x01d536fc +/// @dev the ERC-165 identifier for this interface is 0xedd3a564 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. @@ -60,6 +60,13 @@ /// or in textual repr: approveCross((address,uint256),uint256) function approveCross(CrossAddress memory spender, uint256 amount) external returns (bool); + /// @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); + /// @dev Function that changes total amount of the tokens. /// Throws if `msg.sender` doesn't owns all of the tokens. /// @param amount New total amount of the tokens. --- a/tests/src/eth/reFungibleToken.test.ts +++ b/tests/src/eth/reFungibleToken.test.ts @@ -466,6 +466,21 @@ expect(await collection.getTokenBalance(tokenId, {Substrate: ownerSub.address})).to.be.equal(150n); } }); + + itEth('Check balanceOfCross()', async ({helper}) => { + const collection = await helper.rft.mintCollection(alice, {}); + const owner = await helper.ethCrossAccount.createAccountWithBalance(donor); + const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner.eth}); + const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId); + const tokenContract = await helper.ethNativeContract.rftToken(tokenAddress, owner.eth); + + expect(BigInt(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})) === 200n).to.be.true; + + for (let i = 1n; i < 100n; i++) { + await tokenContract.methods.repartition(i).send({from: owner.eth}); + expect(BigInt(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})) === i).to.be.true; + } + }); }); describe('Refungible: Fees', () => { -- gitstuff