Interact with Cosmos modules and execute complex transactions using CosmJS
Execute custom messages and interact with Cosmos modules using CosmJS with Para wallets.
import { useCosmosClient } from "./useCosmosClient";
import { coins } from "@cosmjs/stargate";
import { MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx";
function CustomTransaction() {
const { signingClient } = useCosmosClient("https://rpc.cosmos.network");
const executeCustomMessage = async () => {
if (!signingClient) return;
const accounts = await signingClient.signer.getAccounts();
const sender = accounts[0].address;
const msgSend = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: MsgSend.fromPartial({
fromAddress: sender,
toAddress: "cosmos1...", // Replace with recipient
amount: coins(1000000, "uatom"),
}),
};
const fee = {
amount: coins(5000, "uatom"),
gas: "200000",
};
try {
const result = await signingClient.signAndBroadcast(
sender,
[msgSend],
fee,
"Custom message via Para"
);
console.log("Transaction hash:", result.transactionHash);
console.log("Code:", result.code);
} catch (error) {
console.error("Transaction failed:", error);
}
};
return (
<button onClick={executeCustomMessage}>Execute Custom Message</button>
);
}