0

I am trying to create a token contract and the token sale contract.

Token Contract : ERC20 token is ready and it is flawless TokenSale Contract : I have passed the "Token"" contract to "TokenSale" and created it. It has the function "pushTokens". It just pushes the coins from owners address to beneficiary address

When I tested in remix using "JavaScript VM" environment, I have the issue in the below location where I transfer the created ERC20 tokens, it fails. I am not able to understand the issue.

I have changed the ownership to "TokenSale" Contract

Note : I don't receive the ETH from user while they using the "TokenSale" contract

Source code of contracts (Flattened file)

Token : https://textuploader.com/1e2jg

TokenSale : https://textuploader.com/1e2jx

Relevant code from Token Contract

library SafeERC20 {
using SafeMath for uint256;

function safeTransfer(IERC20 token, address to, uint256 value) internal {
    require(token.transfer(to, value));    **---------  Issue Line -------**
}

Relevant code from TokenSale Contract

contract TokenSale is Crowdsale{
  
event CrowdsaleFinalized();

constructor(   
ERC20Mintable token
)

public
Crowdsale(token)  
{ 
    require(address(token) != address(0),"Token is not an address");   
}   

}




contract Crowdsale is ReentrancyGuard {
using SafeMath for uint256;
using SafeERC20 for IERC20;

// The token being sold
IERC20 private _token; 

// Amount of wei raised
uint256 private _releasedToken;

event TokensPurchased(address indexed purchaser, address indexed beneficiary, uint256 amountOfTokens);

constructor (IERC20 token) public { 
    require(address(token) != address(0)); 
    _token = token;
}

function () external payable { 
}


function token() public view returns (IERC20) {
    return _token;
} 

function releasedToken() public view returns (uint256) {
    return _releasedToken;
}

function pushTokens(address payable beneficiary,uint256 rate,uint256 amountOfTokens) public nonReentrant payable  {
    _preValidatePurchase(beneficiary, rate, amountOfTokens);

    // update state
    _releasedToken = _releasedToken.add(amountOfTokens);

    _processPurchase(beneficiary, amountOfTokens);
    emit TokensPurchased(msg.sender, beneficiary, amountOfTokens);

    _updatePurchasingState(beneficiary, amountOfTokens);

    _postValidatePurchase(beneficiary, amountOfTokens);
}


function _preValidatePurchase(address beneficiary, uint256 rate, uint256 amountOfTokens) internal view {
    require(beneficiary != address(0)); 
    require(rate > 0); 
    require(amountOfTokens > 0); 
}


function _postValidatePurchase(address beneficiary, uint256 amountOfTokens) internal view {
    // optional override
}

function _deliverTokens(address beneficiary, uint256 tokenAmount) internal {
    _token.safeTransfer(beneficiary, tokenAmount);  
}


function _processPurchase(address beneficiary, uint256 tokenAmount) internal {
    _deliverTokens(beneficiary, tokenAmount);
}


function _updatePurchasingState(address beneficiary, uint256 amountOfTokens) internal {
    // optional override
}

}






contract TokenSale is Crowdsale{
  
event CrowdsaleFinalized();

constructor(   
ERC20Mintable token
)

public
Crowdsale(token)  
{ 
    require(address(token) != address(0),"Token is not an address");   
}   

}

Error Location

enter image description here

Error

enter image description here

Deploy / Run

enter image description here

  • 1
    Please post ALL the relevant code and ONLY the relevant code, in PLAIN-TEXT which allows copy/paste (i.e., no links or images). – goodvibration Nov 27 '20 at 12:25
  • @goodvibration I have added the code so that it is easy for copy / paste. Please ignore the images, it may help others. Thanks ! – iappmaker Nov 27 '20 at 17:07
  • @iappmaker Who account owns the tokens on sale? The TokenSale contract, Token contrar or the owner account? – Ismael Nov 27 '20 at 21:55
  • @Ismael I have changed the ownership to "TokenSale" Contract. Even then the issue persists. – iappmaker Nov 28 '20 at 14:09
  • @iappmaker I have tried the code in remix and when tokens were deposited in the TokenSale contract it worked as expected. – Ismael Nov 28 '20 at 15:23

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.