0.0.2 • Published 3 years ago

peri-finance-js v0.0.2

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

PynthetixJs library

CircleCI npm version Discord Twitter Follow

The Pynthetix-JS Library provides a simple pre-packaged API to communicate with Pynthetix on ethereum. You can use it to contribute to DeFi's growing pynthetic asset ecosystem.

This is particularly useful for hackathon teams to quickly npm install pynthetix-js and start building in just a few minutes.

Under the hood, PynthetixJs uses ethers.js library and its concept of providers and transaction signers.

Install via npm

npm install pynthetix-js

Developer Docs

developer.pynthetix.io

Example for getting the total pUSD stablecoin in circulation

const { PynthetixJs } = require('pynthetix-js');
const perijs = new PynthetixJs(); //uses default ContractSettings - ethers.js default provider, mainnet
(async function() {
  const totalPUSD = await perijs.pUSD.totalSupply();
  const totalPUSDSupply = perijs.utils.formatEther(totalPUSD);
  console.log('pUSDTotalSupply', totalPUSDSupply);
})();

Default settings don't use any signer. That means that constants can be viewed from the contract but executing a transaction will fail. To execute transactions, set up signer.

4 signers are included in the library - Metamask (compatible with Dapp browsers), Trezor, Ledger and PrivateKey. Custom ethers.js compatible signers can be used too.

Example using a metamask signer

const { PynthetixJs } = require('pynthetix-js');
const metaMaskSigner = new PynthetixJs.signers.Metamask();
const perijs = new PynthetixJs({ signer: metaMaskSigner }); //uses Metamask signer and default infura.io provider on mainnet

Example of minting stablecoin(pUSD) with private key signer

const { PynthetixJs } = require('pynthetix-js');
//parameters: default provider, default networkId, private key as a string
const signer = new PynthetixJs.signers.PrivateKey(
  null,
  0,
  '0x0123456789012345678901234567890123456789012345678901234567890123'
);
const perijs = new PynthetixJs({ signer });

async function run() {
  const totalSupply = await perijs.Pynthetix.totalSupply();
  const periTotalSupply = perijs.utils.formatEther(totalSupply);
  console.log('periTotalSupply', periTotalSupply);

  //issue 100 pynths (will throw if insufficient funds for gas)
  try {
    const txObj = await perijs.Pynthetix.issuePynths(perijs.util.parseEther('100')); //execute transaction (requires gas)
    console.log('transaction hash', txObj.hash);
  } catch (e) {
    console.log(e);
  }
}

run();

Live examples

  • Get total pynth supply Get total supply
  • Get collateralized state image

Got any questions?

Join our dev community on Discord: Discord