Solidity
幣安智能鏈代幣:我隨處可見的 tTotal、rTotal、tSupply、rSupply、rOwned、tOwned 是什麼?
查看 Binance Smart Chain 上所有最新的 shitcoins 智能合約程式碼,有很多共同的、反復出現的主題,因為每個項目的大部分程式碼都是複制粘貼的。程式碼也完全沒有註釋,這使得理解什麼是什麼變得很痛苦。
這些聯繫人中的大多數都使用兩種不同類型的變數進行內部令牌計算,一種是被呼叫的
t
,一種是被呼叫r
的。例如,在 TrustPad 的智能合約上(可在此處獲得:bscscan 連結):mapping(address => uint256) private _rOwned; mapping(address => uint256) private _tOwned; mapping(address => mapping(address => uint256)) private _allowances; uint256 private constant MAX = ~uint256(0); uint256 private constant _tTotal = 100 * (10 ** 6) * (10 ** 9); uint256 private _rTotal = (MAX - (MAX % _tTotal)); uint256 private _tFeeTotal; uint256 private _tFeePercent = 2;
任何人都知道
r
and是什麼t
意思,以及為什麼他們維護_tOwned
and_rOwned
,_tTotal
and_rTotal
等?
對於賬戶餘額,這是一種算法,或者實際上只是一些數學運算。而不是使用預設的“tokenSupply”。
Constants Fee is 1% (assuming no burn, just a general 1% fee) tTotal is totalSupply and literally never changes rTotal starts at 127366483000000 and continuously subtracts or adds currentRate Example: You buy 100 Token as tAmount >>> Current Totals: rTotal = 127366483000000 tTotal = 10000 currentRate = rTotal / tTotal = 12736648300 Formula to get amount you will receive > rTransferAmount = [rAmount] - [rFee] [rAmount] = tAmount * currentRate > 1273664830000 = 100 * 12736648300 [rFee] = tFee * currrentRate > 127366483 = .01 * 12736648300 >>> 1273537463517 = [rAmount] - [rFee] = rTransferAmount rTotal is updated to = rTotal - rTransferAmount balanceOf(): = rAmount / currentRate > rAmount[me] is rAmount rAmount = 1273537463517 currentRate = 12735374635.17 Balance: 100 ...Someone else makes a transfer from x0 supply to buy 100 Tokens rTotal = 126092945536483 tTotal = 10000 currentRate = rTotal / tTotal = 12609294553.6483 Formula to get amount you will receive > rTransferAmount = [rAmount] - [rFee] rAmount = tAmount * currentRate > 1260929455364.83 = 100 * 12609294553.6483 rFee = tFee * currrentRate > 126092945.536483 = .01 * 12609294553.6483 >>> rTransferAmount = 1260803362419.293517 They now have 100 ...Now check [me] balance after previous purchase balanceOf(): = rAmount / currentRate rAmount[me] is rAmount rAmount = 1273537463517 currentRate = 12609294553.6483 Balance: 100.9998979799010304029995929297 ...Now lets buy 100 more directly after checking balance ...this is the next transaction after the previous one rTotal = 124832142174063.706483 tTotal = 10000 currentRate = rTotal / tTotal = 12483214217.4063706483 Formula to get amount you will receive > rTransferAmount = [rAmount] - [rFee] rAmount = tAmount * currentRate > 1248321421740.63706483 = 100 * 12483214217.4063706483 rFee = tFee * currrentRate > 124832142.174063706483 = .01 * 12483214217.4063706483 >>> rTransferAmount = 1248196589598.463001123517 We now take our previous rAmount and add the rTransferAmount to update our rAmount > rOwned[me] = rOwned[me] + rTransferAmount > 2521734053115.463001123517 = 1273537463517 + 1248196589598.463001123517 ...Now check [me] balance after previous purchase balanceOf(): = rAmount / currentRate rAmount[me] is rAmount rAmount = 2521734053115.463001123517 currentRate = 12483214217.4063706483 Balance: 202.00999591909607202719956134358 ... now lets sell 100 rTotal = 123583945584465.243481876483 tTotal = 10000 currentRate = rTotal / tTotal = 12358394558.4465243481876483 Formula to get amount you will receive > rTransferAmount = [rAmount] - [rFee] rAmount = tAmount * currentRate > 1235839455844.65243481876483 = 100 * 12358394558.4465243481876483 rFee = tFee * currrentRate > 123583945.584465243481876483 = .01 * 12358394558.4465243481876483 >>> rTransferAmount = 1235715871899.067969575282953517 We now take our previous rAmount and add the rTransferAmount to update our rAmount (we are subtracting because we are selling) > rOwned[me] = rOwned[me] - rTransferAmount > 1286018181216.395031548234046483 = 2521734053115.463001123517 - 1235715871899.067969575282953517 rTotal is updated to = rTotal - rTransferAmount 122348229712566.17551230120004648 = 123583945584465.243481876483 - 1235715871899.067969575282953517 ...Now check [me] balance after previous purchase balanceOf(): = rAmount / currentRate rAmount[me] is rAmount rAmount = 1286018181216.395031548234046483 currentRate = 12358394558.4465243481876483 Balance: 104.06029481697096470326753341015
範例假設反射費用為 1%,僅此而已。通常會有反映和燃燒的費用
Well
_rTotal
很簡單Total Reflections
,_rOwned
指的是使用者擁有的反射次數。這樣想:我們可以將初始_rTotal
設置100
為表示 100%,如果您的_rOwned
值為 10,則您擁有 10% 的總反射。在constructor
部署合約後看到的方法中,整個反射都轉移給了合約的所有者。_rOwned[_msgSender()] = _rTotal;
使用 100 作為全反射有一個問題:solidity 只支持整數,不支持浮點數,所以我們必須以某種方式換一種方式
如果我是對的,他們設置
_rTotal
一個巨大數字的原因是為了使反射非常準確並支持盡可能多的浮點(如果我錯了,請在評論中糾正我),所以如果你擁有類似 0.00812301283120398912312836% 的反射,它應該可以正常工作。我希望我能用簡單的方式解釋它