Kishu 源码分析

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
contract KishuInu is Context, IERC20, Ownable {
using SafeMath for uint256;
using Address for address;

/// 如果账户在 排除名单 外,其持有额度将记录在 _rOwned 中
mapping (address => uint256) private _rOwned;
/// 如果账户在 排除名单 中,其持有额度将记录在 _tOwned 中
mapping (address => uint256) private _tOwned;
/// 授权额度
mapping (address => mapping (address => uint256)) private _allowances;
/// 在 _excluded 数组中的账户,是否是 excluded
mapping (address => bool) private _isExcluded;
/// 在 排除名单 中的账户记录在 _excluded 数组中
address[] private _excluded;
/// 最大 int 值
uint256 private constant MAX = ~uint256(0);
/// 发行总量,这个值是常量,不会变化
uint256 private constant _tTotal = 100000000000 * 10**6 * 10**9;
/// rSpace 中的总量,这个值是变量,通过改变该值,实现 rate(rate = _rTotal/_tTotal) 变化,从而达到分红的目的
uint256 private _rTotal = (MAX - (MAX % _tTotal));
/// 所有交易消耗的总费用
uint256 private _tFeeTotal;

string private _name = 'Kishu Inu';
string private _symbol = 'KISHU';
uint8 private _decimals = 9;

/// 一次交易允许的最大转账额度
uint256 public _maxTxAmount = 100000000 * 10**6 * 10**9;

constructor () public {
_rOwned[_msgSender()] = _rTotal;
emit Transfer(address(0), _msgSender(), _tTotal);
}

function name() public view returns (string memory) {
return _name;
}

function symbol() public view returns (string memory) {
return _symbol;
}

function decimals() public view returns (uint8) {
return _decimals;
}

function totalSupply() public view override returns (uint256) {
return _tTotal;
}

/// 返回 tSpace 中的余额
function balanceOf(address account) public view override returns (uint256) {
if (_isExcluded[account]) return _tOwned[account];
/// 返回 tSpace 中的余额
return tokenFromReflection(_rOwned[account]);
}

function transfer(address recipient, uint256 amount) public override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}

function allowance(address owner, address spender) public view override returns (uint256) {
return _allowances[owner][spender];
}

function approve(address spender, uint256 amount) public override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}

function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
return true;
}

function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true;
}

function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
return true;
}

function isExcluded(address account) public view returns (bool) {
return _isExcluded[account];
}

function totalFees() public view returns (uint256) {
return _tFeeTotal;
}

/// 设置每次最大允许的转账额度
function setMaxTxPercent(uint256 maxTxPercent) external onlyOwner() {
_maxTxAmount = _tTotal.mul(maxTxPercent).div(
10**2
);
}

/// 将 tAmount 的 token 拿出来给所有持有者分红。打个比方,就是在微信群里给所有人发送等额红包。
function reflect(uint256 tAmount) public {
address sender = _msgSender();
require(!_isExcluded[sender], "Excluded addresses cannot call this function");
(uint256 rAmount,,,,) = _getValues(tAmount);
_rOwned[sender] = _rOwned[sender].sub(rAmount);
_rTotal = _rTotal.sub(rAmount);
_tFeeTotal = _tFeeTotal.add(tAmount);
}

/// 当前,将 tAmount 从 tSpace 转换到 rSpace。 不改变任何状态。
function reflectionFromToken(uint256 tAmount, bool deductTransferFee) public view returns(uint256) {
require(tAmount <= _tTotal, "Amount must be less than supply");
if (!deductTransferFee) {
(uint256 rAmount,,,,) = _getValues(tAmount);
return rAmount;
} else {
(,uint256 rTransferAmount,,,) = _getValues(tAmount);
return rTransferAmount;
}
}

/// 当前,将 rAmount 从 rSpace 转换到 tSpace。 不改变任何状态。
function tokenFromReflection(uint256 rAmount) public view returns(uint256) {
require(rAmount <= _rTotal, "Amount must be less than total reflections");
uint256 currentRate = _getRate();
return rAmount.div(currentRate);
}

/// 将指定 account 放到 排除名单 中。并且,将用户在 rSpace 中的 amount 转移到 tSpace 中
/// 对于 排除名单 中的 account,不再参与分红,仅仅活跃在 tSpace 中。
function excludeAccount(address account) external onlyOwner() {
require(!_isExcluded[account], "Account is already excluded");
if(_rOwned[account] > 0) {
// 将该用户在 rSpace 中的持有量转换回 tSpace 中,
// 后续,只要该用户一直在 排除名单 中,其交易过程直接变化 tSpace 中的值,且不接受分红。
_tOwned[account] = tokenFromReflection(_rOwned[account]);
}
_isExcluded[account] = true;
_excluded.push(account);
}

/// 将指定 account 从 排除名单 中移除出来。
/// 移除出来后,重置其在 tSpace 中的数量。
function includeAccount(address account) external onlyOwner() {
require(_isExcluded[account], "Account is already excluded");
for (uint256 i = 0; i < _excluded.length; i++) {
if (_excluded[i] == account) {
_excluded[i] = _excluded[_excluded.length - 1];
_tOwned[account] = 0;
_isExcluded[account] = false;
_excluded.pop();
break;
}
}
}

/// 授权第三方可操作额度
function _approve(address owner, address spender, uint256 amount) private {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");

_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}

/// token 交易过程
function _transfer(address sender, address recipient, uint256 amount) private {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
require(amount > 0, "Transfer amount must be greater than zero");
if(sender != owner() && recipient != owner())
require(amount <= _maxTxAmount, "Transfer amount exceeds the maxTxAmount.");
if (_isExcluded[sender] && !_isExcluded[recipient]) {
// 只有发送者在 排除名单 中
_transferFromExcluded(sender, recipient, amount);
} else if (!_isExcluded[sender] && _isExcluded[recipient]) {
// 只有接收者在 排除名单 中
_transferToExcluded(sender, recipient, amount);
} else if (!_isExcluded[sender] && !_isExcluded[recipient]) {
// 收发双方都不在 排除名单 中
_transferStandard(sender, recipient, amount);
} else if (_isExcluded[sender] && _isExcluded[recipient]) {
// 接收者和发送者都在 排除名单 中
_transferBothExcluded(sender, recipient, amount);
} else {
// 收发双方都不在 排除名单 中
_transferStandard(sender, recipient, amount);
}
}

/// 标准的转账过程,实际上操作的,
function _transferStandard(address sender, address recipient, uint256 tAmount) private {
// 计算转账变量在 rSpace 和 tSpace 中所造成的实际影响
(uint256 rAmount, uint256 rTransferAmount, uint256 rFee, uint256 tTransferAmount, uint256 tFee) = _getValues(tAmount);
// 转账对发送方 rSpace 所造成的实际影响
_rOwned[sender] = _rOwned[sender].sub(rAmount);
// 转账对接收方 rSpace 所造成的实际影响
_rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);
// 更新 _rTotal 和 _tFeeTotal
_reflectFee(rFee, tFee);
emit Transfer(sender, recipient, tTransferAmount);
}

/// 接收方在 排除名单中
function _transferToExcluded(address sender, address recipient, uint256 tAmount) private {
(uint256 rAmount, uint256 rTransferAmount, uint256 rFee, uint256 tTransferAmount, uint256 tFee) = _getValues(tAmount);
// 转账对发送方 rSpace 所造成的实际影响
_rOwned[sender] = _rOwned[sender].sub(rAmount);
// 接收方不会收到分红,直接就是扣除 2% 手续费剩下的
_tOwned[recipient] = _tOwned[recipient].add(tTransferAmount);
_rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);
_reflectFee(rFee, tFee);
emit Transfer(sender, recipient, tTransferAmount);
}

/// 接收方在 排除名单 中
function _transferFromExcluded(address sender, address recipient, uint256 tAmount) private {
(uint256 rAmount, uint256 rTransferAmount, uint256 rFee, uint256 tTransferAmount, uint256 tFee) = _getValues(tAmount);
// 接收方直接扣除发送的余额
_tOwned[sender] = _tOwned[sender].sub(tAmount);
_rOwned[sender] = _rOwned[sender].sub(rAmount);
_rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);
_reflectFee(rFee, tFee);
emit Transfer(sender, recipient, tTransferAmount);
}

function _transferBothExcluded(address sender, address recipient, uint256 tAmount) private {
(uint256 rAmount, uint256 rTransferAmount, uint256 rFee, uint256 tTransferAmount, uint256 tFee) = _getValues(tAmount);
_tOwned[sender] = _tOwned[sender].sub(tAmount);
_rOwned[sender] = _rOwned[sender].sub(rAmount);
_tOwned[recipient] = _tOwned[recipient].add(tTransferAmount);
_rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);
_reflectFee(rFee, tFee);
emit Transfer(sender, recipient, tTransferAmount);
}

function _reflectFee(uint256 rFee, uint256 tFee) private {
_rTotal = _rTotal.sub(rFee);
_tFeeTotal = _tFeeTotal.add(tFee);
}

/// 计算 tSpace 和 rSpace 中的各项变量
function _getValues(uint256 tAmount) private view returns (uint256, uint256, uint256, uint256, uint256) {
(uint256 tTransferAmount, uint256 tFee) = _getTValues(tAmount);
uint256 currentRate = _getRate();
(uint256 rAmount, uint256 rTransferAmount, uint256 rFee) = _getRValues(tAmount, tFee, currentRate);
return (rAmount, rTransferAmount, rFee, tTransferAmount, tFee);
}

/// tSpace 中的各项变量
function _getTValues(uint256 tAmount) private pure returns (uint256, uint256) {
/// 减去转账额度的 2%,注意,这里的 2%,是在 tSpace 中。
uint256 tFee = tAmount.div(100).mul(2);
uint256 tTransferAmount = tAmount.sub(tFee);
return (tTransferAmount, tFee);
}

/// rSpace 中的各项变量
function _getRValues(uint256 tAmount, uint256 tFee, uint256 currentRate) private pure returns (uint256, uint256, uint256) {
uint256 rAmount = tAmount.mul(currentRate);
uint256 rFee = tFee.mul(currentRate);
uint256 rTransferAmount = rAmount.sub(rFee);
return (rAmount, rTransferAmount, rFee);
}

/// 获取当前 rate
function _getRate() private view returns(uint256) {
(uint256 rSupply, uint256 tSupply) = _getCurrentSupply();
return rSupply.div(tSupply);
}

/// 获取当前供应量 rSupply 和 tSupply(去除了 排除账户 之外)
function _getCurrentSupply() private view returns(uint256, uint256) {
uint256 rSupply = _rTotal;
uint256 tSupply = _tTotal;
for (uint256 i = 0; i < _excluded.length; i++) {
// 如果这件事发生,说明所有的 token 都被 排除账户 持有
if (_rOwned[_excluded[i]] > rSupply || _tOwned[_excluded[i]] > tSupply) return (_rTotal, _tTotal);
rSupply = rSupply.sub(_rOwned[_excluded[i]]);
tSupply = tSupply.sub(_tOwned[_excluded[i]]);
}
if (rSupply < _rTotal.div(_tTotal)) return (_rTotal, _tTotal);
return (rSupply, tSupply);
}
}

参考链接

KishuInu

这是Kishu的源码,请教一下哪里有销毁和持币分红功能啊

通缩币代码注释

Mappings rOwned and tOwned

(https://www.keepandshare.com/doc15/24269/reflect-technical-paper-pdf-822k?da=y)

What the hell is _rTotal and _tTotal

通缩分红币Safemoon解读

© 2024 YueGS