4

I'm accepting Ethereum and Bitcoin payments in an trading site and I persist user balances in a database. I'd like to be able to receive ERC20 tokens too (like Bancor) if possible. I am not creating new token contract, just want to accept them as payment.

As the %90 of the answers on the web, they suggest polling filter event of Transfer(address,address,uint256) using web3.js. Please correct me if I'm wrong, but how will I get corresponding TX of that payment from that event?

I don't have deep knowledge about Ethereum and smart contracts.

Mirror: https://github.com/ethereum/web3.js/issues/1109

3

3 Answers 3

3

When watching for events from web3, this is the format of the result you get when one is called:

{ address: '0x9c0ac1e0f0a8e0b01c7b652d5fbe094ddff48b81',
  blockNumber: 704227,
  transactionHash: '0x5887ba4e15d51e1cfddf626ecf416a0002085a1e0929fffe1f90ad69d5040081',
  transactionIndex: 0,
  blockHash: '0x5f485133ec662f556d88affccc18a358375de160178c6cf7cc0cec678d833a2a',
  logIndex: 0,
  removed: false,
  event: 'ExampleEvent',
  args: 
   { argOne: BigNumber { s: 1, e: 0, c: [Array] },
     argTwo: '0x374623456fa2' } }

So you can just get the TX from result.transactionHash

Event in contract:

event ExampleEvent(uint argOne, bytes32 argTwo)

Web3 code:

import exampleContractObject from 'path/to/ExampleContract.json'

ExampleContract = web3.eth.contract(exampleContractObject.abi);
contractInstance = ExampleContract.at('0x9c0ac1e0f0a8e0b01c7b652d5fbe094ddff48b81');
exampleEvent = contractInstance.ExampleEvent();

exampleEvent.watch((err, result) => {
  // Do something 
}

EDIT:

The Transfer event from ERC20 tokens would look like this:

{ address: '<contract_address>',
  blockNumber: <block_number>,
  transactionHash: '<transaction_hash>',
  transactionIndex: <tx_index_in_block,
  blockHash: '<block_hash>',
  logIndex: <log_index>,
  removed: <> ,
  event: 'Transfer',
  args: 
   { _from: <address>,
     _two: <address> ,
     _value: <uint256>} }
0

To accept tokens as payment all you need is an Ethereum account. Tokens can be sent to any address. To receive them, the people who own the tokens simply have to send them in a transaction on the network. I'm assuming that whoever deployed the contract to mint the tokens probably also created a front end user interface for accessing the transferTo() function in their token contract.

So basically if you want to accept tokens as payment, all you have to do is post your Ethereum address and they can be sent. If you want to check how many tokens you have you can use etherscan.io or you can write up some code in Solidity that will check the balance. If the token is ERC20-compliant it will have a function in the contract that you should be able to access that will display tokens for any given address. To access that function you will need the contract ABI from the team who deployed it and then write a bit of Solidity code.

If all you are interested in is accepting a token payment, then the person who has the tokens simply has to send them in a transaction to your account and you will have received them.

The filter stuff would be if you're trying to watch the blockchain for any transfer of the tokens and keep a balance going on your local machine, or using Metamask or something. If you want to persist those balances in a local database, you'll probably have to write up a little bit of Solidity code and then get the contract address and ABI so access it.

Cheers

2
  • What about transaction hash? I certainly need it because my current payment infrastructure depends on TX based validation. I persist them and users must be able to see their "deposit" history.
    – Ozgur
    Oct 14, 2017 at 6:33
  • Also, please do not suggest me third party services like etherscan.
    – Ozgur
    Oct 14, 2017 at 6:58
0

The function you need to make this is transfer(address, value). You have to be the owner of the contract to call this function. You can make this changes locally using ganache and calling the method transfer(address, value) from your contract. If you want to make this directly in the EVM you need to use Infura that would give you the link to the network you are running the contract, so then you could call the functions of your contract using web3. For this you must call the abi code of your contract to create an instance and then call the funcions of the contract.

0

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.