1

我正在尝试在以太坊专用网络中创建合同。

我尝试的是:

在 browser-solidity 中创建了以下合约。

pragma solidity ^0.4.19;

contract SingleNumRegister {
    uint storedData;
    function set(uint x) public {
        storedData = x;
    }
    function get() public constant returns (uint retVal)  {
        return storedData;
    }
}

并复制 web3 部署代码如下。

var singlenumregisterContract = 
web3.eth.contract([{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]);
var singlenumregister = singlenumregisterContract.new(
{
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000f57600080fd5b60d38061001d6000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c14606e575b600080fd5b3415605857600080fd5b606c60048080359060200190919050506094565b005b3415607857600080fd5b607e609e565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a72305820cc0de7aa0d676f08b124d9f878aeecd0c305703a9d59ccde63b557dd8585dfcb0029', 
     gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
     console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})

然后,复制到控制台中。

> personal.unlockAccount(eth.accounts[0])
Unlock account 0x24636f1423f131f5441fbee83323c53c59af247d
Passphrase: 
true
> var singlenumregisterContract = 
web3.eth.contract([{"constant":false,"inputs":
[{"name":"x","type":"uint256"}],"name":"set","outputs":
[],"payable":false,"stateMutability":"nonpayable","type":"function"},
{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]);
undefined
>
> var singlenumregister = singlenumregisterContract.new(
...    {
......      from: web3.eth.accounts[0], 
......      data: '0x6060604052341561000f57600080fd5b60d38061001d6000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c14606e575b600080fd5b3415605857600080fd5b606c60048080359060200190919050506094565b005b3415607857600080fd5b607e609e565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a72305820cc0de7aa0d676f08b124d9f878aeecd0c305703a9d59ccde63b557dd8585dfcb0029', 
......      gas: '4700000'
......    }, function (e, contract){
......     console.log(e, contract);
......     if (typeof contract.address !== 'undefined') {
.........          console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
.........     }
......  })
null [object Object]
undefined
> 

如上所述,我得到了未定义的两个脚本。

我该如何解决这个问题?

我使用以下环境:

  • macOS 10.12.6
  • Geth:1.7.3-稳定
  • Solc:0.4.19+commit.c4cbbb05.Darwin.appleclang
4

1 回答 1

3

你需要JSON.parse()你的 ABI。

var jsonAbi = JSON.parse('[{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]');

var singlenumregisterContract = web3.eth.contract(jsonAbi);
于 2018-01-12T06:45:50.520 回答