Skip to content

Commit

Permalink
feat: add add-to-position/withdraw position cl script (osmosis-labs#5789
Browse files Browse the repository at this point in the history
)

* add incentive claiming to script

* add random slice subset to osmoutils

* update osmoutils
  • Loading branch information
stackman27 authored Jul 8, 2023
1 parent 1e3149f commit a63ee8d
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 34 deletions.
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,23 @@ localnet-cl-external-incentive:
localnet-cl-create-pool:
go run tests/cl-go-client/main.go --operation 4

# claims spread rewards for a random account for a random
# subset of positions.
localnet-cl-claim-spread-rewards:
go run tests/cl-go-client/main.go --operation 5

# claims incentives for a random account for a random
# subset of positions.
localnet-cl-claim-incentives:
go run tests/cl-go-client/main.go --operation 6

localnet-cl-add-to-positions:
go run tests/cl-go-client/main.go --operation 7

localnet-cl-withdraw-positions:
go run tests/cl-go-client/main.go --operation 8


# does both of localnet-cl-create-positions and localnet-cl-small-swap
localnet-cl-positions-small-swaps: localnet-cl-create-positions localnet-cl-small-swap

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/ory/dockertest/v3 v3.10.0
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230629191111-f375469de8b6
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230623115558-38aaab07d343
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230707192814-b6283c63146e
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230328024000-175ec88e4304
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230602130523-f9a94d8bbd10
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -63,7 +63,6 @@ require (
github.com/google/btree v1.1.2 // indirect
github.com/junk1tm/musttag v0.5.0 // indirect
github.com/kkHAIKE/contextcheck v1.1.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/maratori/testableexamples v1.0.0 // indirect
github.com/nunnatsa/ginkgolinter v0.9.0 // indirect
github.com/regen-network/cosmos-proto v0.3.1 // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,6 @@ github.com/klauspost/compress v1.15.11/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrD
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPRLkC4NPOvfYeR5KNOrY6TD+/sAC3HXPZgDYg=
github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -950,8 +949,8 @@ github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:Ylmch
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230629191111-f375469de8b6 h1:Kmkx5Rh72+LB8AL6dc6fZA+IVR0INu0YIiMF2ScDhaQ=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230629191111-f375469de8b6/go.mod h1:JTym95/bqrSnG5MPcXr1YDhv43JdCeo3p+iDbazoX68=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230623115558-38aaab07d343 h1:7V2b3+mSnLnK0Px+Dl3vnxAQgk4SV8e9ohfJ/tKsq0M=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230623115558-38aaab07d343/go.mod h1:FqFOfj9+e5S1I7hR3baGUHrqje3g32EOHAXoOf7R00M=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230707192814-b6283c63146e h1:ZUiM1+ZJ+zY6Br1vfiuSaL8EurM+j98zDPjmeDQQvXw=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230707192814-b6283c63146e/go.mod h1:Pl8Nzx6O6ow/+aqfMoMSz4hX+zz6RrnDYsooptECGxM=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230328024000-175ec88e4304 h1:RIrWLzIiZN5Xd2JOfSOtGZaf6V3qEQYg6EaDTAkMnCo=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230328024000-175ec88e4304/go.mod h1:yPWoJTj5RKrXKUChAicp+G/4Ni/uVEpp27mi/FF/L9c=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230602130523-f9a94d8bbd10 h1:XrES5AHZMZ/Y78boW35PTignkhN9h8VvJ1sP8EJDIu8=
Expand Down
15 changes: 15 additions & 0 deletions osmoutils/slice_helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package osmoutils

import (
"math/rand"
"reflect"
"sort"

Expand Down Expand Up @@ -102,3 +103,17 @@ func Contains[T comparable](slice []T, item T) bool {
}
return false
}

// GetRandomSubset returns a random subset of the given slice
func GetRandomSubset[T any](slice []T) []T {
if len(slice) == 0 {
return []T{}
}

rand.Shuffle(len(slice), func(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
})

n := rand.Intn(len(slice))
return slice[:n]
}
49 changes: 49 additions & 0 deletions osmoutils/slice_helper_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package osmoutils_test

import (
"math/rand"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -123,3 +125,50 @@ func TestContains(t *testing.T) {
})
}
}

func TestGetRandomSubset(t *testing.T) {
tests := []struct {
name string
slice []int
}{
{
name: "Empty slice",
slice: []int{},
},
{
name: "Slice of integers",
slice: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
},
}

rand.Seed(time.Now().UnixNano())

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := osmoutils.GetRandomSubset(tt.slice)

// Check if the length of the returned subset is less than or equal to the length of the original slice
if len(got) > len(tt.slice) {
t.Errorf("GetRandomSubset() returned subset length %d, expected less than or equal to %d", len(got), len(tt.slice))
}

// Check if the returned subset contains only elements from the original slice
for _, v := range got {
if !contains(tt.slice, v) {
t.Errorf("GetRandomSubset() returned element %v not found in the original slice", v)
}
}
})
}
}

// contains checks if a slice contains a specific element
func contains(slice interface{}, element interface{}) bool {
s := reflect.ValueOf(slice)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(s.Index(i).Interface(), element) {
return true
}
}
return false
}
Loading

0 comments on commit a63ee8d

Please sign in to comment.