question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I send an osmosis swap transaction?

See original GitHub issue

Hi,

I’m currently somehow lost in how to use the cosmjs library. I tried a lot of combinations, but I can’t sign and broadcast a osmosis swap transaction.

What I already tried:

  1. Using Stargate Client
		const wallet = await DirectSecp256k1HdWallet.fromMnemonic(this.mnemonic, { prefix: 'osmo' });

		const rpcOsmo = 'https://rpc-osmosis.keplr.app';

		const registry = new Registry([
			...defaultRegistryTypes,
			['/osmosis/gamm/swap-exact-amount-in', MsgSwapExactAmountIn], 
			['osmosis/gamm/swap-exact-amount-in', MsgSwapExactAmountIn], // tried both as typeUrl but both get an error
		]);

		const osmoClient = await SigningStargateClient.connectWithSigner(rpcOsmo, wallet, {
			registry: registry,
		});

msg.ts msg.proto

this.osmoClient.signAndBroadcast(
			this.osmoAccount.address,
			[
				{
					typeUrl: '/osmosis/gamm/swap-exact-amount-in',
					value: {
						sender: 'osmo...',
						routes: [
							{
								poolId: '1',
								tokenOutDenom: 'ATOM',
							},
						],
						tokenIn: {
							denom: 'OSMO',
							amount: '123',
						},
						tokenOutMinAmount: '123',
					},
				},
			],
			{
				gas: '1000000',
				amount: [
					{
						amount: '0',
						denom: 'uosmos',
					},
				],
			},
			''
		);

-> TypeError: message.poolId.isZero is not a function

this.osmoClient.signAndBroadcast(
			this.osmoAccount.address,
			[
				{
					typeUrl: '/osmosis/gamm/swap-exact-amount-in',
					value: MsgSwapExactAmountIn.fromPartial({
						sender: 'osmo...',
						routes: [
							{
								poolId: Long.fromNumber(1),
								tokenOutDenom: 'ATOM',
							},
						],
						tokenIn: {
							denom: 'OSMO',
							amount: '123',
						},
						tokenOutMinAmount: '123',
					}),
				},
			],
			{
				gas: '1000000',
				amount: [
					{
						amount: '0',
						denom: 'uosmos',
					},
				],
			},
			''
		);

-> Broadcasting transaction failed with code 2 (codespace: sdk). Log: unable to resolve type URL /osmosis/gamm/swap-exact-amount-in: tx parse error So it looks like the rpc endpoint of keplr app does not support this type. Looking at the keplr wallet it looks like they use the old client: SigningCosmosClient

  1. Second try with the old client

		const walletCosmos = await Secp256k1HdWallet.fromMnemonic(this.mnemonic, undefined, 'osmo');
		const accountsOsmoCosmos = await walletCosmos.getAccounts();
		const lcdOsmo = 'https://lcd-osmosis.keplr.app'; // with the rpc endpoint again errors
		const osmoCosmosClient = new SigningCosmosClient(lcdOsmo, this.osmoAccountCosmos.address, walletCosmos);

this.osmoCosmosClient.signAndBroadcast(
			[
				{
					type: 'osmosis/gamm/swap-exact-amount-in',
					value: {
						sender: 'osmo...',
						routes: [
							{
								poolId: Long.fromNumber(1),
								tokenOutDenom: 'ATOM',
							},
						],
						tokenIn: {
							denom: 'OSMO',
							amount: '123',
						},
						tokenOutMinAmount: '123',
					},
				},
			],
			{
				gas: '1000000',
				amount: [
					{
						amount: '0',
						denom: 'uosmos',
					},
				],
			},
			''
		);

->

auth.js:11 Uncaught (in promise) Error: Unexpected response data format
    at Object.account (auth.js:11)
    at async SigningCosmosClient.getAccount (cosmosclient.js:100)
    at async SigningCosmosClient.getSequence (cosmosclient.js:90)
    at async SigningCosmosClient.sign (signingcosmosclient.js:67)
    at async SigningCosmosClient.signAndBroadcast (signingcosmosclient.js:59)
{
   "height":"1817054",
   "result":{
      "type":"cosmos-sdk/BaseAccount",
      "value":{
         "address":"osmo...",
         "public_key":{
            "type":"tendermint/PubKeySecp256k1",
            "value":"..."
         },
         "account_number":"...",
         "sequence":"..."
      }
   }
}

As far as I understand is BaseAccount type not supported by the SigningCosmosClient. Does anyone has a running example for custom types?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
pyramationcommented, Apr 18, 2022

https://github.com/cosmology-finance/cosmology/tree/master/packages/core#swapexactamountin

I’ve documented it here. This uses cosmjs but does a lot to manage the registry, etc.

2reactions
puneet2019commented, Dec 16, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the Osmosis DEX to swap IBC-enabled tokens - Medium
Osmosis is an Automated Market Maker within the Cosmos ecosystem, ... Step 7) Click on Swap and Approve the transaction from Keplr.
Read more >
Osmosis DEX User Guide: How to Trade, Stake, & Farm ...
00:00 Intro01:28 About Keplr Wallet 01:52 Install Cosmos (ATOM) App on your Ledger Device02:50 Create Ledger-based Keplr Wallet Using Ledger ...
Read more >
Getting Started | Osmosis Docs
Swapping Tokens​​​ Trading tokens is as easy as clicking on the Trade link and then selecting the pair you would like to trade....
Read more >
Trade in Osmosis - LikeCoin - Decentralize Publishing
Go to Assets and click "Withdraw >" then IBC Withdraw your funds to Keplr for sending them to crypto exchange or another wallet....
Read more >
Beginner's Guide to OSMO Crypto and the Osmosis DEX
Then, once you click swap you will be directed to the Keplr wallet where you'll have to accept the transaction fee and sign...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found