Usage

Multiple Blockchain Support#

Since the SDK supports multiple blockchains, you may want to quickly check the list of supported chain ids before you actually implement anything.

import { ChainId } from '@neptunemutual/sdk'

console.info('Supported Chains %s', ChainId)

/********************************************
Supported Chains {
  '1': 'Ethereum',
  '43113': 'Fuji',
  Ethereum: 1,
  Fuji: 43113
}
********************************************/

Signer or Provider#

Before you interact with the SDK functions, you have to create a provider. The example below shows how to create a provider using a private key. In the next chapter, we will explain creating a provider. For now:

import dotenv from 'dotenv'
import { ethers } from 'ethers'
import { ChainId, cover } from '@neptunemutual/sdk'

dotenv.config()

const myProvider = () => {
  const fakePrivateKey = '0586782a6b30a2526f960bfde45db0470c51919c0ac2ae9ad5ad39b847955109'
  const provider = new ethers.providers.JsonRpcProvider('https://matic-mumbai.chainstacklabs.com')
  return new ethers.Wallet(fakePrivateKey, provider).provider
}

const readCoverInfo = async () => {
  const coverKey = '0x616e696d617465642d6272616e64730000000000000000000000000000000000'
  const provider = myProvider()

  const coverInfo = await cover.getCoverInfo(ChainId.Mumbai, coverKey, provider)
  console.log(coverInfo)
}

readCoverInfo()

Add BigNumber Helper to Your Project#

Create a new file bn.js in your project root. Also install the library bignumber.js.

import BigNumber from 'bignumber.js'

BigNumber.config({ EXPONENTIAL_AT: 99 })

const MULTIPLIER = 10_000

const ether = (x) => new BigNumber((parseFloat(x.toString()) * 10 ** 18).toString()).toString()
const percentage = (x) => new BigNumber((x * MULTIPLIER).toString()).dividedBy(100).toString()
const weiToEther = (x) => new BigNumber(x.toString()).dividedBy(new BigNumber(10).pow(18)).toString()
const formatPercent = (x) => Number(x).toLocaleString(undefined, { style: 'percent', minimumFractionDigits: 2 })
const formatCurrency = (x, symbol) => Number(x).toLocaleString(undefined, { currency: 'USD', style: 'currency', minimumFractionDigits: 2 })
const formatToken = (x, symbol) => Number(x).toLocaleString(undefined, { minimumFractionDigits: 2 }) + (` ${symbol}` || '')
const weiAsPercent = (x) => formatPercent(weiToEther(x))
const weiAsDollars = (x) => formatCurrency(weiToEther(x))
const weiAsToken = (x, symbol) => formatToken(weiToEther(x), symbol)
const weiAsNpm = (x) => formatToken(weiToEther(x), 'NPM')
const toDate = (x) => new Date(parseInt(x.toString()) * 1000)

export {
  ether,
  percentage,
  weiToEther,
  formatPercent,
  weiAsPercent,
  weiAsDollars,
  weiAsToken,
  weiAsNpm,
  toDate
}