difftreelog
added `transferFromCross` method in `ERC20` refungible pallet interface
in: master
5 files changed
pallets/refungible/src/erc_token.rsdiffbeforeafterboth--- a/pallets/refungible/src/erc_token.rs
+++ b/pallets/refungible/src/erc_token.rs
@@ -299,6 +299,31 @@
.map_err(dispatch_to_evm::<T>)?;
Ok(true)
}
+
+ /// @dev Transfer tokens from one address to another
+ /// @param from address The address which you want to send tokens from
+ /// @param to address The address which you want to transfer to
+ /// @param amount uint256 the amount of tokens to be transferred
+ #[weight(<CommonWeights<T>>::transfer_from())]
+ fn transfer_from_cross(
+ &mut self,
+ caller: caller,
+ from: EthCrossAccount,
+ to: EthCrossAccount,
+ amount: uint256,
+ ) -> Result<bool> {
+ let caller = T::CrossAccountId::from_eth(caller);
+ let from = from.into_sub_cross_account::<T>()?;
+ let to = to.into_sub_cross_account::<T>()?;
+ let amount = amount.try_into().map_err(|_| "amount overflow")?;
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
+
+ <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)
+ .map_err(dispatch_to_evm::<T>)?;
+ Ok(true)
+ }
}
impl<T: Config> RefungibleTokenHandle<T> {
pallets/refungible/src/stubs/UniqueRefungibleToken.soldiffbeforeafterboth36 }36 }37}37}383839/// @dev the ERC-165 identifier for this interface is 0x34b53e2039/// @dev the ERC-165 identifier for this interface is 0xe17a7d2b40contract ERC20UniqueExtensions is Dummy, ERC165 {40contract ERC20UniqueExtensions is Dummy, ERC165 {41 /// @dev Function that burns an amount of the token of a given account,41 /// @dev Function that burns an amount of the token of a given account,42 /// deducting from the sender's allowance for said account.42 /// deducting from the sender's allowance for said account.108 return false;108 return false;109 }109 }110111 /// @dev Transfer tokens from one address to another112 /// @param from address The address which you want to send tokens from113 /// @param to address The address which you want to transfer to114 /// @param amount uint256 the amount of tokens to be transferred115 /// @dev EVM selector for this function is: 0xd5cf430b,116 /// or in textual repr: transferFromCross((address,uint256),(address,uint256),uint256)117 function transferFromCross(118 EthCrossAccount memory from,119 EthCrossAccount memory to,120 uint256 amount121 ) public returns (bool) {122 require(false, stub_error);123 from;124 to;125 amount;126 dummy = 0;127 return false;128 }110}129}111130112/// @dev Cross account struct131/// @dev Cross account structtests/src/eth/abi/reFungibleToken.jsondiffbeforeafterboth--- a/tests/src/eth/abi/reFungibleToken.json
+++ b/tests/src/eth/abi/reFungibleToken.json
@@ -222,5 +222,32 @@
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"stateMutability": "nonpayable",
"type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "components": [
+ { "internalType": "address", "name": "eth", "type": "address" },
+ { "internalType": "uint256", "name": "sub", "type": "uint256" }
+ ],
+ "internalType": "struct EthCrossAccount",
+ "name": "from",
+ "type": "tuple"
+ },
+ {
+ "components": [
+ { "internalType": "address", "name": "eth", "type": "address" },
+ { "internalType": "uint256", "name": "sub", "type": "uint256" }
+ ],
+ "internalType": "struct EthCrossAccount",
+ "name": "to",
+ "type": "tuple"
+ },
+ { "internalType": "uint256", "name": "amount", "type": "uint256" }
+ ],
+ "name": "transferFromCross",
+ "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
+ "stateMutability": "nonpayable",
+ "type": "function"
}
]
tests/src/eth/api/UniqueRefungibleToken.soldiffbeforeafterboth--- 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 0x34b53e20
+/// @dev the ERC-165 identifier for this interface is 0xe17a7d2b
interface ERC20UniqueExtensions is Dummy, ERC165 {
/// @dev Function that burns an amount of the token of a given account,
/// deducting from the sender's allowance for said account.
@@ -65,6 +65,18 @@
/// @dev EVM selector for this function is: 0x2ada85ff,
/// or in textual repr: transferCross((address,uint256),uint256)
function transferCross(EthCrossAccount memory to, uint256 amount) external returns (bool);
+
+ /// @dev Transfer tokens from one address to another
+ /// @param from address The address which you want to send tokens from
+ /// @param to address The address which you want to transfer to
+ /// @param amount uint256 the amount of tokens to be transferred
+ /// @dev EVM selector for this function is: 0xd5cf430b,
+ /// or in textual repr: transferFromCross((address,uint256),(address,uint256),uint256)
+ function transferFromCross(
+ EthCrossAccount memory from,
+ EthCrossAccount memory to,
+ uint256 amount
+ ) external returns (bool);
}
/// @dev Cross account struct
tests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth--- a/tests/src/eth/reFungibleToken.test.ts
+++ b/tests/src/eth/reFungibleToken.test.ts
@@ -223,6 +223,46 @@
expect(+balance).to.equal(151);
}
});
+
+ itEth('Can perform transferFromCross()', async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const ownerCross = helper.ethCrossAccount.fromAddress(owner);
+ const spender = await helper.eth.createAccountWithBalance(donor);
+ const receiver = helper.eth.createAccount();
+ const receiverCross = helper.ethCrossAccount.fromAddress(receiver);
+ const collection = await helper.rft.mintCollection(alice);
+ const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});
+
+ const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);
+ const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);
+
+ await contract.methods.approve(spender, 100).send();
+
+ {
+ const result = await contract.methods.transferFromCross(ownerCross, receiverCross, 49).send({from: spender});
+ let event = result.events.Transfer;
+ expect(event.address).to.be.equal(tokenAddress);
+ expect(event.returnValues.from).to.be.equal(owner);
+ expect(event.returnValues.to).to.be.equal(receiver);
+ expect(event.returnValues.value).to.be.equal('49');
+
+ event = result.events.Approval;
+ expect(event.address).to.be.equal(tokenAddress);
+ expect(event.returnValues.owner).to.be.equal(owner);
+ expect(event.returnValues.spender).to.be.equal(spender);
+ expect(event.returnValues.value).to.be.equal('51');
+ }
+
+ {
+ const balance = await contract.methods.balanceOf(receiver).call();
+ expect(+balance).to.equal(49);
+ }
+
+ {
+ const balance = await contract.methods.balanceOf(owner).call();
+ expect(+balance).to.equal(151);
+ }
+ });
itEth('Can perform transfer()', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);