@8pay/sdk 中文文档教程
@8pay/sdk
该 SDK 提供了一种以编程方式与 8Pay 的智能合约进行交互的方法,以及一些帮助函数来简化集成过程。
Requirements
- web3 (1.0.0 or higher)
Installation
这是一个 Node.js 模块,可通过 npm 注册表获取。
安装前,下载并安装 Node.js。
使用 npm install
命令 完成安装:
$ npm install @8pay/sdk
Usage
此模块在引擎盖下使用 web3,这是用于与 Web 3.0。
为了熟悉 web3
,它在发送交易时遵循相同的结构,并且还添加了额外的功能。
首先,创建 8pay 的 SDK 实例并选择您要操作的网络:
const Web3 = require('web3');
const EightPay = require('@8pay/sdk');
const web3 = new Web3('<provider-url>');
const eightPay = new EightPay(web3, EightPay.Network.BSC);
Sending transactions
在将交易广播到区块链之前,有两种方法可以签署交易 :
- With an unlocked account (e.g. when using Metamask)
eightPay.fixedRecurring.bill(planId, subscriptionIds)
.send({ from: account })
- With private key
eightPay.fixedRecurring.bill(planId, subscriptionIds)
.send({ privateKey: '0x32df7......' })
Estimate gas
可以使用 estimateGas
函数估算交易消耗的气体,该函数采用 options
对象并返回所需的气体量:
eightPay.fixedRecurring.bill(planId, subscriptionIds)
.estimateGas(options)
Options
send
和 estimateGas
方法接受 web3
中可用的相同 options
对象,它允许设置参数,如 gas
、gasPrice
, nonce
等等。
Events
发送交易时会发出以下事件:sending
、sent
、transactionHash
、receipt
、confirmation
和错误
。
const receipt = await eightPay.fixedRecurring.bill(planId, subscriptionIds)
.send({ from: account })
.once('sending', payload => {})
.once('sent', payload => {})
.once('transactionHash', hash => {})
.once('receipt', receipt => {})
.on('confirmation', confirmation => {})
.on('error', error => {})
Fixed Recurring
Bill
计划的账单订阅。
参数:
- planId - id of the plan
- subscriptionIds - array of subscription ids
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const receipt = await eightPay.fixedRecurring.bill(planId, subscriptionIds)
.send({ from: account })
Terminate
强制取消计划的订阅。
注意:发件人帐户必须是计划的管理员或具有终止权限的操作帐户。
参数:
- planId - id of the plan
- subscriptionIds - array of subscription ids
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const receipt = await eightPay.fixedRecurring.terminate(planId, subscriptionIds)
.send({ from: account })
Variable Recurring
Bill
计划的账单订阅。
注意:发件人帐户必须是计划的管理员或具有账单权限的操作帐户。
参数:
- planId - id of the plan
- subscriptionIds - array of subscription ids
- amounts - array of amounts to charge for each subscription
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const amounts = [eightPay.utils.parseAmount('10', '8PAY')];
const receipt = await eightPay.variableRecurring.bill(planId, subscriptionIds, amounts)
.send({ from: account })
Terminate
强制取消计划的订阅。
注意:发件人帐户必须是计划的管理员或具有终止权限的操作帐户。
参数:
- planId - id of the plan
- subscriptionIds - array of subscription on-chain ids
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const receipt = await eightPay.variableRecurring.terminate(planId, subscriptionIds)
.send({ from: account })
On Demand
Bill
计划的账单订阅。
注意:发件人帐户必须是计划的管理员或具有账单权限的操作帐户。
参数:
- planId - id of the plan
- subscriptionIds - array of subscription ids
- amounts - array of amounts to charge for each subscription
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const amounts = [eightPay.utils.parseAmount('10', '8PAY')];
const receipt = await eightPay.onDemand.bill(planId, subscriptionIds, amounts)
.send({ from: account })
Terminate
强制取消计划的订阅。
注意:发件人帐户必须是计划的管理员或具有终止权限的操作帐户。
参数:
- planId - id of the plan
- subscriptionIds - array of subscription ids
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const receipt = await eightPay.onDemand.terminate(planId, subscriptionIds)
.send({ from: account })
Accounts
accounts 实用程序可用于从私钥或助记符中获取 account
对象。 account
对象有两个属性:address
和privateKey
。
const privateKey = '<private-key>';
const account = eightPay.accounts.fromPrivateKey(privateKey);
// OR
const mnemonic = '<mnemonic>';
const mnemonicIndex = 0;
const account = eightPay.accounts.fromMnemonic(mnemonic, mnemonicIndex);
console.log(account);
/*
{
address: '0x2F2....',
privateKey: '0x1Ee...'
}
*/
Utils
为了帮助解析来自和来自以太坊小数的金额,您可以使用 parseAmount
和 formatAmount
函数。
const parsedAmount = eightPay.utils.parseAmount('1', '8PAY') // 1000000000000000000
const amount = eightPay.utils.formatAmount(parsedAmount, '8PAY') // 1
@8pay/sdk
The SDK provides a way to programmatically interact with 8Pay's smart contracts along with some helper functions to simplify the integration process.
Requirements
- web3 (1.0.0 or higher)
Installation
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js.
Installation is done using the npm install
command:
$ npm install @8pay/sdk
Usage
This module uses web3 under the hood, which is one of the most popular libraries used to interact with the Web 3.0
.
To keep things familiar with web3
, it follows the same structure when sending transactions and also adds additional functionalities.
To get started, create an instance of the 8pay's SDK and choose the network on which you want to operate:
const Web3 = require('web3');
const EightPay = require('@8pay/sdk');
const web3 = new Web3('<provider-url>');
const eightPay = new EightPay(web3, EightPay.Network.BSC);
Sending transactions
There are two ways to sign a transaction before broadcasting it to the blockchain:
- With an unlocked account (e.g. when using Metamask)
eightPay.fixedRecurring.bill(planId, subscriptionIds)
.send({ from: account })
- With private key
eightPay.fixedRecurring.bill(planId, subscriptionIds)
.send({ privateKey: '0x32df7......' })
Estimate gas
The gas consumed by a transaction can be estimated using the estimateGas
function which takes an options
object and returns the amount of gas required:
eightPay.fixedRecurring.bill(planId, subscriptionIds)
.estimateGas(options)
Options
The send
and estimateGas
methods accept the same options
object available in web3
which allows setting parameters like gas
, gasPrice
, nonce
and so on.
Events
The following events are emitted when sending a transaction: sending
, sent
, transactionHash
, receipt
, confirmation
and error
.
const receipt = await eightPay.fixedRecurring.bill(planId, subscriptionIds)
.send({ from: account })
.once('sending', payload => {})
.once('sent', payload => {})
.once('transactionHash', hash => {})
.once('receipt', receipt => {})
.on('confirmation', confirmation => {})
.on('error', error => {})
Fixed Recurring
Bill
Bills subscriptions of a plan.
Parameters:
- planId - id of the plan
- subscriptionIds - array of subscription ids
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const receipt = await eightPay.fixedRecurring.bill(planId, subscriptionIds)
.send({ from: account })
Terminate
Forcefully cancels subscriptions of a plan.
Note: The sender account must be the plan's admin or an operational account with terminate permission.
Parameters:
- planId - id of the plan
- subscriptionIds - array of subscription ids
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const receipt = await eightPay.fixedRecurring.terminate(planId, subscriptionIds)
.send({ from: account })
Variable Recurring
Bill
Bills subscriptions of a plan.
Note: The sender account must be the plan's admin or an operational account with bill permission.
Parameters:
- planId - id of the plan
- subscriptionIds - array of subscription ids
- amounts - array of amounts to charge for each subscription
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const amounts = [eightPay.utils.parseAmount('10', '8PAY')];
const receipt = await eightPay.variableRecurring.bill(planId, subscriptionIds, amounts)
.send({ from: account })
Terminate
Forcefully cancels subscriptions of a plan.
Note: The sender account must be the plan's admin or an operational account with terminate permission.
Parameters:
- planId - id of the plan
- subscriptionIds - array of subscription on-chain ids
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const receipt = await eightPay.variableRecurring.terminate(planId, subscriptionIds)
.send({ from: account })
On Demand
Bill
Bills subscriptions of a plan.
Note: The sender account must be the plan's admin or an operational account with bill permission.
Parameters:
- planId - id of the plan
- subscriptionIds - array of subscription ids
- amounts - array of amounts to charge for each subscription
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const amounts = [eightPay.utils.parseAmount('10', '8PAY')];
const receipt = await eightPay.onDemand.bill(planId, subscriptionIds, amounts)
.send({ from: account })
Terminate
Forcefully cancels subscriptions of a plan.
Note: The sender account must be the plan's admin or an operational account with terminate permission.
Parameters:
- planId - id of the plan
- subscriptionIds - array of subscription ids
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
const receipt = await eightPay.onDemand.terminate(planId, subscriptionIds)
.send({ from: account })
Accounts
The accounts utility can be used to obtain an account
object from a private key or a mnemonic. The account
object has two properties: address
and privateKey
.
const privateKey = '<private-key>';
const account = eightPay.accounts.fromPrivateKey(privateKey);
// OR
const mnemonic = '<mnemonic>';
const mnemonicIndex = 0;
const account = eightPay.accounts.fromMnemonic(mnemonic, mnemonicIndex);
console.log(account);
/*
{
address: '0x2F2....',
privateKey: '0x1Ee...'
}
*/
Utils
To help in parsing amount from and to ethereum decimals, you can use the parseAmount
and formatAmount
functions.
const parsedAmount = eightPay.utils.parseAmount('1', '8PAY') // 1000000000000000000
const amount = eightPay.utils.formatAmount(parsedAmount, '8PAY') // 1