How do i programatically swap/stake on the Osmosis DEX?

244 views Asked by At

I am working on a project and I need to programaticaly do some token swaps. I know about osmojs. but my project is not JavaScript or TypeScript, so I need to make the RPC calls myself. Unfortunately I could not find much documentation on how to do this (forgive me I am very new to this and Cosmos as a whole), save for the buf.build files which: a) haven't been updated since last year and b) is incomplete, for example it doesn't have the poolmanager module.

Moreover, I recall a page about gRPC on the osmosis docs site, along with links for the protobuf files, which aligns well with what I have been doing for cosmos, but any link that shows up when googling "osmosis grpc" now leads to a 404 page. All I can find now are links to a Tendermint RPC documentation which uses regular HTTP calls with JSON, but somehow there is still protobuf involved (and amino? wasn't that deprecated in favor of protobuf in the cosmos sdk? and speaking of deprecation, isn't Tendermint as a whole a predecessor to CometBFT). I did manage to find the gRPC endpoints for mainnet and testnet by guessing based on the pattern they are naming the rest of their endpoints though...

As you can see, I am very lost, I would appreciate anything that points me in the right direction :)

I tried signing a MsgSwapExactAmountIn

MsgSwapExactAmountIn(
        sender: wallet.bech32Address('osmo'),
        routes: [SwapAmountInRoute(poolId: Int64(1), tokenOutDenom: 'uion')],
        tokenIn: Coin(amount: '1000000', denom: 'uosmo'));

and broadcasting it with cosmos.tx.v1beta1.Service.BroadcastTx on the osmosis testnet, and the response i got was...a golang panic about a nil pointer on the node, which doesn't tell me how to fix the request im making but its enough to tell me that's not how it is done I am guess. The response was:

txhash: 9B4CE689BFB6618ADF048C76C0A3A14244560F1252C6AFD228BC26FC75B6A35C
codespace: undefined
code: 111222
rawLog: recovered: runtime error: invalid memory address or nil pointer dereference
stack:
goroutine 721059165 [running]:
runtime/debug.Stack()
    runtime/debug/stack.go:24 +0x65
github.com/cosmos/cosmos-sdk/baseapp.newDefaultRecoveryMiddleware.func1({0x28158a0, 0x4756310})
    github.com/cosmos/[email protected]/baseapp/recovery.go:71 +0x27
github.com/cosmos/cosmos-sdk/baseapp.newRecoveryMiddleware.func1({0x28158a0?, 0x4756310?})
    github.com/cosmos/[email protected]/baseapp/recovery.go:39 +0x30
github.com/cosmos/cosmos-sdk/baseapp.processRecovery({0x28158a0, 0x4756310}, 0xc04519bb00?)
    github.com/cosmos/[email protected]/baseapp/recovery.go:28 +0x37
github.com/cosmos/cosmos-sdk/baseapp.processRecovery({0x28158a0, 0x4756310}, 0x32b4a20?)
    github.com/cosmos/[email protected]/baseapp/recovery.go:33 +0x5e
github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).runTx.func1()
    github.com/cosmos/[email protected]/baseapp/baseapp.go:620 +0xf0
panic({0x28158a0, 0x4756310})
    runtime/panic.go:884 +0x213
math/big.(*Int).Sign(...)
    math/big/int.go:41
cosmossdk.io/math.Int.IsPositive(...)
    cosmossdk.io/[email protected]/int.go:211
github.com/osmosis-labs/osmosis/v20/x/gamm/types.MsgSwapExactAmountIn.ValidateBasic({{0xc05658acf0, 0x2b}, {0xc049082048, 0x1, 0x1}, {{0xc05b120514, 0x5}, {0xc057b6a8c0}}, {0x0}})
    github.com/osmosis-labs/osmosis/v20/x/gamm/types/msgs.go:85 +0x143
github.com/cosmos/cosmos-sdk/baseapp.validateBasicTxMsgs({0xc011d3a720?, 0x1, 0x140?})
    github.com/cosmos/[email protected]/baseapp/baseapp.go:538 +0x8c
github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).runTx(0xc001203200, 0x0, {0xc0226a3680, 0x127, 0x140})
    github.com/cosmos/[email protected]/baseapp/baseapp.go:653 +0x425
github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).CheckTx(0xc001203200, {{0xc0226a3680?, 0xc0014dc040?, 0x6bc34e?}, 0x14dc130?})
    github.com/cosmos/[email protected]/baseapp/abci.go:276 +0x135
github.com/tendermint/tendermint/abci/client.(*localClient).CheckTxAsync(0xc000f3ef60, {{0xc0226a3680?, 0xd792e0?, 0xc001210e80?}, 0x14dc2a0?})
    github.com/tendermint/[email protected]/abci/client/local_client.go:104 +0x118
github.com/tendermint/tendermint/proxy.(*appConnMempool).CheckTxAsync(0xc001210e80?, {{0xc0226a3680?, 0xb?, 0x6?}, 0x14dc350?})
    github.com/tendermint/[email protected]/proxy/app_conn.go:126 +0x26
github.com/tendermint/tendermint/mempool/v0.(*CListMempool).CheckTx(0xc0023f8fc0, {0xc0226a3680, 0x127, 0x140}, 0x20?, {0x20?, {0x0?, 0x20?}})
    github.com/tendermint/[email protected]/mempool/v0/clist_mempool.go:254 +0x30d
github.com/tendermint/tendermint/rpc/core.BroadcastTxSync(0xc00133c040, {0xc0226a3680, 0x127, 0x140})
    github.com/tendermint/[email protected]/rpc/core/mempool.go:36 +0xf6
github.com/tendermint/tendermint/rpc/client/local.(*Local).BroadcastTxSync(0xc0440fff38?, {0x0?, 0x0?}, {0xc0226a3680?, 0x48afb20?, 0x0?})
    github.com/tendermint/[email protected]/rpc/client/local/local.go:99 +0x2b
github.com/cosmos/cosmos-sdk/client.Context.BroadcastTxSync({{0x0, 0x0, 0x0}, {0x32c40e8, 0xc00133c020}, {0x0, 0x0}, {0x32a62d8, 0xc0010e8810}, 0x0, ...}, ...)
    github.com/cosmos/[email protected]/client/broadcast.go:116 +0x12e
github.com/cosmos/cosmos-sdk/client.Context.BroadcastTx({{0x0, 0x0, 0x0}, {0x32c40e8, 0xc00133c020}, {0x0, 0x0}, {0x32a62d8, 0xc0010e8810}, 0x0, ...}, ...)
    github.com/cosmos/[email protected]/client/broadcast.go:26 +0x85
github.com/cosmos/cosmos-sdk/client.TxServiceBroadcast({_, _}, {{0x0, 0x0, 0x0}, {0x32c40e8, 0xc00133c020}, {0x0, 0x0}, {0x32a62d8, ...}, ...}, ...)
    github.com/cosmos/[email protected]/client/broadcast.go:148 +0x138
github.com/cosmos/cosmos-sdk/x/auth/tx.txServer.BroadcastTx(...)
    github.com/cosmos/[email protected]/x/auth/tx/service.go:153
github.com/cosmos/cosmos-sdk/types/tx._Service_BroadcastTx_Handler.func1({0x329f0f8, 0xc010e001b0}, {0x2a37300?, 0xc052828e40})
    github.com/cosmos/[email protected]/types/tx/service.pb.go:788 +0x78
github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).RegisterGRPCServer.func1({0x329f0f8, 0xc08fd6f320}, {0x2a37300, 0xc052828e40}, 0xc052828e60?, 0xc048e2ff38)
    github.com/cosmos/[email protected]/baseapp/grpcserver.go:66 +0x3a5
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1({0x329f0f8?, 0xc08fd6f320?}, {0x2a37300?, 0xc052828e40?})
    github.com/grpc-ecosystem/[email protected]/chain.go:25 +0x3a
github.com/grpc-ecosystem/go-grpc-middleware/recovery.UnaryServerInterceptor.func1({0x329f0f8?, 0xc08fd6f320?}, {0x2a37300?, 0xc052828e40?}, 0x289c740?, 0xc052828e60?)
    github.com/grpc-ecosystem/[email protected]/recovery/interceptors.go:33 +0xc4
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1({0x329f0f8?, 0xc08fd6f320?}, {0x2a37300?, 0xc052828e40?})
    github.com/grpc-ecosystem/[email protected]/chain.go:25 +0x3a
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1({0x329f0f8, 0xc08fd6f320}, {0x2a37300, 0xc052828e40}, 0xc0112bca01?, 0xc0112bca88?)
    github.com/grpc-ecosystem/[email protected]/chain.go:34 +0xbe
github.com/cosmos/cosmos-sdk/types/tx._Service_BroadcastTx_Handler({0x29b80e0?, 0xc00a454000}, {0x329f0f8, 0xc08fd6f320}, 0xc048d81860, 0xc08fd6f350)
    github.com/cosmos/[email protected]/types/tx/service.pb.go:790 +0x138
github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).RegisterGRPCServer.func2({0x29b80e0, 0xc00a454000}, {0x329f0f8, 0xc08fd6f320}, 0x2ab0d60?, 0xc046a3ea00?)
    github.com/cosmos/[email protected]/baseapp/grpcserver.go:80 +0xdc
google.golang.org/grpc.(*Server).processUnaryRPC(0xc006fda000, {0x32af6a0, 0xc084803680}, 0xc046a3ea00, 0xc00e189fb0, 0xc00a3ab5f0, 0x0)
    google.golang.org/[email protected]/server.go:1210 +0xcbe
google.golang.org/grpc.(*Server).handleStream(0xc006fda000, {0x32af6a0, 0xc084803680}, 0xc046a3ea00, 0x0)
    google.golang.org/[email protected]/server.go:1533 +0xa36
google.golang.org/grpc.(*Server).serveStreams.func1.2()
    google.golang.org/[email protected]/server.go:871 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
    google.golang.org/[email protected]/server.go:869 +0x28a
: panic
1

There are 1 answers

0
Rohit Raj On

I would suggest you use a Python package. Either mospy or osmosis_protobuf. They have everything you need and are simpler