From e0f86c78a536d748b1ac38c3d997e17d6bfa00c8 Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Wed, 01 Feb 2023 08:58:48 +0000 Subject: [PATCH] feat: add allowanceCross method --- --- a/Cargo.lock +++ b/Cargo.lock @@ -6360,7 +6360,7 @@ [[package]] name = "pallet-fungible" -version = "0.1.9" +version = "0.1.10" dependencies = [ "evm-coder", "frame-benchmarking", @@ -6773,7 +6773,7 @@ [[package]] name = "pallet-refungible" -version = "0.2.12" +version = "0.2.13" dependencies = [ "evm-coder", "frame-benchmarking", --- a/pallets/fungible/CHANGELOG.md +++ b/pallets/fungible/CHANGELOG.md @@ -4,6 +4,12 @@ +## [0.1.10] - 2023-02-01 + +### Added + +- The functions `allowanceCross` to `ERC20UniqueExtensions` interface. + ## [0.1.9] - 2022-12-01 ### Added --- a/pallets/fungible/Cargo.toml +++ b/pallets/fungible/Cargo.toml @@ -2,7 +2,7 @@ edition = "2021" license = "GPLv3" name = "pallet-fungible" -version = "0.1.9" +version = "0.1.10" [dependencies] # Note: `package = "parity-scale-codec"` must be supplied since the `Encode` macro searches for it. --- a/pallets/fungible/src/erc.rs +++ b/pallets/fungible/src/erc.rs @@ -161,6 +161,21 @@ where T::AccountId: From<[u8; 32]>, { + /// @dev Function to check the amount of tokens that an owner allowed to a spender. + /// @param owner crossAddress The address which owns the funds. + /// @param spender crossAddress The address which will spend the funds. + /// @return A uint256 specifying the amount of tokens still available for the spender. + fn allowance_cross( + &self, + owner: pallet_common::eth::CrossAddress, + spender: pallet_common::eth::CrossAddress, + ) -> Result { + let owner = owner.into_sub_cross_account::()?; + let spender = spender.into_sub_cross_account::()?; + + Ok(>::get((self.id, owner, spender)).into()) + } + /// @notice A description for the collection. fn description(&self) -> Result { Ok(decode_utf16(self.description.iter().copied()) --- a/pallets/fungible/src/stubs/UniqueFungible.sol +++ b/pallets/fungible/src/stubs/UniqueFungible.sol @@ -511,8 +511,22 @@ bytes value; } -/// @dev the ERC-165 identifier for this interface is 0x65789571 +/// @dev the ERC-165 identifier for this interface is 0x85d7dea6 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. + /// @param spender crossAddress The address which will spend the funds. + /// @return A uint256 specifying the amount of tokens still available for the spender. + /// @dev EVM selector for this function is: 0xe0af4bd7, + /// or in textual repr: allowanceCross((address,uint256),(address,uint256)) + function allowanceCross(CrossAddress memory owner, CrossAddress memory spender) public view returns (uint256) { + require(false, stub_error); + owner; + spender; + dummy; + return 0; + } + /// @notice A description for the collection. /// @dev EVM selector for this function is: 0x7284e416, /// or in textual repr: description() @@ -576,7 +590,7 @@ /// @param amounts array of pairs of account address and amount /// @dev EVM selector for this function is: 0x1acf2d55, /// or in textual repr: mintBulk((address,uint256)[]) - function mintBulk(Tuple9[] memory amounts) public returns (bool) { + function mintBulk(Tuple11[] memory amounts) public returns (bool) { require(false, stub_error); amounts; dummy = 0; @@ -619,7 +633,7 @@ } /// @dev anonymous struct -struct Tuple9 { +struct Tuple11 { address field_0; uint256 field_1; } --- a/pallets/refungible/CHANGELOG.md +++ b/pallets/refungible/CHANGELOG.md @@ -4,6 +4,12 @@ +## [0.2.13] - 2023-02-01 + +### Added + +- The functions `allowanceCross` to `ERC20UniqueExtensions` interface. + ## [0.2.12] - 2023-01-20 ### Fixed --- a/pallets/refungible/Cargo.toml +++ b/pallets/refungible/Cargo.toml @@ -2,7 +2,7 @@ edition = "2021" license = "GPLv3" name = "pallet-refungible" -version = "0.2.12" +version = "0.2.13" [dependencies] # Note: `package = "parity-scale-codec"` must be supplied since the `Encode` macro searches for it. --- a/pallets/refungible/src/erc_token.rs +++ b/pallets/refungible/src/erc_token.rs @@ -203,6 +203,21 @@ where T::AccountId: From<[u8; 32]>, { + /// @dev Function to check the amount of tokens that an owner allowed to a spender. + /// @param owner crossAddress The address which owns the funds. + /// @param spender crossAddress The address which will spend the funds. + /// @return A uint256 specifying the amount of tokens still available for the spender. + fn allowance_cross( + &self, + owner: pallet_common::eth::CrossAddress, + spender: pallet_common::eth::CrossAddress, + ) -> Result { + let owner = owner.into_sub_cross_account::()?; + let spender = spender.into_sub_cross_account::()?; + + Ok(>::get((self.id, self.1, owner, spender)).into()) + } + /// @dev Function that burns an amount of the token of a given account, /// deducting from the sender's allowance for said account. /// @param from The account whose tokens will be burnt. --- a/pallets/refungible/src/stubs/UniqueRefungibleToken.sol +++ b/pallets/refungible/src/stubs/UniqueRefungibleToken.sol @@ -36,8 +36,22 @@ } } -/// @dev the ERC-165 identifier for this interface is 0xe17a7d2b +/// @dev the ERC-165 identifier for this interface is 0x01d536fc 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. + /// @param spender crossAddress The address which will spend the funds. + /// @return A uint256 specifying the amount of tokens still available for the spender. + /// @dev EVM selector for this function is: 0xe0af4bd7, + /// or in textual repr: allowanceCross((address,uint256),(address,uint256)) + function allowanceCross(CrossAddress memory owner, CrossAddress memory spender) public view returns (uint256) { + require(false, stub_error); + owner; + spender; + dummy; + return 0; + } + // /// @dev Function that burns an amount of the token of a given account, // /// deducting from the sender's allowance for said account. // /// @param from The account whose tokens will be burnt. --- a/runtime/common/ethereum/sponsoring/refungible.rs +++ b/runtime/common/ethereum/sponsoring/refungible.rs @@ -341,7 +341,9 @@ ERC165Call(_, _) => None, // Not sponsored - BurnFrom { .. } | BurnFromCross { .. } | Repartition { .. } => None, + AllowanceCross { .. } | BurnFrom { .. } | BurnFromCross { .. } | Repartition { .. } => { + None + } TransferCross { .. } | TransferFromCross { .. } => { let RefungibleTokenHandle(handle, token_id) = token; --- a/tests/src/eth/abi/fungible.json +++ b/tests/src/eth/abi/fungible.json @@ -101,6 +101,32 @@ { "internalType": "uint256", "name": "sub", "type": "uint256" } ], "internalType": "struct CrossAddress", + "name": "owner", + "type": "tuple" + }, + { + "components": [ + { "internalType": "address", "name": "eth", "type": "address" }, + { "internalType": "uint256", "name": "sub", "type": "uint256" } + ], + "internalType": "struct CrossAddress", + "name": "spender", + "type": "tuple" + } + ], + "name": "allowanceCross", + "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": "user", "type": "tuple" } @@ -411,7 +437,7 @@ { "internalType": "address", "name": "field_0", "type": "address" }, { "internalType": "uint256", "name": "field_1", "type": "uint256" } ], - "internalType": "struct Tuple9[]", + "internalType": "struct Tuple11[]", "name": "amounts", "type": "tuple[]" } --- a/tests/src/eth/abi/reFungibleToken.json +++ b/tests/src/eth/abi/reFungibleToken.json @@ -61,6 +61,32 @@ }, { "inputs": [ + { + "components": [ + { "internalType": "address", "name": "eth", "type": "address" }, + { "internalType": "uint256", "name": "sub", "type": "uint256" } + ], + "internalType": "struct CrossAddress", + "name": "owner", + "type": "tuple" + }, + { + "components": [ + { "internalType": "address", "name": "eth", "type": "address" }, + { "internalType": "uint256", "name": "sub", "type": "uint256" } + ], + "internalType": "struct CrossAddress", + "name": "spender", + "type": "tuple" + } + ], + "name": "allowanceCross", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ { "internalType": "address", "name": "spender", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" } ], --- a/tests/src/eth/api/UniqueFungible.sol +++ b/tests/src/eth/api/UniqueFungible.sol @@ -353,8 +353,16 @@ bytes value; } -/// @dev the ERC-165 identifier for this interface is 0x65789571 +/// @dev the ERC-165 identifier for this interface is 0x85d7dea6 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. + /// @param spender crossAddress The address which will spend the funds. + /// @return A uint256 specifying the amount of tokens still available for the spender. + /// @dev EVM selector for this function is: 0xe0af4bd7, + /// or in textual repr: allowanceCross((address,uint256),(address,uint256)) + function allowanceCross(CrossAddress memory owner, CrossAddress memory spender) external view returns (uint256); + /// @notice A description for the collection. /// @dev EVM selector for this function is: 0x7284e416, /// or in textual repr: description() @@ -390,7 +398,7 @@ /// @param amounts array of pairs of account address and amount /// @dev EVM selector for this function is: 0x1acf2d55, /// or in textual repr: mintBulk((address,uint256)[]) - function mintBulk(Tuple9[] memory amounts) external returns (bool); + function mintBulk(Tuple11[] memory amounts) external returns (bool); /// @dev EVM selector for this function is: 0x2ada85ff, /// or in textual repr: transferCross((address,uint256),uint256) @@ -411,7 +419,7 @@ } /// @dev anonymous struct -struct Tuple9 { +struct Tuple11 { address field_0; uint256 field_1; } --- a/tests/src/eth/api/UniqueRefungibleToken.sol +++ b/tests/src/eth/api/UniqueRefungibleToken.sol @@ -23,8 +23,16 @@ function parentTokenId() external view returns (uint256); } -/// @dev the ERC-165 identifier for this interface is 0xe17a7d2b +/// @dev the ERC-165 identifier for this interface is 0x01d536fc 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. + /// @param spender crossAddress The address which will spend the funds. + /// @return A uint256 specifying the amount of tokens still available for the spender. + /// @dev EVM selector for this function is: 0xe0af4bd7, + /// or in textual repr: allowanceCross((address,uint256),(address,uint256)) + function allowanceCross(CrossAddress memory owner, CrossAddress memory spender) external view returns (uint256); + // /// @dev Function that burns an amount of the token of a given account, // /// deducting from the sender's allowance for said account. // /// @param from The account whose tokens will be burnt. --- a/tests/src/eth/fungible.test.ts +++ b/tests/src/eth/fungible.test.ts @@ -134,6 +134,12 @@ const allowance = await contract.methods.allowance(owner, spender).call(); expect(+allowance).to.equal(100); } + { + const ownerCross = helper.ethCrossAccount.fromAddress(owner); + const spenderCross = helper.ethCrossAccount.fromAddress(spender); + const allowance = await contract.methods.allowanceCross(ownerCross, spenderCross).call(); + expect(+allowance).to.equal(100); + } }); itEth('Can perform approveCross()', async ({helper}) => { --- a/tests/src/eth/reFungible.test.ts +++ b/tests/src/eth/reFungible.test.ts @@ -219,8 +219,16 @@ await rftToken.methods.approve(operator, 15n).send({from: owner}); await contract.methods.setApprovalForAll(operator, true).send({from: owner}); await rftToken.methods.burnFrom(owner, 10n).send({from: operator}); + } + { const allowance = await rftToken.methods.allowance(owner, operator).call(); - expect(allowance).to.be.equal('5'); + expect(+allowance).to.be.equal(5); + } + { + const ownerCross = helper.ethCrossAccount.fromAddress(owner); + const operatorCross = helper.ethCrossAccount.fromAddress(operator); + const allowance = await rftToken.methods.allowanceCross(ownerCross, operatorCross).call(); + expect(+allowance).to.equal(5); } }); -- gitstuff