Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add test and fix for transfer() function
  • Loading branch information
zgrannan committed Feb 2, 2024
commit b16dc135f286691577d7fd2d860cd8a432a4dba2
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Fix the `StorageVec` type by excluding the `len_cached` field from its type info - [#2052](https://github.com/paritytech/ink/pull/2052)
- ERC-721 contract checks that the token owner is correct in `transferFrom` - [#2093](https://github.com/paritytech/ink/pull/2093)
- ERC-721: `transfer_token_from` now ensures the token owner is correct - [#2093](https://github.com/paritytech/ink/pull/2093)

## Version 5.0.0-rc

Expand Down
24 changes: 20 additions & 4 deletions integration-tests/erc721/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,6 @@ mod erc721 {
to: AccountId,
id: TokenId,
) -> Result<(), Error> {
if self.token_owner.get(id) != Some(from) {
return Err(Error::NotOwner)
};
self.transfer_token_from(&from, &to, id)?;
Ok(())
}
Expand Down Expand Up @@ -253,6 +250,9 @@ mod erc721 {
if !self.approved_or_owner(Some(caller), id) {
return Err(Error::NotApproved)
};
if self.token_owner.get(id) != Some(*from) {
return Err(Error::NotOwner)
};
self.clear_approval(id);
self.remove_token_from(from, id)?;
self.add_token_to(to, id)?;
Expand Down Expand Up @@ -649,13 +649,29 @@ mod erc721 {
assert_eq!(erc721.mint(2), Ok(()));
// Set caller to Bob
set_caller(accounts.bob);
// Bob makes invalid call to transfer_from
// Bob makes invalid call to transfer_from (Alice is token owner, not Frank)
assert_eq!(
erc721.transfer_from(accounts.frank, accounts.bob, 1),
Err(Error::NotOwner)
);
}

#[ink::test]
fn transfer_fails_not_owner() {
let accounts =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
// Create a new contract instance.
let mut erc721 = Erc721::new();
// Create token Id 1 for Alice
assert_eq!(erc721.mint(1), Ok(()));
// Bob can transfer alice's tokens
assert_eq!(erc721.set_approval_for_all(accounts.bob, true), Ok(()));
// Set caller to bob
set_caller(accounts.bob);
// Bob makes invalid call to transfer (he is not token owner, Alice is)
assert_eq!(erc721.transfer(accounts.bob, 1), Err(Error::NotOwner));
}

fn set_caller(sender: AccountId) {
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(sender);
}
Expand Down