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
'100': 'xDaiChain',
11
'137': 'Polygon',
12
'250': 'Fantom',
13
'1284': 'MoonBeam',
14
'80001': 'Mumbai',
15
Ethereum: 1,
16
Ropsten: 3,
17
BinanceSmartChain: 56,
18
xDaiChain: 100,
19
Polygon: 137,
20
Fantom: 250,
21
MoonBeam: 1284,
22
Mumbai: 80001
23
}
24
********************************************/
25
Copied!

Signer or Provider

The SDK automatically creates a default read-only provider for you. Therefore, you do not need to specify a provider for an operation that does not write to the blockchain.

Getting Cover Info by Passing a Provider

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!
In this case, you're only reading from the blockchain. The above code can be simplified to this:

Getting Cover Info without Any Provider

1
import { ChainId, cover } from '@neptunemutual/sdk'
2
3
const readCoverInfo = async () => {
4
const key = '0x70726f746f3a636f6e7472616374733a636f7665723a6366633a303100000001'
5
6
const coverInfo = await cover.getCoverInfo(ChainId.Mumbai, key)
7
console.log(coverInfo)
8
}
9
10
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!
Last modified 3mo ago