Skip to content

Commit 7624819

Browse files
committed
Imporve validateCoreMintClientExchangeData
1 parent 7dee1a9 commit 7624819

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

pkg/code/currency/validation.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,31 @@ func validateCoreMintClientExchangeData(ctx context.Context, data code_data.Prov
4545
minTransferValue := new(big.Float).Quo(one, big.NewFloat(math.Pow10(currencyDecimals)))
4646

4747
rateErrorThreshold := big.NewFloat(0.001).SetPrec(defaultPrecision)
48-
quarkErrorThreshold := big.NewFloat(1000).SetPrec(defaultPrecision)
4948
nativeAmountErrorThreshold := new(big.Float).Quo(minTransferValue, big.NewFloat(2.0))
5049

50+
nativeAmountLowerBound := new(big.Float).Sub(clientNativeAmount, nativeAmountErrorThreshold)
51+
if nativeAmountLowerBound.Cmp(nativeAmountErrorThreshold) < 0 {
52+
nativeAmountLowerBound = nativeAmountErrorThreshold
53+
}
54+
nativeAmountUpperBound := new(big.Float).Add(clientNativeAmount, nativeAmountErrorThreshold)
55+
quarksLowerBound := new(big.Float).Quo(nativeAmountLowerBound, clientRate)
56+
quarksUpperBound := new(big.Float).Quo(nativeAmountUpperBound, clientRate)
57+
58+
log := logrus.StandardLogger().WithFields(logrus.Fields{
59+
"currency": proto.Currency,
60+
"client_native_amount": clientNativeAmount,
61+
"client_exchange_rate": clientRate,
62+
"client_quarks": proto.Quarks,
63+
"min_transfer_value": minTransferValue,
64+
"native_amount_lower_bound": nativeAmountLowerBound,
65+
"native_amount_upper_bound": nativeAmountUpperBound,
66+
"quarks_lower_bound": nativeAmountLowerBound,
67+
"quarks_upper_bound": nativeAmountUpperBound,
68+
})
69+
5170
if clientNativeAmount.Cmp(nativeAmountErrorThreshold) < 0 {
52-
return false, "native amount is less than minimum transfer value", nil
71+
log.Info("native amount is less than minimum transfer value error threshold")
72+
return false, "native amount is less than minimum transfer value error threshold", nil
5373
}
5474

5575
// Find an exchange rate that the client could have fetched from a RPC call
@@ -74,16 +94,13 @@ func validateCoreMintClientExchangeData(ctx context.Context, data code_data.Prov
7494
}
7595

7696
if !isClientRateValid {
97+
log.Info("fiat exchange rate is stale or invalid")
7798
return false, "fiat exchange rate is stale or invalid", nil
7899
}
79100

80-
// Validate that the native amount and exchange rate fall reasonably within
81-
// the amount of quarks to send in the transaction.
82-
quarksPerUnit := big.NewFloat(float64(common.GetMintQuarksPerUnit(common.CoreMintAccount)))
83-
unitsOfCoreMint := new(big.Float).Quo(clientNativeAmount, clientRate)
84-
expectedQuarks := new(big.Float).Mul(unitsOfCoreMint, quarksPerUnit)
85-
diff := new(big.Float).Abs(new(big.Float).Sub(expectedQuarks, clientQuarks))
86-
if diff.Cmp(quarkErrorThreshold) > 0 {
101+
// Validate that the native amount within half of the minimum transfer value
102+
if clientQuarks.Cmp(quarksLowerBound) < 0 || clientQuarks.Cmp(quarksUpperBound) > 0 {
103+
log.Info("native amount is outside error threshold")
87104
return false, "payment native amount and quark value mismatch", nil
88105
}
89106

@@ -125,8 +142,8 @@ func validateCurrencyLaunchpadClientExchangeData(ctx context.Context, data code_
125142
})
126143

127144
if clientNativeAmount.Cmp(nativeAmountErrorThreshold) < 0 {
128-
log.Info("native amount is less than minimum transfer value")
129-
return false, "native amount is less than minimum transfer value", nil
145+
log.Info("native amount is less than minimum transfer value error threshold")
146+
return false, "native amount is less than minimum transfer value error threshold", nil
130147
}
131148

132149
latestExchangeRateTime := GetLatestExchangeRateTime()
@@ -170,7 +187,7 @@ func validateCurrencyLaunchpadClientExchangeData(ctx context.Context, data code_
170187
})
171188

172189
// Given the sell value, does it align with the native amount in the target currency
173-
// within half a penny?
190+
// within half a minimum transfer unit?
174191
nativeAmountLowerBound := new(big.Float).Sub(clientNativeAmount, nativeAmountErrorThreshold)
175192
if nativeAmountLowerBound.Cmp(nativeAmountErrorThreshold) < 0 {
176193
nativeAmountLowerBound = nativeAmountErrorThreshold

0 commit comments

Comments
 (0)