-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathYieldTokenContract.sol
More file actions
108 lines (94 loc) · 3.92 KB
/
Copy pathYieldTokenContract.sol
File metadata and controls
108 lines (94 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./Interfaces.sol";
contract YieldTokenContract is ERC20, Ownable {
uint256 public constant BASE_RATE_XSEC = 34722222222223; // * 3 eth (daily) / 86400 (seconds in a day)
uint256 public constant MINT_GIFT = 100 ether;
// Apr 30 2033 13:33:33 GMT+0000
uint256 public constant END = 1998480813;
mapping(address => uint256) public Rewards;
mapping(address => uint256) public LastUpdate;
IMainContract public MainContract;
event RewardPaid(address indexed user, uint256 reward);
constructor(address maincontract) ERC20("MutantCawSeed", "MCS") Ownable(msg.sender) {
MainContract = IMainContract(maincontract);
}
function mint(address to, uint256 amount) public {
require(msg.sender == address(MainContract) || msg.sender == owner());
_mint(to, amount);
}
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
// called when minting many NFTs
// updated_amount = (balanceOG(user) * base_rate * delta / 86400) + amount * initial rate
function updateRewardOnMint(address user, uint16 amount) external {
require(msg.sender == address(MainContract), "Can't call this");
uint256 time = min(block.timestamp, END);
uint256 timerUser = LastUpdate[user];
unchecked {
if (timerUser > 0) {
uint256 reward = Rewards[user] + (MainContract.balanceOf(user) * BASE_RATE_XSEC * (time - timerUser));
reward = reward + amount * MINT_GIFT;
Rewards[user] = reward;
} else {
Rewards[user] = Rewards[user] + amount * MINT_GIFT;
}
}
LastUpdate[user] = time;
}
// called on transfers
function updateReward(
address from,
address to /*, uint256 _tokenId*/
) external {
require(msg.sender == address(MainContract));
//if (_tokenId < 1001) {
uint256 time = min(block.timestamp, END);
uint256 timerFrom = LastUpdate[from];
if (timerFrom > 0 && time > timerFrom)
Rewards[from] = Rewards[from] + (MainContract.balanceOf(from) * BASE_RATE_XSEC * (time - timerFrom));
if (timerFrom != END && time > timerFrom) {
LastUpdate[from] = time;
}
if (to != address(0)) {
uint256 timerTo = LastUpdate[to];
if (timerTo > 0 && time > timerTo)
Rewards[to] = Rewards[to] + (MainContract.balanceOf(to) * BASE_RATE_XSEC * (time - timerTo));
if (timerTo != END && time > timerTo) {
LastUpdate[to] = time;
}
}
//}
}
function getReward(address to) external {
require(msg.sender == address(MainContract));
uint256 reward = Rewards[to];
if (reward > 0) {
Rewards[to] = 0;
_mint(to, reward);
emit RewardPaid(to, reward);
}
}
function burn(address from, uint256 amount) external {
require(msg.sender == address(MainContract));
_burn(from, amount);
}
function collect(address from, uint256 amount) external {
require(msg.sender == address(MainContract));
uint256 rew = Rewards[from];
require(rew >= amount, "amount");
Rewards[from] = rew - amount;
//console.log("reward %s - collect %s tokens from %s - balance", from, amount, rew, Rewards[from]);
}
function getTotalClaimable(address user) external view returns (uint256) {
uint256 time = min(block.timestamp, END);
uint256 pending = 0;
if (LastUpdate[user] > 0 && time > LastUpdate[user]) {
pending = (MainContract.balanceOf(user) * BASE_RATE_XSEC * (time - LastUpdate[user]));
}
return Rewards[user] + pending;
}
}