I know this has been asked before in a couple of places, but I haven't progressed since a day. I have the following code to set the initial owner of my ERC20 token. I use the following code in a POST method, where the user would input their address and I would set them as an owner.

I have deployed the contract using Metamask and the "from" account is the same from which I have deployed it from. The testSendAccount is basically a placeholder I use. Is there anything more I should have added in the code?

The code is the following:

'use strict';
const config = require('../config');
var Web3 = require('web3');
var web3 = new Web3();
const infuraApi = require('infura')(config.infuraApiKey);
//Set a provider (HttpProvider)
if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider);
} else {
  // set the provider you want from Web3.providers
  web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/' + infuraApi));

const EthereumTx = require('ethereumjs-tx');
var accountAddressHex = require('metamaskAccount')(config.metamaskAccount.metamaskAddressHex);
var accountAddressPrivateKey  = require('metamaskAccount')(config.metamaskAccount.metamaskAddressPrivateKey);
var privateKey = new Buffer(accountAddressPrivateKey, 'hex')
var count = web3.eth.getTransactionCount(accountAddressHex);
var contractAddress = require('solidityContract')(config.solidityContract.contractAddress);
var contractAbiArray = require('solidityContract')(config.solidityContract.contractABI);
var contract = web3.eth.contract(contractAbiArray).at(contractAddress );

const testSendAccount= [Redacted];
const gasPrice = web3.eth.gasPrice;
const gasPriceHex = web3.toHex(gasPrice);
const gasLimitHex = web3.toHex(3000000);
//const tokenTransferAmount = 1;//EDIT 1

var tokenIdToAssignHex = contract.nextTokenIdToAssign.sendTransaction( {from: accountAddressHex}, function(err, hash){
        if(!err) { tokenIdToAssignHex = hash;
        console.log(tokenIdToAssignHex); }
        else { console.log("Error"); }

 var tokenIdToAssign = contract.nextTokenIdToAssign.call(function(err, res){
          if(!err) { tokenIdToAssign = res;
            console.log(tokenIdToAssign.toNumber()); }
        else { console.log("Error"); }

var rawTransaction = {
    "from": accountAddressHex,
    "nonce": web3.toHex(count),
    "gasPrice": gasPriceHex,
    "gasLimit": gasLimitHex,
    "to": contractAddress,
    "value": "0x0",
    "data": contract.setInitialOwner.getData(testSendAccount, tokenTransferAmount, {from: accountAddressHex}), //contract.transfer.getData("0xCb...", 10, {from: "0x26..."}),
    "chainId": 0x03 //Ropsten id is 3, replace with 1 for main
var tx = new EthereumTx(rawTransaction);
var serializedTx = tx.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
    if (!err) { console.log( 'contract creation tx: ' + hash); }
    else {

EDIT 1 I have changed, const tokenTransferAmount = 1; to const tokenIdToAssign = contract.nextTokenIdToAssign.call(); since this is the variable that takes care of which should be the next token to be set in the contract.

EDIT 2 I have changed the tokenToAssign and added the

var tokenIdToAssignHex = contract.nextTokenIdToAssign.sendTransaction( {from: accountAddressHex}, function(err, hash){ if(!err) { tokenIdToAssignHex = hash; console.log(tokenIdToAssignHex); } else { console.log("Error"); } });


Here is the contract code for the setInitialOwner:

function setInitialOwner(address _to, uint256 _tokenId) public
    tenKLimit (_tokenId)
    yesZeroAddressOwner (_tokenId) 
    notSelfSend (_to, _tokenId) {

        tokenIdToOwner[_tokenId] = _to;
        balanceOfAddress[_to] = balanceOfAddress[_to].add(1);
        emit Assign(_to, _tokenId);


        uint256 length = balanceOf(msg.sender);
        ownedTokensIndexMapping[_tokenId] = length;
        addressToTokenIdByIndex[msg.sender][length] = _tokenId;

        nextTokenIdToAssign = nextTokenFunc(nextTokenIdToAssign);
       hypeKillsTokensRemainingToAssign = tokensRemainingToAssign.sub(1);
  • Is your code working? If not, what's going wrong?
    – user19510
    Mar 29 '18 at 17:44
  • The code is not working. My idea was that after the code execution, the toAccount would be able to see the token in the Metamask wallet, once I add the token in the UI. But this seems not the case. Also I have edited, a line in the code, to get the nextTokenId. This ID we should use during the transfer to know exactly what number we are sending, but when I to console.log() its value, I get "undefined". Mar 30 '18 at 18:41
  • So, one thing I tried to do was web3.eth.getCode(contractAddress);which yield a null. This is odd, since I deployed the contract using Metamask on the ropsten network. Mar 30 '18 at 19:14
  • Do you have a transaction id? Does the transaction completes with succeed status? Does your ERC20 generates a Tranfer event?
    – Ismael
    Apr 1 '18 at 4:45
  • I was able to make one transaction. But I cannot manage to update the nextTokenToAssign with the tokenIdToAssignHexin order to updata the next token id. So I end up with token 1, which will revert any further transaction. I have added the tokenIdToAssignHex, but it yields an error. Apr 1 '18 at 20:47

web3 1.0 API change.

let myContract = new web3.eth.Contract(abi, contractAddress);
let data = myContract.methods.transfer(toAddress, value).encodeABI();
let rawTx = {
    "nonce": web3.utils.toHex(nonce),
    "gasPrice": "0x3b9aca00",
    "gasLimit": web3.utils.toHex(gasLimit),
    "to": contractAddress,
    "value": "0x00",
    "data": data,
const tx = new Tx(rawTx)
let serializedTx = "0x" + tx.serialize().toString('hex');
web3.eth.sendSignedTransaction(serializedTx).on('transactionHash', function (txHash) {

}).on('receipt', function (receipt) {
    console.log("receipt:" + receipt);
}).on('confirmation', function (confirmationNumber, receipt) {
    //console.log("confirmationNumber:" + confirmationNumber + " receipt:" + receipt);
}).on('error', function (error) {


you can see this: Transferring ERC20 tokens from account using web3 over Ropsten

  • Thanks for the web3 1.0 API change tip. I will have to eventually upgrade to 1.0 too. May 15 '18 at 19:29

Since others may encounter this problem, I will post the solution I arrived at. The problem here was that my nextTokenToAssign variable was not updating properly.

I used [myetherwallet.com][1] to deploy the contract and to check its value. When I deployed the contract, if afterwards I did not call the constructor function, setting the initial value of nextTokenToAssign to 1, it would not update during the successive call to the setInitialOwner function. So the solution simply was to simply call the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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