Go-Ethereum

通過私有區塊鏈與部署的合約互動錯誤:無效地址

  • February 26, 2019

我已經使用 geth 在我的私有乙太坊區塊鏈上部署了一個智能合約。

智能合約

pragma solidity >=0.4.22 <0.6.0;

contract GreenPoints {

   string public name;
   string public symbol;
   uint8 public decimals = 18;
   uint256 public totalSupply;

   mapping (address => bool) public frozenAccount;
   mapping (address => uint256) public balanceOf;

   event Transfer(address indexed from, address indexed to, uint256 value);
   event FrozenFunds(address target, bool frozen);
   event Burn(address indexed from, uint256 value);

   constructor(
       uint256 initialSupply,
       string memory tokenName,
       string memory tokenSymbol
   ) public {
       totalSupply = initialSupply * 10 ** uint256(decimals);  
       balanceOf[msg.sender] = totalSupply;                
       name = tokenName;                                   
       symbol = tokenSymbol;                               
   }

   function _transfer(address _from, address _to, uint _value) internal {
       require(_to != address(0x0));
       require(balanceOf[_from] >= _value);
       require(balanceOf[_to] + _value >= balanceOf[_to]);
       uint previousBalances = balanceOf[_from] + balanceOf[_to];
       balanceOf[_from] -= _value;
       balanceOf[_to] += _value;
       emit Transfer(_from, _to, _value);
       assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
   }

   function transfer(address _to, uint256 _value) public returns (bool success) {
       require(!frozenAccount[_to]);                           
       require (balanceOf[msg.sender] >= _value);                

       _transfer(msg.sender, _to, _value);
       return true;
   }

   function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {       
       require (_to != address(0x0));                          
       require (balanceOf[_from] >= _value);                   
       require (balanceOf[_to] + _value >= balanceOf[_to]);    
       require(!frozenAccount[_from]);                         
       require(!frozenAccount[_to]);                           

       _transfer(_from, _to, _value);
       return true;
   }

   function burn(uint256 _value) public returns (bool success) {
       require(balanceOf[msg.sender] >= _value);   
       balanceOf[msg.sender] -= _value;            
       totalSupply -= _value;                      
       emit Burn(msg.sender, _value);
       return true;
   }

   function burnFrom(address _from, uint256 _value) public returns (bool success) {
       require(balanceOf[_from] >= _value);                
       balanceOf[_from] -= _value;                         
       totalSupply -= _value;                              
       emit Burn(_from, _value);
       return true;
   }

   function freezeAccount(address target, bool freeze) public {
       frozenAccount[target] = freeze;
       emit FrozenFunds(target, freeze);
   }
}

這就是我在 geth 控制台上初始化變數的方式。

var abi=[ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256" } ], "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" } ], "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" } ], "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" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "frozenAccount", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "target", "type": "address" }, { "name": "freeze", "type": "bool" } ], "name": "freezeAccount", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "name": "initialSupply", "type": "uint256" }, { "name": "tokenName", "type": "string" }, { "name": "tokenSymbol", "type": "string" } ], "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": false, "name": "target", "type": "address" }, { "indexed": false, "name": "frozen", "type": "bool" } ], "name": "FrozenFunds", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Burn", "type": "event" } ];

var contract=web3.eth.contract(abi);

var contractinstance=contract.at('0x6621b0431ed6f533575380f2ba4788f44b5049b6')

我可以通過使用檢查地址餘額contractinstance.balanceOf('0x029285ee26b9da62f138133a9fe40cb8c2219c82')

但我無法使用契約的transferFrom功能

它導致以下錯誤

contractinstance.transferFrom('0x029285ee26b9da62f138133a9fe40cb8c2219c82', '0xe149cb26b99f176367e32c4ce33bc3bcb455cec6', '100')
Error: invalid address
   at web3.js:3930:15
   at web3.js:3756:20
   at web3.js:5025:28
   at map (<native code>)
   at web3.js:5024:12
   at web3.js:5050:18
   at web3.js:5075:23
   at web3.js:4137:16
   at apply (<native code>)
   at web3.js:4223:16

雖然錢包確實存在

personal.listWallets
[{
   accounts: [{
       address: "0x029285ee26b9da62f138133a9fe40cb8c2219c82",
       url: "keystore:///home/lubuntu/Project/data/keystore/UTC--2019-02-20T06-42-47.223857724Z--029285ee26b9da62f138133a9fe40cb8c2219c82"
   }],
   status: "Unlocked",
   url: "keystore:///home/lubuntu/Project/data/keystore/UTC--2019-02-20T06-42-47.223857724Z--029285ee26b9da62f138133a9fe40cb8c2219c82"
}, {
   accounts: [{
       address: "0xe149cb26b99f176367e32c4ce33bc3bcb455cec6",
       url: "keystore:///home/lubuntu/Project/data/keystore/UTC--2019-02-20T06-43-46.766062805Z--e149cb26b99f176367e32c4ce33bc3bcb455cec6"
   }],
   status: "Locked",
   url: "keystore:///home/lubuntu/Project/data/keystore/UTC--2019-02-20T06-43-46.766062805Z--e149cb26b99f176367e32c4ce33bc3bcb455cec6"
}]

我正在嘗試將代幣從我的主賬戶轉移到另一個在我的私有區塊鏈上部署合約之前創建的賬戶。

它在重置預設帳戶後工作

web3.eth.defaultAccount = eth.accounts[0]

進而

myContractInstance.transferFrom("0x029285ee26b9da62f138133a9fe40cb8c2219c82","0xe149cb26b99f176367e32c4ce33bc3bcb455cec6","10000",{from: eth.accounts[0]})

工作正常。

引用自:https://ethereum.stackexchange.com/questions/67629