How can I send an osmosis swap transaction?
See original GitHub issueHi,
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:
- 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,
});
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
- 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:
- Created 2 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@Phmager you can look at custom protocodecs - https://github.com/cosmos/cosmjs/blob/main/packages/stargate/CUSTOM_PROTOBUF_CODECS.md