Skip to main content
formatting
Source Link
Aniket
  • 3.5k
  • 2
  • 21
  • 43

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.sendermsg.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;
}
}

ThenIThen I set the input value as follows :

enter image description here

When I run sendTokensendToken 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"

The Constructor should be Payable if you send value during transfer

I tried adding payablepayable to the constructor but it gave me no luck either.

Any suggestion ? AnythingWhat is wrong onwith my simple contract ?

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

ThenI set the input value as follows :

enter image description here

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.

Any suggestion ? Anything wrong on my simple contract ?

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 :

enter image description here

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 ?

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Source Link

Solidity - The Constructor should be Payable if you send value during transfer

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

ThenI set the input value as follows :

enter image description here

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.

Any suggestion ? Anything wrong on my simple contract ?