1.0.19 • Published 2 years ago

nubian-coven-sdk v1.0.19

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Nubian Coven SDK

The official Nubain Coven Software Development Kit (SDK) for JavaScript, available for browsers and Node.js backends.

Installation

To get started, install the nubian coven SDK package from npm:

npm install nubian-coven-sdk

Usage

To enable web3 calls via SDK, instantiate web3 library

// in browser
if (window.ethereum) {
  window.web3 = new Web3(window.ethereum)
} else if (window.web3) {
  window.web3 = new Web3(window.web3.currentProvider)
} else {
  window.web3 = new Web3(customProvider)
}
// in nodejs
const Web3 = require('web3')
const NUB = require('nubian-coven-sdk')
const web3 = new Web3(new Web3.providers.HttpProvider(BSC_RPC_URL))

Now instantiate NUB with web3 instance.

// in browser
const nub = new NUB(web3)

// in nodejs
const nub = new NUB({
  web3: web3,
  mode: 'node',
  privateKey: PRIVATE_KEY,
})

The Nubian SDK provides methods to swap and save tokens.

Swap

nub.pancakeswap.swap({ 
  amountA, 
  amountB, 
  tokenA, 
  tokenB,
  path
  [, slippage 
    [, receiver 
      [, ...transactionConfig ]]]
}) => Promise<TransactionReceipt>

This function swaps a token for another. It is a promise that resolves to a transaction receipt if it is successful. It throws an error if it is unsuccessful.

ParameterTypeDescription
tokenAstringAddress of token to swap. Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for BNB.
tokenBstringAddress of token to swap to. Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for BNB.
amountAstring/numberAmount of tokenA to be swapped.
amountBstring/numberAmount of tokenB expected to recieve from swap. You can use the swap path util method to deduce it.
pathstring[]The tokens you want tokenA to swapped to before being swapped to tokenB.
slippagenumberThe percentage amount amountB can reduce by. E.g pass2 if you do not want amountB from swap to go less than 2%. (optional).
recieverstringThe address to receive the swap output. It defaults to the address from the web3 instance in browser mode or the address of the private key in node environment. (optional)
transactionConfigobjectIt is an object that specifies blockchain transaction properties. Check common terms for an exhaustive description.

Swap Path/Token Price

When swapping using Pancakeswap each token has a price and a swap path. The swap path refers to the tokens the token you want to swap will be exchanged with before being converted to the destination token. This function returns the most efficient path for the swap and the amount of tokens you get or need to supply.

nub.pancakeswap.getRoute(
  tokenIn, 
  tokenOut, 
  amount,
  direction, 
  fresh
) => Promise<[string, string[]]>
ParameterTypeDescription
tokenInstringAddress of token to swap. Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for BNB.
tokenOutstringAddress of token to swap to. Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for BNB.
amountstringAmount of tokenIn to swap or the amount of tokenOut you want. It should be formatted with the decimal points.
directionIN\|OUTIt is used to indicate which token owns amount. "IN" indicates amount is the number of tokens to swap while "OUT" indicates amount is the number of tokens you want from swap.
freshbooleanWhen you first call getRoute on a pancakeswap instance, the details used to calculate the path are saved. fresh indicates if you want new details fetched from the chain or old details used.

This function returns a promise that resolves to an object. | Object Property | Type |Description | | ----------- | ---------|------------------------------------------------------- | | amount | string |The amount of tokens you get from swap or amount of tokens needed for swap. Depends on the value of direction. Decimal points are applied. | | path | string[] | An array of the path/route for possible token swap |

Save

nub.venus.deposit({
  amount,
  address
  [, receiver
    [, ...transactionConfig ]]
}) => Promise<TransactionReceipt>

This function saves token deposits in Venus protocol. When you save, Venus gives you vTokens. These are tokens that serve as proof of deposit. To get back your tokens saved, you must provide these vTokens. Venus has a set of tokens it allows you to save.

ParameterTypeDescription
addressstringAddress of token to save. Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for BNB.
amountstring/numberAmount of token to save.
recieverstringThe address to receive the vtokens. It defaults to the address from the web3 instance in browser mode or the address of the private key in node environment. (optional)
transactionConfigobjectIt is an object that specifies blockchain transaction properties. Check common terms for an exhaustive description.

Withdraw Savings

nub.venus.withdraw({
  vTokenAddress
  [, vTokenAmount 
    [, tokenAmount
      [, receiver
        [, ...transactionConfig ]]]]
}) => Promise<TransactionReceipt>

This function allows you to withdraw tokens saved in Venus protocol. You will need to have the corresponding vToken of the token you want to withdraw.

ParameterTypeDescription
vTokenAddressstringvToken address of the token you want to withdraw.
vTokenAmountstring/numberAmount of vTokens you want to withdraw. You must provide this or the tokenAmount. (optional)
tokenAmountstring/numberAmount of vTokenAddress corresponding token you want to withdraw. You must provide this or the vTokenAmount. (optional)
recieverstringThe address to receive the vtokens. It defaults to the address from the web3 instance in browser mode or the address of the private key in node environment. (optional)
transactionConfigobjectIt is an object that specifies blockchain transaction properties. Check common terms for an exhaustive description.

Note: You must provide one of vTokenAmount and tokenAmount.

Util Methods

Token Transfer

You can transfer tokens using the erc20 transfer function. It receives an object as input. It returns a promise that resolves to a transaction object

nub.erc20.transfer({
  token, amount, to [, ...transactionConfig]
}) => Promise<TransactionReceipt>
Object ParametersTypeDescription
tokenstringThe address of the ERC20 token you want to send.
amountstringThe amount of tokens you want to send. It must include the decimal places of the token. E.g 1*10**18 to send 1 WBNB.
tostringThe address you want to send the token to.

Estimate Token Transfer Gas

This helps you get the estimated gas needed to do a token transfer based on current network activity. It takes the same parameters as token transfer.

nub.erc20.estimateTransferGas({
  token, amount, to [, ...transactionConfig]
}) => Promise<TransactionReceipt>

It returns a promise that resolves to the gas object.

BNB Transfer

For BNB transfers, use the eth transfer function. It also receives an object as input. It returns a promise that resolves to a transaction object.

nub.eth.transfer({amount, to [, ...transactionConfig]}) => Promise<TransactionReceipt>;
Object ParametersTypeDescription
amountstringThe amount of BNB you want to send in wei (smallest BNB unit).
tostringThe address you want to send the BNB.

Estimate BNB Transfer Gas

Estimates the fee details needed to make a token transfer.

nub.eth.estimateTransferGas({amount, to [, ...transactionConfig]})

It returns a promise that resolves to the gas object.

Wrap BNB

BNB can be wrapped to WBNB using the SDK.

nub.wbnb.wrap({amount [, ...transactionConfig]}) => Promise<TransactionReceipt>

The function takes in the amount of BNB you want to wrap. It returns a promise that resolves to a transaction receipt.

Estimate Wrap Gas

nub.wbnb.estimateWrapGas({amount [, ...transactionConfig]}) => Promise<GasObject>

The function takes in the amount of BNB you want to use in estimation. It returns a promise that resolves to the gas object.

Unwrap BNB

WBNB can be unwrapped to BNB.

nub.wbnb.unwrap({amount [, ...transactionConfig]}) => Promise<TransactionReceipt>

The function takes in the amount of WBNB you want to unwrap. You must have this amount of WBNB already approved. It returns a promise that resolves to a transaction receipt.

Estimate Unwrap Gas

nub.wbnb.unwrap({amount [, ...transactionConfig]}) => Promise<GasObject>

The function takes in the amount of WBNB you want to unwrap.

Approval

You can approve addresses to spend the ERC20 token. It also receives an object as input and returns a promise that resolves to a transaction object.

nub.erc20.approve({
  token
  [, amount
    [, to
      [, ...transactionConfig]]]
}) => Promise<TransactionReceipt>
Object ParametersTypeDescription
tokenstringAddress of the token you want to approve for a spender.
amountstringThe amount of tokens to be approved for spending. If empty it defaults to maxUint256 i.e an infinite approval. It must include the decimal places of the token. E.g 1*10**18 to send 1 WBNB. (optional)
tostringThe address to be approved. It defaults to the Wizard address if not passed.(optional)

Estimate Approval Gas

This helps you get the estimated gas needed to do an approval based on current network activity. It takes the same parameters as approvals.

nub.erc20.estimateApproveGas({
  token
  [, amount
    [, to
      [, ...transactionConfig]]]
}) => Promise<GasObject>

It returns a promise that resolves to the gas object.

Pancakeswap LpToken Price

You can get the price of a Pancakeswap Liquidity provider token (lptoken) in US dollars using this function. It returns a promise that resolves to a number and takes the address of the lp token as an input.

nub.pancakeswap.getLpPrice(tokenAddress) => Promise<number>

Common Terms

Transaction Config
Parameter (optional)TypeDescription
gasPricestring/numberThe gas price in gwei. Mostly used in Node implementation to configure the transaction confirmation speed.
valuestring/numberAmount of BNB which you want to send along with the transaction (in wei).
noncestring/numberNonce of your sender account. Mostly used in Node implementation to send transaction with a particular nonce either to override unconfirmed transaction or some other purpose.
fromstringThe address to send the transaction from. It defaults to the address from the web3 instance in browser mode or the address of the private key in node environment.
Transaction Receipt

The transaction receipt describes various properties of the blockchain transaction. It is properly described in the web3js documentation.

Gas Object

This object contains all the fee/gas information needed for a transaction.

PropertyDescription
gasThe amount of gas to be used.
priceThe price for each unit of gas in wei.
feeThe fee for the transaction. (gas*price).
maxUint256

This is the the maximum figure that can be held in the uint256 type of solidity. It can be represented in Javascript using 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff.

vTokens

vTokens are ERC20 tokens that Venus mints to an address when it makes a successful deposit into the Venus protocol. These tokens are returned when a withdrawal is made. The amount an address has saved can be deduced from the amount of vTokens in its balance. These tokens are transferrable. Here is a list of tokens supported by Venus and their corresponding vToken addresses:

TokenIdTokenvToken
BNB0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE0xA07c5b74C9B40447a954e1466938b865b6BBea36
BUSD0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D560x95c78222B3D6e262426483D42CfA53685A67Ab9D
SXP0x47BEAd2563dCBf3bF2c9407fEa4dC236fAbA485A0x2fF3d0F6990a40261c66E1ff2017aCBc282EB6d0
USDC0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d0xecA88125a5ADbe82614ffC12D0DB554E2e2867C8
USDT0x55d398326f99059fF775485246999027B31979550xfD5840Cd36d94D7229439859C0112a4185BC0255
XVS0xcF6BB5389c92Bdda8a3747Ddb454cB7a64626C630x151B1e2635A717bcDc836ECd6FbB62B674FE3E1D
BTC0x7130d2A12B9BCbFAe4f2634d864A1Ee1Ce3Ead9c0x882C173bC7Ff3b7786CA16dfeD3DFFfb9Ee7847B
ETH0x2170Ed0880ac9A755fd29B2688956BD959F933F80xf508fCD89b8bd15579dc79A6827cB4686A3592c8
LTC0x4338665CBB7B2485A8855A139b75D5e34AB0DB940x57A5297F2cB2c0AaC9D554660acd6D385Ab50c6B
XRP0x1D2F0da169ceB9fC7B3144628dB156f3F6c60dBE0xB248a295732e0225acd3337607cc01068e3b9c10
BCH0x8ff795a6f4d97e7887c79bea79aba5cc76444adf0x5f0388ebc2b94fa8e123f404b79ccf5f40b29176
DOT0x7083609fce4d1d8dc0c979aab8c869ea2c8734020x1610bc33319e9398de5f57b33a5b184c806ad217
LINK0xf8a0bf9cf54bb92f17374d9e9a321e6a111a51bd0x650b940a1033b8a1b1873f78730fcfc73ec11f1f
DAI0x1af3f329e8be154074d8769d1ffa4ee058b1dbc30x334b3ecb4dca3593bccc3c7ebd1a1c1d1780fbf1
FIL0x0d8ce2a99bb6e3b7db580ed848240e4a0f9ae1530xf91d58b5ae142dacc749f58a49fcbac340cb0343
BETH0x250632378e573c6be1ac2f97fcdf00515d0aa91b0x972207a639cc1b374b893cc33fa251b55ceb7c0
ADA0x3ee2200efb3400fabb9aacf31297cbdd1d435d470x9a0af7fdb2065ce470d72664de73cae409da28ec
DOGE0xba2ae424d960c26247dd6c32edc70b295c744c430xec3422ef92b2fb59e84c8b02ba73f1fe84ed8d71
MATIC0xcc42724c6683b7e57334c4e856f4c9965ed682bd0x5c9476fcd6a4f9a3654139721c949c2233bbbbc8
CAKE0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce820x86ac3974e2bd0d60825230fa6f355ff11409df5c
AAVE0xfb6115445bff7b52feb98650c87f44907e58f8020x26da28954763b92139ed49283625cecaf52c6f94
TUSD0x14016e85a25aeb13065688cafb43044c2ef867840x08ceb3f4a7ed3500ca0982bcd0fc7816688084c3
1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.11

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago