injectivejs
usage
npm install injectivejs
RPC Clients
import { injective } from 'injectivejs';
const { createRPCQueryClient } = injective.ClientFactory;
const client = await createRPCQueryClient({ rpcEndpoint: RPC_ENDPOINT });
const balance = await client.cosmos.bank.v1beta1
.allBalances({ address: 'inj1addresshere' });
const balances = await client.injective.exchange.v1beta1
.exchangeBalances()
Composing Messages
Import the injective
object from injectivejs
.
import { injective } from 'injectivejs';
const {
createSpotLimitOrder,
createSpotMarketOrder,
deposit
} = injective.exchange.v1beta1.MessageComposer.withTypeUrl;
Audi
const {
bid
} = injective.auction.v1beta1.MessageComposer.withTypeUrl;
Exchange
const {
adminUpdateBinaryOptionsMarket,
batchCancelBinaryOptionsOrders,
batchCancelDerivativeOrders,
batchCancelSpotOrders,
batchCreateDerivativeLimitOrders,
batchCreateSpotLimitOrders,
batchUpdateOrders,
cancelBinaryOptionsOrder,
cancelDerivativeOrder,
cancelSpotOrder,
createBinaryOptionsLimitOrder,
createBinaryOptionsMarketOrder,
createDerivativeLimitOrder,
createDerivativeMarketOrder,
createSpotLimitOrder,
createSpotMarketOrder,
deposit,
exec,
externalTransfer,
increasePositionMargin,
instantBinaryOptionsMarketLaunch,
instantExpiryFuturesMarketLaunch,
instantPerpetualMarketLaunch,
instantSpotMarketLaunch,
liquidatePosition,
rewardsOptOut,
subaccountTransfer,
withdraw
} = injective.exchange.v1beta1.MessageComposer.withTypeUrl;
Insurance
const {
createInsuranceFund,
requestRedemption,
underwrite
} = injective.insurance.v1beta1.MessageComposer.withTypeUrl;
OCR
const {
acceptPayeeship,
createFeed,
fundFeedRewardPool,
setPayees,
transferPayeeship,
transmit,
updateFeed,
withdrawFeedRewardPool
} = injective.ocr.v1beta1.MessageComposer.withTypeUrl;
Oracle
const {
relayBandRates,
relayCoinbaseMessages,
relayPriceFeedPrice,
relayProviderPrices,
requestBandIBCRates
} = injective.oracle.v1beta1.MessageComposer.withTypeUrl;
Peggy
const {
cancelSendToEth,
confirmBatch,
depositClaim,
eRC20DeployedClaim,
requestBatch,
sendToEth,
setOrchestratorAddresses,
submitBadSignatureEvidence,
valsetConfirm,
valsetUpdateClaim,
withdrawClaim
} = injective.peggy.v1.MessageComposer.withTypeUrl;
CosmWasm Messages
import { cosmwasm } from "injectivejs";
const {
clearAdmin,
executeContract,
instantiateContract,
migrateContract,
storeCode,
updateAdmin
} = cosmwasm.wasm.v1.MessageComposer.withTypeUrl;
IBC Messages
import { ibc } from 'injectivejs';
const {
transfer
} = ibc.applications.transfer.v1.MessageComposer.withTypeUrl
Cosmos Messages
import { cosmos } from 'injectivejs';
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;
Connecting with Wallets and Signing Messages
⚡️ For web interfaces, we recommend using cosmos-kit. Continue below to see how to manually construct signers and clients.
Here are the docs on creating signers in cosmos-kit that can be used with Keplr and other wallets.
Initializing the Stargate Client
Use getSigningInjectiveClient
to get your SigningStargateClient
, with the proto/amino messages full-loaded. No need to manually add amino types, just require and initialize the client:
import { getSigningInjectiveClient } from 'injectivejs';
const stargateClient = await getSigningInjectiveClient({
rpcEndpoint,
signer
});
Creating Signers
To broadcast messages, you can create signers with a variety of options:
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 === 'injective');
const signer = await getOfflineSigner({
mnemonic,
chain
});
Broadcasting messages
Now that you have your stargateClient
, you can broadcast messages:
const { send } = cosmos.bank.v1beta1.MessageComposer.withTypeUrl;
const msg = send({
amount: [
{
denom: 'inj',
amount: '1000'
}
],
toAddress: address,
fromAddress: address
});
const fee: StdFee = {
amount: [
{
denom: 'inj',
amount: '864'
}
],
gas: '86364'
};
const response = await stargateClient.signAndBroadcast(address, [msg], fee);
Advanced Usage
If you want to manually construct a stargate client
import { OfflineSigner, GeneratedType, Registry } from "@cosmjs/proto-signing";
import { AminoTypes, SigningStargateClient } from "@cosmjs/stargate";
import {
cosmosAminoConverters,
cosmosProtoRegistry,
cosmwasmAminoConverters,
cosmwasmProtoRegistry,
ibcProtoRegistry,
ibcAminoConverters,
injectiveAminoConverters,
injectiveProtoRegistry
} from 'injectivejs';
const signer: OfflineSigner =
const rpcEndpint = 'https://rpc.cosmos.directory/injective';
const protoRegistry: ReadonlyArray<[string, GeneratedType]> = [
...cosmosProtoRegistry,
...publicawesomeProtoRegistry,
...cosmwasmProtoRegistry,
...ibcProtoRegistry,
...injectiveProtoRegistry
];
const aminoConverters = {
...cosmosAminoConverters,
...publicawesomeAminoConverters,
...cosmwasmAminoConverters,
...ibcAminoConverters,
...injectiveAminoConverters
};
const registry = new Registry(protoRegistry);
const aminoTypes = new AminoTypes(aminoConverters);
const stargateClient = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, {
registry,
aminoTypes
});
Credits
🛠 Built by Cosmology — if you like our tools, please consider delegating to our validator ⚛️
Code built with the help of these related projects: