@@ -7,28 +7,45 @@ import (
77 "github.com/pegnet/pegnetd/config"
88)
99
10- // Convert takes an input amount and returns an output amount that can be created
10+ // Convert
11+ // takes an input amount and returns an output amount that can be created
1112// from it, given the two rates `fromRate` and `toRate` denominated in 1e-8 USD.
1213// All parameters must be in their lowest divisible unit as whole numbers.
13- // X fromType -> ?? toType
1414//
15- // X fromType fromRate USD 1 toType
16- // ---------- * ------------ * ---------- = ?? toType
15+ // Prior to PIP-10:
16+ // RateSource = fromRate
17+ // RateDest = toRate
18+ //
19+ // Under PIP-10:
20+ // RateSource = min(fromRate, fromAvg)
21+ // RateDest = max(toRate, toAvg)
22+ //
23+ // A Source Tokens
24+ // X Destination Tokens
25+ //
26+ // A fromRate USD 1
27+ // ---------- * ------------ * ---------- = X
1728// 1 1 fromType toRate USD
29+ //
1830func Convert (height uint32 , amount int64 , fromRate , fromAvg , toRate , toAvg uint64 ) (int64 , error ) {
19- if amount < 0 {
20- return 0 , fmt .Errorf ("invalid amount: must be greater than or equal to zero" )
21- }
22- if fromRate == 0 || toRate == 0 {
23- return 0 , fmt .Errorf ("invalid rate: 0" )
31+ if amount < 0 { // Must have something to convert; negative numbers
32+ return 0 , fmt .Errorf ("invalid amount: must be greater than or equal to zero" ) // do not qualify
2433 }
2534
26- if height >= config .PIP10AverageActivation {
27- if fromRate > fromAvg {
28- fromRate = fromAvg
35+ if fromRate == 0 || toRate == 0 { // If either rate (fromRate or toRate) is zero
36+ return 0 , fmt .Errorf ("invalid rate: 0" ) // then we can't do the conversion
37+ }
38+ RateSource := fromRate
39+ RateDest := toRate
40+ if height >= config .PIP10AverageActivation { // Check that PIP-10 has been activated.
41+ if fromAvg == 0 || toAvg == 0 { // If either Average (fromAvg or toAvg) is zero
42+ return 0 , fmt .Errorf ("invalid rate: 0" ) // then we can't do the conversion
43+ }
44+ if fromRate > fromAvg { // Use the min(fromRate, fromAvg)
45+ RateSource = fromAvg
2946 }
30- if toRate < toAvg {
31- toRate = toAvg
47+ if toRate < toAvg { // Use the max(toRate,toAvg)
48+ RateDest = toAvg
3249 }
3350 }
3451
@@ -37,8 +54,8 @@ func Convert(height uint32, amount int64, fromRate, fromAvg, toRate, toAvg uint6
3754 // accuracy a miner reports. Anything beyond the 8th decimal point, we cannot account for.
3855 //
3956 // Uses big ints to avoid overflows.
40- fr := new (big.Int ).SetUint64 (fromRate )
41- tr := new (big.Int ).SetUint64 (toRate )
57+ fr := new (big.Int ).SetUint64 (RateSource )
58+ tr := new (big.Int ).SetUint64 (RateDest )
4259 amt := big .NewInt (amount )
4360
4461 // Now we can run the conversion
0 commit comments