Execute your swap or call on Cosmos

Executing a route requires a signer to be set up. How to set up a cosmos signer.

Sending transaction from EVM works by using the same flow described here. Below is a guide for sending transactions from Cosmos chains.

[SDK <=v1.12.0] executeRoute

// package.json cosmjs dependencies required
"@cosmjs/stargate": "^0.31.0"

Type: #executeroute

const tx = await squid.executeRoute({ signer, signerAddress, route });

const cosmosTx = tx as DeliverTxResponse;

const txHash = cosmosTx.transactionHash;

[SDK >=v1.12.1] executeRoute

// package.json cosmjs dependencies required
"@cosmjs/stargate": "^0.31.3",
"@cosmjs/cosmwasm-stargate": "^0.31.3",

Type: #executeroute

import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";

// optional, but preffered to use instead of SigningStargateClient
const signingClient = await SigningCosmWasmClient.connectWithSigner(
    rpc,
    cosmosSigner
);

const txRaw = (await squid.executeRoute({ 
    signer: signingClient as any,
    signerAddress,
    route 
})) as TxRaw;

const cosmosTx = await signingClient.broadcastTx(
    TxRaw.encode(txRaw).finish()
); // DeliverTxResponse

const txHash = cosmosTx.transactionHash;

signerAddress

Signer Address is a cosmos address of the transaction sender. Example code snippet below shows how to retrieve it.

[SDK <=v1.12.0] Example code snippet

const mnemonic ="<your_mnemonic>";
const chainRpc = "<chain_rpc>";

const offlineSigner: OfflineDirectSigner = await DirectSecp256k1HdWallet.fromMnemonic(
    mnemonic, 
    {
      prefix: "<sending_cosmos_chain_prefix>",
    }
);

const signerAddress = (await offlineSigner.getAccounts())[0].address;

const signer = await SigningStargateClient.connectWithSigner(
    chainRpc,
    offlineSigner
);

const params = {
    fromChain: "osmo-test-5", // Osmosis Testnet
    fromToken: "ibc/DE6792CF9E521F6AD6E9A4BDF6225C9571A3B74ACC0A529F92BC5122A39D2E58", // nUSDC on Osmosis
    fromAmount: "1000000", // 1 nUSDC
    toChain: 43113, // Avalanche Fuji Tesntet
    toToken: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // AVAX on Avalanche
    fromAddress: "osmo1eaztm3pqrkw2xgt0lxppahtx5v5pndmjg6yfrh", // Cosmos Sender address
    toAddress: "0x747A6e3824FAB0d1266306C3b492fcB941C5dd93", // the recipient of the trade
    slippage: 1.00, // 1.00 = 1% max slippage across the entire route
    enableForecall: true, // instant execution service, defaults to true
    quoteOnly: false // optional, defaults to false
};

const { route } = await squid.getRoute(params)

const cosmosTx = (await squid.executeRoute({
    signer,
    signerAddress,
    route,
})) as DeliverTxResponse;

console.log(cosmosTx.transactionHash)

[SDK >=v1.12.1] Example code snippet

const mnemonic ="<your_mnemonic>";
const chainRpc = "<chain_rpc>";

const offlineSigner: OfflineDirectSigner = await DirectSecp256k1HdWallet.fromMnemonic(
    mnemonic, 
    {
      prefix: "<sending_cosmos_chain_prefix>",
    }
);

const signerAddress = (await offlineSigner.getAccounts())[0].address;

const signer = await SigningCosmWasmClient.connectWithSigner(
    chainRpc,
    offlineSigner
);

const params = {
    fromChain: "osmo-test-5", // Osmosis Testnet
    fromToken: "ibc/DE6792CF9E521F6AD6E9A4BDF6225C9571A3B74ACC0A529F92BC5122A39D2E58", // nUSDC on Osmosis
    fromAmount: "1000000", // 1 nUSDC
    toChain: 43113, // Avalanche Fuji Tesntet
    toToken: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // AVAX on Avalanche
    fromAddress: "osmo1eaztm3pqrkw2xgt0lxppahtx5v5pndmjg6yfrh", // Cosmos Sender address
    toAddress: "0x747A6e3824FAB0d1266306C3b492fcB941C5dd93", // the recipient of the trade
    slippage: 1.00, // 1.00 = 1% max slippage across the entire route
    enableForecall: true, // instant execution service, defaults to true
    quoteOnly: false // optional, defaults to false
};

const { route } = await squid.getRoute(params)

const txRaw = (await squid.executeRoute({ signer, signerAddress, route })) as TxRaw;

const cosmosTx = await signingClient.broadcastTx(
    TxRaw.encode(txRaw).finish()
); // DeliverTxResponse

console.log(cosmosTx.transactionHash);

Last updated