Core Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
contract LocalTradingToken is ERC20, Ownable, ReentrancyGuard {
uint256 public cachedTokenPrice = 100 * 10 ** 2; // 1.00
uint256 public cachedBuyFee = 200; // 2%
uint256 public cachedSellFee = 200; // 2%
uint256 public lastFeeUpdate;
uint256 public lastPriceUpdate;
// events
event TokenPriceSynced(uint256 newPrice);
event FeesSynced(uint256 newBuyFee, uint256 newSellFee);
// contrcuts
constructor() ERC20("LocalTradingToken", "LTT") Ownable(msg.sender) {}
// floats of token
function decimals() public pure override returns (uint8) {
return 2;
}
// local oracle
function updateFromOracle(
uint256 newPrice,
uint256 buyFee,
uint256 sellFee
) external onlyOwner {
cachedTokenPrice = newPrice * 10 ** 2;
cachedBuyFee = buyFee;
cachedSellFee = sellFee;
lastPriceUpdate = block.timestamp;
lastFeeUpdate = block.timestamp;
emit TokenPriceSynced(newPrice);
emit FeesSynced(buyFee, sellFee);
}
// buy token
function buyTokens(uint256 amount) external payable nonReentrant {
uint256 cost = (amount * cachedTokenPrice) / 10 ** 2;
uint256 fee = (cost * cachedBuyFee) / 10000;
uint256 total = cost + fee;
require(msg.value >= total, unicode"not eno");
_mint(msg.sender, amount);
}
// sell tokens
function sellTokens(uint256 amount) external nonReentrant {
uint256 value = (amount * cachedTokenPrice) / 10 ** 2;
uint256 fee = (value * cachedSellFee) / 10000;
uint256 net = value - fee;
require(address(this).balance >= net, unicode"not eno");
_burn(msg.sender, amount);
payable(msg.sender).transfer(net);
}
// with draw owner
function withdraw() external onlyOwner {
payable(owner()).transfer(address(this).balance);
}
//
receive() external payable {}
}
Last updated