admin管理员组

文章数量:1391008

Here is a question from a coding interview. I am just wondering what would be the major pitfall in this small smart contract in your opinion:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract TokenSale {
    address public owner;
    uint256 public price = 1 ether;
    uint256 public tokensSold;

    constructor() {
        owner = msg.sender;
    }

    function buyTokens(uint256 amount) external payable {
        require(msg.value == amount * price, "Incorrect Ether amount");

        tokensSold += amount;
        (bool sent, ) = owner.call{value: msg.value}("");
        require(sent, "Failed to send Ether");
    }

    function endSale() external {
        require(msg.sender == owner, "Only owner can end sale");
        selfdestruct(payable(owner));
    }
}

Here is a question from a coding interview. I am just wondering what would be the major pitfall in this small smart contract in your opinion:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract TokenSale {
    address public owner;
    uint256 public price = 1 ether;
    uint256 public tokensSold;

    constructor() {
        owner = msg.sender;
    }

    function buyTokens(uint256 amount) external payable {
        require(msg.value == amount * price, "Incorrect Ether amount");

        tokensSold += amount;
        (bool sent, ) = owner.call{value: msg.value}("");
        require(sent, "Failed to send Ether");
    }

    function endSale() external {
        require(msg.sender == owner, "Only owner can end sale");
        selfdestruct(payable(owner));
    }
}
Share Improve this question asked Mar 15 at 8:04 Tony NagyTony Nagy 768 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

There is no actual token transfer in buyTokens(). In addition, no tracking of tokens sold per buyer.

It would be fairly complicated to distribute the tokens to the buyers after the presale (need to loop though tx history to this contract) if this was in production.

本文标签: ethereumPitfalls in small smart contractStack Overflow