Javascript Reference
We have created a sample project that has many of the most common functions needed while interacting with Sei. Cloning and running our js-examples repo is a great place to start for anyone new to Sei.
Please see @sei-js/core and @sei-js/react for helpful functions and components to get started working with Sei.
import { connect, suggestChain } from '@sei-js/core'
// Add chain config (only required if Sei is not natively supported)
await suggestChain('keplr', { chainId, chainName, rpcUrl, restUrl });
// Connect to the wallet
const { connectedWallet, offlineSigner } = await connect('keplr', 'atlantic-1');
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
// Generate a 12-word mnemonic
const wallet = await DirectSecp256k1HdWallet.generate(12, {
prefix: "sei",
});
const [firstAccount] = await wallet.getAccounts();
Using the
getQueryClient
function you will be returned a query client with helper queries for all the available Sei chain modules.import { getQueryClient } from '@sei-js/core';
const queryClient = await getQueryClient(REST_URL);
// Getting the market summary from the Sei dex module
queryClient.seiprotocol.seichain.dex.getMarketSummary(params);
import { getQueryClient } from '@sei-js/core';
const queryClient = await getQueryClient(REST_URL);
// Getting user balances from the Cosmos bank module
queryClient.cosmos.bank.v1beta1.allBalances({ address });
import { getQueryClient } from "@sei-js/core";
const queryClient = await getQueryClient(REST_URL);
// Getting tx details from Cosmos tx module
queryClient.cosmos.tx.v1beta1.getTx({ hash });
The client returned from
getSigningClient
can be used to create, sign, and broadcast transactions to a given rpc url.import { getSigningClient } from '@sei-js/core';
const signingClient = await getSigningClient(RPC_URL, signer);
import { getSigningClient } from '@sei-js/core';
import { calculateFee } from '@cosmjs/stargate';
const signingClient = await getSigningClient(RPC_URL, signer);
const fee = calculateFee(100000, "0.1usei");
const amount = { amount: SEND_AMOUNT, denom: TOKEN_DENOM };
const sendResponse = await signingClient.sendTokens(SENDER_ADDRESS, DESTINATION_ADDRESS, [amount], fee);
import { getSigningClient } from '@sei-js/core';
import { calculateFee } from '@cosmjs/stargate';
const signingClient = await getSigningClient(RPC_URL, signer);
const fee = calculateFee(150000, "0.1usei");
const amount = { amount: SEND_AMOUNT, denom: TOKEN_DENOM };
const ibcResponse = await signingClient.sendIbcTokens(SENDER_ADDRESS, DESTINATION_ADDRESSS, amount, 'transfer', CHANNEL_ID, undefined, undefined, fee)
IBC transfers require relayers to be set up and require you to have the target channel id before transfer.
import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing";
import { calculateFee, defaultRegistryTypes } from "@cosmjs/stargate";
import { getSigningClient } from '@sei-js/core';
import { seiprotocol, seiprotocolProtoRegistry } from "@sei-js/proto";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(MNEMONIC);
const [firstAccount] = await wallet.getAccounts();
// Set up Sei proto registry
const registry = new Registry([
...defaultRegistryTypes,
...seiprotocolProtoRegistry,
]);
// Create a client
const signingClient = await getSigningClient(RPC_URL, signer, { registry });
// Create message to place an order
const { placeOrders } = seiprotocol.seichain.dex.MessageComposer.withTypeUrl;
const msg = placeOrders({ contractAddr, creator, funds, orders });
const fee = calculateFee(150000, "0.1usei");
// Sign and broadcast the message
const response = signingClient.signAndBroadcast(firstAccount.address, [msg], fee);
Use
getCosmWasmClient
for any smart contract query msg's.import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { getCosmWasmClient } from "@sei-js/core";
// Create a CosmWasmClient
const client = await getCosmWasmClient(RPC_URL);
// Create the query msg json
const queryMsg = {
tokens: {
owner: address,
},
};
// Query a smart contract state
const queryResponse = client.queryContractSmart(CONTRACT_ADDR, queryMsg);
Use
getSigningCosmWasmClient
for any smart contract execute msg's.import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { calculateFee } from "@cosmjs/stargate";
import { getSigningCosmWasmClient } from "@sei-js/core";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(MNEMONIC);
const [firstAccount] = await wallet.getAccounts();
// Create a CosmWasmClient
const client = await getSigningCosmWasmClient(RPC_URL, wallet);
// Execute a message on a smart contract
const fee = calculateFee(150000, "0.1usei");
const msg = { mint: {} };
const result = await client.execute(SENDER_ADDRESS, CONTRACT_ADDRESS, msg, fee);
Last modified 1mo ago