Giter Site home page Giter Site logo

geoffmunn / feather.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from terra-money/feather.js

0.0 0.0 0.0 6.59 MB

馃捇 JavaScript SDK for Station, written in TypeScript

Home Page: https://featherjs.pages.dev

License: MIT License

JavaScript 0.23% TypeScript 99.77%

feather.js's Introduction

The JavaScript SDK for Terra and Feather chains


GitHub npm (scoped)

Explore the Docs 禄

ExamplesAPI ReferenceNPM PackageGitHub

Feather.js is a JavaScript SDK for writing applications that interact with the Terra blockchain from either Node.js, browser, or React Native environments and provides simple abstractions over core data structures, serialization, key management, and API request generation.

Features

  • Written in TypeScript, with type definitions
  • Versatile support for key management solutions
  • Works in Node.js, in the browser, and React Native
  • Exposes the Terra API through LCDClient
  • Parses responses into native JavaScript types

We highly suggest using Feather.js with TypeScript, or JavaScript in a code editor that has support for type declarations, so you can take advantage of the helpful type hints that are included with the package.

Installation

Grab the latest version off NPM:

npm install @terra-money/feather.js

Usage

Feather.js can be used in Node.js, as well as inside the browser. Please check the docs for notes on how to get up and running.

Getting blockchain data

import { LCDClient, Coin } from '@terra-money/feather.js';

// connect to testnet
const lcd = LCDClient.fromDefaultConfig('testnet');

// connect to mainnet
const lcd = LCDClient.fromDefaultConfig('mainnet');

// To use LocalTerra or a custom endpoint
const lcd = new LCDClient({
  localterra: {
    // key must be the chainID
    lcd: 'http://localhost:1317',
    chainID: 'localterra',
    gasAdjustment: 1.75,
    gasPrices: { uluna: 0.15 },
    prefix: 'terra', // bech32 prefix, used by the LCD to understand which is the right chain to query
  },
});

// get the current balance of `terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v`
// LCD understand automatically the chain to query using the bech32 prefix of the address
const balance = lcd.bank.balance(
  'terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v'
);
console.log(balance);

// get the total circulation supply
// LCD needs a chainID to understand the chain it should query
const total = lcd.bank.total('phoenix-1');
console.log(total);

Broadcasting transactions

First, get some testnet tokens for terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v, or use LocalTerra.

import { LCDClient, MsgSend, MnemonicKey } from '@terra-money/feather.js';

// create a key out of a mnemonic
const mk = new MnemonicKey({
  mnemonic:
    'notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius',
});

// connect to testnet
const lcd = LCDClient.fromDefaultConfig('testnet');

// a wallet can be created out of any key
// wallets abstract transaction building
const wallet = lcd.wallet(mk);

// create a simple message that moves coin balances
const send = new MsgSend(
  mk.accAddress('terra'), // .accAddress is now a function which require the prefix as parameter
  'terra17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp',
  { uluna: 1200000 }
);

wallet
  .createAndSignTx({
    msgs: [send],
    memo: 'test from feather.js!',
    chainID: 'pisco-1', // now here a chainID must be specified
  })
  .then(tx => lcd.tx.broadcast(tx, 'pisco-1')) // same here
  .then(result => {
    console.log(`TX hash: ${result.txhash}`);
  });

Tx with messages from custom modules

wallet
  // feather js detect that the tx contains a MsgAminoCustom and will use SIGN_MODE_AMINO_JSON instead of SIGN_MODE_DIRECT
  .createAndSignTx({
    msgs: [
      new MsgAminoCustom({
        type: 'osmosis/gamm/swap-exact-amount-in',
        value: {
          sender: 'osmo1...',
          routes: [
            {
              pool_id: '2',
              token_out_denom: 'uion',
            },
          ],
          token_in: {
            denom: 'uosmo',
            amount: '10000000',
          },
          token_out_min_amount: '8538',
        },
      }),
      // you can add more messages here if needed
    ],
    memo: 'test from feather.js!',
    chainID: 'osmosis-1',
  })
  .then(tx => lcd.tx.broadcast(tx, 'osmosis-1'))
  .then(result => {
    console.log(`TX hash: ${result.txhash}`);
  });

Feather.js in the browser

You can access all the objects of the @terra-money/feather.js from the global Feather object if you load Feather.js with a <script> tag.

Include the following in your browser:

<script
  crossorigin
  src="https://unpkg.com/@terra-money/feather.js/dist/bundle.js"
></script>

You can find a small JSFiddle example that refreshes current Oracle votes here.

Feather.js in React Native

In order to use Feather.js inside React Native, you need to add the node-libs-react-native package and react-native-get-random-values package to your React Native app's package.json.

yarn add node-libs-react-native react-native-get-random-values

You will need to register Node.js native modules in an entry point of your application, such as index.tsx:

import 'node-libs-react-native/globals';
import 'react-native-get-random-values';

Also, add resolvers to your metro.config.js

module.exports {
  // ...
  resolver: {
    // ...
    extraNodeModules: require('node-libs-react-native'),
  },
  // ...
}

License

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

漏 2022 Terraform Labs, PTE.


feather.js's People

Contributors

ouiliame avatar hanjukim avatar emidev98 avatar yun-yeo avatar alecande11 avatar vritra4 avatar freeelancer avatar octalmage avatar gregnuj avatar jared-tfl avatar erichgorski avatar rtviii avatar decryptdavid avatar radzionc avatar snyk-bot avatar llllllluc avatar dependabot[bot] avatar fragwuerdig avatar jkhaui avatar 0xslipk avatar evanorti avatar alpac-4 avatar kiruse avatar evilpeach avatar arian81 avatar trytocatcharg avatar romainlanz avatar 0xphilipp avatar matanhamilis avatar kmartin62 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.