DEV Community

Gucci.koin 🔮
Gucci.koin 🔮

Posted on • Edited on

Connect to Koinos Blockchain in 10 Minutes

Koinos is made to be accessible to developers and users. With just a few lines of code, we can interact with the blockchain.

This tutorial has been expanded at Learn Koinos Github repo.

Koinos Community Telegram

Koinos Wiki

Getting Started

Learn to Use Terminal

Install Node.js

Clone this repo

git clone https://github.com/isaacdozier/learn-koinos.git
cd learn-koinos
Enter fullscreen mode Exit fullscreen mode

How to Clone a Repository

Install Dependencies

npm install @koindx/v2-sdk
Enter fullscreen mode Exit fullscreen mode

The Basics of Dependencies for Node.js & NPM

Koindx V2-SDK Examples

Run Examples

Example_4.js : returns Koin & USDT Pool Reserves

const { ChainId, Fetcher, KOIN, Token, Percent} = require("@koindx/v2-sdk");
const BigNumber = require('bignumber.js');

async function go(){
    try{
        // DECLARE ASSETS
        // KOIN can be declared directly with Koindx sdk
        const koin = new KOIN(ChainId.MAINNET);

        // USDT is NOT a native asset and is declared using the Token class
        // This same operation would be used for other alt-token in the ecosystem
        const usdt_contract_address = '19WrWze3XAoMa3Mwqys4rJMP6emZX2wfpH'
        const usdt = new Token(ChainId.MAINNET, usdt_contract_address);

        // This retrieves the Koin/USDT pool info by utilizing the Koindx SDK
        const PAIR = await Fetcher.fetchPairData(ChainId.MAINNET, koin, usdt);

        // Lets declare our availbile reserves to get an estimated ratio
        // This can be used to get an estimated exchange rate for any given pool
        // We are using Percent to parse a BigNumber.js object
        const koin_reserves = new Percent(PAIR.reserve_0)
        const usdt_reserves = new Percent(PAIR.reserve_1)

        console.log('Koin Reserves: ', koin_reserves.numerator)
        console.log('USDT Reserves: ', usdt_reserves.numerator / usdt_reserves.denominator)

    } catch (error) {
        console.error(error);
    }
}

go()
Enter fullscreen mode Exit fullscreen mode

Terminal

node example_4.js
Enter fullscreen mode Exit fullscreen mode

Output

Koin Reserves:  BigNumber { s: 1, e: 12, c: [ 8752538877288 ] }
USDT Reserves:  877155023.1824
Enter fullscreen mode Exit fullscreen mode

BigNumber.js is an arithmitic standard used for working with a wide range of numbers, strings and object types representing a numeric value.

"The library exports a single constructor function, BigNumber, which accepts a value of type Number, String or BigNumber"

We are only reading these values as they are returned to us, but if you want to create them in your app you need to include the dependency below.

const BigNumber = require('bignumber.js');
Enter fullscreen mode Exit fullscreen mode

This makes working with numbers easier for developers.

Learn how to work with BigNumber.js

Arithmic functions with numbers and strings

console.log(koin_reserves * 1)
Enter fullscreen mode Exit fullscreen mode
Output: 8810421275766
Enter fullscreen mode Exit fullscreen mode
console.log(koin_reserves * '1')
Enter fullscreen mode Exit fullscreen mode
Output: 8810421275766
Enter fullscreen mode Exit fullscreen mode

Simplify operations with Percent

const koin_reserves = new Percent(PAIR.reserve_0)

console.log('Koin Reserves: ', koin_reserves.numerator / koin_reserves.denominator)
Enter fullscreen mode Exit fullscreen mode
Koin Reserves:  875956851.2107
USDT Reserves:  Percent {
  numerator: BigNumber { s: 1, e: 12, c: [ 8764493211385 ] },
  denominator: BigNumber { s: 1, e: 4, c: [ 10000 ] }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)