I was just wondering and do some search on how token works, and what I get is :
Send ethereum to smart contract => Smart contract have a price for the token, the value, and return back the token to msg.sender
as address by dividing amount of value sent and the token price, so I created following contract
pragma solidity ^0.4.0;
contract SimpleToken {
address client;
uint256 public totalSupply;
uint remaining;
uint price;
uint ethval;
mapping (address => uint) investors;
event testDebug(string _msg,address client,uint256 totalSupply,uint receivedValue);
function SimpleToken(uint _totalSupply) //constructor
{
client = msg.sender;
totalSupply = _totalSupply;
remaining = 0;
}
function setEthValue(uint _value){
ethval = _value;
}
function sendToken() payable
{
testDebug("Before",client,totalSupply,investors[msg.sender]);
assert(remaining < totalSupply);
uint noOfTokens = ethval/price;
assert(noOfTokens < totalSupply-remaining);
investors[msg.sender] = noOfTokens;
remaining = remaining+noOfTokens;
testDebug("After",client,remaining,noOfTokens);
client.transfer(noOfTokens);
}
function setTokenPrice(uint _price)
{
price = _price;
}
function getEthVal() constant returns(uint){
return ethval;
}
}
Then I set the input value as follows :
When I run sendToken
function which will be sending token to sender account, why does it return error
The Constructor should be Payable if you send value during transfer
I tried adding payable
to the constructor but it gave me no luck either.
What is wrong with my simple contract ?