git.delta.rocks / unique-network / refs/commits / 39430dd15dad

difftreelog

Merge pull request #897 from UniqueNetwork/feature/impl_getApproved

Yaroslav Bolyukin2023-03-30parents: #2df4b51 #0361756.patch.diff
in: master

6 files changed

modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -515,10 +515,18 @@
 		Ok(())
 	}
 
-	/// @dev Not implemented
-	fn get_approved(&self, _token_id: U256) -> Result<Address> {
-		// TODO: Not implemetable
-		Err("not implemented".into())
+	/// @notice Get the approved address for a single NFT
+	/// @dev Throws if `tokenId` is not a valid NFT
+	/// @param tokenId The NFT to find the approved address for
+	/// @return The approved address for this NFT, or the zero address if there is none
+	fn get_approved(&self, token_id: U256) -> Result<Address> {
+		let token_id = token_id.try_into()?;
+		let operator = <Pallet<T>>::get_allowance(self, token_id).map_err(dispatch_to_evm::<T>)?;
+		Ok(if let Some(operator) = operator {
+			*operator.as_eth()
+		} else {
+			Address::zero()
+		})
 	}
 
 	/// @notice Tells whether the given `owner` approves the `operator`.
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -1082,6 +1082,17 @@
 		}
 	}
 
+	pub fn get_allowance(
+		collection: &NonfungibleHandle<T>,
+		token_id: TokenId,
+	) -> Result<Option<T::CrossAccountId>, DispatchError> {
+		ensure!(
+			<TokenData<T>>::get((collection.id, token_id)).is_some(),
+			<CommonError<T>>::TokenNotFound
+		);
+		Ok(<Allowance<T>>::get((collection.id, token_id)))
+	}
+
 	/// Set allowance for the spender to `transfer` or `burn` sender's token.
 	///
 	/// - `token`: Token the spender is allowed to `transfer` or `burn`.
modifiedpallets/nonfungible/src/stubs/UniqueNFT.rawdiffbeforeafterboth

binary blob — no preview

modifiedpallets/nonfungible/src/stubs/UniqueNFT.soldiffbeforeafterboth
--- a/pallets/nonfungible/src/stubs/UniqueNFT.sol
+++ b/pallets/nonfungible/src/stubs/UniqueNFT.sol
@@ -1156,7 +1156,10 @@
 		dummy = 0;
 	}
 
-	/// @dev Not implemented
+	/// @notice Get the approved address for a single NFT
+	/// @dev Throws if `tokenId` is not a valid NFT
+	/// @param tokenId The NFT to find the approved address for
+	/// @return The approved address for this NFT, or the zero address if there is none
 	/// @dev EVM selector for this function is: 0x081812fc,
 	///  or in textual repr: getApproved(uint256)
 	function getApproved(uint256 tokenId) public view returns (address) {
modifiedtests/src/eth/api/UniqueNFT.soldiffbeforeafterboth
781 /// or in textual repr: setApprovalForAll(address,bool)781 /// or in textual repr: setApprovalForAll(address,bool)
782 function setApprovalForAll(address operator, bool approved) external;782 function setApprovalForAll(address operator, bool approved) external;
783783
784 /// @dev Not implemented784 /// @notice Get the approved address for a single NFT
785 /// @dev Throws if `tokenId` is not a valid NFT
786 /// @param tokenId The NFT to find the approved address for
787 /// @return The approved address for this NFT, or the zero address if there is none
785 /// @dev EVM selector for this function is: 0x081812fc,788 /// @dev EVM selector for this function is: 0x081812fc,
786 /// or in textual repr: getApproved(uint256)789 /// or in textual repr: getApproved(uint256)
787 function getApproved(uint256 tokenId) external view returns (address);790 function getApproved(uint256 tokenId) external view returns (address);
modifiedtests/src/eth/nonFungible.test.tsdiffbeforeafterboth
--- a/tests/src/eth/nonFungible.test.ts
+++ b/tests/src/eth/nonFungible.test.ts
@@ -230,6 +230,14 @@
     const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
 
     {
+      const badTokenId = await contract.methods.nextTokenId().call() + 1;
+      await expect(contract.methods.getApproved(badTokenId).call()).to.be.rejectedWith('revert TokenNotFound');
+    }
+    {
+      const approved = await contract.methods.getApproved(tokenId).call();
+      expect(approved).to.be.equal('0x0000000000000000000000000000000000000000');
+    }
+    {
       const result = await contract.methods.approve(spender, tokenId).send({from: owner});
 
       const event = result.events.Approval;
@@ -238,6 +246,10 @@
       expect(event.returnValues.approved).to.be.equal(spender);
       expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);
     }
+    {
+      const approved = await contract.methods.getApproved(tokenId).call();
+      expect(approved).to.be.equal(spender);
+    }
   });
 
   itEth('Can perform setApprovalForAll()', async ({helper}) => {