0

The below is the code for The token:

  pragma solidity ^0.4.16;

   interface tokenRecipient { function receiveApproval(address _from, uint256 
   _value, address _token, bytes _extraData) external; }

   contract CCTCOIN {

string public name;
string public symbol;
uint8 public decimals = 8;
uint256 public totalSupply;

mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;


event Transfer(address indexed from, address indexed to, uint256 value);


event Burn(address indexed from, uint256 value);

constructor(

) public {
    totalSupply = 21000000 * 10 ** uint256(decimals);  
    balanceOf[msg.sender] = totalSupply;               
    name = "CCTCOIN";                                  
    symbol = "CCTC";                               
}


function _transfer(address _from, address _to, uint _value) internal {

    require(_to != 0x0);
    require(balanceOf[_from] >= _value);
    require(balanceOf[_to] + _value >= balanceOf[_to]);
    uint previousBalances = balanceOf[_from] + balanceOf[_to];
    balanceOf[_from] -= _value;
    balanceOf[_to] += _value;
    emit Transfer(_from, _to, _value);
    assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}


function transfer(address _to, uint256 _value) public {
    _transfer(msg.sender, _to, _value);
}


function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
    require(_value <= allowance[_from][msg.sender]);  
    allowance[_from][msg.sender] -= _value;
    _transfer(_from, _to, _value);
    return true;
}


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


function approveAndCall(address _spender, uint256 _value, bytes _extraData)
    public
    returns (bool success) {
    tokenRecipient spender = tokenRecipient(_spender);
    if (approve(_spender, _value)) {
        spender.receiveApproval(msg.sender, _value, this, _extraData);
        return true;
    }
}

function burn(uint256 _value) public returns (bool success) {
    require(balanceOf[msg.sender] >= _value);   
    balanceOf[msg.sender] -= _value;          
    totalSupply -= _value;                      
    emit Burn(msg.sender, _value);
    return true;
}


function burnFrom(address _from, uint256 _value) public returns (bool success) {
    require(balanceOf[_from] >= _value);                
    require(_value <= allowance[_from][msg.sender]);    
    balanceOf[_from] -= _value;                         
    allowance[_from][msg.sender] -= _value;             
    totalSupply -= _value;                              
    emit Burn(_from, _value);
    return true;
}
}

This is the code for token distribution contract, the rate is fixed and when this is executed in ropsten it runs fine with any token address but on the main net it is always throwing an exception:

pragma solidity ^0.4.0;


// ----------------------------------------------------------------------------
// Safe maths
// ----------------------------------------------------------------------------
contract SafeMath {
    function safeAdd(uint a, uint b) internal pure returns (uint c) {
        c = a + b;
        require(c >= a);
    }
    function safeSub(uint a, uint b) internal pure returns (uint c) {
        require(b <= a);
        c = a - b;
    }
    function safeMul(uint a, uint b) internal pure returns (uint c) {
        c = a * b;
        require(a == 0 || c / a == b);
    }
    function safeDiv(uint a, uint b) internal pure returns (uint c) {
        require(b > 0);
        c = a / b;
    }
}


interface token {
    function transfer(address to, uint tokens) external;
    function balanceOf(address tokenOwner) external returns(uint balance);
}





// ----------------------------------------------------------------------------
// Owned contract
// ----------------------------------------------------------------------------
contract Owned {
    address public owner;
    address public newOwner;

    event OwnershipTransferred(address indexed _from, address indexed _to);

    constructor() public {
        owner = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function transferOwnership(address _newOwner) public onlyOwner {
        newOwner = _newOwner;
        emit OwnershipTransferred(owner, _newOwner);
    }

}

contract CCTCSALE is  Owned, SafeMath {


    token public reward;
    uint public Ownerbalance;

    mapping(address => uint) balances;



    constructor() public {
        address CCTCTokenAddress = 0x5f9f9c3148ca875fc28b4075cc3195f7e040a1a4;

        reward = token(CCTCTokenAddress);

    }
    function getbalance() public returns(uint) {
        uint test = reward.balanceOf(this);
        return (test);
    }

    function () public payable {

        uint tokens;
        tokens = msg.value * 500;
        balances[msg.sender] = safeAdd(balances[msg.sender], tokens);
        reward.transfer(msg.sender, tokens);
    }

    function safeWithdrawal() public onlyOwner {
            uint amount = this.balance;
            owner.transfer(amount);
    }


    function withdrawTokens() public onlyOwner{
        Ownerbalance = reward.balanceOf(this);
        reward.transfer(owner, Ownerbalance);

    }

}
  • What operation are you trying to execute in the contract? What error do you get? Do you have transaction hash? – Ismael Jul 24 '18 at 15:54
  • etherscan.io/tx/… – Deependra singh Jul 24 '18 at 18:00
  • This is the transaction of previous contract. This contract also throws the same exception – Deependra singh Jul 24 '18 at 18:00
  • I am trying to send ethers to the contract to get tokens – Deependra singh Jul 24 '18 at 18:01
0

I think the problem is in your fallback function

function () payable public {

    amount = msg.value;
    uint256 tobesent = amount/price;
    tokenReward.transfer(msg.sender, tobesent*10e17);

}

In your transaction you send 0.00028 Ether and in the constructor you set price = 0.00028 ether so you end with tobesent = 1.

Now you want to send tobesent*10e17 = 1e18, but your token has 8 decimals. I think you want to send tobesent*1e8 instead.

  • It worked. However "1e8" transfer 10 tokens, so I have to make "1e7". – Deependra singh Jul 25 '18 at 6:12

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.