ERC721 testing transferFrom() from any account

How can I test (truffle test or development console) the following:

await nft.transferFrom(accounts[0], accounts[1], 1); 
await nft.transferFrom(accounts[1], accounts[0],1);

The second line throws

ERC721: transfer caller is not owner nor approved

If I change to

await nft.transferFrom(accounts[1], accounts[0],1,{from: accounts[1]});

the following exception is thrown:

Ownable: caller is not the owner

How can I test a transfer from another account then accounts[0] ?

1 Like

Hi @Homer,

If accounts[1] is the owner of token 1 (and it exists) then the following should work:
await nft.transferFrom(accounts[1], accounts[0],1,{from: accounts[1]});

You can check the ownerOf the token before and after.

I recommend trying in a console:

(To setup I used Create an NFT and deploy to a public testnet, using Truffle)

$ mkdir -p build/contracts/
$ cp node_modules/@openzeppelin/contracts/build/contracts/* build/contracts/
$ npx truffle develop
Truffle Develop started at http://127.0.0.1:9545/

Accounts:
(0) 0x0445c33bdce670d57189158b88c0034b579f37ce
(1) 0x46b68a577f95d02d2732cbe93c1809e9ca25b443
...
truffle(develop)> nft = await ERC721PresetMinterPauserAutoId.new("My NFT","NFT", "https://my-json-server.typicode.com/abcoathup/samplenft/tokens/")
undefined
truffle(develop)> nft.mint(accounts[0])
{ tx:
...
truffle(develop)> nft.mint(accounts[0])
{ tx:
...
truffle(develop)> await nft.transferFrom(accounts[0], accounts[1], 1);
{ tx:
...
truffle(develop)> accounts[1]
'0x46B68A577f95d02d2732cbe93c1809E9CA25b443'
truffle(develop)> await nft.ownerOf(1)
'0x46B68A577f95d02d2732cbe93c1809E9CA25b443'
truffle(develop)> await nft.transferFrom(accounts[1], accounts[0],1, {from:accounts[1]});
{ tx:
...
truffle(develop)> accounts[0]
'0x0445c33BdCe670D57189158b88c0034B579f37cE'
truffle(develop)> await nft.ownerOf(1)
'0x0445c33BdCe670D57189158b88c0034B579f37cE'
1 Like

Perfect. That works! In addition there was a function with a modifier onlyOwner which is called by my transferFrom() function. Thank you very much @abcoathup !

1 Like