-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* separate a computeAndRankRoutesByDirectQuote method * refactor: better pricing * more tests and impl * lint * updates * lint * updates
- Loading branch information
Showing
9 changed files
with
345 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package usecase | ||
|
||
import "github.com/osmosis-labs/osmosis/osmomath" | ||
|
||
var ( | ||
TenE9 = osmomath.NewInt(1_000_000_000) | ||
TenE8 = osmomath.NewInt(100_000_000) | ||
TenE7 = osmomath.NewInt(10_000_000) | ||
TenE6 = osmomath.NewInt(1_000_000) | ||
TenE5 = osmomath.NewInt(100_000) | ||
TenE4 = osmomath.NewInt(10_000) | ||
TenE3 = osmomath.NewInt(1_000) | ||
TenE2 = osmomath.NewInt(100) | ||
TenE1 = osmomath.NewInt(10) | ||
) | ||
|
||
// GetPrecomputeOrderOfMagnitude returns the order of magnitude of the given amount. | ||
// Uses look up table for precomputed order of magnitudes. | ||
func GetPrecomputeOrderOfMagnitude(amount osmomath.Int) int { | ||
if amount.GT(TenE9) { | ||
a := amount.Quo(TenE9) | ||
return 9 + GetPrecomputeOrderOfMagnitude(a) | ||
} | ||
if amount.GTE(TenE9) { | ||
return 9 | ||
} | ||
if amount.GTE(TenE8) { | ||
return 8 | ||
} | ||
if amount.GTE(TenE7) { | ||
return 7 | ||
} | ||
if amount.GTE(TenE6) { | ||
return 6 | ||
} | ||
if amount.GTE(TenE5) { | ||
return 5 | ||
} | ||
if amount.GTE(TenE4) { | ||
return 4 | ||
} | ||
if amount.GTE(TenE3) { | ||
return 3 | ||
} | ||
if amount.GTE(TenE2) { | ||
return 2 | ||
} | ||
if amount.GTE(TenE1) { | ||
return 1 | ||
} | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package usecase_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/osmosis-labs/osmosis/osmomath" | ||
"github.com/osmosis-labs/sqs/router/usecase" | ||
) | ||
|
||
var ( | ||
testAmount = osmomath.NewInt(1234567890323344555) | ||
) | ||
|
||
func (s *RouterTestSuite) TestGetPrecomputeOrderOfMagnitude() { | ||
|
||
tests := map[string]struct { | ||
amount osmomath.Int | ||
}{ | ||
"0 = 0": { | ||
amount: osmomath.ZeroInt(), | ||
}, | ||
"1 = 0": { | ||
amount: osmomath.OneInt(), | ||
}, | ||
"9.99 = 0": { | ||
amount: osmomath.NewInt(9), | ||
}, | ||
"10^9 - 1": { | ||
amount: usecase.TenE9.Sub(osmomath.OneInt()), | ||
}, | ||
"10^9": { | ||
amount: usecase.TenE9, | ||
}, | ||
"10^9 +1": { | ||
amount: usecase.TenE9.Add(osmomath.OneInt()), | ||
}, | ||
"10^18 +1": { | ||
amount: usecase.TenE9.Mul(usecase.TenE9).Add(osmomath.OneInt()), | ||
}, | ||
"10^15 +5": { | ||
amount: usecase.TenE9.Mul(usecase.TenE6).Add(osmomath.OneInt()), | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
s.Run(name, func() { | ||
|
||
actual := usecase.GetPrecomputeOrderOfMagnitude(tc.amount) | ||
|
||
expected := osmomath.OrderOfMagnitude(tc.amount.ToLegacyDec()) | ||
|
||
s.Require().Equal(expected, actual) | ||
}) | ||
} | ||
|
||
} | ||
|
||
// go test -benchmem -run=^$ -bench ^BenchmarkGetPrecomputeOrderOfMagnitude$ github.com/osmosis-labs/sqs/router/usecase -count=6 | ||
func BenchmarkGetPrecomputeOrderOfMagnitude(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_ = usecase.GetPrecomputeOrderOfMagnitude(testAmount) | ||
} | ||
} | ||
|
||
// go test -benchmem -run=^$ -bench ^BenchmarkOrderOfMagnitude$ github.com/osmosis-labs/sqs/router/usecase -count=6 > old | ||
func BenchmarkOrderOfMagnitude(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_ = osmomath.OrderOfMagnitude(testAmount.ToLegacyDec()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.