difftreelog
added cross methods: `burnFromCross` , `transferCross`, `approveCorss` in `ERC20` refungible pallet interface
in: master
5 files changed
pallets/refungible/src/erc_token.rsdiffbeforeafterboth35use pallet_common::{35use pallet_common::{36 CommonWeightInfo,36 CommonWeightInfo,37 erc::{CommonEvmHandler, PrecompileResult},37 erc::{CommonEvmHandler, PrecompileResult},38 eth::collection_id_to_address,38 eth::{collection_id_to_address, EthCrossAccount},39};39};40use pallet_evm::{account::CrossAccountId, PrecompileHandle};40use pallet_evm::{account::CrossAccountId, PrecompileHandle};41use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};41use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};200200201#[solidity_interface(name = ERC20UniqueExtensions)]201#[solidity_interface(name = ERC20UniqueExtensions)]202impl<T: Config> RefungibleTokenHandle<T> {202impl<T: Config> RefungibleTokenHandle<T>203where204 T::AccountId: From<[u8; 32]>,205{203 /// @dev Function that burns an amount of the token of a given account,206 /// @dev Function that burns an amount of the token of a given account,204 /// deducting from the sender's allowance for said account.207 /// deducting from the sender's allowance for said account.218 Ok(true)221 Ok(true)219 }222 }220223224 /// @dev Function that burns an amount of the token of a given account,225 /// deducting from the sender's allowance for said account.226 /// @param from The account whose tokens will be burnt.227 /// @param amount The amount that will be burnt.228 #[weight(<SelfWeightOf<T>>::burn_from())]229 fn burn_from_cross(230 &mut self,231 caller: caller,232 from: EthCrossAccount,233 amount: uint256,234 ) -> Result<bool> {235 let caller = T::CrossAccountId::from_eth(caller);236 let from = from.into_sub_cross_account::<T>()?;237 let amount = amount.try_into().map_err(|_| "amount overflow")?;238 let budget = self239 .recorder240 .weight_calls_budget(<StructureWeight<T>>::find_parent());241242 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)243 .map_err(dispatch_to_evm::<T>)?;244 Ok(true)245 }246247 /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.248 /// Beware that changing an allowance with this method brings the risk that someone may use both the old249 /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this250 /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:251 /// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729252 /// @param spender The crossaccount which will spend the funds.253 /// @param amount The amount of tokens to be spent.254 #[weight(<SelfWeightOf<T>>::approve())]255 fn approve_cross(256 &mut self,257 caller: caller,258 spender: EthCrossAccount,259 amount: uint256,260 ) -> Result<bool> {261 let caller = T::CrossAccountId::from_eth(caller);262 let spender = spender.into_sub_cross_account::<T>()?;263 let amount = amount.try_into().map_err(|_| "amount overflow")?;264265 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)266 .map_err(dispatch_to_evm::<T>)?;267 Ok(true)268 }221 /// @dev Function that changes total amount of the tokens.269 /// @dev Function that changes total amount of the tokens.222 /// Throws if `msg.sender` doesn't owns all of the tokens.270 /// Throws if `msg.sender` doesn't owns all of the tokens.223 /// @param amount New total amount of the tokens.271 /// @param amount New total amount of the tokens.230 Ok(true)278 Ok(true)231 }279 }280281 /// @dev Transfer token for a specified address282 /// @param to The crossaccount to transfer to.283 /// @param amount The amount to be transferred.284 #[weight(<CommonWeights<T>>::transfer())]285 fn transfer_cross(286 &mut self,287 caller: caller,288 to: EthCrossAccount,289 amount: uint256,290 ) -> Result<bool> {291 let caller = T::CrossAccountId::from_eth(caller);292 let to = to.into_sub_cross_account::<T>()?;293 let amount = amount.try_into().map_err(|_| "amount overflow")?;294 let budget = self295 .recorder296 .weight_calls_budget(<StructureWeight<T>>::find_parent());297298 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)299 .map_err(dispatch_to_evm::<T>)?;300 Ok(true)301 }232}302}233303234impl<T: Config> RefungibleTokenHandle<T> {304impl<T: Config> RefungibleTokenHandle<T> {pallets/refungible/src/stubs/UniqueRefungibleToken.soldiffbeforeafterboth36 }36 }37}37}383839/// @dev the ERC-165 identifier for this interface is 0xab8deb3739/// @dev the ERC-165 identifier for this interface is 0x34b53e2040contract 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.52 return false;52 return false;53 }53 }5455 /// @dev Function that burns an amount of the token of a given account,56 /// deducting from the sender's allowance for said account.57 /// @param from The account whose tokens will be burnt.58 /// @param amount The amount that will be burnt.59 /// @dev EVM selector for this function is: 0xbb2f5a58,60 /// or in textual repr: burnFromCross((address,uint256),uint256)61 function burnFromCross(EthCrossAccount memory from, uint256 amount) public returns (bool) {62 require(false, stub_error);63 from;64 amount;65 dummy = 0;66 return false;67 }6869 /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.70 /// Beware that changing an allowance with this method brings the risk that someone may use both the old71 /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this72 /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:73 /// https://github.com/ethereum/EIPs/issues/20#issuecomment-26352472974 /// @param spender The crossaccount which will spend the funds.75 /// @param amount The amount of tokens to be spent.76 /// @dev EVM selector for this function is: 0x0ecd0ab0,77 /// or in textual repr: approveCross((address,uint256),uint256)78 function approveCross(EthCrossAccount memory spender, uint256 amount) public returns (bool) {79 require(false, stub_error);80 spender;81 amount;82 dummy = 0;83 return false;84 }548555 /// @dev Function that changes total amount of the tokens.86 /// @dev Function that changes total amount of the tokens.56 /// Throws if `msg.sender` doesn't owns all of the tokens.87 /// Throws if `msg.sender` doesn't owns all of the tokens.64 return false;95 return false;65 }96 }9798 /// @dev Transfer token for a specified address99 /// @param to The crossaccount to transfer to.100 /// @param amount The amount to be transferred.101 /// @dev EVM selector for this function is: 0x2ada85ff,102 /// or in textual repr: transferCross((address,uint256),uint256)103 function transferCross(EthCrossAccount memory to, uint256 amount) public returns (bool) {104 require(false, stub_error);105 to;106 amount;107 dummy = 0;108 return false;109 }66}110}111112/// @dev Cross account struct113struct EthCrossAccount {114 address eth;115 uint256 sub;116}6711768/// @dev inlined interface118/// @dev inlined interface69contract ERC20Events {119contract ERC20Events {tests/src/eth/abi/reFungibleToken.jsondiffbeforeafterboth69 "stateMutability": "nonpayable",69 "stateMutability": "nonpayable",70 "type": "function"70 "type": "function"71 },71 },72 {73 "inputs": [74 {75 "components": [76 { "internalType": "address", "name": "eth", "type": "address" },77 { "internalType": "uint256", "name": "sub", "type": "uint256" }78 ],79 "internalType": "struct EthCrossAccount",80 "name": "spender",81 "type": "tuple"82 },83 { "internalType": "uint256", "name": "amount", "type": "uint256" }84 ],85 "name": "approveCross",86 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],87 "stateMutability": "nonpayable",88 "type": "function"89 },72 {90 {73 "inputs": [91 "inputs": [74 { "internalType": "address", "name": "owner", "type": "address" }92 { "internalType": "address", "name": "owner", "type": "address" }88 "stateMutability": "nonpayable",106 "stateMutability": "nonpayable",89 "type": "function"107 "type": "function"90 },108 },109 {110 "inputs": [111 {112 "components": [113 { "internalType": "address", "name": "eth", "type": "address" },114 { "internalType": "uint256", "name": "sub", "type": "uint256" }115 ],116 "internalType": "struct EthCrossAccount",117 "name": "from",118 "type": "tuple"119 },120 { "internalType": "uint256", "name": "amount", "type": "uint256" }121 ],122 "name": "burnFromCross",123 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],124 "stateMutability": "nonpayable",125 "type": "function"126 },91 {127 {92 "inputs": [],128 "inputs": [],93 "name": "decimals",129 "name": "decimals",158 "stateMutability": "nonpayable",194 "stateMutability": "nonpayable",159 "type": "function"195 "type": "function"160 },196 },197 {198 "inputs": [199 {200 "components": [201 { "internalType": "address", "name": "eth", "type": "address" },202 { "internalType": "uint256", "name": "sub", "type": "uint256" }203 ],204 "internalType": "struct EthCrossAccount",205 "name": "to",206 "type": "tuple"207 },208 { "internalType": "uint256", "name": "amount", "type": "uint256" }209 ],210 "name": "transferCross",211 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],212 "stateMutability": "nonpayable",213 "type": "function"214 },161 {215 {162 "inputs": [216 "inputs": [163 { "internalType": "address", "name": "from", "type": "address" },217 { "internalType": "address", "name": "from", "type": "address" },tests/src/eth/api/UniqueRefungibleToken.soldiffbeforeafterboth23 function parentTokenId() external view returns (uint256);23 function parentTokenId() external view returns (uint256);24}24}252526/// @dev the ERC-165 identifier for this interface is 0xab8deb3726/// @dev the ERC-165 identifier for this interface is 0x34b53e2027interface ERC20UniqueExtensions is Dummy, ERC165 {27interface ERC20UniqueExtensions is Dummy, ERC165 {28 /// @dev Function that burns an amount of the token of a given account,28 /// @dev Function that burns an amount of the token of a given account,29 /// deducting from the sender's allowance for said account.29 /// deducting from the sender's allowance for said account.33 /// or in textual repr: burnFrom(address,uint256)33 /// or in textual repr: burnFrom(address,uint256)34 function burnFrom(address from, uint256 amount) external returns (bool);34 function burnFrom(address from, uint256 amount) external returns (bool);3536 /// @dev Function that burns an amount of the token of a given account,37 /// deducting from the sender's allowance for said account.38 /// @param from The account whose tokens will be burnt.39 /// @param amount The amount that will be burnt.40 /// @dev EVM selector for this function is: 0xbb2f5a58,41 /// or in textual repr: burnFromCross((address,uint256),uint256)42 function burnFromCross(EthCrossAccount memory from, uint256 amount) external returns (bool);4344 /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.45 /// Beware that changing an allowance with this method brings the risk that someone may use both the old46 /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this47 /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:48 /// https://github.com/ethereum/EIPs/issues/20#issuecomment-26352472949 /// @param spender The crossaccount which will spend the funds.50 /// @param amount The amount of tokens to be spent.51 /// @dev EVM selector for this function is: 0x0ecd0ab0,52 /// or in textual repr: approveCross((address,uint256),uint256)53 function approveCross(EthCrossAccount memory spender, uint256 amount) external returns (bool);355436 /// @dev Function that changes total amount of the tokens.55 /// @dev Function that changes total amount of the tokens.37 /// Throws if `msg.sender` doesn't owns all of the tokens.56 /// Throws if `msg.sender` doesn't owns all of the tokens.40 /// or in textual repr: repartition(uint256)59 /// or in textual repr: repartition(uint256)41 function repartition(uint256 amount) external returns (bool);60 function repartition(uint256 amount) external returns (bool);6162 /// @dev Transfer token for a specified address63 /// @param to The crossaccount to transfer to.64 /// @param amount The amount to be transferred.65 /// @dev EVM selector for this function is: 0x2ada85ff,66 /// or in textual repr: transferCross((address,uint256),uint256)67 function transferCross(EthCrossAccount memory to, uint256 amount) external returns (bool);42}68}6970/// @dev Cross account struct71struct EthCrossAccount {72 address eth;73 uint256 sub;74}437544/// @dev inlined interface76/// @dev inlined interface45interface ERC20Events {77interface ERC20Events {tests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth160 }160 }161 });161 });162162 163 itEth('Can perform approveCross()', async ({helper}) => {164 const owner = await helper.eth.createAccountWithBalance(donor);165 const spender = helper.eth.createAccount();166 const spenderCross = helper.ethCrossAccount.fromAddress(spender);167168 const collection = await helper.rft.mintCollection(alice);169 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});170171 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);172 const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);173174 {175 const result = await contract.methods.approveCross(spenderCross, 100).send({from: owner});176 const event = result.events.Approval;177 expect(event.address).to.be.equal(tokenAddress);178 expect(event.returnValues.owner).to.be.equal(owner);179 expect(event.returnValues.spender).to.be.equal(spender);180 expect(event.returnValues.value).to.be.equal('100');181 }182183 {184 const allowance = await contract.methods.allowance(owner, spender).call();185 expect(+allowance).to.equal(100);186 }187 });188 163 itEth('Can perform transferFrom()', async ({helper}) => {189 itEth('Can perform transferFrom()', async ({helper}) => {164 const owner = await helper.eth.createAccountWithBalance(donor);190 const owner = await helper.eth.createAccountWithBalance(donor);267 expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);293 expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);268 }));294 }));269295296 itEth('Can perform transferCross()', async ({helper}) => {297 const owner = await helper.eth.createAccountWithBalance(donor);298 const receiver = helper.eth.createAccount();299 const receiverCross = helper.ethCrossAccount.fromAddress(receiver);300 const collection = await helper.rft.mintCollection(alice);301 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});302303 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);304 const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);305306 {307 const result = await contract.methods.transferCross(receiverCross, 50).send({from: owner});308 const event = result.events.Transfer;309 expect(event.address).to.be.equal(tokenAddress);310 expect(event.returnValues.from).to.be.equal(owner);311 expect(event.returnValues.to).to.be.equal(receiver);312 expect(event.returnValues.value).to.be.equal('50');313 }314315 {316 const balance = await contract.methods.balanceOf(owner).call();317 expect(+balance).to.equal(150);318 }319320 {321 const balance = await contract.methods.balanceOf(receiver).call();322 expect(+balance).to.equal(50);323 }324 });270 itEth('Can perform repartition()', async ({helper}) => {325 itEth('Can perform repartition()', async ({helper}) => {271 const owner = await helper.eth.createAccountWithBalance(donor);326 const owner = await helper.eth.createAccountWithBalance(donor);272 const receiver = await helper.eth.createAccountWithBalance(donor);327 const receiver = await helper.eth.createAccountWithBalance(donor);354 expect(event.returnValues.tokenId).to.be.equal(tokenId);409 expect(event.returnValues.tokenId).to.be.equal(tokenId);355 });410 });411 412 itEth('Can perform burnFromCross()', async ({helper}) => {413 const owner = await helper.eth.createAccountWithBalance(donor);414 const ownerCross = helper.ethCrossAccount.fromAddress(owner);415 const spender = await helper.eth.createAccountWithBalance(donor);416 const spenderCross = helper.ethCrossAccount.fromAddress(spender);417418 const collection = await helper.rft.mintCollection(alice);419 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});420421 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);422 const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);423424 await contract.methods.approveCross(spenderCross, 100).send({from: owner});425 426 await expect(contract.methods.burnFromCross(ownerCross, 50).send({from: spender})).to.be.fulfilled;427 await expect(contract.methods.burnFromCross(ownerCross, 100).send({from: spender})).to.be.rejected;428 expect(await contract.methods.balanceOf(owner).call({from: owner})).to.be.equal('150');429 });356});430});357431358describe('Refungible: Fees', () => {432describe('Refungible: Fees', () => {