0

i'm trying to test a crowdsale with a already existing token with fixed supply. I have my token already in place and everything is working just fine if i used the account to transfer my tokens but i can not attached to a crowdsale, for token i used the ERC20 standard , maybe i missed something.

my token is something like where StandardToken is the pattern from https://github.com/OpenZeppelin/zeppelin-solidity/tree/master/contracts/token :

contract INOToken is StandardToken, Ownable {

string public constant name = "test";
string public constant symbol = "WOX";
uint8  public constant decimals = 18; 

functionINOToken() public {
    totalSupply = 300 * 10**6  *  10**18;       
    balances[owner] = totalSupply; // put all the tokens on the owner 
                                   // address
}

function getTotalSupply() public view returns (uint256) {
    return totalSupply;
  }
}

in this point, token is deployed and is working fine.

Next, i have a very simple contract for simulations of a crowdsale, but i can not use the already existing token, i tried with using an interface and using the token address. What i'm missing, thanks a lot for your help.

 interface token {
     function balanceOf(address who) public constant returns (uint256);
     function transfer(address to, uint256 value) public returns (bool);
     function getTotalSupply() public view returns (uint256);
 }


contract INOCoinBaseCrowdsale {
   using SafeMath for uint256;

  // The token being sold
  token public tokenReward;

  //commented version 
  // INOCoinToken public tokenReward;

  // address where funds are collected
  address public wallet;

  address public tokenAddress;

  // amount of raised money in wei
  uint256 public weiRaised;

  //  crowdsale limit 
  uint256 public constant CROWDSALE_LIMIT = 1 * (10 ** 6) * (10 ** 18);


  event INOCoinTokenPurchase(address indexed purchaser, address indexed       
                       beneficiary, uint256 value, uint256 amount);

  function INOCoinBaseCrowdsale(address _wallet, address _tokens) public{
         require(_wallet != address(0));
         tokenAddress = _tokens;
         tokenReward = token(tokenAddress);
         wallet = _wallet;
   }

  // fallback function can be used to buy tokens
  function () public payable {
      buyTokens(msg.sender);
  }

  // low level token purchase function
  function buyTokens(address beneficiary) public payable {
       require(beneficiary != address(0));
       require(validPurchase());

       uint256 weiAmount = msg.value;

       // calculate token to be substracted
       uint256 tokens = computeTokens(weiAmount);

       require(isWithinCrowdsaleLimit(tokens));

       // update state
       weiRaised = weiRaised.add(weiAmount);

      // send tokens to beneficiary
      tokenReward.transfer(beneficiary, tokens);

      INOCoinTokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

     forwardFunds();
 }


  // send ether to the fund collection wallet
  function forwardFunds() internal {
      wallet.transfer(msg.value);
  }

 // @return true if the transaction can buy tokens
 function validPurchase() internal view returns (bool) {
     return msg.value != 0;
  }


 function isWithinCrowdsaleLimit(uint256 _tokens) internal view returns 
     (bool) {
    uint256 soldAmount = 
    tokenReward.getTotalSupply().sub(tokenReward.balanceOf(tokenAddress));

    uint256 soldAmountAfterTransfer = soldAmount.add(_tokens); 

    return soldAmountAfterTransfer <= CROWDSALE_LIMIT;  
 }


function computeTokens(uint256 weiAmount) internal view returns (uint256) {
    uint256 appliedBonus = 0;

    return weiAmount.mul(30).mul(100 + appliedBonus);
  }
 }

Many thanks in advance

1 Answer 1

2

The problem you are most likely facing is that when you deploy your token contract you are assigning all the balance to the account deploying the contract (you).

But then, when someone buys tokens, the crowdsale contract is attempting to transfer the tokens the crowdsale should have, but it has none.

You either have the crowdsale contract be the one that deploys the token, so the balance gets assigned to it. OR, manually transfer tokens from your account to the crowdsale contract.

2
  • Thanks a lot for your information, here are the next actions that i made: 1: i made a transfer of some token from tokenOwner to contract = ok (in i check the balance are there), 2: i approved that contract can spend those tokens = ok. but i can not send ether to contract is tell me that this transaction will fail,..... i'm close to fix this but something is till missing
    – J.P
    Commented Jan 7, 2018 at 15:26
  • solved, tokenReward.getTotalSupply().sub(tokenReward.balanceOf(tokenAddress)); this part must be replaced by tokenReward.balanceOf(this).sub(_tokens)); because i want to test how many tokens are in contract address not in the owner, because those i can not use. many thanks man, all the best
    – J.P
    Commented Jan 8, 2018 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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