Skip to content

Commit 21296d1

Browse files
author
chenf
committed
Fix bug and optimize
1 parent 0fa4bc5 commit 21296d1

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

contracts/NestBatchMining.sol

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,6 @@ contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
622622
// Currently, mined represents the number of blocks has mined
623623
(uint mined, uint totalShares) = _calcMinedBlocks(pair.sheets, index, sheet);
624624

625-
// When rewardPerBlock is very large, this calculated may be truncated,
626-
// resulting in a significant reduction in the actual reward,
627-
// This situation is unreasonable and needs to be borne by the opener.
628625
reward += (
629626
mined
630627
* shares
@@ -667,7 +664,7 @@ contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
667664
reward = vault;
668665
}
669666
// Record the vault for each channel to prevent the opener use the funds in this contract without increase
670-
channel.vault = uint96(vault - reward);
667+
channel.vault = uint128(vault - reward);
671668

672669
// Record reward
673670
_unfreeze(balances, channel.reward, reward, accountIndex);
@@ -732,12 +729,6 @@ contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
732729
return _calcMinedBlocks(sheets, index, sheets[index]);
733730
}
734731

735-
/// @dev The function returns eth rewards of specified ntoken
736-
/// @param channelId Target channelId
737-
function totalETHRewards(uint channelId) external view override returns (uint) {
738-
return uint(_channels[channelId].rewards);
739-
}
740-
741732
/// @dev Pay
742733
/// @param channelId Target channelId
743734
/// @param to Address to receive
@@ -837,9 +828,9 @@ contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
837828
uint prev = uint(p0.height);
838829
// It's not necessary to load the price information in p0
839830
// Eth count variable used to calculate price
840-
uint totalEthNum = 0;
831+
uint totalToken0Scales = 0;
841832
// Token count variable for price calculation
842-
uint totalTokenValue = 0;
833+
uint totalToken1Value = 0;
843834
// Block number of current sheet
844835
uint height = 0;
845836

@@ -866,20 +857,20 @@ contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
866857
// Not the same block (or flag is false), calculate the price and update it
867858
if (flag || prev != height) {
868859

869-
// totalEthNum > 0 Can calculate the price
870-
if (totalEthNum > 0) {
860+
// totalToken0Scales > 0 Can calculate the price
861+
if (totalToken0Scales > 0) {
871862

872863
// Calculate average price and Volatility
873864
// Calculation method of volatility of follow-up price
874865
uint tmp = _decodeFloat(p0.priceFloat);
875866
// New price
876-
uint price = totalTokenValue / totalEthNum;
867+
uint price = totalToken1Value / totalToken0Scales;
877868
// Update price
878-
p0.remainScales = uint32(totalEthNum);
869+
p0.remainScales = uint32(totalToken0Scales);
879870
p0.priceFloat = _encodeFloat(price);
880871
// Clear cumulative values
881-
totalEthNum = 0;
882-
totalTokenValue = 0;
872+
totalToken0Scales = 0;
873+
totalToken1Value = 0;
883874

884875
if (tmp > 0) {
885876
// Calculate average price
@@ -935,8 +926,8 @@ contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
935926
}
936927

937928
// Cumulative price information
938-
totalEthNum += uint(sheet.remainScales);
939-
totalTokenValue += _decodeFloat(sheet.priceFloat) * uint(sheet.remainScales);
929+
totalToken0Scales += uint(sheet.remainScales);
930+
totalToken1Value += _decodeFloat(sheet.priceFloat) * uint(sheet.remainScales);
940931
}
941932

942933
// Update price information
@@ -1168,8 +1159,8 @@ contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
11681159
}
11691160

11701161
// Calculate price
1171-
uint totalEthNum = 0;
1172-
uint totalTokenValue = 0;
1162+
uint totalToken0Scales = 0;
1163+
uint totalToken1Value = 0;
11731164
uint h = 0;
11741165
uint remainScales;
11751166
PriceSheet memory sheet;
@@ -1189,8 +1180,8 @@ contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
11891180
} else if (h != sheetHeight) {
11901181
break;
11911182
}
1192-
totalEthNum += remainScales;
1193-
totalTokenValue += _decodeFloat(sheet.priceFloat) * remainScales;
1183+
totalToken0Scales += remainScales;
1184+
totalToken1Value += _decodeFloat(sheet.priceFloat) * remainScales;
11941185
}
11951186
}
11961187

@@ -1206,13 +1197,13 @@ contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
12061197
} else if (h != sheetHeight) {
12071198
break;
12081199
}
1209-
totalEthNum += remainScales;
1210-
totalTokenValue += _decodeFloat(sheet.priceFloat) * remainScales;
1200+
totalToken0Scales += remainScales;
1201+
totalToken1Value += _decodeFloat(sheet.priceFloat) * remainScales;
12111202
}
12121203
}
12131204

1214-
if (totalEthNum > 0) {
1215-
return (h + priceEffectSpan, totalTokenValue / totalEthNum);
1205+
if (totalToken0Scales > 0) {
1206+
return (h + priceEffectSpan, totalToken1Value / totalToken0Scales);
12161207
}
12171208
return (0, 0);
12181209
}
@@ -1229,29 +1220,29 @@ contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
12291220

12301221
uint priceEffectSpan = uint(_config.priceEffectSpan);
12311222
uint index = sheets.length;
1232-
uint totalEthNum = 0;
1233-
uint totalTokenValue = 0;
1223+
uint totalToken0Scales = 0;
1224+
uint totalToken1Value = 0;
12341225
uint height = 0;
12351226

12361227
for (uint i = 0; i < count;) {
12371228

12381229
bool flag = index == 0;
12391230
if (flag || height != uint((sheet = sheets[--index]).height)) {
1240-
if (totalEthNum > 0 && height + priceEffectSpan < block.number) {
1231+
if (totalToken0Scales > 0 && height + priceEffectSpan < block.number) {
12411232
array[i++] = height + priceEffectSpan;
1242-
array[i++] = totalTokenValue / totalEthNum;
1233+
array[i++] = totalToken1Value / totalToken0Scales;
12431234
}
12441235
if (flag) {
12451236
break;
12461237
}
1247-
totalEthNum = 0;
1248-
totalTokenValue = 0;
1238+
totalToken0Scales = 0;
1239+
totalToken1Value = 0;
12491240
height = uint(sheet.height);
12501241
}
12511242

12521243
uint remainScales = uint(sheet.remainScales);
1253-
totalEthNum += remainScales;
1254-
totalTokenValue += _decodeFloat(sheet.priceFloat) * remainScales;
1244+
totalToken0Scales += remainScales;
1245+
totalToken1Value += _decodeFloat(sheet.priceFloat) * remainScales;
12551246
}
12561247

12571248
return array;

contracts/NestBatchPlatform2.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ contract NestBatchPlatform2 is NestBatchMining, INestBatchPriceView, INestBatchP
236236
require(msg.value == fee, "NOP:!fee");
237237
}
238238

239-
channel.rewards += _toUInt96(fee);
239+
if (fee > 0) {
240+
channel.rewards += _toUInt96(fee);
241+
}
240242
}
241243
}

contracts/interfaces/INestBatchMining.sol

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,6 @@ interface INestBatchMining {
237237
uint index
238238
) external view returns (uint minedBlocks, uint totalShares);
239239

240-
/// @dev The function returns eth rewards of specified ntoken
241-
/// @param channelId Target channelId
242-
function totalETHRewards(uint channelId) external view returns (uint);
243-
244240
/// @dev Pay
245241
/// @param channelId Target channelId
246242
/// @param to Address to receive

0 commit comments

Comments
 (0)