无法从智能合约获取客户端的状态变量

Unable to get state variable in client side from smart contract

我正在学习使用以太坊区块链构建 ICO。我已经编写了代币销售的智能合约并且运行良好。我也为它编写了测试,但是当我试图在客户端站点上获取状态变量的值时,它给我错误

我的代币销售代码:

pragma solidity ^0.4.24;

import './KhananiToken.sol';

contract KhananiTokenSale {
    address admin;
    KhananiToken public tokenContract;
    uint256 public tokenPrice;
    uint256 public tokensSold;

    event Sell(
        address   _buyer,
        uint256  _amount
    );

    constructor (KhananiToken _tokenContract, uint256 _tokenPrice ) public {
        //Assign an Admin
        admin = msg.sender; //address of person how deployed the contract

        tokenContract = _tokenContract;
        tokenPrice = _tokenPrice;
    }

    function multiply(uint x, uint y) internal pure returns(uint z) {
        require(y == 0 || (z = x * y) / y == x);
    }

    function buyTokens(uint256 _numberOfTokens) public payable {
        require(msg.value == multiply(_numberOfTokens , tokenPrice));

        require(tokenContract.balanceOf(this) >= _numberOfTokens);        

        require(tokenContract.transfer(msg.sender, _numberOfTokens));        

        tokensSold += _numberOfTokens;

        Sell(msg.sender, _numberOfTokens);
    }

}

我的迁移代码:

module.exports = function(deployer) {
  var tokenSupply = 1000000;
  var tokenPrice = 1000000000000000; // is 0.001 Ehter

  deployer.deploy(KhananiToken, tokenSupply).then(function(TokenAddress){
    return  deployer.deploy(KhananiTokenSale, TokenAddress.address, tokenPrice);
  }); //1000000 it the inital token supply

};

我的客户端代码:

App.contracts.KhananiTokenSale.deployed().then(function(instance){
      khananiTokenSaleInstance = instance;
      return instance.tokenPrice();
    }).then(function(tokenPrice){
      console.log('tokenPrice',tokenPrice)
      console.log('tokenPrice',App.tokenPrice)
      App.tokenPrice = tokenPrice;
      //$('.token-price').html(App.tokenPrice)
    })

重新调整后 instance.tokenPrice() 代码不会进入 .then 函数,因此 console.log('tokenPrice',tokenPrice) 不起作用。 在 chrome 我收到这个错误

MetaMask - RPC Error: Internal JSON-RPC error. {code: -32603, message: "Internal JSON-RPC error."} Uncaught (in promise) Error: Internal JSON-RPC error. at Object.InvalidResponse (inpage.js:1)

在 MetaMask 中,我遇到了这个错误

Error: [ethjs-rpc] rpc error with payload {"id":1913523409875,"jsonrpc":"2.0","params":["0xf8920785174876e8008307a12094ab306a5cb13cca96bb50864e34ad92b3462af4b28711c37937e08000a43610724e0000000000000000000000000000000000000000000000000000000000000005822d45a0ad3178b0e1121d7dacc39a7a90481fd87644eb07e67f0c638b2566827051a08ca03ee4cc4c432bbf02fbbdf9a0f2737c9d65d11a0e98376c86bf8621a343a3b41a"],"method":"eth_sendRawTransaction"} Error: Attempting to run transaction which calls a contract function, but recipient address 0xab306a5cb13cca96bb50864e34ad92b3462af4b2 is not a contract address

试试这个:

App.contracts.KhananiTokenSale.deployed().then(function(instance){
    khananiTokenSaleInstance = instance;
    return instance.tokenPrice.call();
}).then(function(tokenPrice){
    console.log("tokenPrice", tokenPrice);
})

这是一个简单的规则:

  • 当您想进行交易时,即更改区块链中的数据,请使用instance.functionName()
  • 当你只想从区块链读取数据而不更改任何数据时,使用instance.getterFunctionOrVariableName.call();