Git Product home page Git Product logo

chains-lib's Introduction

Chainslib

This TypeScript library is designed to provide a unified interface for working with multiple blockchain networks, allowing developers to interact with various blockchain chains seamlessly. It supports a wide range of features and blockchain networks, making it a versatile tool for blockchain development.

Features

This library offers the following key features:

Hardware Wallet Support: It provides integration with Ledger and Trezor hardware wallets for enhanced security in blockchain transactions.

Wallet Types: You can work with both seed phrase and private key wallets for managing your blockchain assets.

Standardized RPC Interface: The library offers a consistent RPC interface for fetching blockchain information and broadcasting transactions across different supported chains.

Supported Chains

This library currently supports the following blockchain networks: // example for fallback data source // example with custom provider

Chain Provider Datasources Signers Custom chain
Bitcoin Bitcoin Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor No
Ethereum EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
BNB Smart Chain EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Polygon EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Avalanche EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Fantom EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Arbitrum EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Aurora EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Canto EVM EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Optimism EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Klaytn EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Cronos EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Binance Binance Indexer SeedPhrase, PrivateKey, Ledger, Trezor No
BitcoinCash BitcoinCash Indexer SeedPhrase, PrivateKey, Ledger, Trezor No
Cosmos Hub Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Osmosis Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Axelar Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Juno Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Crescent Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Kava Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Stargaze Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Akash Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Cronos Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Kujira Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Stride Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Mars Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Terra Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Dogecoin Dogecoin Indexer SeedPhrase, PrivateKey, Ledger, Trezor No
Litecoin Litecoin Indexer SeedPhrase, PrivateKey, Ledger, Trezor No
Solana Solana Indexer, Chain SeedPhrase, PrivateKey, Ledger No
ThorChain ThorChain Indexer, Chain SeedPhrase, Ledger No
MayaChain ThorChain Indexer, Chain SeedPhrase, Ledger No
Tron Tron Indexer, Chain SeedPhrase, PrivateKey, Ledger No

Installation

To use this library in your TypeScript project, you can install it via npm or yarn:

npm install @xdefi-tech/chains
# or
yarn add @xdefi-tech/chains

Manifest

Each provider may have different manifests, but they share common fields. For more details, please refer to the README of the respective provider.

  • name: The name of the blockchain network. Uses only for display name to user
  • description: A brief description or additional information about the blockchain network.
  • rpcURL: The URL endpoint for the Remote Procedure Call (RPC) interface of the blockchain network.
  • chainSymbol: The symbol representing the blockchain network.
  • blockExplorerURL: The URL of a block explorer service specific to the blockchain. Block explorers allow users to view details about blocks, transactions, addresses, and other blockchain-related data.
  • chainId: The unique identifier of the blockchain network.
  • chain: The name of the blockchain network. If you are using IndexerDataSource, it must be obtained from the registry.
  • decimals: The number of decimal places used by the native currency of the blockchain network.
  • feeGasStep: An object containing gas step values for different fee levels (high, medium, low) used in transactions.

Usage

Here's a basic example of how to use this library in your TypeScript application:

1. Import Required Modules

import { BitcoinProvider } from './chain.provider';
import LedgerSigner from './ledger.signer';
import { MsgBody, Msg } from '../msg';

2. Initialize Bitcoin Provider

Initialize the Bitcoin provider with the necessary configurations:

const provider = new BitcoinProvider(new IndexerDataSource(BITCOIN_MANIFEST));

3. Create Transaction Input

Define the transaction input data, including the sender (from), recipient (to), and the amount to send.

const txInput: MsgBody = {
  from: 'FROM ADDRESS',
  to: 'TO ADDRESS',
  amount: 0.000001,
};

4. Create a Transaction Message

Create a transaction message using the provider:

const message: Msg = provider.createMsg(txInput);

5. Sign the Transaction with Ledger

Use the LedgerSigner to sign the transaction. Provide the message and the derivation path:

const transport = await Transport.create();
const signer = new LedgerSigner(transport);
const derivationPath = "m/84'/0'/0'/0/0";

await signer.sign(message, derivationPath);
// finally close
transport.close();

6. Broadcast the Transaction

Now that the transaction is signed, you can broadcast it to the Bitcoin network using the BitcoinProvider. This step assumes that the transaction is already signed within the message.

await provider.broadcast([message]);

Fallback datasource

  • If an error occurs while fetching data from the primary data source, the FallbackDataSource will automatically switch to one of the alternative data sources configured in the constructor.
  • The number of retry attempts and the order in which the alternative data sources are used can be customized based on the requirements and preferences of the application.
import { FallbackDataSource } from '@xdefi-tech/chains-core';
import { EvmProvider, EVM_MANIFESTS } from '@xdefi-tech/chains-evm';

const provider = new EvmProvider(
  new FallbackDataSource(
    EVM_MANIFEST.ethereum,
    {
      attempts: 5,
    },
    new EvmProvider.dataSourceList.IndexerDataSource(EVM_MANIFEST.ethereum),
    new EvmProvider.dataSourceList.ChainDataSource(EVM_MANIFEST.ethereum)
  )
);

// The same as any other provider
const response = await provider.getBalance(
  '0x1234567890123456789012345678901234567890'
);
const data = await response.getData();

Retrieving a Transaction

1. Get Transaction Hash

If you have a transaction hash, you can retrieve the transaction details. Use the getTransaction method of the BitcoinProvider:

const txHash = 'TX HAS';
const txData = await provider.getTransaction(txHash);

The txData object will contain transaction details, including the transaction hash.

Disclaimer

This library is provided as-is, and we make no warranties or guarantees regarding its functionality or security. Always exercise caution and proper security practices when working with blockchain assets.

Happy blockchain development! 🚀🔗

chains-lib's People

Contributors

danilxdefi avatar samsheff avatar xdefi-bot avatar evgeniixdefi avatar arturdevico avatar davidp94 avatar sotatek-tyler-nguyen4 avatar abhishekjha-devops avatar abhishekgotoadmin avatar amurmurmur avatar liammcaweeney 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.