git.delta.rocks / unique-network / refs/commits / 238681dec68a

difftreelog

added `transferFromCross` method in `ERC20` refungible pallet interface

PraetorP2022-12-07parent: #b238f7b.patch.diff
in: master

5 files changed

modifiedpallets/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> {
modifiedpallets/refungible/src/stubs/UniqueRefungibleToken.soldiffbeforeafterboth
--- 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 0x34b53e20
+/// @dev the ERC-165 identifier for this interface is 0xe17a7d2b
 contract 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.
@@ -107,6 +107,25 @@
 		dummy = 0;
 		return false;
 	}
+
+	/// @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
+	) public returns (bool) {
+		require(false, stub_error);
+		from;
+		to;
+		amount;
+		dummy = 0;
+		return false;
+	}
 }
 
 /// @dev Cross account struct
modifiedtests/src/eth/abi/reFungibleToken.jsondiffbeforeafterboth
before · tests/src/eth/abi/reFungibleToken.json
1[2  {3    "anonymous": false,4    "inputs": [5      {6        "indexed": true,7        "internalType": "address",8        "name": "owner",9        "type": "address"10      },11      {12        "indexed": true,13        "internalType": "address",14        "name": "spender",15        "type": "address"16      },17      {18        "indexed": false,19        "internalType": "uint256",20        "name": "value",21        "type": "uint256"22      }23    ],24    "name": "Approval",25    "type": "event"26  },27  {28    "anonymous": false,29    "inputs": [30      {31        "indexed": true,32        "internalType": "address",33        "name": "from",34        "type": "address"35      },36      {37        "indexed": true,38        "internalType": "address",39        "name": "to",40        "type": "address"41      },42      {43        "indexed": false,44        "internalType": "uint256",45        "name": "value",46        "type": "uint256"47      }48    ],49    "name": "Transfer",50    "type": "event"51  },52  {53    "inputs": [54      { "internalType": "address", "name": "owner", "type": "address" },55      { "internalType": "address", "name": "spender", "type": "address" }56    ],57    "name": "allowance",58    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],59    "stateMutability": "view",60    "type": "function"61  },62  {63    "inputs": [64      { "internalType": "address", "name": "spender", "type": "address" },65      { "internalType": "uint256", "name": "amount", "type": "uint256" }66    ],67    "name": "approve",68    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],69    "stateMutability": "nonpayable",70    "type": "function"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  },90  {91    "inputs": [92      { "internalType": "address", "name": "owner", "type": "address" }93    ],94    "name": "balanceOf",95    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],96    "stateMutability": "view",97    "type": "function"98  },99  {100    "inputs": [101      { "internalType": "address", "name": "from", "type": "address" },102      { "internalType": "uint256", "name": "amount", "type": "uint256" }103    ],104    "name": "burnFrom",105    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],106    "stateMutability": "nonpayable",107    "type": "function"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  },127  {128    "inputs": [],129    "name": "decimals",130    "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }],131    "stateMutability": "view",132    "type": "function"133  },134  {135    "inputs": [],136    "name": "name",137    "outputs": [{ "internalType": "string", "name": "", "type": "string" }],138    "stateMutability": "view",139    "type": "function"140  },141  {142    "inputs": [],143    "name": "parentToken",144    "outputs": [{ "internalType": "address", "name": "", "type": "address" }],145    "stateMutability": "view",146    "type": "function"147  },148  {149    "inputs": [],150    "name": "parentTokenId",151    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],152    "stateMutability": "view",153    "type": "function"154  },155  {156    "inputs": [157      { "internalType": "uint256", "name": "amount", "type": "uint256" }158    ],159    "name": "repartition",160    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],161    "stateMutability": "nonpayable",162    "type": "function"163  },164  {165    "inputs": [166      { "internalType": "bytes4", "name": "interfaceID", "type": "bytes4" }167    ],168    "name": "supportsInterface",169    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],170    "stateMutability": "view",171    "type": "function"172  },173  {174    "inputs": [],175    "name": "symbol",176    "outputs": [{ "internalType": "string", "name": "", "type": "string" }],177    "stateMutability": "view",178    "type": "function"179  },180  {181    "inputs": [],182    "name": "totalSupply",183    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],184    "stateMutability": "view",185    "type": "function"186  },187  {188    "inputs": [189      { "internalType": "address", "name": "to", "type": "address" },190      { "internalType": "uint256", "name": "amount", "type": "uint256" }191    ],192    "name": "transfer",193    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],194    "stateMutability": "nonpayable",195    "type": "function"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  },215  {216    "inputs": [217      { "internalType": "address", "name": "from", "type": "address" },218      { "internalType": "address", "name": "to", "type": "address" },219      { "internalType": "uint256", "name": "amount", "type": "uint256" }220    ],221    "name": "transferFrom",222    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],223    "stateMutability": "nonpayable",224    "type": "function"225  }226]
after · tests/src/eth/abi/reFungibleToken.json
1[2  {3    "anonymous": false,4    "inputs": [5      {6        "indexed": true,7        "internalType": "address",8        "name": "owner",9        "type": "address"10      },11      {12        "indexed": true,13        "internalType": "address",14        "name": "spender",15        "type": "address"16      },17      {18        "indexed": false,19        "internalType": "uint256",20        "name": "value",21        "type": "uint256"22      }23    ],24    "name": "Approval",25    "type": "event"26  },27  {28    "anonymous": false,29    "inputs": [30      {31        "indexed": true,32        "internalType": "address",33        "name": "from",34        "type": "address"35      },36      {37        "indexed": true,38        "internalType": "address",39        "name": "to",40        "type": "address"41      },42      {43        "indexed": false,44        "internalType": "uint256",45        "name": "value",46        "type": "uint256"47      }48    ],49    "name": "Transfer",50    "type": "event"51  },52  {53    "inputs": [54      { "internalType": "address", "name": "owner", "type": "address" },55      { "internalType": "address", "name": "spender", "type": "address" }56    ],57    "name": "allowance",58    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],59    "stateMutability": "view",60    "type": "function"61  },62  {63    "inputs": [64      { "internalType": "address", "name": "spender", "type": "address" },65      { "internalType": "uint256", "name": "amount", "type": "uint256" }66    ],67    "name": "approve",68    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],69    "stateMutability": "nonpayable",70    "type": "function"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  },90  {91    "inputs": [92      { "internalType": "address", "name": "owner", "type": "address" }93    ],94    "name": "balanceOf",95    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],96    "stateMutability": "view",97    "type": "function"98  },99  {100    "inputs": [101      { "internalType": "address", "name": "from", "type": "address" },102      { "internalType": "uint256", "name": "amount", "type": "uint256" }103    ],104    "name": "burnFrom",105    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],106    "stateMutability": "nonpayable",107    "type": "function"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  },127  {128    "inputs": [],129    "name": "decimals",130    "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }],131    "stateMutability": "view",132    "type": "function"133  },134  {135    "inputs": [],136    "name": "name",137    "outputs": [{ "internalType": "string", "name": "", "type": "string" }],138    "stateMutability": "view",139    "type": "function"140  },141  {142    "inputs": [],143    "name": "parentToken",144    "outputs": [{ "internalType": "address", "name": "", "type": "address" }],145    "stateMutability": "view",146    "type": "function"147  },148  {149    "inputs": [],150    "name": "parentTokenId",151    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],152    "stateMutability": "view",153    "type": "function"154  },155  {156    "inputs": [157      { "internalType": "uint256", "name": "amount", "type": "uint256" }158    ],159    "name": "repartition",160    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],161    "stateMutability": "nonpayable",162    "type": "function"163  },164  {165    "inputs": [166      { "internalType": "bytes4", "name": "interfaceID", "type": "bytes4" }167    ],168    "name": "supportsInterface",169    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],170    "stateMutability": "view",171    "type": "function"172  },173  {174    "inputs": [],175    "name": "symbol",176    "outputs": [{ "internalType": "string", "name": "", "type": "string" }],177    "stateMutability": "view",178    "type": "function"179  },180  {181    "inputs": [],182    "name": "totalSupply",183    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],184    "stateMutability": "view",185    "type": "function"186  },187  {188    "inputs": [189      { "internalType": "address", "name": "to", "type": "address" },190      { "internalType": "uint256", "name": "amount", "type": "uint256" }191    ],192    "name": "transfer",193    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],194    "stateMutability": "nonpayable",195    "type": "function"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  },215  {216    "inputs": [217      { "internalType": "address", "name": "from", "type": "address" },218      { "internalType": "address", "name": "to", "type": "address" },219      { "internalType": "uint256", "name": "amount", "type": "uint256" }220    ],221    "name": "transferFrom",222    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],223    "stateMutability": "nonpayable",224    "type": "function"225  },226  {227    "inputs": [228      {229        "components": [230          { "internalType": "address", "name": "eth", "type": "address" },231          { "internalType": "uint256", "name": "sub", "type": "uint256" }232        ],233        "internalType": "struct EthCrossAccount",234        "name": "from",235        "type": "tuple"236      },237      {238        "components": [239          { "internalType": "address", "name": "eth", "type": "address" },240          { "internalType": "uint256", "name": "sub", "type": "uint256" }241        ],242        "internalType": "struct EthCrossAccount",243        "name": "to",244        "type": "tuple"245      },246      { "internalType": "uint256", "name": "amount", "type": "uint256" }247    ],248    "name": "transferFromCross",249    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],250    "stateMutability": "nonpayable",251    "type": "function"252  }253]
modifiedtests/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
modifiedtests/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);