forked from andrecronje/rarity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gold.sol
99 lines (77 loc) · 3.16 KB
/
gold.sol
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
interface rarity {
function level(uint) external view returns (uint);
function getApproved(uint) external view returns (address);
function ownerOf(uint) external view returns (address);
}
contract rarity_gold {
string public constant name = "Rarity Gold";
string public constant symbol = "gold";
uint8 public constant decimals = 18;
uint public totalSupply = 0;
rarity constant rm = rarity(0xce761D788DF608BD21bdd59d6f4B54b2e27F25Bb);
mapping(uint => mapping (uint => uint)) public allowance;
mapping(uint => uint) public balanceOf;
mapping(uint => uint) public claimed;
event Transfer(uint indexed from, uint indexed to, uint amount);
event Approval(uint indexed from, uint indexed to, uint amount);
function wealth_by_level(uint level) public pure returns (uint wealth) {
for (uint i = 1; i < level; i++) {
wealth += i * 1000e18;
}
}
function _isApprovedOrOwner(uint _summoner) internal view returns (bool) {
return rm.getApproved(_summoner) == msg.sender || rm.ownerOf(_summoner) == msg.sender;
}
function claimable(uint summoner) external view returns (uint amount) {
require(_isApprovedOrOwner(summoner));
uint _current_level = rm.level(summoner);
uint _claimed_for = claimed[summoner]+1;
for (uint i = _claimed_for; i <= _current_level; i++) {
amount += wealth_by_level(i);
}
}
function claim(uint summoner) external {
require(_isApprovedOrOwner(summoner));
uint _current_level = rm.level(summoner);
uint _claimed_for = claimed[summoner]+1;
for (uint i = _claimed_for; i <= _current_level; i++) {
_mint(summoner, wealth_by_level(i));
}
claimed[summoner] = _current_level;
}
function _mint(uint dst, uint amount) internal {
totalSupply += amount;
balanceOf[dst] += amount;
emit Transfer(dst, dst, amount);
}
function approve(uint from, uint spender, uint amount) external returns (bool) {
require(_isApprovedOrOwner(from));
allowance[from][spender] = amount;
emit Approval(from, spender, amount);
return true;
}
function transfer(uint from, uint to, uint amount) external returns (bool) {
require(_isApprovedOrOwner(from));
_transferTokens(from, to, amount);
return true;
}
function transferFrom(uint executor, uint from, uint to, uint amount) external returns (bool) {
require(_isApprovedOrOwner(executor));
uint spender = executor;
uint spenderAllowance = allowance[from][spender];
if (spender != from && spenderAllowance != type(uint).max) {
uint newAllowance = spenderAllowance - amount;
allowance[from][spender] = newAllowance;
emit Approval(from, spender, newAllowance);
}
_transferTokens(from, to, amount);
return true;
}
function _transferTokens(uint from, uint to, uint amount) internal {
balanceOf[from] -= amount;
balanceOf[to] += amount;
emit Transfer(from, to, amount);
}
}