0

Sorry if this has been asked , I can't find solution .

What I want to achieve is having token that users can buy and sell (price taken from contract ).I have used web3 before for sending transactions but those contracts does not implement payable methods , just basic transfer and transferFrom which I use this way

 contract.methods.transfer(sendTo, amount).send().then(function (res) {}).catch()

In this scenario money amount is not related to contract as I can pass as many tokens as I want ,but I am pretty sure it is not right .

I was wandering if all token transactions are made this way as I have seen contracts which have price inside of the contract let's say 0.1 ether for instance . My Idea is if web3 is running in front and we can pass any argument we want , how I can be sure that user will get right amount of tokens for the x amount of ether .

I haven't seen any web3 tutorials on this , only raw transfer and transferFrom

Let's say for this contract how I can implement selling/buying the right way , I mean web3 part

 pragma solidity ^0.4.11;

  //safemath and irc200 contracts are skipped

contract FNGToken is IERC20 {

     using SafeMath for uint256;
     string public name = "name";
     string public symbol = "sym";
     uint public decimals = 18;
     uint public _totalSupply = 100000000e18;
     mapping (address => uint256) balances;
     // Owner of account approves the transfer of an amount to another account
    mapping (address => mapping(address => uint256)) allowed;
    uint256 public startTime;
    address public owner;
    // how many token units a buyer gets per wei
    uint public PRICE = 1000;

// amount of raised money in wei

event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
modifier onlyOwner {
    require(msg.sender == owner);
    _;
}

function FNGToken() public payable {
    startTime = now;
    owner = msg.sender;
    balances[owner] = _totalSupply; 
}


function () public payable {
    tokensale(msg.sender);
}


function tokensale(address recipient) public payable {
    require(recipient != 0x0);
    uint256 weiAmount = msg.value;
    uint tokens = weiAmount.mul(getPrice());
    require(_totalSupply >= tokens);
    balances[owner] = balances[owner].sub(tokens);
    balances[recipient] = balances[recipient].add(tokens);
    _totalSupply = _totalSupply.sub(tokens);
    TokenPurchase(msg.sender, recipient, weiAmount, tokens);
}



function totalSupply() public constant returns (uint256) {
    return _totalSupply;
}


function balanceOf(address who) public constant returns (uint256) {
    return balances[who];
}
// Token distribution to founder, develoment team, partners, charity, and bounty
function sendXToken(address to, uint256 value) public onlyOwner {
    require (
        to != 0x0 && value > 0 && _totalSupply >= value
    );
    balances[owner] = balances[owner].sub(value);
    balances[to] = balances[to].add(value);
    _totalSupply = _totalSupply.sub(value);
   Transfer(owner, to, value);
}

function destroyFNGToken(address to, uint256 value) public onlyOwner {
    require (
            to != 0x0 && value > 0 && _totalSupply >= value
        );
    balances[to] = balances[to].sub(value);
}



function transfer(address to, uint256 value) public {
    require (
        balances[msg.sender] >= value && value > 0
    );
    balances[msg.sender] = balances[msg.sender].sub(value);
    balances[to] = balances[to].add(value);
    Transfer(msg.sender, to, value);
}


function transferFrom(address from, address to, uint256 value) public {
    require (
        allowed[from][msg.sender] >= value && balances[from] >= value && value > 0
    );
    balances[from] = balances[from].sub(value);
    balances[to] = balances[to].add(value);
    allowed[from][msg.sender] = allowed[from][msg.sender].sub(value);
    Transfer(from, to, value);
}

function approve(address spender, uint256 value) public {
    require (
        balances[msg.sender] >= value && value > 0
    );
    allowed[msg.sender][spender] = value;
    Approval(msg.sender, spender, value);
}

function allowance(address _owner, address spender) public constant returns (uint256) {
    return allowed[_owner][spender];
}

  function getPrice() public constant returns (uint result) {
    return PRICE;
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.