0

So I learnt that ERC20 tokens have Transfer and TransferFrom event. From my understanding Transfer event is when a transaction comes from an address to the smart contract instructing the transfer of some of its tokens to another address. TransferFrom, comes from an address that has been pre approved to transfer tokens from another address.

Here are my questions:

  • What event is triggered when normal transfer of Eth from one account to another account occurs?
  • What example are there on how to subscribe to these events using the JSON_RPC endpoints? Is subscribing to ERC20 token events different from subscribing to normal Ethereum transfer?
  • How will same transactions look like using a library like ethers-rs

1 Answer 1

1

ERC20 tokens are smart contracts that comply with the EIP-20 standard.

This standard states 2 different transfer functions: transfer and transferFrom, which work as you have defined:

  • transfer

Transfers _value amount of tokens to address _to, and MUST fire the Transfer event. The function SHOULD throw if the message caller’s account balance does not have enough tokens to spend. Note Transfers of 0 values MUST be treated as normal transfers and fire the Transfer event.

function transfer(address _to, uint256 _value) public returns (bool success)
  • transferFrom

Transfers _value amount of tokens from address _from to address _to, and MUST fire the Transfer event. The transferFrom method is used for a withdraw workflow, allowing contracts to transfer tokens on your behalf. This can be used for example to allow a contract to transfer tokens on your behalf and/or to charge fees in sub-currencies. The function SHOULD throw unless the _from account has deliberately authorized the sender of the message via some mechanism. Note Transfers of 0 values MUST be treated as normal transfers and fire the Transfer event.

function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)

They both refer to the same event Transfer:

  • Transfer

MUST trigger when tokens are transferred, including zero value transfers. A token contract which creates new tokens SHOULD trigger a Transfer event with the _from address set to 0x0 when tokens are created.

event Transfer(address indexed _from, address indexed _to, uint256 _value)

Finally, answering your questions:

  • What event is triggered when normal transfer of Eth from one account to another account occurs?

There is no event emitted when an account transfers Ether to another account as events are accessible solely by declaring them in smart contracts and then calling them.

  • What example are there on how to subscribe to these events using the JSON_RPC endpoints? Is subscribing to ERC20 token events different from subscribing to normal Ethereum transfer?

You can subscribe to ERC20 Transfer events using ethers.js as documentation shows:

// This filter could also be generated with the Contract or
// Interface API. If address is not specified, any address
// matches and if topics is not specified, any log matches
filter = {
    address: "dai.tokens.ethers.eth",
    topics: [
        utils.id("Transfer(address,address,uint256)")
    ]
}
provider.on(filter, (log, event) => {
    // Emitted whenever a DAI token transfer occurs
})

You cannot directly subscribe to Ether transfer transactions as they are potentially every single transaction.

  • How will same transactions look like using a library like ethers-rs

You can find event examples in ethers.js documentation:

abi = [
  "event Transfer(address indexed src, address indexed dst, uint val)"
];

contract = new Contract(tokenAddress, abi, provider);

// List all token transfers *from* myAddress
contract.filters.Transfer(myAddress)
// {
//   address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
//   topics: [
//     '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
//     '0x0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba72'
//   ]
// }
1
  • if no event is triggered with normal transaction, how do one listen to "transfer activities"? Dec 29, 2022 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.