git.delta.rocks / unique-network / refs/commits / 0595f0d3e1c9

difftreelog

feat implement ERC721 `getApproved` for NFT.

Trubnikov Sergey2023-03-07parent: #94299ed.patch.diff
in: master

3 files changed

modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
516 }516 }
517517
518 /// @dev Not implemented518 /// @dev Not implemented
519 fn get_approved(&self, _token_id: U256) -> Result<Address> {519 fn get_approved(&self, token_id: U256) -> Result<Address> {
520 // TODO: Not implemetable520 let token = token_id.try_into()?;
521 let operator = <Pallet<T>>::get_allowance(self, token).map_err(dispatch_to_evm::<T>)?;
521 Err("not implemented".into())522 Ok(if let Some(operator) = operator {
523 *operator.as_eth()
524 } else {
525 Address::zero()
526 })
522 }527 }
523528
524 /// @notice Tells whether the given `owner` approves the `operator`.529 /// @notice Tells whether the given `owner` approves the `operator`.
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
1083 }1083 }
1084 }1084 }
1085
1086 pub fn get_allowance(
1087 collection: &NonfungibleHandle<T>,
1088 token: TokenId,
1089 ) -> Result<Option<T::CrossAccountId>, DispatchError> {
1090 ensure! {
1091 <TokenData<T>>::iter_keys().find(
1092 |(c, t)| return *c == collection.id && *t == token).is_some()
1093 ,<CommonError<T>>::TokenNotFound
1094 };
1095 Ok(<Allowance<T>>::get((collection.id, token)))
1096 }
10851097
1086 /// Set allowance for the spender to `transfer` or `burn` sender's token.1098 /// Set allowance for the spender to `transfer` or `burn` sender's token.
1087 ///1099 ///
modifiedtests/src/eth/nonFungible.test.tsdiffbeforeafterboth
240 }240 }
241 });241 });
242
243 itEth('Can perform setApproval()', async ({helper}) => {
244 const owner = await helper.eth.createAccountWithBalance(donor);
245 const operator = helper.eth.createAccount();
246
247 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');
248 const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
249
250 const result = await contract.methods.mint(owner).send({from: owner});
251 const tokenId = result.events.Transfer.returnValues.tokenId;
252
253 {
254 const approved = await contract.methods.getApproved(tokenId).call();
255 expect(approved).to.be.equal('0x0000000000000000000000000000000000000000');
256 }
257 await contract.methods.approve(operator, tokenId).send({from: owner});
258 {
259 const approved = await contract.methods.getApproved(tokenId).call();
260 expect(approved).to.be.equal(operator);
261 }
262 });
263
242264
243 itEth('Can perform setApprovalForAll()', async ({helper}) => {265 itEth('Can perform setApprovalForAll()', async ({helper}) => {