16.14.0 • Published 8 months ago

osmojs v16.14.0

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
github
Last release
8 months ago

OsmoJS

OsmosJS makes it easy to compose and broadcast Osmosis and Cosmos messages, with all of the proto and amino encoding handled for you.


install

npm install osmojs

Table of contents

Usage

RPC Clients

import { osmosis } from 'osmojs';

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

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

// you can also query the osmosis pools
const response = await client.osmosis.gamm.v1beta1.pools();

** Every RPC endpoint is available! Simply use vscode or another tool to visually explore through autocomplete all of the RPC endpoints available on the RPCQueryClient!

Composing Messages

Import the osmosis object from osmojs.

In this case, we're show the messages available from the osmosis.gamm.v1beta1 module:

import { osmosis } from 'osmojs';

const {
    joinPool,
    exitPool,
    exitSwapExternAmountOut,
    exitSwapShareAmountIn,
    joinSwapExternAmountIn,
    joinSwapShareAmountOut,
    swapExactAmountIn,
    swapExactAmountOut
} = osmosis.gamm.v1beta1.MessageComposer.withTypeUrl;

To see a complete list of messages, see all the messages below.

Now you can construct messages. If you use vscode or another typescript-enabled IDE, you should also be able to use ctrl+space to see auto-completion of the fields required for the message.

import { coin } from '@cosmjs/amino';

const msg = swapExactAmountIn({
  sender,
  routes,
  tokenIn: coin(amount, denom),
  tokenOutMinAmount
});

To calculate the routes and tokenOutMinAmount, you can use @osmonauts/math:

import { getRoutesForTrade, calcAmountWithSlippage } from "@osmonauts/math";

const routes = getRoutesForTrade({
  trade: {
    sell: {
      denom: tokenIn.denom,
      amount: tokenInAmount,
    },
    buy: {
      denom: tokenOut.denom,
      amount: tokenOutAmount,
    },
  },
  pairs,
});

const tokenOutMinAmount = calcAmountWithSlippage(tokenOutAmount, slippage);

For more details, check out the swap-tokens example.

Lockup Messages

import { osmosis } from 'osmojs';
const {
    beginUnlocking,
    beginUnlockingAll,
    lockTokens
} = osmosis.lockup.MessageComposer.withTypeUrl;

Superfluid Messages

import { osmosis } from 'osmojs';
const {
    lockAndSuperfluidDelegate,
    superfluidDelegate,
    superfluidUnbondLock,
    superfluidUndelegate
} = osmosis.superfluid.MessageComposer.withTypeUrl;

Incentives Messages

import { osmosis } from 'osmojs';
const {
    addToGauge,
    createGauge
} = osmosis.incentives.MessageComposer.withTypeUrl;

Gamm Messages

import { osmosis } from 'osmojs';
const {
    joinPool,
    exitPool,
    exitSwapExternAmountOut,
    exitSwapShareAmountIn,
    joinSwapExternAmountIn,
    joinSwapShareAmountOut,
    swapExactAmountIn,
    swapExactAmountOut
} = osmosis.gamm.v1beta1.MessageComposer.withTypeUrl;

CosmWasm Messages

import { cosmwasm } from "osmojs";

const {
    clearAdmin,
    executeContract,
    instantiateContract,
    migrateContract,
    storeCode,
    updateAdmin
} = cosmwasm.wasm.v1.MessageComposer.withTypeUrl;

IBC Messages

import { ibc } from 'osmojs';

const {
    transfer
} = ibc.applications.transfer.v1.MessageComposer.withTypeUrl

Cosmos Messages

import { cosmos } from 'osmojs';

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;

Calculating Fees

Make sure to create a fee object in addition to your message.

For most messages, you can use the predefined fee objects.

import { FEES } from '@osmonauts/utils';

const fee = FEES.osmosis.swapExactAmountIn();

You can also specify low, medium, or high for fees:

const fee = FEES.osmosis.swapExactAmountIn('low');
const fee = FEES.osmosis.swapExactAmountIn('medium');
const fee = FEES.osmosis.swapExactAmountIn('high');

Or you can construct manually if you wish:

import { coins } from '@cosmjs/amino';

const fee = {
    amount: coins(0, 'uosmo'),
    gas: '250000'
}

if you are broadcasting multiple messages in a batch, you should simulate your tx and estimate the fee

import { Dec, IntPretty } from '@keplr-wallet/unit';

const gasEstimated = await stargateClient.simulate(address, msgs, memo);
const fee = {
  amount: coins(0, 'uosmo'),
  gas: new IntPretty(new Dec(gasEstimated).mul(new Dec(1.3)))
    .maxDecimals(0)
    .locale(false)
    .toString()
};

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 getSigningOsmosisClient to get your SigningStargateClient, with the Osmosis proto/amino messages full-loaded. No need to manually add amino types, just require and initialize the client:

import { getSigningOsmosisClient } from 'osmojs';

const client = await getSigningOsmosisClient({
  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 === 'osmosis');
  const signer = await getOfflineSigner({
    mnemonic,
    chain
  });

Broadcasting messages

Now that you have your client, you can broadcast messages:

const { send } = cosmos.bank.v1beta1.MessageComposer.withTypeUrl;

const msg = send({
    amount: [
    {
        denom: 'uosmo',
        amount: '1000'
    }
    ],
    toAddress: address,
    fromAddress: address
});

const fee: StdFee = {
    amount: [
    {
        denom: 'uosmo',
        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,
    osmosisAminoConverters,
    osmosisProtoRegistry
} from 'osmojs';

const signer: OfflineSigner = /* create your signer (see above)  */
const rpcEndpoint = 'https://rpc.cosmos.directory/osmosis'; // or another URL

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

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

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

And then get all submodules if necessary:

git submodule update --init

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 osmojs:

yarn codegen

Note: please get all sub-modules before generating code, since some proto files within default config are included in sub-modules.

Publishing

Build the types and then publish:

yarn build:ts
yarn publish

Related

Checkout these related projects:

  • @cosmology/telescope Your Frontend Companion for Building with TypeScript with Cosmos SDK Modules.
  • @cosmwasm/ts-codegen Convert your CosmWasm smart contracts into dev-friendly TypeScript classes.
  • chain-registry Everything from token symbols, logos, and IBC denominations for all assets you want to support in your application.
  • cosmos-kit Experience the convenience of connecting with a variety of web3 wallets through a single, streamlined interface.
  • create-cosmos-app Set up a modern Cosmos app by running one command.
  • interchain-ui The Interchain Design System, empowering developers with a flexible, easy-to-use UI kit.
  • starship Unified Testing and Development for the Interchain.

Credits

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

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.

16.14.0

8 months ago

16.13.0

8 months ago

16.11.0

8 months ago

16.12.1

8 months ago

16.12.0

8 months ago

16.10.0

10 months ago

16.9.2

10 months ago

16.9.0

11 months ago

16.8.2

11 months ago

16.8.3

11 months ago

16.8.1

11 months ago

16.8.0

11 months ago

16.7.0

12 months ago

16.6.0

12 months ago

15.3.0

2 years ago

16.1.0

1 year ago

16.5.1

1 year ago

16.5.0

1 year ago

15.4.0

2 years ago

16.2.0

1 year ago

16.2.1

1 year ago

15.5.0

1 year ago

16.3.0

1 year ago

16.0.0

1 year ago

16.4.0

1 year ago

15.2.0

2 years ago

15.2.1

2 years ago

15.0.2

2 years ago

15.0.3

2 years ago

15.0.1

2 years ago

15.0.4

2 years ago

15.0.5

2 years ago

15.1.0-rc.1

2 years ago

15.1.0-rc.0

2 years ago

14.0.0-rc.0

2 years ago

0.36.0

2 years ago

0.32.0

2 years ago

0.29.0

2 years ago

0.37.0

2 years ago

0.33.0

2 years ago

13.0.0

2 years ago

13.0.1

2 years ago

0.34.0

2 years ago

0.30.0

2 years ago

0.35.0

2 years ago

0.31.2

2 years ago

13.0.0-rc.0

2 years ago

0.31.1

2 years ago

0.31.0

2 years ago

0.28.0

2 years ago

0.20.1

2 years ago

0.20.0

2 years ago

0.17.0

2 years ago

0.25.0

2 years ago

0.21.0

2 years ago

0.18.1

2 years ago

0.18.2

2 years ago

0.18.3

2 years ago

0.14.0

2 years ago

0.18.0

2 years ago

0.26.0

2 years ago

0.22.0

2 years ago

0.19.0

2 years ago

0.11.0

2 years ago

0.11.1

2 years ago

0.11.2

2 years ago

0.11.3

2 years ago

0.15.0

2 years ago

0.15.1

2 years ago

0.15.2

2 years ago

0.15.3

2 years ago

0.27.0

2 years ago

0.23.0

2 years ago

0.12.0

2 years ago

0.12.1

2 years ago

0.12.2

2 years ago

0.16.0

2 years ago

0.16.1

2 years ago

0.16.2

2 years ago

0.24.0

2 years ago

0.20.2

2 years ago

0.4.42

3 years ago

0.4.43

3 years ago

0.4.40

3 years ago

0.4.41

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.48

3 years ago

0.4.49

3 years ago

0.4.46

3 years ago

0.4.47

3 years ago

0.4.44

3 years ago

0.4.45

3 years ago

0.4.31

3 years ago

0.4.32

3 years ago

0.4.30

3 years ago

0.4.39

3 years ago

0.4.37

3 years ago

0.4.38

3 years ago

0.4.35

3 years ago

0.4.36

3 years ago

0.4.33

3 years ago

0.4.34

3 years ago

0.9.0

2 years ago

0.7.2

2 years ago

0.5.4

2 years ago

0.7.1

2 years ago

0.5.3

2 years ago

0.9.2

2 years ago

0.7.4

2 years ago

0.5.6

2 years ago

0.9.1

2 years ago

0.7.3

2 years ago

0.5.5

2 years ago

0.5.0

2 years ago

0.7.0

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.4.20

3 years ago

0.4.21

3 years ago

0.5.8

2 years ago

0.9.3

2 years ago

0.7.5

2 years ago

0.5.7

2 years ago

0.5.9

2 years ago

0.4.28

3 years ago

0.4.29

3 years ago

0.4.26

3 years ago

0.4.27

3 years ago

0.4.24

3 years ago

0.4.25

3 years ago

0.4.22

3 years ago

0.4.23

3 years ago

0.4.19

3 years ago

0.10.2

2 years ago

0.10.3

2 years ago

0.10.4

2 years ago

0.10.5

2 years ago

0.10.6

2 years ago

0.4.53

3 years ago

0.4.54

2 years ago

0.4.10

3 years ago

0.4.51

3 years ago

0.4.52

3 years ago

0.4.50

3 years ago

0.10.0

2 years ago

0.4.17

3 years ago

0.4.18

3 years ago

0.4.15

3 years ago

0.4.16

3 years ago

0.4.57

2 years ago

0.4.13

3 years ago

0.4.58

2 years ago

0.4.14

3 years ago

0.4.55

2 years ago

0.4.11

3 years ago

0.4.56

2 years ago

0.4.12

3 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.6.2

2 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.3.0

3 years ago