【发布时间】:2019-05-04 19:26:35
【问题描述】:
我正在学习使用以太坊区块链构建 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)
})
retun instance.tokenPrice() 代码没有进入 .then 函数后,因此 console.log('tokenPrice',tokenPrice) 不起作用。 在 chrome 中我得到这个错误
MetaMask - RPC 错误:内部 JSON-RPC 错误。 {代码:-32603,消息: “内部 JSON-RPC 错误。”} 未捕获(承诺)错误:内部 JSON-RPC 错误。 在 Object.InvalidResponse (inpage.js:1)
在 MetaMask 中,我收到此错误
错误:[ethjs-rpc] 有效负载的 rpc 错误 { “ID”:1913523409875 “jsonrpc”: “2.0”, “PARAMS”:[ “0xf8920785174876e8008307a12094ab306a5cb13cca96bb50864e34ad92b3462af4b28711c37937e08000a43610724e0000000000000000000000000000000000000000000000000000000000000005822d45a0ad3178b0e1121d7dacc39a7a90481fd87644eb07e67f0c638b2566827051a08ca03ee4cc4c432bbf02fbbdf9a0f2737c9d65d11a0e98376c86bf8621a343a3b41a”], “方法”: “eth_sendRawTransaction”} 错误:尝试运行调用合约函数的事务, 但收件人地址 0xab306a5cb13cca96bb50864e34ad92b3462af4b2 是 不是合约地址
【问题讨论】:
标签: blockchain ethereum solidity smartcontracts truffle