@@ -1039,7 +1039,8 @@ CCoinbaseCurrencyState CConnectedChains::AddPrelaunchConversions(CCurrencyDefini
1039
1039
// currency state to be up to just before the start block
1040
1040
std::multimap<uint160, std::pair<CInputDescriptor, CReserveTransfer>> unspentTransfers;
1041
1041
std::map<uint160, int32_t > currencyIndexes = currencyState.GetReserveMap ();
1042
- int32_t nativeIdx = -1 ;
1042
+ int32_t nativeIdx = currencyIndexes.count (curDef.systemID ) ? currencyIndexes[curDef.systemID ] : -1 ;
1043
+
1043
1044
if (GetChainTransfers (unspentTransfers, curDef.GetID (), fromHeight, height < curDef.startBlock ? height : curDef.startBlock - 1 ))
1044
1045
{
1045
1046
currencyState.ClearForNextBlock ();
@@ -1067,11 +1068,10 @@ CCoinbaseCurrencyState CConnectedChains::AddPrelaunchConversions(CCurrencyDefini
1067
1068
{
1068
1069
currencyState.nativeConversionFees += conversionFee;
1069
1070
currencyState.nativeFees += conversionFee;
1070
- nativeIdx = curIdx;
1071
1071
}
1072
-
1073
1072
currencyState.fees [curIdx] += conversionFee;
1074
1073
currencyState.nativeFees += transfer.second .second .CalculateTransferFee (transfer.second .second .destination );
1074
+ currencyState.fees [nativeIdx] += transfer.second .second .CalculateTransferFee (transfer.second .second .destination );
1075
1075
currencyState.conversionFees [curIdx] += conversionFee;
1076
1076
}
1077
1077
}
@@ -1081,41 +1081,31 @@ CCoinbaseCurrencyState CConnectedChains::AddPrelaunchConversions(CCurrencyDefini
1081
1081
{
1082
1082
// convert all non-native fees to native and update the price as a result
1083
1083
bool isFeeConversion = false ;
1084
- std::vector<int64_t > reservesToConvert;
1085
- std::vector<int64_t > fractionalToConvert;
1086
- std::vector<std::vector<int64_t >> crossConversions = std::vector<std::vector<int64_t >>(curDef.currencies .size (), std::vector<int64_t >(curDef.currencies .size ()));
1084
+ int numCurrencies = curDef.currencies .size ();
1085
+ std::vector<int64_t > reservesToConvert (numCurrencies, 0 );
1086
+ std::vector<int64_t > fractionalToConvert (numCurrencies, 0 );
1087
+ CAmount newSupply = currencyState.supply ;
1087
1088
1088
- for (int i = 0 ; i < curDef. currencies . size () ; i++)
1089
+ for (int i = 0 ; i < numCurrencies ; i++)
1089
1090
{
1090
- currencyState.conversionPrice [i] = currencyState.PriceInReserve (i, true );
1091
+ curDef. conversions [i] = currencyState.conversionPrice [i] = currencyState.PriceInReserve (i, true );
1091
1092
1092
1093
// all currencies except the native currency of the system will be converted to the native currency
1093
1094
if (currencyState.fees [i] && curDef.currencies [i] != curDef.systemID )
1094
1095
{
1095
- reservesToConvert.push_back (currencyState.fees [i] + currencyState.reserves [i]);
1096
- fractionalToConvert.push_back (currencyState.ReserveToNative (currencyState.reserves [i], i));
1097
- if (nativeIdx != -1 )
1098
- {
1099
- crossConversions[i][nativeIdx] = currencyState.fees [i];
1100
- isFeeConversion = true ;
1101
- }
1102
- }
1103
- else
1104
- {
1105
- reservesToConvert.push_back (0 );
1106
- fractionalToConvert.push_back (0 );
1096
+ fractionalToConvert[nativeIdx] += currencyState.ReserveToNativeRaw (currencyState.fees [i], currencyState.conversionPrice [i]);
1097
+ newSupply += fractionalToConvert[i];
1098
+ isFeeConversion = true ;
1107
1099
}
1108
1100
}
1101
+ currencyState.supply = newSupply;
1109
1102
1110
1103
// convert all non-native fee currencies to native and adjust prices
1111
1104
if (isFeeConversion)
1112
1105
{
1113
1106
CCurrencyState converterState = static_cast <CCurrencyState>(currencyState);
1114
- curDef.conversions = currencyState.conversionPrice = converterState.ConvertAmounts (reservesToConvert, fractionalToConvert, currencyState, &crossConversions);
1115
- }
1116
- else
1117
- {
1118
- curDef.conversions = currencyState.PricesInReserve ();
1107
+ currencyState.viaConversionPrice =
1108
+ converterState.ConvertAmounts (reservesToConvert, fractionalToConvert, currencyState);
1119
1109
}
1120
1110
}
1121
1111
@@ -1136,19 +1126,35 @@ CCoinbaseCurrencyState CConnectedChains::AddPrelaunchConversions(CCurrencyDefini
1136
1126
{
1137
1127
// get a ratio and reduce all prices by that ratio
1138
1128
static arith_uint256 bigSatoshi (SATOSHIDEN);
1139
- arith_uint256 newRatio ((calculatedSupply * bigSatoshi) / currencyState.supply );
1129
+ arith_uint256 bigMultipliedSupply (calculatedSupply * bigSatoshi);
1130
+ arith_uint256 newRatio (bigMultipliedSupply / curDef.initialFractionalSupply );
1131
+ // truncate up, not down, to prevent any overflow at all
1132
+ if (newRatio * curDef.initialFractionalSupply < bigMultipliedSupply)
1133
+ {
1134
+ newRatio++;
1135
+ }
1140
1136
for (auto &rate : currencyState.conversionPrice )
1141
1137
{
1142
1138
arith_uint256 numerator = rate * newRatio;
1143
1139
rate = (numerator / bigSatoshi).GetLow64 ();
1144
- // truncate up, not down, to prevent any overflow at all
1145
- if (((numerator / bigSatoshi) >> 1 ) > 0 )
1140
+ if ((numerator - (bigSatoshi * rate)) > 0 )
1146
1141
{
1147
1142
rate++;
1148
1143
}
1149
1144
}
1150
1145
}
1151
1146
1147
+ calculatedSupply = 0 ;
1148
+ for (auto &transfer : unspentTransfers)
1149
+ {
1150
+ if (transfer.second .second .IsPreConversion ())
1151
+ {
1152
+ CAmount toConvert = transfer.second .second .nValue - CReserveTransactionDescriptor::CalculateConversionFee (transfer.second .second .nValue );
1153
+ calculatedSupply += CCurrencyState::ReserveToNativeRaw (toConvert, currencyState.conversionPrice [currencyIndexes[transfer.second .second .currencyID ]]);
1154
+ }
1155
+ }
1156
+ printf (" Calculated supply %s\n " , ValueFromAmount (calculatedSupply).write ().c_str ());
1157
+
1152
1158
// now, remove carveout percentage from each weight & reserve
1153
1159
// for currency state
1154
1160
int32_t preLaunchCarveOutTotal = 0 ;
0 commit comments