简体   繁体   中英

Why my web3.js call ethereum smart contract without error but have no effect?

I had used MIST to deploy an ethereum token contract to my private chain. I can use MIST to interact with this token contract without any problem. I can transfer token between accounts. But when I use my web3.js script to interact with my private chain, it had no error BUT it just couldn't transfer token between accounts. But I can use this web3.js script to get correct balance of the accounts. I am new to ethereum development. Any helps are welcome! I use the command to start private chain:

geth --identity "ImsaIco" --ipcdisable --nodiscover --rpc --rpcport "9001" --rpccorsdomain "*" --datadir "D:\abiz\blc\dev\icoc\chain" --port "8001" --rpcapi "db,eth,net,web3,personal" --networkid 1001 --mine

To let Mist connect to my private chain I use this command:

"D:\abiz\blc\software\mist\Mist.exe" --rpc "\\.\pipe\geth.ipc"

This is my web3.js script:

var Web3 = require('web3');
var net = require('net');
var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9001"));

var accounts;
// Copied from MIST interface
var heCoinContractAbi = [ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string", "value": "HeCoin" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "approve", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256", "value": "2e+25" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8", "value": "18" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "burn", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256", "value": "56000000000000000000" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "burnFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string", "value": "HEC" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" }, { "name": "_extraData", "type": "bytes" } ], "name": "approveAndCall", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" }, { "name": "", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "inputs": [ { "name": "initialSupply", "type": "uint256", "index": 0, "typeShort": "uint", "bits": "256", "displayName": "initial Supply", "template": "elements_input_uint", "value": "20000000" }, { "name": "tokenName", "type": "string", "index": 1, "typeShort": "string", "bits": "", "displayName": "token Name", "template": "elements_input_string", "value": "HeCoin" }, { "name": "tokenSymbol", "type": "string", "index": 2, "typeShort": "string", "bits": "", "displayName": "token Symbol", "template": "elements_input_string", "value": "HEC" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Burn", "type": "event" } ];


 web3.eth.getAccounts(function(error, response){ 
    if(!error) {
        accounts = response;
        callContractTransfer();
        //getContractAccountBalance();
     } else {
        console.error(error); // there was an error, so let's see it.
     }
});

function callContractTransfer() {
    var hecoinContract = new web3.eth.Contract(heCoinContractAbi);
    hecoinContract.options.address = '0xa960fFc27EF72f2db7AB6c86BaB9549FFcf20717'; // Copied from MIST interface
    hecoinContract.methods.transfer(accounts[1], web3.utils.toWei('30', 'ether')).call({from: accounts[0]}, function(error, result) {
        console.log('error:' + error + '!');
        console.log('result:' + JSON.stringify(result) + '!');
    });
}

function getContractAccountBalance() {
    var hecoinContract = new web3.eth.Contract(heCoinContractAbi);
    hecoinContract.options.address = '0xa960fFc27EF72f2db7AB6c86BaB9549FFcf20717';
    hecoinContract.methods.balanceOf(accounts[1]).call({from: accounts[0]}, function(error, result) {
        console.log('getContractAccountBalance error:' + error + '!');
        console.log(accounts[1] + ':getContractAccountBalance result:' + result + '!');
    });
}

According to https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-call the method call :

Will call a “constant” method and execute its smart contract method in the EVM without sending any transaction.

You should use the "send" method : https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send with your function "transfer"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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