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.
1
import { ChainId } from '@neptunemutual/sdk'
2
3
console.info('Supported Chains %s', ChainId)
4
5
/********************************************
6
Supported Chains {
7
'1': 'Ethereum',
8
'3': 'Ropsten',
9
'56': 'BinanceSmartChain',
10
'97': 'BscTestnet',
11
'137': 'Polygon',
12
'80001': 'Mumbai',
13
Ethereum: 1,
14
Ropsten: 3,
15
BinanceSmartChain: 56,
16
BscTestnet: 97,
17
Polygon: 137,
18
Mumbai: 80001
19
}
20
********************************************/
21
Copied!

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:
1
import { ethers } from 'ethers'
2
import { ChainId, cover } from '@neptunemutual/sdk'
3
4
const myProvider = () => {
5
const fakePrivateKey = '0586782a6b30a2526f960bfde45db0470c51919c0ac2ae9ad5ad39b847955109'
6
const provider = new ethers.providers.JsonRpcProvider('https://rpc-mumbai.maticvigil.com')
7
return new ethers.Wallet(fakePrivateKey, provider)
8
}
9
10
const readCoverInfo = async () => {
11
const key = '0x70726f746f3a636f6e7472616374733a636f7665723a6366633a303100000001'
12
const provider = myProvider()
13
14
const coverInfo = await cover.getCoverInfo(ChainId.Mumbai, key, provider)
15
console.log(coverInfo)
16
}
17
18
readCoverInfo()
Copied!

Add BigNumber Helper to Your Project

Create a new file bn.js in your project root. Also install the library bignumber.js.
1
import BigNumber from 'bignumber.js'
2
3
BigNumber.config({ EXPONENTIAL_AT: 99 })
4
5
const ether = (x) => BigNumber((parseFloat(x.toString()) * 10 ** 18).toString()).toString()
6
const weiToEther = (x) => parseInt(x.toString()) / 10 ** 18
7
const formatPercent = (x) => Number(x).toLocaleString(undefined, { style: 'percent', minimumFractionDigits: 2 })
8
const formatCurrency = (x, symbol) => Number(x).toLocaleString(undefined, { currency: 'USD', style: 'currency', minimumFractionDigits: 2 })
9
const formatToken = (x, symbol) => Number(x).toLocaleString(undefined, { minimumFractionDigits: 2 }) + (` ${symbol}` || '')
10
const weiAsPercent = (x) => formatPercent(weiToEther(x))
11
const weiAsDollars = (x) => formatCurrency(weiToEther(x))
12
const weiAsToken = (x, symbol) => formatToken(weiToEther(x), symbol)
13
const weiAsNep = (x) => formatToken(weiToEther(x), 'NEP')
14
const toDate = (x) => new Date(parseInt(x.toString()) * 1000)
15
16
export {
17
ether,
18
weiToEther,
19
formatPercent,
20
weiAsPercent,
21
weiAsDollars,
22
weiAsToken,
23
weiAsNep,
24
toDate
25
}
Copied!