Skip to content

Commit 66af915

Browse files
committed
Update exchange data validation with currency minimum transfer values
1 parent 5494432 commit 66af915

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

pkg/code/currency/validation.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package currency
22

33
import (
44
"context"
5+
"math"
56
"math/big"
67
"time"
78

@@ -39,9 +40,17 @@ func validateCoreMintClientExchangeData(ctx context.Context, data code_data.Prov
3940
clientNativeAmount := big.NewFloat(proto.NativeAmount).SetPrec(defaultPrecision)
4041
clientQuarks := big.NewFloat(float64(proto.Quarks)).SetPrec(defaultPrecision)
4142

43+
currencyDecimals := currency_lib.GetDecimals(currency_lib.Code(proto.Currency))
44+
one := big.NewFloat(1.0).SetPrec(defaultPrecision)
45+
minTransferValue := new(big.Float).Quo(one, big.NewFloat(math.Pow10(currencyDecimals)))
46+
4247
rateErrorThreshold := big.NewFloat(0.001).SetPrec(defaultPrecision)
4348
quarkErrorThreshold := big.NewFloat(1000).SetPrec(defaultPrecision)
4449

50+
if clientNativeAmount.Cmp(minTransferValue) < 0 {
51+
return false, "native amount is less than minimum transfer value", nil
52+
}
53+
4554
// Find an exchange rate that the client could have fetched from a RPC call
4655
// within a reasonable time in the past
4756
var isClientRateValid bool
@@ -73,7 +82,7 @@ func validateCoreMintClientExchangeData(ctx context.Context, data code_data.Prov
7382
unitsOfCoreMint := new(big.Float).Quo(clientNativeAmount, clientRate)
7483
expectedQuarks := new(big.Float).Mul(unitsOfCoreMint, quarksPerUnit)
7584
diff := new(big.Float).Abs(new(big.Float).Sub(expectedQuarks, clientQuarks))
76-
if diff.Cmp(quarkErrorThreshold) > 1000 {
85+
if diff.Cmp(quarkErrorThreshold) > 0 {
7786
return false, "payment native amount and quark value mismatch", nil
7887
}
7988

@@ -97,8 +106,12 @@ func validateCurrencyLaunchpadClientExchangeData(ctx context.Context, data code_
97106
clientRate := big.NewFloat(proto.ExchangeRate).SetPrec(defaultPrecision)
98107
clientNativeAmount := big.NewFloat(proto.NativeAmount).SetPrec(defaultPrecision)
99108

109+
currencyDecimals := currency_lib.GetDecimals(currency_lib.Code(proto.Currency))
110+
one := big.NewFloat(1.0).SetPrec(defaultPrecision)
111+
minTransferValue := new(big.Float).Quo(one, big.NewFloat(math.Pow10(currencyDecimals)))
112+
100113
rateErrorThreshold := big.NewFloat(0.001).SetPrec(defaultPrecision)
101-
nativeAmountErrorThreshold := big.NewFloat(0.005).SetPrec(defaultPrecision)
114+
nativeAmountErrorThreshold := new(big.Float).Quo(minTransferValue, big.NewFloat(2.0))
102115

103116
log := logrus.StandardLogger().WithFields(logrus.Fields{
104117
"currency": proto.Currency,
@@ -107,8 +120,14 @@ func validateCurrencyLaunchpadClientExchangeData(ctx context.Context, data code_
107120
"client_token_units": clientTokenUnits,
108121
"client_quarks": proto.Quarks,
109122
"mint": mintAccount.PublicKey().ToBase58(),
123+
"min_transfer_value": minTransferValue,
110124
})
111125

126+
if clientNativeAmount.Cmp(minTransferValue) < 0 {
127+
log.Info("native amount is less than minimum transfer value")
128+
return false, "native amount is less than minimum transfer value", nil
129+
}
130+
112131
latestExchangeRateTime := GetLatestExchangeRateTime()
113132
for i := range 2 {
114133
exchangeRateTime := latestExchangeRateTime.Add(time.Duration(-i) * timePerExchangeRateUpdate)

pkg/currency/decimal.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package currency
2+
3+
func GetDecimals(code Code) int {
4+
switch code {
5+
case AFN,
6+
ALL,
7+
BIF,
8+
CLP,
9+
COP,
10+
DJF,
11+
GNF,
12+
IQD,
13+
IDR,
14+
IRR,
15+
ISK,
16+
JPY,
17+
KMF,
18+
KPW,
19+
KRW,
20+
LAK,
21+
LBP,
22+
MGA,
23+
MMK,
24+
MRU,
25+
PYG,
26+
RSD,
27+
RWF,
28+
SLL,
29+
SOS,
30+
SYP,
31+
TZS,
32+
UGX,
33+
UYU,
34+
VND,
35+
VUV,
36+
XAF,
37+
XOF,
38+
XPF,
39+
YER:
40+
return 0
41+
case BHD,
42+
JOD,
43+
KWD,
44+
LYD,
45+
OMR,
46+
TND:
47+
return 3
48+
}
49+
return 2
50+
}

0 commit comments

Comments
 (0)