From 444325cf71812e052e46b93fc48dcfb0c8a9a28f Mon Sep 17 00:00:00 2001 From: Roman Akhtariev Date: Wed, 7 Jun 2023 15:37:25 +0000 Subject: [PATCH 1/5] feat: CL swap tool --- Makefile | 11 ++- tests/cl-go-client/README.md | 52 +++++++++++++- tests/cl-go-client/go.mod | 26 +++---- tests/cl-go-client/go.sum | 14 ++++ tests/cl-go-client/main.go | 136 +++++++++++++++++++++++++++++++---- 5 files changed, 211 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index ed122769529..dcb28872158 100644 --- a/Makefile +++ b/Makefile @@ -446,9 +446,16 @@ localnet-state-export-stop: localnet-state-export-clean: localnet-clean -# create 1000 concentrated-liquidity positions in localosmosis at pool id 1 +# create 100 concentrated-liquidity positions in localosmosis at pool id 1 localnet-cl-create-positions: - go run tests/cl-go-client/main.go + go run tests/cl-go-client/main.go --operation 0 + +# does 100 swaps in localosmosis at pool id 1 +localnet-cl-swap: + go run tests/cl-go-client/main.go --operation 1 + +# does both of localnet-cl-create-positions and localnet-cl-swap +localnet-cl-positions-and-swaps: localnet-cl-create-positions localnet-cl-swap # This script retrieves Uniswap v3 Ethereum position data # from subgraph. It uses WETH / USDC pool. This is helpful diff --git a/tests/cl-go-client/README.md b/tests/cl-go-client/README.md index 75d7c60898f..1220d64eb29 100644 --- a/tests/cl-go-client/README.md +++ b/tests/cl-go-client/README.md @@ -24,8 +24,36 @@ make localnet-keys See `tests/localosmosis` for more info. +## Testnet Setup + +Configure a different `osmosisd` environment with configs. + +```bash +make set-env .osmosisd-testnet-script + +osmosisd init test-script + +cd $HOME/.osmosisd-testnet-script/config + +nano client.toml +``` + +Replace node RPC with the testnet value and save, + +Next, manually edit the `localosmosisFromHomePath` variable in the script: +https://github.com/osmosis-labs/osmosis/blob/98025f185ab2ee1b060511ed22679112abcc08fa/tests/cl-go-client/main.go#L28 + +Set the value to `.osmosisd-testnet-script` and save. + +Now, you are able to run this script on testnet. This assummes that +testnet accounts have been set up with the default test accounts +and balances. By default, we mean accounts created with +`make localnet-keys`. + ## Running +### Crete Positions + ```bash make localnet-cl-create-positions ``` @@ -33,4 +61,26 @@ make localnet-cl-create-positions In the current state, it does the following: - Queries status of the chain to make sure it's running. - Queries pool with id 1. If does not exist, creates it -- Sets up one CL position +- Sets up 100 CL positions (count configured at the top of the file) + +### Make Swaps + +```bash +make localnet-cl-swap +``` + +In the current state, it does the following: +- Queries status of the chain to make sure it's running. +- Queries pool with id 1. +- Performs 100 swaps against the pool with id 1. + +Note that this script does not set up positions, assumming they are +already set up. + +### Create Positions and Swap + +```bash +make localnet-cl-positions-and-swaps +``` + +This script runs "Create Positions" and "Make Swaps" scripts in sequence. diff --git a/tests/cl-go-client/go.mod b/tests/cl-go-client/go.mod index 9ef435a9fd5..1cc2a5f7204 100644 --- a/tests/cl-go-client/go.mod +++ b/tests/cl-go-client/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/cosmos/cosmos-sdk v0.47.2 github.com/ignite/cli v0.23.0 - github.com/osmosis-labs/osmosis/v15 v15.0.0-20230601221251-b764323ce770 + github.com/osmosis-labs/osmosis/v15 v15.0.0-20230602143948-7c9acb65e46c ) @@ -79,7 +79,7 @@ require ( github.com/libp2p/go-buffer-pool v0.1.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.17 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect github.com/minio/highwayhash v1.0.2 // indirect @@ -87,9 +87,9 @@ require ( github.com/mtibben/percent v0.2.1 // indirect github.com/nxadm/tail v1.4.8 // indirect github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230516205127-c213fddde069 // indirect - github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230529060317-d6d2dda0fb22 // indirect + github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230606194542-47ea1e2c85ca // indirect github.com/osmosis-labs/osmosis/v16 v16.0.0-20230603032959-4d2ba21b8d1e // indirect - github.com/pelletier/go-toml/v2 v2.0.6 // indirect + github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect @@ -103,13 +103,13 @@ require ( github.com/rs/cors v1.8.2 // indirect github.com/rs/zerolog v1.27.0 // indirect github.com/sasha-s/go-deadlock v0.3.1 // indirect - github.com/spf13/afero v1.9.3 // indirect + github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.1 // indirect github.com/spf13/cobra v1.7.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/spf13/viper v1.15.0 // indirect - github.com/stretchr/testify v1.8.3 // indirect + github.com/spf13/viper v1.16.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect github.com/subosito/gotenv v1.4.2 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect github.com/tendermint/btcd v0.1.1 // indirect @@ -123,16 +123,16 @@ require ( github.com/zondax/ledger-go v0.14.1 // indirect go.etcd.io/bbolt v1.3.6 // indirect go.opencensus.io v0.24.0 // indirect - golang.org/x/crypto v0.5.0 // indirect + golang.org/x/crypto v0.9.0 // indirect golang.org/x/exp v0.0.0-20230131160201-f062dba9d201 // indirect golang.org/x/mod v0.10.0 // indirect - golang.org/x/net v0.9.0 // indirect + golang.org/x/net v0.10.0 // indirect golang.org/x/sync v0.1.0 // indirect - golang.org/x/sys v0.7.0 // indirect - golang.org/x/term v0.7.0 // indirect + golang.org/x/sys v0.8.0 // indirect + golang.org/x/term v0.8.0 // indirect golang.org/x/text v0.9.0 // indirect - google.golang.org/genproto v0.0.0-20230125152338-dcaf20b6aeaa // indirect - google.golang.org/grpc v1.53.0 // indirect + google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect + google.golang.org/grpc v1.55.0 // indirect google.golang.org/protobuf v1.30.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/tests/cl-go-client/go.sum b/tests/cl-go-client/go.sum index 8d9f0192f84..f8fa21d7348 100644 --- a/tests/cl-go-client/go.sum +++ b/tests/cl-go-client/go.sum @@ -601,6 +601,7 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= @@ -691,6 +692,7 @@ github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230516205127-c213fddde06 github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230516205127-c213fddde069/go.mod h1:a7lhiXRpn8QJ21OhFpaEnUNErTSIafaYpp02q6uI/Dk= github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230529060317-d6d2dda0fb22 h1:I14d+U4gDSL5dHoQ3G+kGLhZP5Zj3mOxMb/97Xflusc= github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230529060317-d6d2dda0fb22/go.mod h1:GIvgXqD8NOtrpu5bJ052tZxWLFj4ekpi1BMwEHIvXVU= +github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230606194542-47ea1e2c85ca/go.mod h1:FqFOfj9+e5S1I7hR3baGUHrqje3g32EOHAXoOf7R00M= github.com/osmosis-labs/osmosis/v16 v16.0.0-20230602200356-bdf5b96b3674 h1:sCXD8SajkGC3AmHrwiklgTWHZUXcSHm8YFJKbvw6Lk4= github.com/osmosis-labs/osmosis/v16 v16.0.0-20230602200356-bdf5b96b3674/go.mod h1:Vg+05vXFc682OEF52HTqhEKF+deQ0GSt9rkisCFJ8Ug= github.com/osmosis-labs/osmosis/v16 v16.0.0-20230603032959-4d2ba21b8d1e h1:Rbkpe0cLh67eyWpCMN8u/6xDNHlWimrLceMEhtNJ0TI= @@ -710,6 +712,7 @@ github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtP github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= +github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac= github.com/peterh/liner v1.0.1-0.20180619022028-8c1271fcf47f/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc= github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0= @@ -814,6 +817,7 @@ github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2 github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk= github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= +github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA= github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48= @@ -831,6 +835,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU= github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA= +github.com/spf13/viper v1.16.0/go.mod h1:yg78JgCJcbrQOvV9YLXgkLaZqUidkY9K+Dd1FofRzQg= github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q= github.com/strangelove-ventures/packet-forward-middleware/v4 v4.0.5 h1:KKUqeGhVBK38+1LwThC8IeIcsJZ6COX5kvhiJroFqCM= github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= @@ -853,6 +858,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8= github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= @@ -954,8 +960,10 @@ golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE= golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= +golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -1048,8 +1056,10 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -1149,12 +1159,15 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ= golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -1313,6 +1326,7 @@ google.golang.org/genproto v0.0.0-20210126160654-44e461bb6506/go.mod h1:FWY/as6D google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20230125152338-dcaf20b6aeaa h1:qQPhfbPO23fwm/9lQr91L1u62Zo6cm+zI+slZT+uf+o= google.golang.org/genproto v0.0.0-20230125152338-dcaf20b6aeaa/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM= +google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU= google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= diff --git a/tests/cl-go-client/main.go b/tests/cl-go-client/main.go index 6c49011886d..119a40c672b 100644 --- a/tests/cl-go-client/main.go +++ b/tests/cl-go-client/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "flag" "fmt" "log" "math" @@ -22,6 +23,17 @@ import ( poolmanagertypes "github.com/osmosis-labs/osmosis/v16/x/poolmanager/types" ) +// operation defines the desired operation to be run by this script. +type operation int + +const ( + // createPositions creates positions in the CL pool with id expectedPoolId. + createPositions operation = iota + + // makeManySwaps makes many swaps in the CL pool with id expectedPoolId. + makeManySwaps +) + const ( expectedPoolId uint64 = 1 addressPrefix = "osmo" @@ -31,10 +43,12 @@ const ( denom1 = "uusdc" tickSpacing int64 = 100 accountNamePrefix = "lo-test" - numPositions = 1_000 + numPositions = 100 + numSwaps = 100 minAmountDeposited = int64(1_000_000) randSeed = 1 maxAmountDeposited = 1_00_000_000 + maxAmountSingleSwap = 1_000_000 ) var ( @@ -44,6 +58,14 @@ var ( ) func main() { + var ( + desiredOperation int + ) + + flag.IntVar(&desiredOperation, "operation", 0, fmt.Sprintf("operation to run:\nget subgraph data: %v, convert subgraph positions to osmo genesis: %v\nmerge converted subgraph genesis and localosmosis genesis: %v")) + + flag.Parse() + ctx := context.Background() clientHome := getClientHomePath() @@ -89,11 +111,23 @@ func main() { } } - minTick, maxTick := cltypes.MinTick, cltypes.MaxTick - log.Println(minTick, " ", maxTick) - rand.Seed(randSeed) + switch operation(desiredOperation) { + case createPositions: + createManyRandomPositions(igniteClient, expectedPoolId, numPositions) + return + case makeManySwaps: + swapSmallAmountsContinuously(igniteClient, expectedPoolId, numSwaps) + return + default: + log.Fatalf("invalid operation: %d", desiredOperation) + } +} + +func createManyRandomPositions(igniteClient cosmosclient.Client, poolId uint64, numPositions int) { + minTick, maxTick := cltypes.MinTick, cltypes.MaxTick + log.Println(minTick, " ", maxTick) for i := 0; i < numPositions; i++ { var ( // 1 to 9. These are localosmosis keyring test accounts with names such as: @@ -115,15 +149,66 @@ func main() { log.Println("creating position: pool id", expectedPoolId, "accountName", accountName, "lowerTick", lowerTick, "upperTick", upperTick, "token0Desired", tokenDesired0, "tokenDesired1", tokenDesired1, "defaultMinAmount", defaultMinAmount) maxRetries := 100 + var err error + for j := 0; j < maxRetries; j++ { + amt0, amt1, liquidity, err := createPosition(igniteClient, expectedPoolId, accountName, lowerTick, upperTick, tokensDesired, defaultMinAmount, defaultMinAmount) + log.Println("created position: amt0", amt0, "amt1", amt1, "liquidity", liquidity) + if err != nil { + log.Println("retrying, error occurred while creating position: ", err) + time.Sleep(8 * time.Second) + } else { + time.Sleep(200 * time.Millisecond) + break + } + } + + if err != nil { + log.Fatal(err) + } + } +} + +func swapSmallAmountsContinuously(igniteClient cosmosclient.Client, poolId uint64, numSwaps int) { + for i := 0; i < numSwaps; i++ { + var ( + randAccountNum = rand.Intn(8) + 1 + accountName = fmt.Sprintf("%s%d", accountNamePrefix, randAccountNum) + + isToken0In = rand.Intn(2) == 0 + + tokenOutMinAmount = sdk.OneInt() + ) + + tokenInDenom := denom0 + tokenOutDenom := denom1 + if !isToken0In { + tokenInDenom = denom1 + tokenOutDenom = denom0 + } + tokenInCoin := sdk.NewCoin(tokenInDenom, sdk.NewInt(rand.Int63n(maxAmountSingleSwap))) + + maxRetries := 100 + var err error for j := 0; j < maxRetries; j++ { - amt0, amt1, liquidity := createPosition(igniteClient, expectedPoolId, accountName, lowerTick, upperTick, tokensDesired, defaultMinAmount, defaultMinAmount) - if err == nil { - log.Println("created position: amt0", amt0, "amt1", amt1, "liquidity", liquidity) + + log.Println("making swap in: pool id", expectedPoolId, "tokenIn", tokenInCoin, "tokenOutDenom", tokenOutDenom, "tokenOutMinAmount", tokenOutMinAmount, "from", accountName) + + tokenOutAmount, err := makeSwap(igniteClient, expectedPoolId, accountName, tokenInCoin, tokenOutDenom, tokenOutMinAmount) + if err != nil { + log.Println("retrying, error occurred while creating position: ", err) + time.Sleep(8 * time.Second) + } else { + log.Println("swap made, token out amount: ", tokenOutAmount) + time.Sleep(200 * time.Millisecond) break } - time.Sleep(8 * time.Second) + } + if err != nil { + log.Fatal(err) } } + + log.Println("finished swapping, num swaps done", numSwaps) } func createPool(igniteClient cosmosclient.Client, accountName string) uint64 { @@ -145,7 +230,7 @@ func createPool(igniteClient cosmosclient.Client, accountName string) uint64 { return resp.PoolID } -func createPosition(client cosmosclient.Client, poolId uint64, senderKeyringAccountName string, lowerTick int64, upperTick int64, tokensProvided sdk.Coins, tokenMinAmount0, tokenMinAmount1 sdk.Int) (amountCreated0, amountCreated1 sdk.Int, liquidityCreated sdk.Dec) { +func createPosition(client cosmosclient.Client, poolId uint64, senderKeyringAccountName string, lowerTick int64, upperTick int64, tokensProvided sdk.Coins, tokenMinAmount0, tokenMinAmount1 sdk.Int) (amountCreated0, amountCreated1 sdk.Int, liquidityCreated sdk.Dec, err error) { accountMutex.Lock() // Lock access to getAccountAddressFromKeyring senderAddress := getAccountAddressFromKeyring(client, senderKeyringAccountName) accountMutex.Unlock() // Unlock access to getAccountAddressFromKeyring @@ -161,13 +246,40 @@ func createPosition(client cosmosclient.Client, poolId uint64, senderKeyringAcco } txResp, err := client.BroadcastTx(senderKeyringAccountName, msg) if err != nil { - log.Fatal(err) + return sdk.Int{}, sdk.Int{}, sdk.Dec{}, err } resp := cltypes.MsgCreatePositionResponse{} if err := txResp.Decode(&resp); err != nil { - log.Fatal(err) + return sdk.Int{}, sdk.Int{}, sdk.Dec{}, err + } + return resp.Amount0, resp.Amount1, resp.LiquidityCreated, nil +} + +func makeSwap(client cosmosclient.Client, poolId uint64, senderKeyringAccountName string, tokenInCoin sdk.Coin, tokenOutDenom string, tokenOutMinAmount sdk.Int) (sdk.Int, error) { + accountMutex.Lock() // Lock access to getAccountAddressFromKeyring + senderAddress := getAccountAddressFromKeyring(client, senderKeyringAccountName) + accountMutex.Unlock() // Unlock access to getAccountAddressFromKeyring + + msg := &poolmanagertypes.MsgSwapExactAmountIn{ + Sender: senderAddress, + Routes: []poolmanagertypes.SwapAmountInRoute{ + { + PoolId: expectedPoolId, + TokenOutDenom: tokenOutDenom, + }, + }, + TokenIn: tokenInCoin, + TokenOutMinAmount: tokenOutMinAmount, + } + txResp, err := client.BroadcastTx(senderKeyringAccountName, msg) + if err != nil { + return sdk.Int{}, err + } + resp := poolmanagertypes.MsgSwapExactAmountInResponse{} + if err := txResp.Decode(&resp); err != nil { + return sdk.Int{}, err } - return resp.Amount0, resp.Amount1, resp.LiquidityCreated + return resp.TokenOutAmount, nil } func getAccountAddressFromKeyring(igniteClient cosmosclient.Client, accountName string) string { From e10ea0ed3176780d69bd95b64ffdecab648b79f0 Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 7 Jun 2023 11:56:44 -0400 Subject: [PATCH 2/5] updates --- tests/cl-go-client/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cl-go-client/README.md b/tests/cl-go-client/README.md index 1220d64eb29..72af4fa1919 100644 --- a/tests/cl-go-client/README.md +++ b/tests/cl-go-client/README.md @@ -41,7 +41,7 @@ nano client.toml Replace node RPC with the testnet value and save, Next, manually edit the `localosmosisFromHomePath` variable in the script: -https://github.com/osmosis-labs/osmosis/blob/98025f185ab2ee1b060511ed22679112abcc08fa/tests/cl-go-client/main.go#L28 + Set the value to `.osmosisd-testnet-script` and save. From 27489bafe61dd5ef5e00544a99250fb0d59b68b0 Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 7 Jun 2023 11:57:57 -0400 Subject: [PATCH 3/5] updates --- tests/cl-go-client/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cl-go-client/main.go b/tests/cl-go-client/main.go index 119a40c672b..67f188b3285 100644 --- a/tests/cl-go-client/main.go +++ b/tests/cl-go-client/main.go @@ -62,7 +62,7 @@ func main() { desiredOperation int ) - flag.IntVar(&desiredOperation, "operation", 0, fmt.Sprintf("operation to run:\nget subgraph data: %v, convert subgraph positions to osmo genesis: %v\nmerge converted subgraph genesis and localosmosis genesis: %v")) + flag.IntVar(&desiredOperation, "operation", 0, fmt.Sprintf("operation to run:\ncreate positions: %v, make many swaps: %v", createPositions, makeManySwaps)) flag.Parse() From 8f09c79205828e76862645d771fff2d6721ae291 Mon Sep 17 00:00:00 2001 From: Roman Akhtariev Date: Wed, 7 Jun 2023 16:05:15 +0000 Subject: [PATCH 4/5] clean up --- tests/cl-go-client/main.go | 78 ++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/tests/cl-go-client/main.go b/tests/cl-go-client/main.go index 67f188b3285..aabb4638c20 100644 --- a/tests/cl-go-client/main.go +++ b/tests/cl-go-client/main.go @@ -146,25 +146,10 @@ func createManyRandomPositions(igniteClient cosmosclient.Client, poolId uint64, tokensDesired = sdk.NewCoins(tokenDesired0, tokenDesired1) ) - log.Println("creating position: pool id", expectedPoolId, "accountName", accountName, "lowerTick", lowerTick, "upperTick", upperTick, "token0Desired", tokenDesired0, "tokenDesired1", tokenDesired1, "defaultMinAmount", defaultMinAmount) - - maxRetries := 100 - var err error - for j := 0; j < maxRetries; j++ { - amt0, amt1, liquidity, err := createPosition(igniteClient, expectedPoolId, accountName, lowerTick, upperTick, tokensDesired, defaultMinAmount, defaultMinAmount) - log.Println("created position: amt0", amt0, "amt1", amt1, "liquidity", liquidity) - if err != nil { - log.Println("retrying, error occurred while creating position: ", err) - time.Sleep(8 * time.Second) - } else { - time.Sleep(200 * time.Millisecond) - break - } - } - - if err != nil { - log.Fatal(err) - } + runMessageWithRetries(func() error { + _, _, _, err := createPosition(igniteClient, expectedPoolId, accountName, lowerTick, upperTick, tokensDesired, defaultMinAmount, defaultMinAmount) + return err + }) } } @@ -187,25 +172,9 @@ func swapSmallAmountsContinuously(igniteClient cosmosclient.Client, poolId uint6 } tokenInCoin := sdk.NewCoin(tokenInDenom, sdk.NewInt(rand.Int63n(maxAmountSingleSwap))) - maxRetries := 100 - var err error - for j := 0; j < maxRetries; j++ { - - log.Println("making swap in: pool id", expectedPoolId, "tokenIn", tokenInCoin, "tokenOutDenom", tokenOutDenom, "tokenOutMinAmount", tokenOutMinAmount, "from", accountName) - - tokenOutAmount, err := makeSwap(igniteClient, expectedPoolId, accountName, tokenInCoin, tokenOutDenom, tokenOutMinAmount) - if err != nil { - log.Println("retrying, error occurred while creating position: ", err) - time.Sleep(8 * time.Second) - } else { - log.Println("swap made, token out amount: ", tokenOutAmount) - time.Sleep(200 * time.Millisecond) - break - } - } - if err != nil { - log.Fatal(err) - } + runMessageWithRetries(func() error { + return makeSwap(igniteClient, expectedPoolId, accountName, tokenInCoin, tokenOutDenom, tokenOutMinAmount) + }) } log.Println("finished swapping, num swaps done", numSwaps) @@ -235,6 +204,8 @@ func createPosition(client cosmosclient.Client, poolId uint64, senderKeyringAcco senderAddress := getAccountAddressFromKeyring(client, senderKeyringAccountName) accountMutex.Unlock() // Unlock access to getAccountAddressFromKeyring + log.Println("creating position: pool id", expectedPoolId, "accountName", senderKeyringAccountName, "lowerTick", lowerTick, "upperTick", upperTick, "token0Desired", tokensProvided[0], "tokenDesired1", tokensProvided[1], "defaultMinAmount", defaultMinAmount) + msg := &cltypes.MsgCreatePosition{ PoolId: poolId, Sender: senderAddress, @@ -252,14 +223,17 @@ func createPosition(client cosmosclient.Client, poolId uint64, senderKeyringAcco if err := txResp.Decode(&resp); err != nil { return sdk.Int{}, sdk.Int{}, sdk.Dec{}, err } + log.Println("created position: amt0", resp.Amount0, "amt1", resp.Amount1, "liquidity", resp.LiquidityCreated) return resp.Amount0, resp.Amount1, resp.LiquidityCreated, nil } -func makeSwap(client cosmosclient.Client, poolId uint64, senderKeyringAccountName string, tokenInCoin sdk.Coin, tokenOutDenom string, tokenOutMinAmount sdk.Int) (sdk.Int, error) { +func makeSwap(client cosmosclient.Client, poolId uint64, senderKeyringAccountName string, tokenInCoin sdk.Coin, tokenOutDenom string, tokenOutMinAmount sdk.Int) error { accountMutex.Lock() // Lock access to getAccountAddressFromKeyring senderAddress := getAccountAddressFromKeyring(client, senderKeyringAccountName) accountMutex.Unlock() // Unlock access to getAccountAddressFromKeyring + log.Println("making swap in: pool id", expectedPoolId, "tokenIn", tokenInCoin, "tokenOutDenom", tokenOutDenom, "tokenOutMinAmount", tokenOutMinAmount, "from", senderKeyringAccountName) + msg := &poolmanagertypes.MsgSwapExactAmountIn{ Sender: senderAddress, Routes: []poolmanagertypes.SwapAmountInRoute{ @@ -273,13 +247,15 @@ func makeSwap(client cosmosclient.Client, poolId uint64, senderKeyringAccountNam } txResp, err := client.BroadcastTx(senderKeyringAccountName, msg) if err != nil { - return sdk.Int{}, err + return err } resp := poolmanagertypes.MsgSwapExactAmountInResponse{} if err := txResp.Decode(&resp); err != nil { - return sdk.Int{}, err + return err } - return resp.TokenOutAmount, nil + + log.Println("swap made, token out amount: ", resp.TokenOutAmount) + return nil } func getAccountAddressFromKeyring(igniteClient cosmosclient.Client, accountName string) string { @@ -305,6 +281,24 @@ func getClientHomePath() string { return currentUser.HomeDir + localosmosisFromHomePath } +func runMessageWithRetries(runMsg func() error) { + maxRetries := 100 + var err error + for j := 0; j < maxRetries; j++ { + err := runMsg() + if err != nil { + log.Println("retrying, error occurred while creating position: ", err) + time.Sleep(8 * time.Second) + } else { + time.Sleep(200 * time.Millisecond) + break + } + } + if err != nil { + log.Fatal(err) + } +} + func roundTickDown(tickIndex int64, tickSpacing int64) int64 { // Round the tick index down to the nearest tick spacing if the tickIndex is in between authorized tick values // Note that this is Euclidean modulus. From 2dade3e93acb0127b9ad537e770234b6f64b6b8f Mon Sep 17 00:00:00 2001 From: Roman Akhtariev Date: Wed, 7 Jun 2023 17:25:48 +0000 Subject: [PATCH 5/5] support for large swaps --- Makefile | 18 +++++--- tests/cl-go-client/README.md | 39 ++++++++++++++--- tests/cl-go-client/main.go | 81 +++++++++++++++++++++++++++++------- 3 files changed, 112 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index dcb28872158..384a0eaeabf 100644 --- a/Makefile +++ b/Makefile @@ -450,12 +450,20 @@ localnet-state-export-clean: localnet-clean localnet-cl-create-positions: go run tests/cl-go-client/main.go --operation 0 -# does 100 swaps in localosmosis at pool id 1 -localnet-cl-swap: - go run tests/cl-go-client/main.go --operation 1 +# does 100 small randomized swaps in localosmosis at pool id 1 +localnet-cl-small-swap: + go run tests/cl-go-client/main.go --operation 1 -# does both of localnet-cl-create-positions and localnet-cl-swap -localnet-cl-positions-and-swaps: localnet-cl-create-positions localnet-cl-swap +# does 100 large swaps where the output of the previous swap is swapped back at the +# next swap. localosmosis at pool id 1 +localnet-cl-large-swap: + go run tests/cl-go-client/main.go --operation 2 + +# 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 + +# does both of localnet-cl-create-positions and localnet-cl-large-swap +localnet-cl-positions-large-swaps: localnet-cl-create-positions localnet-cl-large-swap # This script retrieves Uniswap v3 Ethereum position data # from subgraph. It uses WETH / USDC pool. This is helpful diff --git a/tests/cl-go-client/README.md b/tests/cl-go-client/README.md index 72af4fa1919..ddcd6f3522a 100644 --- a/tests/cl-go-client/README.md +++ b/tests/cl-go-client/README.md @@ -63,24 +63,51 @@ In the current state, it does the following: - Queries pool with id 1. If does not exist, creates it - Sets up 100 CL positions (count configured at the top of the file) -### Make Swaps +### Make Small Randomized Swaps ```bash -make localnet-cl-swap +make localnet-cl-small-swap ``` In the current state, it does the following: - Queries status of the chain to make sure it's running. - Queries pool with id 1. -- Performs 100 swaps against the pool with id 1. +- Performs 100 randomized swaps against the pool with id 1. Note that this script does not set up positions, assumming they are already set up. -### Create Positions and Swap +### Create Positions and Make Small Randomized Swaps ```bash -make localnet-cl-positions-and-swaps +make localnet-cl-positions-small-swaps ``` -This script runs "Create Positions" and "Make Swaps" scripts in sequence. +This script runs "Create Positions" and "Make Small Randomized Swaps" scripts in sequence. + +### Make Large Invertible Swaps + +```bash +make localnet-cl-large-swap +``` + +In the current state, it does the following: +- Queries status of the chain to make sure it's running. +- Queries pool with id 1. +- Performs 100 large swaps where the output of the previous swap is swapped back at the +next swap. + +In other words, it takes one large amount and swaps it into the pool. Then, takes output token +and swaps it back while accounting for the spread factor. This is done to +ensure that we cross ticks while minimizing the chance of running out of funds or liquidity. + +Note that this script does not set up positions, assumming they are +already set up. + +### Create Positions and Make Large Invertible Swaps + +```bash +make localnet-cl-positions-large-swaps +``` + +This script runs "Create Positions" and "Make Large Invertible Swaps" scripts in sequence. diff --git a/tests/cl-go-client/main.go b/tests/cl-go-client/main.go index aabb4638c20..16364af7aca 100644 --- a/tests/cl-go-client/main.go +++ b/tests/cl-go-client/main.go @@ -30,8 +30,14 @@ const ( // createPositions creates positions in the CL pool with id expectedPoolId. createPositions operation = iota - // makeManySwaps makes many swaps in the CL pool with id expectedPoolId. - makeManySwaps + // makeManySmallSwaps makes many swaps in the CL pool with id expectedPoolId. + makeManySmallSwaps + + // makeManyLargeSwaps makes many large swaps in the CL pool with id expectedPoolId. + // it takes one large amount and swaps it into the pool. Then, takes output token + // and swaps it back while accounting for the spread factor. This is done to + // ensure that we cross ticks while minimizing the chance of running out of funds or liquidity. + makeManyInvertibleLargeSwaps ) const ( @@ -49,12 +55,14 @@ const ( randSeed = 1 maxAmountDeposited = 1_00_000_000 maxAmountSingleSwap = 1_000_000 + largeSwapAmount = 90_000_000_000 ) var ( - defaultAccountName = fmt.Sprintf("%s%d", accountNamePrefix, 1) - defaultMinAmount = sdk.ZeroInt() - accountMutex sync.Mutex + defaultAccountName = fmt.Sprintf("%s%d", accountNamePrefix, 1) + defaultMinAmount = sdk.ZeroInt() + defaultSpreadFactor = sdk.MustNewDecFromStr("0.001") + accountMutex sync.Mutex ) func main() { @@ -62,7 +70,7 @@ func main() { desiredOperation int ) - flag.IntVar(&desiredOperation, "operation", 0, fmt.Sprintf("operation to run:\ncreate positions: %v, make many swaps: %v", createPositions, makeManySwaps)) + flag.IntVar(&desiredOperation, "operation", 0, fmt.Sprintf("operation to run:\ncreate positions: %v, make many swaps: %v", createPositions, makeManySmallSwaps)) flag.Parse() @@ -117,9 +125,11 @@ func main() { case createPositions: createManyRandomPositions(igniteClient, expectedPoolId, numPositions) return - case makeManySwaps: - swapSmallAmountsContinuously(igniteClient, expectedPoolId, numSwaps) + case makeManySmallSwaps: + swapRandomSmallAmountsContinuously(igniteClient, expectedPoolId, numSwaps) return + case makeManyInvertibleLargeSwaps: + swapGivenLargeAmountsBothDirections(igniteClient, expectedPoolId, numSwaps, largeSwapAmount) default: log.Fatalf("invalid operation: %d", desiredOperation) } @@ -153,7 +163,7 @@ func createManyRandomPositions(igniteClient cosmosclient.Client, poolId uint64, } } -func swapSmallAmountsContinuously(igniteClient cosmosclient.Client, poolId uint64, numSwaps int) { +func swapRandomSmallAmountsContinuously(igniteClient cosmosclient.Client, poolId uint64, numSwaps int) { for i := 0; i < numSwaps; i++ { var ( randAccountNum = rand.Intn(8) + 1 @@ -173,7 +183,48 @@ func swapSmallAmountsContinuously(igniteClient cosmosclient.Client, poolId uint6 tokenInCoin := sdk.NewCoin(tokenInDenom, sdk.NewInt(rand.Int63n(maxAmountSingleSwap))) runMessageWithRetries(func() error { - return makeSwap(igniteClient, expectedPoolId, accountName, tokenInCoin, tokenOutDenom, tokenOutMinAmount) + _, err := makeSwap(igniteClient, expectedPoolId, accountName, tokenInCoin, tokenOutDenom, tokenOutMinAmount) + return err + }) + } + + log.Println("finished swapping, num swaps done", numSwaps) +} + +func swapGivenLargeAmountsBothDirections(igniteClient cosmosclient.Client, poolId uint64, numSwaps int, largeStartAmount int64) { + var ( + randAccountNum = rand.Intn(8) + 1 + accountName = fmt.Sprintf("%s%d", accountNamePrefix, randAccountNum) + + isToken0In = rand.Intn(2) == 0 + + tokenOutMinAmount = sdk.OneInt() + ) + + tokenInDenom := denom0 + tokenOutDenom := denom1 + if !isToken0In { + tokenInDenom = denom1 + tokenOutDenom = denom0 + } + + tokenInCoin := sdk.NewCoin(tokenInDenom, sdk.NewInt(largeStartAmount)) + + for i := 0; i < numSwaps; i++ { + runMessageWithRetries(func() error { + tokenOut, err := makeSwap(igniteClient, expectedPoolId, accountName, tokenInCoin, tokenOutDenom, tokenOutMinAmount) + + if err == nil { + // Swap the resulting amount out back while accounting for spread factor. + // This is to make sure we can continue swapping back and forth and not run + // out of funds or liquidity. + tempTokenInDenom := tokenInCoin.Denom + // new token in = token out / (1 - spread factor) + tokenInCoin = sdk.NewCoin(tokenOutDenom, tokenOut.ToDec().Quo(sdk.OneDec().Sub(defaultSpreadFactor)).RoundInt()) + tokenOutDenom = tempTokenInDenom + } + + return err }) } @@ -186,7 +237,7 @@ func createPool(igniteClient cosmosclient.Client, accountName string) uint64 { Denom1: denom0, Denom0: denom1, TickSpacing: 1, - SpreadFactor: sdk.ZeroDec(), + SpreadFactor: defaultSpreadFactor, } txResp, err := igniteClient.BroadcastTx(accountName, msg) if err != nil { @@ -227,7 +278,7 @@ func createPosition(client cosmosclient.Client, poolId uint64, senderKeyringAcco return resp.Amount0, resp.Amount1, resp.LiquidityCreated, nil } -func makeSwap(client cosmosclient.Client, poolId uint64, senderKeyringAccountName string, tokenInCoin sdk.Coin, tokenOutDenom string, tokenOutMinAmount sdk.Int) error { +func makeSwap(client cosmosclient.Client, poolId uint64, senderKeyringAccountName string, tokenInCoin sdk.Coin, tokenOutDenom string, tokenOutMinAmount sdk.Int) (sdk.Int, error) { accountMutex.Lock() // Lock access to getAccountAddressFromKeyring senderAddress := getAccountAddressFromKeyring(client, senderKeyringAccountName) accountMutex.Unlock() // Unlock access to getAccountAddressFromKeyring @@ -247,15 +298,15 @@ func makeSwap(client cosmosclient.Client, poolId uint64, senderKeyringAccountNam } txResp, err := client.BroadcastTx(senderKeyringAccountName, msg) if err != nil { - return err + return sdk.Int{}, err } resp := poolmanagertypes.MsgSwapExactAmountInResponse{} if err := txResp.Decode(&resp); err != nil { - return err + return sdk.Int{}, err } log.Println("swap made, token out amount: ", resp.TokenOutAmount) - return nil + return resp.TokenOutAmount, nil } func getAccountAddressFromKeyring(igniteClient cosmosclient.Client, accountName string) string {