Skip to content

Commit

Permalink
docs: Remove payment section and add getBalance() for payment network…
Browse files Browse the repository at this point in the history
…s documentation (#171)
  • Loading branch information
lumtis authored Mar 13, 2020
1 parent 3cd958c commit e08aa71
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,35 +104,6 @@ const proxyContractCreateParams = {
console.log(`Request created with erc20 proxy contract payment network: ${request.requestId}`);
})();

/**
* ### Paying an erc20 proxy contract request with Request payment processor
*
* Requests using ERC20 proxy contract payment network can be paid with the Request payment processor, in order to simplify interactions with the involved smart contracts
*/

// Import necessary packages
import { hasSufficientFunds, payRequest } from '@requestnetwork/payment-processor';
import { Wallet } from 'ethers';

// Create a wallet for the payer for demo purpose
const wallet = Wallet.createRandom();

(async () => {
const request = await requestNetwork.createRequest(proxyContractCreateParams);

// Check the payer has sufficient fund for the data
const payerAddress = wallet.address;
const requestData = request.getData();
if (!(await hasSufficientFunds(requestData, payerAddress))) {
throw new Error('You do not have enough funds to pay this request');
}

// Pay the request
// The value provided for wallet can be a Web3Provider from 'ethers/providers' to be able to pay with Metamask
const tx = await payRequest(requestData, wallet);
await tx.wait(1);
})();

/**
* ## Request creation with address based payment network
*
Expand Down Expand Up @@ -161,8 +132,29 @@ const addressBasedCreateParams = {
})();

/**
* ### Paying an erc20 address based request
* ## Checking the balance of an erc20 request
*
* Requests created with the address based payment network must be manually paid by sending an erc20 transfer to the payment address.
* You can do this by calling `transfer(to, amount)` method of the erc20 token. `to` is the payment address and `amount` the amount the payer wants to pay.
* The function getData() of a request provides its balance
* This function is available independently of the payment network used
*/

// Import Big Number package
const BN = require('bn.js')

(async () => {
// Use a proxy contract request for example
const request = await requestNetwork.createRequest(proxyContractCreateParams);

// Check the balance of the request
const requestData = request.getData();
const balance = requestData.balance;
console.log(`Balance of the erc20 proxy contract request: ${balance}`);

// Check if the request has been paid
// Convert the balance to big number type for comparison
const expectedAmount = new BN(requestData.expectedAmount);
const balanceBigNumber = new BN(balance);

// Check if balanceBigNumber is greater or equal to expectedAmount
const paid = balanceBigNumber.gte(expectedAmount);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,27 @@ const ethInputDataCreateParams = {
})();

/**
* ### Paying an ether request with Request payment processor
* ## Checking balance
*
* Requests using ether payment network can be paid with the Request payment processor
* The function getData() of a request provides its balance
*/

// Import necessary packages
import { hasSufficientFunds, payRequest } from '@requestnetwork/payment-processor';
import { Wallet } from 'ethers';

// Create a wallet for the payer for demo purpose
const wallet = Wallet.createRandom();

(async () => {
const request = await requestNetwork.createRequest(ethInputDataCreateParams);

// Check the payer has sufficient fund for the data
const payerAddress = wallet.address;
const requestData = request.getData();
if (!(await hasSufficientFunds(requestData, payerAddress))) {
throw new Error('You do not have enough funds to pay this request');
}

// Pay the request
// The value provided for wallet can be a Web3Provider from 'ethers/providers' to be able to pay with Metamask
const tx = await payRequest(requestData, wallet);
await tx.wait(1);
})();
// Import Big Number package
const BN = require('bn.js')

(async () => {
const request = await requestNetwork.createRequest(ethInputDataCreateParams);

// Check the balance of the request
const requestData = request.getData();
const balance = requestData.balance;
console.log(`Balance of the ether input data request: ${balance}`);

// Check if the request has been paid
// Convert the balance to big number type for comparison
const expectedAmount = new BN(requestData.expectedAmount);
const balanceBigNumber = new BN(balance);

// Check if balanceBigNumber is greater or equal to expectedAmount
const paid = balanceBigNumber.gte(expectedAmount);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,28 @@ const bitcoinAddressBasedCreateParams = {
console.log(`Request created with Bitcoin payment network: ${request.requestId}`);
})();

/**
* ### Paying a Bitcoin request
/**
* ## Checking balance
*
* The Request payment processor package doesn't support the payment of Bitcoin request.
* The Bitcoin payment network is of type "Address Based", therefore, the payer can simply pay the request by sending the right amount of BTC to the payment address.
* The function getData() of a request provides its balance
*/

// Import Big Number package
const BN = require('bn.js')

(async () => {
const request = await requestNetwork.createRequest(bitcoinAddressBasedCreateParams);

// Check the balance of the request
const requestData = request.getData();
const balance = requestData.balance;
console.log(`Balance of the bitcoin address based request: ${balance}`);

// Check if the request has been paid
// Convert the balance to big number type for comparison
const expectedAmount = new BN(requestData.expectedAmount);
const balanceBigNumber = new BN(balance);

// Check if balanceBigNumber is greater or equal to expectedAmount
const paid = balanceBigNumber.gte(expectedAmount);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ const declarativeCreateParams = {
})();

/**
* ### Declaring sent and received payments
* ## Declaring sent and received payments and checking balance
*
* The Declarative payment network doesn't provide payment detection method to determine the balance of the request
* The balance of the request is defined by the declared payments by the payee and the declared refunds by the payer
*/

// Import Big Number package
const BN = require('bn.js')

(async () => {
const request = await requestNetwork.createRequest(declarativeCreateParams);

Expand All @@ -133,5 +136,17 @@ const declarativeCreateParams = {

// Declaring a sent refund, this amount is not taken into account for the request balance
request.declareSentPayment('900', 'received too much', payeeIdentity);
})();

// Check the balance of the request
const requestData = request.getData();
const balance = requestData.balance;
console.log(`Balance of the declarative request: ${balance}`);

// Check if the request has been paid
// Convert the balance to big number type for comparison
const expectedAmount = new BN(requestData.expectedAmount);
const balanceBigNumber = new BN(balance);

// Check if balanceBigNumber is greater or equal to expectedAmount
const paid = balanceBigNumber.gte(expectedAmount);
})();
1 change: 1 addition & 0 deletions packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@requestnetwork/types": "0.12.0",
"@requestnetwork/payment-processor": "0.13.0",
"@requestnetwork/smart-contracts": "0.4.0",
"bn.js": "5.1.1",
"classnames": "2.2.6",
"core-js": "3.6.4",
"ethers": "^4.0.45",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3644,6 +3644,11 @@ bn.js@4.11.8, bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.11.0, bn.js@^4.
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
integrity sha512-ItfYfPLkWHUjckQCk8xC+LwxgK8NYcXywGigJgSwOP8Y2iyWT4f2vsZnoOXTTbo+o5yXmIUJ4gn5538SO5S3gA==

bn.js@^5.1.1:
version "5.1.1"
resolved "http://localhost:4873/bn.js/-/bn.js-5.1.1.tgz#48efc4031a9c4041b9c99c6941d903463ab62eb5"
integrity sha512-IUTD/REb78Z2eodka1QZyyEk66pciRcP6Sroka0aI3tG/iwIdYLrBD62RsubR7vqdt3WyX8p4jxeatzmRSphtA==

body-parser@1.19.0, body-parser@^1.16.0:
version "1.19.0"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
Expand Down

0 comments on commit e08aa71

Please sign in to comment.