Unable to get state variable in client side from smart contract

时间:2018-12-03 13:24:19

标签: blockchain ethereum solidity smartcontracts truffle

I am learning to build ICO using ethereum block chain. I have written the smart contract for token sale and its working fine. I have also written tests for it but When I am trying to get the value of state varible on client site its giving me error

My Token Sale Code:

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);
    }

}

My migration Code:

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

};

My client side code:

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)
    })

After retun instance.tokenPrice() code doesn't go in the .then function therefore console.log('tokenPrice',tokenPrice) is not working. In chrome Im getting this error

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)

In MetaMask, I'm getting this error

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

1 个答案:

答案 0 :(得分:2)

尝试一下:

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();