@@ -15,6 +15,10 @@ import (
1515 "github.com/code-payments/code-server/pkg/solana/currencycreator"
1616)
1717
18+ const (
19+ defaultPrecision = 128
20+ )
21+
1822// ValidateClientExchangeData validates proto exchange data provided by a client
1923func ValidateClientExchangeData (ctx context.Context , data code_data.Provider , proto * transactionpb.ExchangeData ) (bool , string , error ) {
2024 mint , err := common .GetBackwardsCompatMint (proto .Mint )
@@ -31,12 +35,12 @@ func ValidateClientExchangeData(ctx context.Context, data code_data.Provider, pr
3135func validateCoreMintClientExchangeData (ctx context.Context , data code_data.Provider , proto * transactionpb.ExchangeData ) (bool , string , error ) {
3236 latestExchangeRateTime := GetLatestExchangeRateTime ()
3337
34- clientRate := big .NewFloat (proto .ExchangeRate ).SetPrec (18 )
35- clientNativeAmount := big .NewFloat (proto .NativeAmount ).SetPrec (18 )
36- clientQuarks := big .NewFloat (float64 (proto .Quarks )).SetPrec (18 )
38+ clientRate := big .NewFloat (proto .ExchangeRate ).SetPrec (defaultPrecision )
39+ clientNativeAmount := big .NewFloat (proto .NativeAmount ).SetPrec (defaultPrecision )
40+ clientQuarks := big .NewFloat (float64 (proto .Quarks )).SetPrec (defaultPrecision )
3741
38- rateErrorThreshold := big .NewFloat (0.01 ).SetPrec (18 )
39- quarkErrorThreshold := big .NewFloat (1000 ).SetPrec (18 )
42+ rateErrorThreshold := big .NewFloat (0.01 ).SetPrec (defaultPrecision )
43+ quarkErrorThreshold := big .NewFloat (1000 ).SetPrec (defaultPrecision )
4044
4145 // Find an exchange rate that the client could have fetched from a RPC call
4246 // within a reasonable time in the past
@@ -82,12 +86,12 @@ func validateCurrencyLaunchpadClientExchangeData(ctx context.Context, data code_
8286 return false , "" , err
8387 }
8488
85- clientQuarks := big .NewFloat (float64 (proto .Quarks )).SetPrec (18 )
86- clientRate := big .NewFloat (proto .ExchangeRate ).SetPrec (18 )
87- clientNativeAmount := big .NewFloat (proto .NativeAmount ).SetPrec (18 )
89+ clientQuarks := big .NewFloat (float64 (proto .Quarks )).SetPrec (defaultPrecision )
90+ clientRate := big .NewFloat (proto .ExchangeRate ).SetPrec (defaultPrecision )
91+ clientNativeAmount := big .NewFloat (proto .NativeAmount ).SetPrec (defaultPrecision )
8892
89- rateErrorThreshold := big .NewFloat (0.01 ).SetPrec (18 )
90- nativeAmountErrorThreshold := big .NewFloat (0.005 ).SetPrec (18 )
93+ rateErrorThreshold := big .NewFloat (0.01 ).SetPrec (defaultPrecision )
94+ nativeAmountErrorThreshold := big .NewFloat (0.005 ).SetPrec (defaultPrecision )
9195
9296 log := logrus .StandardLogger ().WithFields (logrus.Fields {
9397 "currency" : proto .Currency ,
@@ -117,7 +121,7 @@ func validateCurrencyLaunchpadClientExchangeData(ctx context.Context, data code_
117121 } else if err != nil {
118122 return false , "" , err
119123 }
120- usdRate := big .NewFloat (usdExchangeRateRecord .Rate ).SetPrec (18 )
124+ usdRate := big .NewFloat (usdExchangeRateRecord .Rate ).SetPrec (defaultPrecision )
121125
122126 var otherExchangeRateRecord * currency.ExchangeRateRecord
123127 if proto .Currency == string (currency_lib .USD ) {
@@ -130,7 +134,7 @@ func validateCurrencyLaunchpadClientExchangeData(ctx context.Context, data code_
130134 return false , "" , err
131135 }
132136 }
133- otherRate := big .NewFloat (otherExchangeRateRecord .Rate ).SetPrec (18 )
137+ otherRate := big .NewFloat (otherExchangeRateRecord .Rate ).SetPrec (defaultPrecision )
134138
135139 // How much core mint would be received for a sell against the currency creator program?
136140 coreMintSellValueInQuarks , _ := currencycreator .EstimateSell (& currencycreator.EstimateSellArgs {
@@ -148,8 +152,8 @@ func validateCurrencyLaunchpadClientExchangeData(ctx context.Context, data code_
148152 }
149153 nativeAmountUpperBound := new (big.Float ).Add (clientNativeAmount , nativeAmountErrorThreshold )
150154 coreMintSellValueInUnits := new (big.Float ).Quo (
151- big .NewFloat (float64 (coreMintSellValueInQuarks )).SetPrec (18 ),
152- big .NewFloat (float64 (coreMintQuarksPerUnit )).SetPrec (18 ),
155+ big .NewFloat (float64 (coreMintSellValueInQuarks )).SetPrec (defaultPrecision ),
156+ big .NewFloat (float64 (coreMintQuarksPerUnit )).SetPrec (defaultPrecision ),
153157 )
154158 potentialNativeAmount := new (big.Float ).Mul (new (big.Float ).Quo (otherRate , usdRate ), coreMintSellValueInUnits )
155159 if potentialNativeAmount .Cmp (nativeAmountLowerBound ) < 0 || potentialNativeAmount .Cmp (nativeAmountUpperBound ) > 0 {
@@ -165,7 +169,7 @@ func validateCurrencyLaunchpadClientExchangeData(ctx context.Context, data code_
165169 // For the valid native amount, is the exchange rate calculated correctly?
166170 otherMintUnits := new (big.Float ).Quo (
167171 clientQuarks ,
168- big .NewFloat (float64 (otherMintQuarksPerUnit )).SetPrec (18 ),
172+ big .NewFloat (float64 (otherMintQuarksPerUnit )).SetPrec (defaultPrecision ),
169173 )
170174 expectedRate := new (big.Float ).Quo (potentialNativeAmount , otherMintUnits )
171175 percentDiff := new (big.Float ).Quo (new (big.Float ).Abs (new (big.Float ).Sub (clientRate , expectedRate )), expectedRate )
0 commit comments