简体   繁体   English

使用web3 Ropsten Infura TestNet传输ERC20令牌

[英]Transferring ERC20 Tokens using web3 Ropsten Infura TestNet

I created a contract token using the sample solidity code tutorial. 我使用示例可靠性代码教程创建了一个合同令牌。 It has a function called transfer to send tokens between accounts: 它有一个名为transfer的函数,用于在帐户之间发送令牌:

function transfer(address _to, uint256 _value)

I need to now connect to this contract using web3, and then send a certain number of tokens generated to another account. 我现在需要使用web3连接到此合同,然后将生成的一定数量的令牌发送到另一个帐户。 I've been struggling with how to do this for quite some time and hoping this community could help. 我一直在努力解决如何做这个问题很长一段时间,并希望这个社区可以提供帮助。 Here is what I have thus far, using web3 version 0.20.0: 这是我到目前为止,使用web3版本0.20.0:

const Web3 = require("web3");
const web3 = new Web3();
web3.setProvider(new 
web3.providers.HttpProvider("https://ropsten.infura.io/XXXXXX"));
var abi = [ {} ] // redacted on purpose
var count = web3.eth.getTransactionCount("0x9...");
var abiArray = abi;
var contractAddress = "0x2...";
var contract =  web3.eth.contract(abiArray).at(contractAddress);

var data = contract.transfer.getData("0x2...", 10000, {from: "0x9..."});
var gasPrice = web3.eth.gasPrice;
var gasLimit = 90000;

var rawTransaction = {
  "from": "0x9...",
  "nonce": web3.toHex(count),
  "gasPrice": web3.toHex(gasPrice),
  "gasLimit": web3.toHex(gasLimit),
  "to": "0x2...",
  "value": "0x1",
  "data": data,
  "chainId": 0x03
};

var privKey = new Buffer('XXXXXXXXXXXXXX', 'hex');
var tx = new Tx(rawTransaction);

tx.sign(privKey);
var serializedTx = tx.serialize();

web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
  if (!err)
      console.log(hash);
  else
      console.log(err);
});

This transaction works but it's sending ether as opposed to the actual ERC20 token. 此事务有效,但它发送的是ether而不是实际的ERC20令牌。 I'm really at a loss for why this is the case and would appreciate any help whatsoever. 我真的不知道为什么会这样,并且会感激任何帮助。

This may be late, but for the future users. 这可能会迟到,但对于未来的用户。 The reason you are sending ethers in place of the token, is that in the raw transaction field of value you are inputting "0x1". 你发送ethers代替令牌的原因是你在输入“0x1”的原始事务字段中的值。 To send the ERC20 token you should leave it at "0x0". 要发送ERC20令牌,您应将其保留为“0x0”。

If you are facing problem in just the ether being sent, make sure you put the value attribute in your raw transaction object as "0x0" and in the to attribute put the smart contract address as shown below : 如果您在发送的以太网中遇到问题,请确保将value属性放在原始事务对象中作为"0x0"并在to属性中放置智能合约地址 ,如下所示:

var rawTransaction = {
  "from": "0x9...",
  "nonce": web3.toHex(count),
  "gasPrice": web3.toHex(gasPrice),
  "gasLimit": web3.toHex(gasLimit),
  "to": "<contract Address>",
  "value": "0x0",
  "data": data,
  "chainId": 0x03
};

If you are still facing problem, read below to see my running version of sending an erc20 Token : 如果您仍然遇到问题,请阅读以下内容,了解我发送erc20令牌的运行版本:

When you are creating the contract instance add the from attribute in the options as below : 在创建合同实例时,在选项中添加from属性,如下所示:

var contract = new web3.eth.Contract(erc20ABI, contractAddress, {
  from: '<Address from where you are spending>'
});

Secondly, while creating the data part of raw transaction do it as below : 其次,在创建原始事务的data部分时,请执行以下操作:

var data = contract.methods.transfer('<Toaddress>', amount)).encodeABI();

Now you can follow the same steps you did to broadcast transaction to the network. 现在,您可以按照与将业务广播到网络相同的步骤进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM