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
1 Answer
Reset to default 0There 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
版权声明:本文标题:ethereum - Pitfalls in small smart contract - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744620721a2616007.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论