Giter Site home page Giter Site logo

demondvn / cosmos-ts-sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nibiruchain/ts-sdk

0.0 0.0 0.0 2.44 MB

TypeScript-based client for interacting with the Nibiru blockchain

License: MIT License

Shell 0.14% JavaScript 0.44% TypeScript 99.42%

cosmos-ts-sdk's Introduction

The official TypeScript SDK for the Nibiru blockchain

The NibiJS (@nibiruchain/nibijs) package makes it possible to interact with Nibiru from a Node.js or browser environment. nibijs provides simple abstractions for core data structures, serialization, key management, API requests, and the submission of transactions.

The nibijs source code can be found in the "packages" directory. The types and classes generated from Nibiru's .proto files are inside a separate npm package called @nibiruchain/protojs.

Table of Contents

To learn more about Nibiru, see docs.nibiru.fi


Installation

@nibiruchain/nibijs is available on the npm registry.

npm install @nibiruchain/nibijs # or yarn add

Usage

The entrypoint for nibijs is the Sdk object, which is meant to mimic the root of a command line interface. It can be used for both queries and transactions.

Example: Creating a wallet

import { newRandomWallet, WalletHD } from "@nibiruchain/nibijs/dist/tx"
const wallet: WalletHD = await newRandomWallet()
const [{ address }] = await wallet.getAccounts()

// Save the mnemonic somewhere to re-use the account
console.log("mnemonic: ", wallet.mnemonic)
console.log("address: ", address)

Example: Querying

import { Testnet, newSdk } from "@nibiruchain/nibijs"
const sdk = newSdk(Testnet, myMnemonic)

const balances = await sdk.query.bank.allBalances(address)
console.log("balances: %o", balances)

const allPools = await sdk.query.vpool.allPools()
console.log("allPools: %o", allPools)

const blockHeight = 1
const block = sdk.tmClient.block(blockHeight)

Example: Sending funds

import { Testnet, newSdk, newCoins, Coin } from "@nibiruchain/nibijs"
const sdk = newSdk(Testnet, myMnemonic)
const tokens: Coin[] = newCoins(5, "unibi")
const toAddr: string = "..." // bech32 address of the receiving party
let txResp = sdk.tx.sendTokens(toAddr, tokens)

Example: Transaction with arbitrary messages

import { Testnet, newSdk, newCoins, Coin, DeliverTxResponse } from "@nibiruchain/nibijs"
import { Msg } from "@nibiruchain/nibijs/msg"

const sdk = newSdk(Testnet, myMnemonic)
let msgs: TxMessage[] = [
  Msg.perp.openPosition({
    tokenPair: pair,
    baseAssetAmountLimit: 0,
    leverage: 1,
    quoteAssetAmount: 10,
    sender: fromAddr,
    goLong: true,
  }),
  Msg.perp.addMargin({
    sender: fromAddr,
    tokenPair: pair,
    margin: newCoin("20", "unusd"),
  }),
]
let txResp: DeliverTxResponse = await sdk.tx.signAndBroadcast(...msgs)

Codebase structure

Directories of @nibiruchain/nibijs Purpose/Utility
common home to several commonly needed types, constants and configurations such as Network.
msg Implements functions for creating messages (Msgs). These are objects that trigger state-transitions and get wrapped into transactions.
query For querying state via the consensus engine of a full-node and the application blockchain interface (ABCI).
tx For signing and to submitting transactions given a set of Msg objects.
wallet A simple wrapper around the Keplr wallet. This module will grow as support is added for other wallets (like MetaMask).

@nibiruchain/protojs provides types generated from the protocol buffers of the Cosmos-SDK, Tendermint Core, and Nibiru Chain. For most use cases, it won't be necessary to interact with this layer.


Development Quick Start

  1. First install yarn.

    npm install -g yarn
  2. Then, install package dependencies. At the root of the repository, run

    yarn 
  3. Lastly, compile the code in each package.

    yarn build

See HACKING.md for the full development guide. It includes instructions on:

  1. Running tests
  2. Generating code for the @nibiruchain/protojs package
  3. Generating documentation in HTML or Markdown from the comments of @nibiruchain/nibijs

๐Ÿ”“ License

This software is licensed under the MIT license. See LICENSE for full disclosure.

ยฉ 2022 Nibi, Inc.

cosmos-ts-sdk's People

Contributors

unique-divine avatar k-yang avatar marcel-krsh avatar demondvn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.