为什么我的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方法调用:
Will call a “constant” method and execute its smart contract method in the EVM without sending any transaction.
您应该使用 "send" 方法:https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send 与您的函数 "transfer"
我曾使用 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方法调用:
Will call a “constant” method and execute its smart contract method in the EVM without sending any transaction.
您应该使用 "send" 方法:https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send 与您的函数 "transfer"