-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into zetacore-solana-minimum-withdraw
- Loading branch information
Showing
48 changed files
with
1,294 additions
and
500 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
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
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,21 @@ | ||
package math | ||
|
||
import "cosmossdk.io/math" | ||
|
||
// IncreaseUintByPercent is a function that increases Uint by a percentage. | ||
// Example: IncreaseUintByPercent(4000, 20) = 4000 * 1,2 = 4800 | ||
// Returns result and increase amount. | ||
func IncreaseUintByPercent(amount math.Uint, percent uint64) (math.Uint, math.Uint) { | ||
switch { | ||
case percent == 0: | ||
// noop | ||
return amount, math.ZeroUint() | ||
case percent%100 == 0: | ||
// optimization: a simple multiplication | ||
increase := amount.MulUint64(percent / 100) | ||
return amount.Add(increase), increase | ||
default: | ||
increase := amount.MulUint64(percent).QuoUint64(100) | ||
return amount.Add(increase), increase | ||
} | ||
} |
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,42 @@ | ||
package math | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIncreaseUintByPercent(t *testing.T) { | ||
for i, tt := range []struct { | ||
in math.Uint | ||
percent uint64 | ||
expected math.Uint | ||
}{ | ||
{in: math.NewUint(444), percent: 0, expected: math.NewUint(444)}, | ||
{in: math.NewUint(100), percent: 4, expected: math.NewUint(104)}, | ||
{in: math.NewUint(100), percent: 100, expected: math.NewUint(200)}, | ||
{in: math.NewUint(4000), percent: 50, expected: math.NewUint(6000)}, | ||
{in: math.NewUint(2500), percent: 100, expected: math.NewUint(5000)}, | ||
{in: math.NewUint(10000), percent: 33, expected: math.NewUint(13300)}, | ||
} { | ||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { | ||
actual, increase := IncreaseUintByPercent(tt.in, tt.percent) | ||
|
||
original := actual.Sub(increase).Uint64() | ||
|
||
assert.Equal(t, int(tt.expected.Uint64()), int(actual.Uint64())) | ||
assert.Equal(t, int(tt.in.Uint64()), int(original)) | ||
|
||
t.Logf( | ||
"input: %d, percent: %d, expected: %d, actual: %d, increase: %d", | ||
tt.in.Uint64(), | ||
tt.percent, | ||
tt.expected.Uint64(), | ||
actual.Uint64(), | ||
increase.Uint64(), | ||
) | ||
}) | ||
} | ||
} |
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 math | ||
|
||
import "slices" | ||
|
||
type number interface { | ||
~int | ~int8 | ~int16 | ~int32 | ~int64 | | ||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ||
} | ||
|
||
// SliceMedianValue returns the median value of the given slice. | ||
// Returns 0 for an empty slice. If inPlace is true, the input slice will be sorted in place. | ||
// Otherwise, a copy of the input slice will be sorted. | ||
func SliceMedianValue[T number](items []T, inPlace bool) T { | ||
if inPlace { | ||
return sliceMedianValue(items) | ||
} | ||
|
||
copied := make([]T, len(items)) | ||
copy(copied, items) | ||
|
||
out := sliceMedianValue(copied) | ||
|
||
// We don't need the copy anymore | ||
//nolint:ineffassign // let's help the garbage collector :) | ||
copied = nil | ||
|
||
return out | ||
} | ||
|
||
func sliceMedianValue[T number](items []T) T { | ||
switch len(items) { | ||
case 0: | ||
return 0 | ||
case 1: | ||
return items[0] | ||
} | ||
|
||
slices.Sort(items) | ||
|
||
// note that int division is used here e.g. 5/2 => 2 | ||
|
||
// []int{1 2 3 4 5} => items[(5/2)] => items[2] => 3 | ||
if len(items)%2 == 1 { | ||
return items[len(items)/2] | ||
} | ||
|
||
// odd number of items | ||
rightIndex := len(items) / 2 | ||
leftIndex := rightIndex - 1 | ||
|
||
// []int{1 2 3 4} => (items[1] + items[2]) / 2 => 5/2 => 2 | ||
return (items[leftIndex] + items[rightIndex]) / 2 | ||
} |
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,75 @@ | ||
package math | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSliceMedianValue(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
input []int | ||
expected int | ||
inPlace bool | ||
}{ | ||
{ | ||
name: "empty", | ||
input: nil, | ||
expected: 0, | ||
inPlace: false, | ||
}, | ||
{ | ||
name: "single", | ||
input: []int{10}, | ||
expected: 10, | ||
}, | ||
{ | ||
name: "two", | ||
input: []int{10, 20}, | ||
expected: 15, | ||
}, | ||
{ | ||
name: "even", | ||
input: []int{30, 20, 10, 20}, | ||
expected: 20, | ||
}, | ||
{ | ||
name: "even in-place", | ||
input: []int{30, 20, 10, 20}, | ||
expected: 20, | ||
inPlace: true, | ||
}, | ||
{ | ||
name: "odd", | ||
input: []int{5, 5, 6, 1, 1, 1, 4}, | ||
expected: 4, | ||
}, | ||
{ | ||
name: "odd in-place", | ||
input: []int{1, 1, 1, 1, 7, 7, 7, 7}, | ||
expected: 4, | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// ASSERT | ||
// Given a copy of the input slice | ||
var snapshot []int | ||
for _, v := range tt.input { | ||
snapshot = append(snapshot, v) | ||
} | ||
|
||
// ACT | ||
out := SliceMedianValue(tt.input, tt.inPlace) | ||
|
||
// ASSERT | ||
assert.Equal(t, tt.expected, out) | ||
|
||
// Check that elements of the input slice are unchanged | ||
if !tt.inPlace { | ||
assert.Equal(t, snapshot, tt.input) | ||
} | ||
}) | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.