1

I am new to Ethereium Block Chain and have been trying some code written in solidity as follows.

pragma solidity ^0.5.0;

import "./MyERC20Token.sol";
import "./ERC20Burnable.sol";


contract MyERC20TokenSale {
address payable admin;
MyERC20Token public tokenContract;
uint256 public tokenPrice;
uint256 public tokensSold;

event Sell(address _buyer, uint256 _amount);

constructor(MyERC20Token _tokenContract, uint256 _tokenPrice) public {
    admin = msg.sender;
    tokenContract = _tokenContract;
    tokenPrice = _tokenPrice;
}

function multiply(uint x, uint y) internal pure returns (uint z) {
    require(y == 0 || (z = x * y) / y == x);
}

function buyTokens(uint256 _numberOfTokens) public payable {
    require(msg.value == multiply(_numberOfTokens, tokenPrice));
    require(tokenContract.balanceOf(address(this)) >= _numberOfTokens);
    require(tokenContract.transfer(msg.sender, _numberOfTokens));

    tokensSold += _numberOfTokens;

    emit Sell(msg.sender, _numberOfTokens);
}

function endSale() public {
    require(msg.sender == admin);
    require(tokenContract.transfer(admin, tokenContract.balanceOf(address(this))));

    admin.transfer(address(this).balance);
}
 }

I wish to return all the raised tokens to the admin and burn the unused tokens. But when I am doing endSale(), I am getting Error : invalid opcode.

It will be a really helping if someone can go through this code.

0

require(msg.sender == admin);

You need to send the transaction from the same account that deployed the contract. admin was set at deployment time with admin = msg.sender; in the constructor.

require(tokenContract.transfer(admin, tokenContract.balanceOf(address(this))));

Transfers the token balance of the sale contract to the admin. Should be okay.

admin.transfer(address(this).balance);

Transfer the ether balance to admin. Should be okay.

Best guess is you are not meeting the first requirement.

Hope it helps.

| improve this answer | |
  • It's not helping. I tried. retracing the whole process. – Kunal Verma Feb 10 at 6:40
0

The issue is resolved by changing pragma solidity ^0.5.0 to pragma solidity ^0.5.1. It is working now. Thanks for the help.

| improve this answer | |

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.