最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ethereum - Pitfalls in small smart contract - Stack Overflow

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论