Home Reference Source

Kyber Fed Price Reserve JS SDK

This SDK allows market makers and developers to deploy, maintain and operate an on-chain Kyber Network Fed Price Reserve using Javascript and node.

Installation

Install the package with:

npm install --save kyber-fpr-sdk    

Usage

There are 2 main classes in this SDK.

Deployer Class

The Deployer class only needs the web3 provider to init. After deployment, it returns a set of addresses for required contracts.

// ethereum Remote Node Provider, for example infura.io
const provider = new Web3.providers.HttpProvider('ethereum-node-url')
const web3 = new Web3(provider)

// initialize account from private key
const account = dpl.web3.eth.accounts.privateKeyToAccount('private-key')
// initialize account from keystore file
// const account = dpl.web3.eth.accounts.decrypt(fs.readFileSync(), "your-keystore-passphrase");

dpl.web3.eth.accounts.wallet.add(account)

const dpl = new Deployer(web3)

let addresses;
(async () =>  { 
    addresses = await dpl.deploy(account)
    console.log(addresses)
})()

Reserve Class

Reserve class allow users to make call to the smart contracts and query its state on the blockchain.

The following example queries the sanityRatesContract's admin and the SanityRates contract:

const reserve = new Reserve(web3, addresses);
const KNCTokenAddress = "0x095c48fbaa566917474c48f745e7a430ffe7bc27";
const ETHTokenAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";

(async () => {
  // get sanityContract address
  console.log('SanityRates contract address is ', await reserve.sanityRates.admin())
  const sanityRates = await reserve.getSanityRate(KNCTokenAddress, ETHTokenAddress)
  console.log('SanityRates for KNC-ETH is ', sanityRates)
})();

Permission Control

More on permission control at permission groups. To set permission with SDK, call to the contract that needs to change account's role with these methods from baseContract. The following example add Operator 0x0a4c79cE84202b03e95B7a692E5D728d83C44c76 to ConversionRates contract

const reserve = new Reserve(web3, addresses);

(async () => {
  // admin operations
  await reserve.ConversionRates.addOperator(adminAccount, '0x0a4c79cE84202b03e95B7a692E5D728d83C44c76');
  console.log(await reserve.ConversionRates.getOperators())
})();

Control Rates

Control rates operations can be called directly as reserve Object's methods. There are 5 operations regarding set rates: setRate, setSanityRates, setReasonableDiff, setQtyStepFuncion and setImbalanceStepFunction. More about the meaning of these operations can be viewed in the onboarding tutorials. The following example set the base rate for KNC token.

const reserve = new Reserve(web3, addresses);
const operatorAccount = web3.eth.accounts.privateKeyToAccount('operatorAccountPrivateKey');
const KNCTokenAddress = "0x095c48fbaa566917474c48f745e7a430ffe7bc27";

(async () => {
  // create rateSetting object and set base buy/ sell rate.
  rate = new RateSetting(KNCTokenAddress, 10000000, 1100000)
  await reserve.setRate( 
    operatorAccount,
    [rate],
    (await web3.eth.getBlockNumber()) + 1
  )
  // should log 10000000 and 1100000 as buy/sell rate
  console.log(await reserve.getBuyRates(KNCTokenAddress, 1,await web3.eth.getBlockNumber()))
  console.log(await reserve.getSellRates(KNCTokenAddress, 1,await web3.eth.getBlockNumber()))
})();

Fund Security

To secure reserve's fund, there are two main operations:

The following example show how to stop trade from the reserve and withdraw 1000 KNC from reserve to a receiver account to secure the fund:

  const reserve = new Reserve(web3, addresses);
  const adminAccount = web3.eth.accounts.privateKeyToAccount('adminAccountPrivateKey');
  const operatorAccount = web3.eth.accounts.privateKeyToAccount('operatorAccountPrivateKey');
  const alerterAccount = web3.eth.accounts.privateKeyToAccount('alerterAccountPrivateKey');
  const receiverAddress = '0x69E3D8B2AE1613bEe2De17C5101E58CDae8a59D4' ;
  const KNCTokenAddress = '0x095c48fbaa566917474c48f745e7a430ffe7bc27';

  (async () => {
    // stop trade. 
    await reserveContract.disableTrade(alerterAccount)
    // approve receiver to receive KNC from this reserve
    await reserveContract.approveWithdrawAddress(operatorAccount,KNCTokenAddress, receiverAddress)
    if (await reserveContract.approvedWithdrawAddresses(receiverAddress, KNCTokenAddress) == true) {
      await reserveContract.withdraw(adminAccount, KNCTokenAddress, 1000)
    } else {
      console.log('cannot withdraw KNC at this moment, please retry again later')
    }
  })();