Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct way to send erc20 token with web3? #2017

Closed
medied opened this issue Oct 24, 2018 · 7 comments
Closed

Correct way to send erc20 token with web3? #2017

medied opened this issue Oct 24, 2018 · 7 comments
Labels

Comments

@medied
Copy link

medied commented Oct 24, 2018

Couple of similar issues are #1775 and #1151, but one difference is that I'm using web3 v0.20.7. I was initially going off of human-standard-token-abi docs (reading calls such as balanceOf() are working fine), then tried with sendTransaction() directly. I'm getting the same error in both approaches though:

Failed to load resource: the server responded with a status of 405 () [https://rinkeby.infura.io/v3/xxx] DEBUG - error in _sendToken web3Util.js:148 Error: Invalid JSON RPC response: ""

This is what the method looks like:

const _sendToken = (contractAddress, _to, _from, value) => {
  const contract = web3.eth.contract(abi).at(contractAddress);

  const rawTransaction = {
    from: _from,
    to: contractAddress,
    value: 0,
    data: contract.transfer.getData(_to, value),
    gas: 200000,
    chainId: 3,
  };

  web3.eth.sendTransaction(rawTransaction, (error, receipt) => {
    if (error) {
      console.log('DEBUG - error in _sendToken ', error)
    }
    console.log(receipt);
  });

  // Initial try following human-standard-token-abi
  // tokenInstance.transfer(_to, value, { from: _from }, function (err, txHash) {
  //   if (err) console.error(err);

  //   if (txHash) {
  //     console.log('Transaction sent!');
  //     console.dir(txHash);
  //   }
  // });
};

Any insight on what to try? What's the right way to do this with web3 v0.20.7

@vibern0
Copy link

vibern0 commented Oct 24, 2018

Hi,
send from one account to another? Are you using metamask extension?

I've never done this kind of thing the way you are doing. I always have an ERC20 contract that holds balances and has a transfer method. Just call that method. Done.
In the end, if you follow the ERC20 standard you have those methods ethereum/EIPs#20

Btw, I'm using web3.js and open-zeppelin for a while, always using the most recent versions (even the beta ones) and I never had problems.

@medied
Copy link
Author

medied commented Oct 24, 2018

Hey @obernardovieira, yea just from one account to another. I have Metamask available but I initially thought it wasn't going to be necessary precisely because of the standard erc20 transfer() method you mention. As in something like below outlined here:

tokenInstance.transfer(_to, value, { from: _from }, function (err, txHash) {
    if (err) console.error(err);

    if (txHash) {
      console.log('Transaction sent!');
      console.dir(txHash);
    }
  });

What version of web3 are you using? Token I'm testing with is with Zeppelin tech as well, using zOS

@vibern0
Copy link

vibern0 commented Oct 25, 2018

web3-1.0.0-beta35
Does this example you posted, work ?

@medied
Copy link
Author

medied commented Oct 25, 2018

No with that example I'm getting the Invalid JSON RPC response: "" error as well, starting to wonder if the web3 version is the issue (am using v0.20.7)

@nivida nivida added the 0.x.x label Oct 31, 2018
@Aniket-Engg
Copy link

@medied In your rawtransaction you are passing chainId as 3 and you seems sending transaction using rinkeby infura whose id is 4. I think this mismatch may be a reason for the error.

@medied
Copy link
Author

medied commented Nov 20, 2018

@Aniket-Engg @obernardovieira thank you both for looking into this! I ended up upgrading web3 to 1.x.x and was able to get it working, safe to close this issue from my end

@medied medied closed this as completed Nov 20, 2018
@jsliu27
Copy link

jsliu27 commented Nov 30, 2018

contract.transfer
contract.transfer is undefined
web vue + web3
web3@0.20.X
@medied

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants