[Ethereum] the difference between the transfer and transferFrom function in an ERC721 contract

erc-721web3js

Using web.js, I am trying to transfer erc721 tokens across different addresses.
In case of cryptokitties, I can do the transfer using transfer function only while in case of cryptoheroes, I can use the same contract but do the transfer only using transferFrom.

What exactly is the difference between the two?
How can I write a generic code which will work when I want to transfer the tokens for all ERC721 contract? Or do I need to have the contract ABI for each contract?

Best Answer

The function transferFrom in token contracts refers to the transfer of tokens that you do not own, but rather have been approved to spend. It is used together with the approve function, when an address establishes a certain allowance of tokens for another address to spend for them. Hence, when using transferFrom you establish what address the tokens that you are spending are coming from, whereas transfer is just for your own tokens.

And regarding interaction between contracts, you could implement a function to allow transfers of tokens from other contracts directly in your smart contract. However, web3.js is not going to help you achieve that if the function is not present in the smart contract itself. For that, you'd have to specify the contract addresses and ABIs to interact with separately.

Related Topic