injectivejs
TypeScript icon, indicating that this package has built-in type declarations

1.5.0 • Public • Published

injectivejs


Building the future of decentralized exchange

install

npm install injectivejs

Table of contents

Usage

Contracts

Registry

import { contracts } from 'injectivejs';
const { RegistryClient } = contracts.Registry;

const client = new RegistryClient(
    signingCosmWasmClient, // make sure to create your client (see docs below)
    senderAddress,
    contractAddress
);

Registry.activate

await client.activate({
    contractAddress
})

Registry.getActiveContracts

const activeContracts = await client.getActiveContracts();
const {
    address,
    gas_limit,
    gas_price,
    is_executable
} = activeContracts.contracts[0];

RPC Clients

import { injective } from 'injectivejs';

const { createRPCQueryClient } = injective.ClientFactory; 
const client = await createRPCQueryClient({ rpcEndpoint: RPC_ENDPOINT });

// now you can query the cosmos modules
const balance = await client.cosmos.bank.v1beta1
    .allBalances({ address: 'inj1addresshere' });

// you can also query the injective modules
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;

Auction Messages

const {
    bid
} = injective.auction.v1beta1.MessageComposer.withTypeUrl;

Exchange Messages

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 Messages

const {
    createInsuranceFund,
    requestRedemption,
    underwrite
} = injective.insurance.v1beta1.MessageComposer.withTypeUrl;

OCR Messages

const {
    acceptPayeeship,
    createFeed,
    fundFeedRewardPool,
    setPayees,
    transferPayeeship,
    transmit,
    updateFeed,
    withdrawFeedRewardPool
} = injective.ocr.v1beta1.MessageComposer.withTypeUrl;

Oracle Messages

const {
    relayBandRates,
    relayCoinbaseMessages,
    relayPriceFeedPrice,
    relayProviderPrices,
    requestBandIBCRates
} = injective.oracle.v1beta1.MessageComposer.withTypeUrl;

Peggy Messages

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 // OfflineSigner
});

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 = /* create your signer (see above)  */
const rpcEndpoint = 'https://rpc.cosmos.directory/injective'; // or another URL

const protoRegistry: ReadonlyArray<[string, GeneratedType]> = [
    ...cosmosProtoRegistry,
    ...cosmwasmProtoRegistry,
    ...ibcProtoRegistry,
    ...injectiveProtoRegistry
];

const aminoConverters = {
    ...cosmosAminoConverters,
    ...cosmwasmAminoConverters,
    ...ibcAminoConverters,
    ...injectiveAminoConverters
};

const registry = new Registry(protoRegistry);
const aminoTypes = new AminoTypes(aminoConverters);

const stargateClient = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, {
    registry,
    aminoTypes
});

Developing

When first cloning the repo:

yarn
yarn build

Codegen

Contract schemas live in ./contracts, and protos in ./proto. Look inside of scripts/codegen.js and configure the settings for bundling your SDK and contracts into injectivejs:

yarn codegen

Publishing

Build the types and then publish:

yarn build:ts
yarn publish

Credits

🛠 Built by Cosmology — if you like our tools, please consider delegating to our validator ⚛️

Code built with the help of these related projects:

Readme

Keywords

none

Package Sidebar

Install

npm i injectivejs

Weekly Downloads

95

Version

1.5.0

License

SEE LICENSE IN LICENSE

Unpacked Size

8.53 MB

Total Files

1000

Last publish

Collaborators

  • shanemoore
  • pyramation