1

I want to create 15 tokens every time the releaseToken() method receives the string hello. I tried to create this condition but it doesn't work. This is code I've written so far:

pragma solidity ^0.4.11;

import './IERC20.sol';
import './SafeMath.sol';

contract testcoin{

    using SafeMath for uint256;

    uint public _totalSupply = 0;

    string public constant symbol = "TES";
    string public constant name = "testcoin";
    uint8 public constant decimals = 3;
    string public contractString = 'hello';

    uint256 public constant RATE = 500;
    address public owner;

    mapping(address => uint256) balances;
    mapping(address => mapping(address => uint256)) allowed; 

    function releaseToken(string _string)  payable{
        require(keccak256(_string) == keccak256(contractString));
        createTokens();
    }

    function testcoin(){
        owner = msg.sender;
    }

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

    function balanceOf(address _owner) constant returns (uint256 balance){
        return balances[_owner];
    }

    function transfer(address _to, uint256 _value) returns (bool success){
        require(
            balances[msg.sender] >= _value
            && _value > 0
        );
        balances[msg.sender] -= _value;
        balances[_to] += _value;
        Transfer(msg.sender, _to, _value);
        return true;
    }

    function createTokens() payable {
        uint256 tokens = msg.value.mul(RATE);
        balances[msg.sender] = balances[msg.sender].add(tokens);
        _totalSupply = _totalSupply.add(tokens);
        owner.transfer(msg.value);
    }

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

    function balanceOf(address _owner) constant returns (uint256 balance){
        return balances[_owner];
    }

    function transfer(address _to, uint256 _value) returns (bool success){
        require(
            balances[msg.sender] >= _value
            && _value > 0
        );
        balances[msg.sender] = balances[msg.sender].sub(_value); //changed
        balances[_to] += _value;
        Transfer(msg.sender, _to, _value);
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) returns (bool success){
        require(
            allowed[_from][msg.sender] >= _value
            && balances[_from] >= _value
            && _value > 0
        );
        balances[_from] -= _value;
        balances[_to] += _value;
        allowed[_from][msg.sender] -= _value;
        Transfer(_from, _to, _value);
        return true;
    }

    function approve(address _spender, uint256 _value) returns (bool success){
        allowed[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender, _value);
        return true;
    }

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

    event Transfer(address indexed _from, address indexed _to, uint256 _value);

    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

Where should I fix it? Thank you for your answer.

  • What error are you getting? Your require logic seems correct and i am assuming the single quotes around 'hello' are just from a bad copy and paste. – Victory Feb 11 '18 at 20:43
  • What's wrong with single quotes? – smarx Feb 11 '18 at 22:09
1

Your code doesn't compile because there are two definitions of a number of functions: totalSupply, transfer, and balanceOf. I got rid of that (and also SafeMath, just because I didn't have it handy in Remix). After that, things seemed to work. I deployed the contract, and it had a totalSupply of 0. Then I called releaseToken("hello"), sending 1 wei, and totalSupply increased to 500.

Make sure you're sending ether with your call to releaseToken. Right now it just calls createTokens, which creates 500 units per wei sent.

If you want it to instead just create 15 tokens without requiring payment, then you want something like this instead:

function releaseToken(string _string) public {
    require(keccak256(_string) == keccak256(contractString));
    uint256 amount = 15 * uint256(10)**decimals;
    balances[msg.sender] += amount;
    _totalSupply += amount;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.