Last active
July 18, 2022 09:49
-
-
Save kalloc/bd961565bc63ac307a673331a0bb2d22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.0; | |
interface IERC20 { | |
function totalSupply() external view returns (uint256); | |
function balanceOf(address account) external view returns (uint256); | |
function allowance(address owner, address spender) external view returns (uint256); | |
function transfer(address recipient, uint256 amount) external returns (bool); | |
function approve(address spender, uint256 amount) external returns (bool); | |
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
event Approval(address indexed owner, address indexed spender, uint256 value); | |
} | |
contract gutTest is IERC20 { | |
string public constant name = "@italexey"; | |
string public constant symbol = "test"; | |
uint8 public constant decimals = 18; | |
mapping(address => uint256) public lockedPacked; | |
mapping(address => uint256) public balances; | |
mapping(address => mapping (address => uint256)) allowed; | |
uint256 totalSupply_ = 0; | |
uint256 public periodSecond = 86400 * 200; | |
uint256 public price; | |
constructor() { | |
price = 0.1 ether; | |
} | |
function totalSupply() public override view returns (uint256) { | |
return totalSupply_; | |
} | |
function balanceOf(address tokenOwner) public override view returns (uint256) { | |
return balances[tokenOwner]; | |
} | |
function delayedBuy() payable public returns (uint256 perSecond, uint32 lockedAt, uint32 lockedUntil, uint32 left, uint32 avail, uint256 leftAmount, uint256 availAmount) { | |
require(msg.value > 0, "ERR_NOT_ENOUGH"); | |
claim(); | |
uint256 leftValue = 0; | |
(uint256 perSecond, uint32 lockedAt, uint32 lockedUntil, uint32 left, uint32 avail, uint256 leftAmount, uint256 availAmount) = claimable(msg.sender); | |
if (perSecond > 0) { | |
leftValue = leftAmount * price / 1 ether; | |
} | |
uint256 totalAmount = (msg.value + leftValue) * 1 ether / price; | |
perSecond = totalAmount / periodSecond; | |
lockedPacked[msg.sender] = | |
(perSecond << 64) + (block.timestamp << 32) + (block.timestamp + periodSecond) | |
; | |
return claimable(msg.sender); | |
} | |
function claimable(address sender) public view returns (uint256 perSecond, uint32 lockedAt, uint32 lockedUntil, uint32 left, uint32 avail, uint256 leftAmount, uint256 availAmount) { | |
perSecond = uint128(lockedPacked[sender] >> 64); | |
lockedAt = uint32(uint64(lockedPacked[sender] - (perSecond << 64)) >> 32); | |
lockedUntil = uint32(uint64(lockedPacked[sender] - (perSecond << 64)) - (uint64(lockedAt) << 32)); | |
if(lockedUntil < block.timestamp) { | |
avail = lockedUntil - lockedAt; | |
} else { | |
avail = uint32(block.timestamp - lockedAt); | |
} | |
left = lockedUntil - lockedAt; | |
leftAmount = perSecond * left; | |
availAmount = perSecond * avail; | |
} | |
function claim() public { | |
(uint256 perSecond, uint32 lockedAt, uint32 lockedUntil, , , , uint256 availAmount) = claimable(msg.sender); | |
if(availAmount == 0) return; | |
if(lockedUntil < block.timestamp) { | |
lockedPacked[msg.sender] = 0; | |
} else { | |
lockedAt = uint32(block.timestamp); | |
lockedPacked[msg.sender] = | |
(perSecond << 64) + (uint256(lockedAt) << 32) + uint256(lockedUntil) | |
; | |
} | |
balances[msg.sender] += availAmount; | |
totalSupply_ += availAmount; | |
emit Transfer(address(0x0), msg.sender, availAmount); | |
} | |
function transfer(address receiver, uint256 numTokens) public override returns (bool) { | |
require(numTokens <= balances[msg.sender]); | |
balances[msg.sender] = balances[msg.sender]-numTokens; | |
balances[receiver] = balances[receiver]+numTokens; | |
emit Transfer(msg.sender, receiver, numTokens); | |
return true; | |
} | |
function approve(address delegate, uint256 numTokens) public override returns (bool) { | |
allowed[msg.sender][delegate] = numTokens; | |
emit Approval(msg.sender, delegate, numTokens); | |
return true; | |
} | |
function allowance(address owner, address delegate) public override view returns (uint) { | |
return allowed[owner][delegate]; | |
} | |
function transferFrom(address owner, address buyer, uint256 numTokens) public override returns (bool) { | |
require(numTokens <= balances[owner]); | |
require(numTokens <= allowed[owner][msg.sender]); | |
balances[owner] = balances[owner]-numTokens; | |
allowed[owner][msg.sender] = allowed[owner][msg.sender]-numTokens; | |
balances[buyer] = balances[buyer]+numTokens; | |
emit Transfer(owner, buyer, numTokens); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment