@2pi-network/sdk 中文文档教程

发布于 3年前 浏览 19 项目主页 更新于 3年前

Welcome to 2PI SDK

用于使用 2PI 协议构建的 JavaScript SDK

Installation

Using yarn

yarn add @2pi-network/sdk

Using npm

npm i @2pi-network/sdk

Usage

以下是使用 SDK 的快速浏览。 这假设您有一些环境变量:

  • MNEMONIC: this should contain a valid mnemonic.
  • API_KEY: API key value.
  • API_SECRET: API secret value.
const { TwoPi } = require('@2pi-network/sdk')

const main = async () => {
  const mnemonic  = process.env.MNEMONIC
  const apiKey    = process.env.API_KEY
  const apiSecret = process.env.API_SECRET
  const twoPi     = new TwoPi({ mnemonic, apiKey, apiSecret })
  const vaults    = await twoPi.getVaults()

  vaults.forEach(vault => {
    console.log('Identifier',     vault.identifier)
    console.log('PID',            vault.pid)
    console.log('Token',          vault.token)
    console.log('Address',        vault.address)
    console.log('Token address',  vault.tokenAddress)
    console.log('Token decimals', vault.tokenDecimals)
    console.log('Vault decimals', vault.vaultDecimals)
    console.log('APY',            vault.apy)
    console.log('TVL',            vault.tvl)
    console.log('Balances',       vault.balances)
    console.log('Deposits',       vault.deposits)
  })
}

main().then(() => {
  process.exit(0)
}).catch(error => {
  console.error(error)
  process.exit(1)
})

Public classes

Private classes (may become public on future releases)

TwoPi instance

这是几乎所有交互的入口点。 您将被要求提供至少 3 个参数:

  • One mnemonic, this would be used to sign and send the relevant transactions.
  • API key, together with API secret will be used to connect with 2PI network API.
  • API secret, together with API key will be used to connect with 2PI network API.

TwoPi public attributes

在每个 twoPi 实例上,您可以访问以下属性:

  • mnemonic?: the provided mnemonic for this instance.
  • path?: the provided derivation path for this instance (defaults to m/44'/60'/0'/0/0).
  • address?: the public address derived from the provided mnemonic.
  • apiKey?: the provided API key. [^1]
  • apiSecret?: the provided API secret. [^1]
  • endpoint: the API endpoint in use.
  • wallet: the wallet instance, derived from the provided mnemonic.

[^1]:如果未提供,您仍然可以使用所有未经身份验证的操作(getVaults()deposit(...)withdraw(...))。

TwoPi public methods

  • constructor({mnemonic?, path?, apiKey?, apiSecret?, endpoint?}) returns a new instance. Refer to TwoPi public attributes to get a description of each argument.
  • async getVaults({networks?, partner?}) it returns an array of available vaults (each of which are Vault instances). You can give an optional filter object like {networks: ['mumbai']} to narrow the results to the specified networks. If you are a partner, the filter also support custom vaults by using an object like {partner: 'company_name'}.
  • async deposit({amount, vaultIdentifier, unit?, referrer?}) it makes a deposit on the given pool. For amount prefer string to keep precision. If unit is 'wei' (default) amount would not be converted. If unit is 'native' the provided amount would be interpreted like fetched directly from some UI (for example 1 for ETH would be converted to 1 * 1e18). The vaultIdentifier argument can be omitted, the only (and default) options for the time being is mumbai_dai. The referrer argument can be an address to associate who brought the user making the deposit (only assigned on the very first call for any given sender, after that is ignored).
  • status: can be 'success' or 'error'
  • transactions?: array of executed transactions as transaction receipts (in case of error, the last one should have the required information to trace the reason).
  • message?: in case of error, the overall main reason description.
  • cursor?: in case of error, the index (zero based) of the failed transaction.
  • async withdraw({amount, vaultIdentifier, unit?}) it makes a withdraw on the given pool. For amount prefer string to keep precision. If unit is 'wei' (default) amount would not be converted. If unit is 'native' the provided amount would be interpreted like fetched directly from some UI (for example 1 for ETH would be converted to 1 * 1e18). The vaultIdentifier argument can be omitted, the only (and default) options for the time being is mumbai_dai. Returns an object with the following attributes:
  • status: can be 'success', 'failed' or 'error'
  • transactions?: array of executed transactions as transaction receipts (in case of error, the last one should have the required information to trace the reason).
  • message?: in case of failed or error, the overall main reason description.
  • cursor?: in case of failed or error, the index (zero based) of the failed transaction.
  • async faucet({network, address}) it transfer some native tokens to the given address. You should ask our team to enable this feature (use the Discord channel below). For network the only allowed value for the time being is 'polygon'. If address has already the minimum native token balance (0.1 MATIC on Polygon) nothing is done, it gets completed to this value otherwise. Returns an object with the following attributes:
  • status: can be 'success', 'error'
  • message?: in case of failed or error, the overall main reason description.

Vault private attributes

在每个 vault 实例上,您都可以访问以下属性:

  • identifier: the vault identifier, used as argument on deposit and withdraw operation (referred as vaultIdentifier).
  • pid: the vault internal ID.
  • token: string identifying the token being maximized.
  • address: string with the vault main contract address.
  • tokenAddress: string with the vault token address.
  • tokenDecimals: how many decimals to consider for the token.
  • vaultDecimals: how many decimals to consider for the vault.
  • apy: the current vault APY.
  • tvl: the current vault TVL expressed in wei of the underlying token (as string).
  • balances: array of the current balances of the registered wallets (represented in objects { wallet: string, amount: string }).
  • deposits: array of the current deposits of the registered wallets (represented in objects { wallet: string, amount: string }).

Vault private methods

  • constructor({identifier, pid, token, address, tokenAddress, tokenDecimals, vaultDecimals, apy, tvl, balances, deposits}) refer to Vault private attributes to get a description of each argument and attribute.

Warning

存储助记符和 API 密钥/秘密数据时始终要小心。

Let's talk!

Welcome to 2PI SDK

JavaScript SDK for building with 2PI Protocol

Installation

Using yarn

yarn add @2pi-network/sdk

Using npm

npm i @2pi-network/sdk

Usage

Here is a quick look at using the SDK. This assume you have some environment variables:

  • MNEMONIC: this should contain a valid mnemonic.
  • API_KEY: API key value.
  • API_SECRET: API secret value.
const { TwoPi } = require('@2pi-network/sdk')

const main = async () => {
  const mnemonic  = process.env.MNEMONIC
  const apiKey    = process.env.API_KEY
  const apiSecret = process.env.API_SECRET
  const twoPi     = new TwoPi({ mnemonic, apiKey, apiSecret })
  const vaults    = await twoPi.getVaults()

  vaults.forEach(vault => {
    console.log('Identifier',     vault.identifier)
    console.log('PID',            vault.pid)
    console.log('Token',          vault.token)
    console.log('Address',        vault.address)
    console.log('Token address',  vault.tokenAddress)
    console.log('Token decimals', vault.tokenDecimals)
    console.log('Vault decimals', vault.vaultDecimals)
    console.log('APY',            vault.apy)
    console.log('TVL',            vault.tvl)
    console.log('Balances',       vault.balances)
    console.log('Deposits',       vault.deposits)
  })
}

main().then(() => {
  process.exit(0)
}).catch(error => {
  console.error(error)
  process.exit(1)
})

Public classes

Private classes (may become public on future releases)

TwoPi instance

This is the entry point of almost any interaction. You will be asked to provide at least 3 arguments:

  • One mnemonic, this would be used to sign and send the relevant transactions.
  • API key, together with API secret will be used to connect with 2PI network API.
  • API secret, together with API key will be used to connect with 2PI network API.

TwoPi public attributes

On every twoPi instance you can access the following attributes:

  • mnemonic?: the provided mnemonic for this instance.
  • path?: the provided derivation path for this instance (defaults to m/44'/60'/0'/0/0).
  • address?: the public address derived from the provided mnemonic.
  • apiKey?: the provided API key. [^1]
  • apiSecret?: the provided API secret. [^1]
  • endpoint: the API endpoint in use.
  • wallet: the wallet instance, derived from the provided mnemonic.

[^1]: If not provided, you can still use all the unauthenticated operations (getVaults(), deposit(...) and withdraw(...)).

TwoPi public methods

  • constructor({mnemonic?, path?, apiKey?, apiSecret?, endpoint?}) returns a new instance. Refer to TwoPi public attributes to get a description of each argument.
  • async getVaults({networks?, partner?}) it returns an array of available vaults (each of which are Vault instances). You can give an optional filter object like {networks: ['mumbai']} to narrow the results to the specified networks. If you are a partner, the filter also support custom vaults by using an object like {partner: 'company_name'}.
  • async deposit({amount, vaultIdentifier, unit?, referrer?}) it makes a deposit on the given pool. For amount prefer string to keep precision. If unit is 'wei' (default) amount would not be converted. If unit is 'native' the provided amount would be interpreted like fetched directly from some UI (for example 1 for ETH would be converted to 1 * 1e18). The vaultIdentifier argument can be omitted, the only (and default) options for the time being is mumbai_dai. The referrer argument can be an address to associate who brought the user making the deposit (only assigned on the very first call for any given sender, after that is ignored).
  • status: can be 'success' or 'error'
  • transactions?: array of executed transactions as transaction receipts (in case of error, the last one should have the required information to trace the reason).
  • message?: in case of error, the overall main reason description.
  • cursor?: in case of error, the index (zero based) of the failed transaction.
  • async withdraw({amount, vaultIdentifier, unit?}) it makes a withdraw on the given pool. For amount prefer string to keep precision. If unit is 'wei' (default) amount would not be converted. If unit is 'native' the provided amount would be interpreted like fetched directly from some UI (for example 1 for ETH would be converted to 1 * 1e18). The vaultIdentifier argument can be omitted, the only (and default) options for the time being is mumbai_dai. Returns an object with the following attributes:
  • status: can be 'success', 'failed' or 'error'
  • transactions?: array of executed transactions as transaction receipts (in case of error, the last one should have the required information to trace the reason).
  • message?: in case of failed or error, the overall main reason description.
  • cursor?: in case of failed or error, the index (zero based) of the failed transaction.
  • async faucet({network, address}) it transfer some native tokens to the given address. You should ask our team to enable this feature (use the Discord channel below). For network the only allowed value for the time being is 'polygon'. If address has already the minimum native token balance (0.1 MATIC on Polygon) nothing is done, it gets completed to this value otherwise. Returns an object with the following attributes:
  • status: can be 'success', 'error'
  • message?: in case of failed or error, the overall main reason description.

Vault private attributes

On every vault instance you can access the following attributes:

  • identifier: the vault identifier, used as argument on deposit and withdraw operation (referred as vaultIdentifier).
  • pid: the vault internal ID.
  • token: string identifying the token being maximized.
  • address: string with the vault main contract address.
  • tokenAddress: string with the vault token address.
  • tokenDecimals: how many decimals to consider for the token.
  • vaultDecimals: how many decimals to consider for the vault.
  • apy: the current vault APY.
  • tvl: the current vault TVL expressed in wei of the underlying token (as string).
  • balances: array of the current balances of the registered wallets (represented in objects { wallet: string, amount: string }).
  • deposits: array of the current deposits of the registered wallets (represented in objects { wallet: string, amount: string }).

Vault private methods

  • constructor({identifier, pid, token, address, tokenAddress, tokenDecimals, vaultDecimals, apy, tvl, balances, deposits}) refer to Vault private attributes to get a description of each argument and attribute.

Warning

Always be careful when storing mnemonic and API keys / secret data.

Let's talk!

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文