繁体   English   中英

为什么我的web3.js调用以太坊智能合约没有错误但没有效果?

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

我曾使用MIST将以太坊代币合约部署到我的私人链中。 我可以使用MIST与此令牌合约进行交互,而不会出现任何问题。 我可以在帐户之间转移令牌。 但是,当我使用web3.js脚本与我的私有链进行交互时,它没有错误,但是它无法在帐户之间转移令牌。 但是我可以使用此web3.js脚本来获取正确的帐户余额。 我是以太坊开发的新手。 欢迎任何帮助! 我使用以下命令启动私有链:

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

为了让Mist连接到我的私有链,我使用以下命令:

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

这是我的web3.js脚本:

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 + '!');
    });
}

根据https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-call方法调用:

将调用“常量”方法并在EVM中执行其智能合约方法,而无需发送任何交易。

您应该使用“发送”方法: https : //web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send与函数“传输”一起使用

暂无
暂无

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

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