无法执行简单合约

时间:2019-04-14 03:13:40

标签: blockchain private ethereum solidity

仅尝试设置一个简单的存储示例,如此处所示:https://solidity.readthedocs.io/en/v0.5.7/style-guide.html?highlight=simplestorage#natspec这是我到目前为止所拥有的

index.sol

pragma solidity ^0.5.2;

contract SimpleStorage {
    uint storedData;

    function set(uint x) public {
        storedData = x;
    }

    function get() public view returns (uint) {
        return storedData;
    }
}

我运行时生成以下文件

echo var compiledStorage = `solc --combined-json abi,bin,interface contracts/SimpleStorage/index.sol` > contracts/SimpleStorage/index.js

index.js

var compiledStorage = {"contracts":{"contracts/SimpleStorage/index.sol:SimpleStorage":{"abi":"[{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]","bin":"608060405234801561001057600080fd5b5060bd8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806360fe47b11460375780636d4ce63c146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686088565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea165627a7a723058202d6164869800689651c219b1586634d754009604bc8adf8656a4145f348b1ac60029"}},"version":"0.5.7+commit.6da8b019.Darwin.appleclang"}

一切似乎都很好。我使用geth --dev console启动我的私有区块链,确保我的私有区块链上有一个平衡点

> eth.getBalance(eth.accounts[0])
1.15792089237316195423570985008687907853269984665640564039457584007913129639927e+77

然后我加载脚本

> loadScript('contracts/SimpleStorage/index.js')
> compiledStorage
{
  contracts: {
    contracts/SimpleStorage/index.sol:SimpleStorage: {
      abi: "[{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]",
      bin: "608060405234801561001057600080fd5b5060bd8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806360fe47b11460375780636d4ce63c146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686088565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea165627a7a723058202d6164869800689651c219b1586634d754009604bc8adf8656a4145f348b1ac60029"
    }
  },
  version: "0.5.7+commit.6da8b019.Darwin.appleclang"
}

超级

我创建了我的abi,hex和将来的部署交易

> var ABI = compiledStorage.contracts['contracts/SimpleStorage/index.sol:SimpleStorage'].abi
> var bin = '0x' + compiledStorage.contracts['contracts/SimpleStorage/index.sol:SimpleStorage'].bin
> var deploymentTx = {from:eth.accounts[0],data:bin,gas:100000}

我创建我的界面和该界面的实例

> var storageInterface = eth.contract(JSON.parse(ABI))
> var storageInstance = storageInterface.new(deploymentTx)

采矿工作已经完成,我检查以确保我有地址,并且得到了我的合同二进制文件。

> var contractAddress = eth.getTransactionReceipt(storageInstance.transactionHash).contractAddress
> contractAddress
"0x05d7a6d44e773d6e50b6dbf6029d06e09c86dec7"
> var simpleStorage = storageInterface.at(contractAddress)

现在我撞墙了。

> simpleStorage.get.call()
0
> simpleStorage.set.sendTransaction(5, {from:eth.accounts[0]:gas:100000})
...
> simpleStorage.get.call()
0

应该{​​{1}}否?

0 个答案:

没有答案