interchain
interchain makes it easy to compose and broadcast Cosmos messages, with all of the proto and amino encoding handled for you.
usage
npm install interchain
Cosmos Messages
import { cosmos } from 'interchain';
const {
fundCommunityPool,
setWithdrawAddress,
withdrawDelegatorReward,
withdrawValidatorCommission
} = cosmos.distribution.v1beta1.MessageComposer.fromPartial;
const {
multiSend,
send
} = cosmos.bank.v1beta1.MessageComposer.fromPartial;
const {
beginRedelegate,
createValidator,
delegate,
editValidator,
undelegate
} = cosmos.staking.v1beta1.MessageComposer.fromPartial;
const {
deposit,
submitProposal,
vote,
voteWeighted
} = cosmos.gov.v1beta1.MessageComposer.fromPartial;
CosmWasm Messages
import { cosmwasm } from "interchain";
const {
clearAdmin,
executeContract,
instantiateContract,
migrateContract,
storeCode,
updateAdmin
} = cosmwasm.wasm.v1.MessageComposer.withTypeUrl;
IBC Messages
import { ibc } from 'interchain';
const {
transfer
} = ibc.applications.transfer.v1.MessageComposer.withTypeUrl
Initializing the Stargate Client
Use getSigningCosmosClient
to get your SigningStargateClient
, with the Cosmos proto/amino messages full-loaded. No need to manually add amino types, just require and initialize the client:
import { getSigningCosmosClient } from 'interchain';
const client = await getSigningCosmosClient({
rpcEndpoint,
signer // OfflineSigner
});
Stargate Client Options
If you need the options in scenarios where you're using tools like cosmos-kit, you can get the options to pass into the provider:
import { getSigningCosmosClientOptions } from 'interchain';
Creating Signers
To broadcast messages, you can create signers with a variety of options:
- cosmos-kit (recommended)
- keplr
- cosmjs
Cosmos Kit
We recommend using cosmos-kit for creating signers that work with Keplr and other wallets.
Amino Signer
Likely you'll want to use the Amino, so unless you need proto, you should use this one:
import { getOfflineSignerAmino as getOfflineSigner } from 'cosmjs-utils';
Proto Signer
import { getOfflineSignerProto as getOfflineSigner } from 'cosmjs-utils';
WARNING: NOT RECOMMENDED TO USE PLAIN-TEXT MNEMONICS. Please take care of your security and use best practices such as AES encryption and/or methods from 12factor applications.
import { chains } from 'chain-registry';
const mnemonic =
'unfold client turtle either pilot stock floor glow toward bullet car science';
const chain = chains.find(({ chain_name }) => chain_name === 'cosmoshub');
const signer = await getOfflineSigner({
mnemonic,
chain
});
Broadcasting messages
Now that you have your client
, you can broadcast messages:
import { signAndBroadcast } from 'cosmjs-utils';
const res = await signAndBroadcast({
client, // SigningStargateClient
chainId: 'cosmoshub-4',
address,
msgs: [msg],
fee,
memo: ''
});
Credits
Built by Cosmology — if you like our tools, please consider delegating to our validator ⚛️
Code built with the help of these related projects:
- @osmonauts/telescope a "babel for the Cosmos", Telescope is a TypeScript Transpiler for Cosmos Protobufs.
- cosmos-kit A wallet connector for the Cosmos ⚛️
Disclaimer
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED “AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.