我正在学习使用以太区块链构建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错误:内部JSON-RPC错误。{代码:-32603,消息:“内部JSON-RPC错误。”}未捕获(in promise)错误:内部JSON-RPC错误。在Object.InvalidResponse (inpage.js:1)
在MetaMask中,我得到了这个错误
https://stackoverflow.com/questions/53594821
复制相似问题