harb/src/Stake.sol

156 lines
5.4 KiB
Solidity
Raw Normal View History

2024-02-21 22:20:04 +01:00
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.13;
import "./interfaces/IStake.sol";
import "./interfaces/IHarb.sol";
contract Stake is IStake {
// when ustaking, at least authorizedSupply/minUnstake stake should be claimed
uint256 internal constant MAX_STAKE = 20; // 20% of HARB supply
uint256 internal constant MAX_TAX = 1000; // max 1000% tax
uint256 internal constant TAX_RATE_BASE = 100;
uint256 public immutable totalSupply;
address private immutable tokenContract;
address private immutable taxPool;
/**
* @dev Attempted to deposit more assets than the max amount for `receiver`.
*/
error ExceededAvailableStake(address receiver, uint256 stakeWanted, uint256 availableStake);
error TaxTooLow(address receiver, uint64 taxRateWanted, uint64 taxRateMet, uint256 positionId);
error SharesTooLow(address receiver, uint256 assets, uint256 sharesWanted, uint256 minStake);
error NoPermission(address requester, address owner);
error ExitTooEarly(address owner, uint256 positionID, uint32 creationTimestamp);
struct StakingPosition {
uint256 stakeShare;
address owner;
uint32 creationTimestamp;
uint32 lastTaxPaymentTimestamp;
uint32 taxRate; // value of 60 = 60%
}
uint256 public outstandingStake;
uint256 private lastTokenId;
uint256 public minStake;
mapping (uint256 positionID => StakingPosition) public positions;
constructor(
string memory name,
string memory symbol,
address _tokenContract
) ERC20(name, symbol) {
tokenContract = _tokenContract;
IHarb harb = IHarb(_tokenContract);
totalSupply = 100 * 10 ** 5 * harb.decimals();
taxPool = harb.taxPool();
}
function dormantSupply() public view override returns(uint256) {
return totalSupply * (100 - MAX_STAKE) / 100;
}
function assetsToShares(uint256 assets) private view returns (uint256) {
return assets * totalSupply / IERC20(_tokenContract).totalSupply();
}
function sharesToAssets(uint256 shares) private view returns (uint256) {
return shares * IERC20(_tokenContract).totalSupply() / totalSupply;
}
function snatch(uint256 assets, address receiver, uint64 taxRate, uint256[] positions) public returns(uint256) {
// check lower boundary
uint256 sharesWanted = assetsToShares(assets);
if (sharesWanted < minStake) {
revert SharesTooLow(receiver, assets, sharesWanted, minStake);
}
// run through all suggested positions
for (uint i = 0; i < positions.length; i++) {
StakingPosition pos = positions[i];
// check that tax lower
if (taxRate <= pos.perSecondTaxRate) {
revert TaxTooLow(receiver, taxRate, pos.perSecondTaxRate, i);
}
// dissolve position
_payTax(pos);
_exitPosition(pos);
}
// now try to make a new position in the free space and hope it is big enough
uint256 availableStake = authorizedStake - outstandingStake;
if (sharesWanted > availableStake) {
revert ExceededAvailableStake(receiver, sharesWanted, availableStake);
}
// transfer
SafeERC20.safeTransferFrom(tokenContract, _msgSender(), address(this), assets);
// mint
StakingPosition storage sp = c.funders[lastTokenId++];
sp.stakeShare = shares;
sp.owner = receiver;
sp.lastTaxPaymentTimestamp = now;
sp.creationTimestamp = now;
sp.perSecondTaxRate = taxRate;
outstandingStake += sharesWanted;
return lastTokenId;
}
function exitPosition(uint256 positionID) public {
StakingPosition pos = positions[positionID];
if(pos.owner != _msgSender()) {
NoPermission(_msgSender(), pos.owner);
}
// to prevent snatch-and-exit grieving attack
if(now - pos.creationTimestamp < 60 * 60 * 24 * 3) {
ExitTooEarly(pos.owner, positionID, pos.creationTimestamp);
}
_payTax(pos);
_exitPosition(pos);
}
function payTax(uint256 positionID) public {
StakingPosition pos = positions[positionID];
_payTax(pos);
}
function _payTax(StakingPosition storage pos) private {
uint256 elapsedTime = now - pos.lastTaxPaymentTimestamp;
uint256 assetsBefore = sharesToAssets(pos.stakeShare);
uint256 taxDue = assetsBefore * pos.taxRate * elapsedTime / (365 * 24 * 60 * 60) / TAX_RATE_BASE;
if (taxDue >= assetsBefore) {
// can not pay more tax than value of position
taxDue = assetsBefore;
}
SafeERC20.safeTransfer(tokenContract, taxPool, taxDue);
if (assetsBefore - taxDue > 0) {
// if something left over, update storage
sp.stakeShares = assetsToShares(assetsBefore - taxDue);
sp.lastTaxPaymentTimestamp = now;
} else {
// if nothing left over, liquidate position
outstandingStake -= sp.stakeShare;
delete sp;
}
}
function _exitPosition(StakingPosition storage pos) private {
outstandingStake -= pos.stakeShare;
address owner = pos.owner;
uint256 assets = sharesToAssets(pos.stakeShare);
delete pos;
SafeERC20.safeTransfer(tokenContract, owner, assets);
}
}