-1

I need to test erc20 token transaction in localhost. For that I had run Etherium wallet on my terminal using rpc. I had an error of unknown address. Below is my code.

I had given all details like contract address, others address in the script directly. I am using web3js.

    <?php 
$abi_get=file_get_contents('https://api.etherscan.io/api?module=contract&action=getabi&address=0xf5e7f08c91b5d8579746eaad70ac509e94e2f1d3&apikey=5N8DNJPJJH5J7X7IN3VHSBBCHDIE9SV3GA');
$abi_result = json_decode($abi_get,true);
$abi=$abi_result['result'];


?>
<script src="<?php echo js_url();?>/web3.min.js"></script>


 <script>
transfer();
var http = require('http');
var Web3 = require('web3');
var Tx = require('ethereumjs-tx');
var Bf=require('Buffer');

function transfer()
{
    //var Web3 = require("web3");
    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("http://localhost:8585"));
}

    //var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    var from_address = "0x26650Af2BF9093A32e0E8361F89dB1e7B7609442";
    var toAddress   = "0xabef6c10571f45ceaf245dddf98894527c87677f";
    var value        = 10;
     var abiArray        = <?php echo $abi; ?>;
    var contractAddress = "0xf5e7f08c91b5d8579746eaad70ac509e94e2f1d3";
    var contract        = web3.eth.contract(abiArray).at(contractAddress);





   var bal=web3.fromWei(contract.balanceOf(from_address),"ether");

   console.log(bal);
   var trans=contract.transfer(toAddress,web3.toWei(1,"ether"),{from:from_address});


       var rawTransaction = {
        "from": from_address,
        "nonce": web3.toHex(count),
        "gasPrice": "0x04e3b29200",
        "gasLimit": "0x7458",
        "to": contractAddress,
        "value": "0x0",
        "data": contract.transfer.getData(toAddress, 10, {from: from_address}),
        "chainId": 0x03

    };


    var privKey = new Buffer('83c737e4e5030e1fe3a2ed4762147cf9d9da80132d904d424460ed41d4591afd', '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);
    });


}    
</script>
halfer
  • 19,824
  • 17
  • 99
  • 186
Prem
  • 19
  • 7

1 Answers1

0

Sorry low rep or I would comment: You might need to sign the transaction. Sending data is different on etherium than viewing data. Specifically if you aren't using something like metamask as your provider (you are using local host) you will have to manually sign transactions.

Also I'm not sure if this is a throwaway wallet but never post your private key!