Navbar
javascript

Welcome

Welcome to the Lendroid-2-JS!

Lendroid-2-JS is a library to wrapper the lendroid Server and Lendroid Contract API's.
You can view code examples in the dark area to the right.

It uses web3.js latest

Installation

To install:

yarn add lendroid-protocol

You can install Lendroid-2-JS using Yarn.

For the specific version, please check it's releases on Github

How to Start ?

Import

To import:

import { Lendroid } from "lendroid-protocol";

First, you need to Import Lendroid-2-JS library.

Initialize

To initialize:

const options = {
  type: "metamask",
  tokens,
  onEvent: handleMessage
};

const LendroidJS = new Lendroid(options);
Key Type Default Description
type string metamask Provider Type. (metamask, torus, formtatic)
tokens object - Deployed tokens. example
onEvent function - Event listener. Events

What's new ?

1. Multiple Providers

Lendroid-2-JS working with Metamask as well as Torus and Fortmatic now.

2. Event handler

Example:

this.onEvent(Events.NETWORK_CHANGED);
this.onEvent(Events.BALANCE_UPDATED, { data });
this.onEvent(Events.BALANCE_FAILED, err);
Key Type Default Description
event string - Event name.
params object - Handle params.

3. Dealing promise

Every public methods are returning Promise which can be managed by UI.

Properties

web3Utils

Provide Web3Utils.

contracts

Example:

{
  contracts: {
    Protocol: PROTOCOL_CONTRACT,
    TOKEN_1_SYMBOL: TOKEN_1_CONTRACT,
    TOKEN_2_SYMBOL: TOKEN_2_CONTRACT,
    // ... other token contract
  },
  balances: {
    ETH: ETH_BALANCE,
    LST: LST_BALANCE,
    WETH: WETH_BALANCE,
    TOKEN_1_SYMBOL: TOKEN_1_BALANCE,
    TOKEN_2_SYMBOL: TOKEN_2_BALANCE,
    // ... other token balances
  },
  allowances: {
    LST: LST_ALLOWANCE,
    WETH: WETH_ALLOWANCE,
    TOKEN_1_SYMBOL: TOKEN_1_ALLOWANCE,
    TOKEN_2_SYMBOL: TOKEN_2_ALLOWANCE,
    // ... other token allowances
  },
  positions: {
    lent: [...],
    borrowed: [...]
  },
}

const { contracts } = library

Fetch Contracts, Blance, LSFUI Tokens, Pools and Names, Positions. And provide interface for Contract functions.

Key Type Default Description
contracts object {} Token Contract Instances.
expiries array - Registered expiries.
supportTokens object - Contract addresses by Network.

Contract Instances

Web3 Contract Instances of Supported Tokens. Check here for supported Token from Lendroid-2-JS.

Note: You can define undeployed ABIs here. And set all inside of supportTokens - check example.

You need to provide contract tokens from Wallet. Check example here.

contracts.expiries

const { expiries } = library.contracts

Every last thursday of month for current year. And manual expiries. You can check how to calculate from here

Key Type Default Description
name string - Expiry name
timestamp number - Unix timestamp of expiry.
fullName string - E.g - Feb 2020
date string - E.g - 2020-02-27

Methods

enable

Sample Code

// Metamask
this.library.enable(window.ethereum, "metamask");

// Torus
const { default: Torus } = require("@toruslabs/torus-embed");
const torus = new Torus();
await torus.init();
await torus.login();
this.library.enable(torus.provider, "torus");

// Fortmatic
const Fortmatic = require("fortmatic");
const fm = new Fortmatic(process.env.FORTMATIC_API_KEY);
this.library.enable(fm.getProvider(), "fortmatic");

library.enable(provider, type)

Enable provider for the app and initilize contracts

Parameters

Key Type Default Description
postData object - Data to create Orders.
callback function - Callback Function

Be sure postData will be matched to your order model on your API.

contracts.constructor

contracts.onNetworkChanged

Watch network id and re-initialize protocol

contracts.onUpdateAddress

Update address and fetch pools and positions

contracts.getBalances

Sample Code

library.contracts
  .onSplit(token, {
    amount,
    expiry,
    underlying: type ? underlying : undefined,
    strike: type ? Number(strike) : undefined,
  })
  .then(() => {
    library.contracts.getBalances(true)
    callback && callback()
  })

library.contracts.getBalances(true)

Manual method to fetch balances. Also dealing with fetching new contracts.

contracts.getPoolNames

Sample Code

library.contracts.onRegisterPoolName(poolName).then(() => {
  setSuccess(true)
  library.contracts.getPoolNames()
})

library.contracts.getPoolNames()

Manual method to fetch pool names.

contracts.getRiskFreePools

Sample Code

library.contracts.onPurchase(id, type, marketInfo).then(() => {
  library.contracts.getRiskFreePools()
})

library.contracts.getRiskFreePools()

Manual method to fetch pools (risk-free).

contracts.getRiskyPools

Sample Code

library.contracts.onPurchase(id, type, marketInfo).then(() => {
  library.contracts.getRiskyPools()
})

library.contracts.getRiskyPools()

Manual method to fetch pools (risky).

contracts.getPositions

Sample Code

library.contracts
  .onAvailLoan({ ...form, strike })
  .then(() => {
    console.log('Loan Created')
    library.contracts.getPositions()
  })
  .finally(() => setForm(defaults))

library.contracts.getPositions()

Manual method to fetch positions.

Contract Methods

contracts.onWrap

Sample Code

library.contracts.onWrap(token, amount).then(() => {
  library.contracts.getBalances()
})

library.contracts.onWrap(token, amount)

Wrap Token into L-Token.

contracts.onUnwrap

Sample Code

library.contracts.onUnwrap(token, amount).then(() => {
  library.contracts.getBalances()
})

library.contracts.onUnwrap(token, amount)

Unwrap L-Token into Token.

contracts.onSplit

Sample Code

library.contracts
  .onSplit(token, {
    amount,
    expiry,
    underlying: type ? underlying : undefined,
    strike: type ? Number(strike) : undefined,
  })
  .then(() => {
    library.contracts.getBalances(true)
  })

library.contracts.onSplit(token, form)

Split token into I+F or I+S+U tokens. It will create new IFSU tokens, so withContract parameter should be true for getBalances method.

contracts.onFuse

Sample Code

library.contracts
  .onFuse(token, {
    amount,
    expiry,
    underlying,
    strike: Number(strike),
  })
  .then(() => {
    library.contracts.getBalances()
  })

library.contracts.onFuse(token, form)

Fuse split tokens back to wrapped tokens.

contracts.onRegisterLookUpStake

Sample Code

const handlePoolName = e => {
  const val = e ? e.target.value : ''
  setPoolName(val)
  throttleFunction(
    len => library.contracts.onRegisterLookUpStake(len).then(handleStake),
    200,
    val.length
  )
}

library.contracts.onRegisterLookUpStake(poolNameLength)

Look up stack value for pool name register by length of name.

contracts.onRegisterPoolName

Sample Code

library.contracts.onRegisterPoolName(poolName).then(() => {
  library.contracts.getPoolNames()
})

library.contracts.onRegisterPoolName(poolName)

Register pool name.

contracts.onCreatePool

Sample Code

library.contracts
  .onCreatePool(form)
  .then(() => {
    if (riskFree) {
      library.contracts.getRiskFreePools()
    } else {
      library.contracts.getRiskyPools()
    }
  })

library.contracts.onCreatePool(form)

Create a new Pool.

contracts.onOfferNewToken

Sample Code

library.contracts
  .onOfferNewToken(poolId, {
    expiry,
    underlying,
    strike: Number(strike),
    iCostPerDay,
    sCostPerDay,
  })
  .then(() => {
    fetchPools()
  })

library.contracts.onOfferNewToken(poolId, form)

Offer new token to Pool.

contracts.onWithdrawEarnings

Sample Code

library.contracts.onWithdrawEarnings(poolId, riskFree).then(() => {
  fetchPools()
})

library.contracts.onWithdrawEarnings(poolId, riskFree)

Withdraw earnings from Pool.

contracts.onClosePool

Sample Code

library.contracts.onClosePool(poolId, riskFree).then(() => {
  fetchPools()
})

library.contracts.onClosePool(poolId, riskFree)

Close Pool.

contracts.onChangePrice

Sample Code

const options = {
  riskFree,
  type,
  marketInfo: marketInfoParam || marketInfo,
}

library.contracts.onChangePrice(poolId, value, options).then(() => {
  fetchPools()
})

library.contracts.onChangePrice(poolId, value, options)

Change Price for splitted tokens.

contracts.onIncreaseCapacity

Sample Code

const options = {
  riskFree,
  type,
  marketInfo: marketInfoParam || marketInfo,
}

library.contracts.onIncreaseCapacity(poolId, value, options).then(() => {
  fetchPools()
})

library.contracts.onIncreaseCapacity(poolId, value, options)

Increase splitted tokens capacity.

contracts.onDecreaseCapacity

Sample Code

const options = {
  riskFree,
  type,
  marketInfo: marketInfoParam || marketInfo,
}

library.contracts.onDecreaseCapacity(poolId, value, options).then(() => {
  fetchPools()
})

library.contracts.onDecreaseCapacity(poolId, value, options)

Decrease splitted tokens capacity.

contracts.onRetireToken

Sample Code

const options = {
  riskFree,
  type,
  marketInfo: marketInfoParam || marketInfo,
}

library.contracts.onRetireToken(poolId, options).then(() => {
  fetchPools()
})

library.contracts.onRetireToken(poolId, options)

Retire offered token.

contracts.onContribute

Sample Code

library.contracts
  .onContribute(poolId, amount, { riskFree })
  .then(() => {
    if (riskFree) {
      library.contracts.getRiskFreePools()
    } else {
      library.contracts.getRiskyPools()
    }
    library.contracts.getBalances()
  })

library.contracts.onContribute(poolId, value, options)

Withdraw contributed balance from Pool.

contracts.onWithdrawContribute

Sample Code

library.contracts
  .onWithdrawContribute(poolId, amount, { riskFree })
  .then(() => {
    if (riskFree) {
      library.contracts.getRiskFreePools()
    } else {
      library.contracts.getRiskyPools()
    }
    library.contracts.getBalances()
  })

library.contracts.onWithdrawContribute(poolId, value, options)

Withdraw contributed balance from Pool.

contracts.onPurchase

Sample Code

const marketInfo = [expiry, underlying, strike]
library.contracts.onPurchase(id, type, marketInfo).then(() => {
  library.contracts.getRiskFreePools()
  library.contracts.getRiskyPools()
})

library.contracts.onPurchase(poolId, type, param)

Purchase splitted token to Pool.

contracts.onAvailLoan

Sample Code

library.contracts
  .onAvailLoan({
    currency,
    expiry,
    underlying,
    strike,
    amount
  })
  .then(() => {
    library.contracts.getPositions()
  })

library.contracts.onAvailLoan(form)

Borrow a loan(create a position).

contracts.onRepay

Sample Code

library.contracts
  .onRepay(positionId, amount)
  .then(() => {
    library.contracts.getPositions()
  })

library.contracts.onRepay(positionId, amount)

Re-pay to loan.

contracts.onWithdrawCollateral

Sample Code

library.contracts
  .onWithdrawCollateral(positionId)
  .then(() => {
    library.contracts.getPositions()
  })

library.contracts.onWithdrawCollateral(positionId)

Withdraw collateral to loan.

Web3Utils Class

It uses origin Web3 latest. And you can find its definition on Lendroid Services

toWei

Sample Code

library.web3Utils.toWei(1);
// return : "1000000000000000000"

library.web3Utils.toWei(number)

Converts any ether value value into wei. It refers Web3

Parameters

Key Type Default Description
number string,number,BN - Value of ether.

wei are the smallest ethere unit, and you should always make calculations in wei and convert only for display reasons.

fromWei

Sample Code

library.web3Utils.fromWei("1000000000000000000");
// return : "1"

library.web3Utils.fromWei(number)

Converts any wei value into ether value. It refers Web3

Parameters

Key Type Default Description
number string,number,BN - Value of ether.

toBN

Sample Code

library.web3Utils.toBN(1234).toString();
// return : "1234"

library.web3Utils.toBN(number)

Convert any given value (including BigNumber.js instances) into a BN.js instance, for handling big numbers in JavaScript. It refers Web3

Parameters

Key Type Default Description
number string,number,BN - Value of ether.

toDecimal

Sample Code

library.web3Utils.toDecimal(0xea).toString();
// return : 234

library.web3Utils.toDecimal(hex)

Number representation of a given HEX value. It refers Web3

Parameters

Key Type Default Description
hex `string HEX` -

substract

Sample Code

library.web3Utils.substract(1, 1);
// return : 0

BN Substraction between numbers. Reference

substractBN

Sample Code

library.web3Utils.substractBN("1000000000000000000", "1000000000000000000");
// return : 0

Substract between Big-Numbers. Reference

createContract

Sample Code

library.web3Utils.createContract(contractABI, supportTokens[token][network]);

createContract(abi, address)

Returns Web3 contract for ABI and contract address.

Parameters

Key Type Default Description
abi object - Contract ABI.
address string - Contract Addresss.

sendSignedTransaction

Sample Code

library.web3Utils.sendSignedTransaction(approval._signed_transaction);

sendSignedTransaction(signedTransactionData)

Returns Web3 contract for ABI and contract address.

Parameters

Key Type Default Description
signedTransactionData string - Signed Data by web3.

getBlockTimeStamp

Sample Code

library.web3Utils.getBlockTimeStamp();

getBlockTimeStamp()

Return latest block timestamp.

Errors

The Kittn API uses the following error codes:

Error Code Meaning
400 Bad Request -- Your request is invalid.
401 Unauthorized -- Your API key is wrong.
403 Forbidden -- The kitten requested is hidden for administrators only.
404 Not Found -- The specified kitten could not be found.
405 Method Not Allowed -- You tried to access a kitten with an invalid method.
406 Not Acceptable -- You requested a format that isn't json.
410 Gone -- The kitten requested has been removed from our servers.
418 I'm a teapot.
429 Too Many Requests -- You're requesting too many kittens! Slow down!
500 Internal Server Error -- We had a problem with our server. Try again later.
503 Service Unavailable -- We're temporarily offline for maintenance. Please try again later.