0

When I call mint function from token address it doesn't send the tokens to given address but sends to token address.

function mint(address _beneficiary, uint256 _value) public returns (bool success) { require( msg.sender == saleAddress || msg.sender == admin, "Sender must be saleAddress or admin."); balanceOf[_beneficiary] += _value; supply += _value; emit Mint(msg.sender, _beneficiary, _value); return true; Event:

event Mint(
address indexed _sender,
address indexed _beneficiary,
uint256 _value

It's interesting that this call from Sales Contract works:

function startNewPhase(uint256 _tokenPrice, uint256 tokenAmount) public {
require(msg.sender == admin, "Can only be inoked by admin");
require(tokenContract.mint(address(this), tokenAmount));
saleActive = true;
tokenPrice = _tokenPrice;

Whole code:

Migrations.sol:

  // SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract Migrations {
  address public owner = msg.sender;
  uint public last_completed_migration;

  modifier restricted() {
    require(
      msg.sender == owner,
      "This function is restricted to the contract's owner"
    );
    _;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }
}

Sale.sol:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;


import "./Token.sol";

contract Sale {

    address admin;
    Token public tokenContract;
    uint256 public tokenPrice;
    uint256 public tokenSold;
    bool public saleActive;

    event Sell(
        address  _buyer,
        uint256 _amount
    );

    constructor(Token _tokenContract, uint256 _tokenPrice) {
        admin = msg.sender;
        tokenContract = _tokenContract;
        tokenPrice = _tokenPrice;
        saleActive = true;
    }
    
    modifier onlyOwner {
        require(msg.sender == admin, "Can only be inoked by admin");
        _;
    }

    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(saleActive == true, "Sale inactive");
        require(msg.value == multiply(_numberOfTokens, tokenPrice));
        require(tokenContract.balanceOf(address(this)) >= _numberOfTokens, "Not enough tokens for sale");
        require(tokenContract.transfer(msg.sender, _numberOfTokens));

        tokenSold += _numberOfTokens;
        emit Sell(msg.sender, _numberOfTokens);
    }

    function startNewPhase(uint256 _tokenPrice, uint256 tokenAmount) public onlyOwner {
        require(tokenContract.mint(address(this), tokenAmount));
        saleActive = true;
        tokenPrice = _tokenPrice;

    }

    function endSale() public onlyOwner{
        require(tokenContract.transfer(admin, tokenContract.balanceOf(address(this))));
        payable(admin).transfer(address(this).balance);
        saleActive = false;
    }
}

Token.sol:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Token {
    
    string public name = "Token";
    string public symbol = "TKN";
    
    address public admin;
    address public saleAddress;

    uint256 public totalSupply = 5500000;
    uint256 public cicrulatingSupply;
    uint256 public decimals = 5;

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

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

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

    event Mint(
        address indexed _sender,
        address indexed _beneficiary,
        uint256 _value
    );

    constructor() {
        admin = msg.sender;
    }

    function writeSale(address _saleAddress) public returns (bool success) {
        require(msg.sender == admin, "Sender must be address");
        saleAddress = _saleAddress;
        return true;
    }

    function transfer (address _to, uint256 _value) public returns (bool success) {
        require(balanceOf[msg.sender] >= _value, 'Not enough funds');
        
        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
        return true;
    }

    function mint(address _beneficiary, uint256 _value) public returns (bool success) {
        require( msg.sender == saleAddress || msg.sender == admin, "Sender must be saleAddress or admin.");
        require( cicrulatingSupply + _value <= totalSupply, "Reached totalSupply cap");
        balanceOf[_beneficiary] += _value;
        cicrulatingSupply += _value;
        emit Mint(msg.sender, _beneficiary, _value);
        return true;
    }

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

        emit Approval(msg.sender, _spender, _value);
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require( balanceOf[_from] >= _value);
        require (allowance[_from][msg.sender] >= _value);

        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;

        allowance[_from][msg.sender] -= _value;

        emit Transfer(_from, _to, _value);
        return true;
    }
    
            
}

1 Answer 1

1

Your code looks fine.

  1. In Mint function the token is getting added to beneficiary
  2. You are passing beneficiary as sale contract. So amount will sent to sale contract.

Can you post your transaction, for clear understanding.

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.