@2pi-network/sdk 中文文档教程
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 tom/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. Foramount
prefer string to keep precision. Ifunit
is'wei'
(default) amount would not be converted. Ifunit
is'native'
the provided amount would be interpreted like fetched directly from some UI (for example 1 for ETH would be converted to1 * 1e18
). ThevaultIdentifier
argument can be omitted, the only (and default) options for the time being ismumbai_dai
. Thereferrer
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. Foramount
prefer string to keep precision. Ifunit
is'wei'
(default) amount would not be converted. Ifunit
is'native'
the provided amount would be interpreted like fetched directly from some UI (for example 1 for ETH would be converted to1 * 1e18
). ThevaultIdentifier
argument can be omitted, the only (and default) options for the time being ismumbai_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 givenaddress
. You should ask our team to enable this feature (use the Discord channel below). Fornetwork
the only allowed value for the time being is'polygon'
. Ifaddress
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 asvaultIdentifier
).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!
- Join our #devs channel on Discord!
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 tom/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. Foramount
prefer string to keep precision. Ifunit
is'wei'
(default) amount would not be converted. Ifunit
is'native'
the provided amount would be interpreted like fetched directly from some UI (for example 1 for ETH would be converted to1 * 1e18
). ThevaultIdentifier
argument can be omitted, the only (and default) options for the time being ismumbai_dai
. Thereferrer
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. Foramount
prefer string to keep precision. Ifunit
is'wei'
(default) amount would not be converted. Ifunit
is'native'
the provided amount would be interpreted like fetched directly from some UI (for example 1 for ETH would be converted to1 * 1e18
). ThevaultIdentifier
argument can be omitted, the only (and default) options for the time being ismumbai_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 givenaddress
. You should ask our team to enable this feature (use the Discord channel below). Fornetwork
the only allowed value for the time being is'polygon'
. Ifaddress
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 asvaultIdentifier
).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!
- Join our #devs channel on Discord!
你可能也喜欢
- 4house-libts-places-autocomplete 中文文档教程
- @0x/contract-artifacts 中文文档教程
- @0x5e/react-native-alipay 中文文档教程
- @0xcert/ethereum-bitski-backend-provider 中文文档教程
- @1dnakukreja/common-img 中文文档教程
- @36node/bus-core-sdk 中文文档教程
- @ab-inbev-z-tech/fintech-checkout 中文文档教程
- @abacus-insights/fhir-works-on-aws-interface 中文文档教程
- @abbott-platform/botkit-storage-datastore 中文文档教程
- @abecodes/hasher64 中文文档教程