Skip to content

Commit

Permalink
chore: new package for payment detection (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjlevesque committed Feb 10, 2020
1 parent 9ab725d commit 7f2e5c2
Show file tree
Hide file tree
Showing 57 changed files with 242 additions and 56 deletions.
29 changes: 29 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,30 @@ jobs:
root: *working_directory
paths:
- packages/multi-format/coverage/
test-payment-detection:
docker:
- *node_image
- *ganache_image
working_directory: *working_directory
steps:
- attach_workspace:
at: *working_directory
- run:
name: 'Build payment-detection'
command: 'yarn workspace @requestnetwork/payment-detection run build'
- run:
name: 'Lint payment-detection'
command: 'yarn workspace @requestnetwork/payment-detection run lint'
- run:
name: 'Deploy test contracts from smart-contracts'
command: 'yarn workspace @requestnetwork/smart-contracts run deploy'
- run:
name: 'Test payment-detection'
command: 'yarn workspace @requestnetwork/payment-detection run test'
- persist_to_workspace:
root: *working_directory
paths:
- packages/payment-detection/coverage/
publish-coverage:
docker:
- *node_image
Expand Down Expand Up @@ -590,6 +614,9 @@ workflows:
- test-prototype-estimator:
requires:
- build
- test-payment-detection:
requires:
- build

# Publish the code coverage
- publish-coverage:
Expand All @@ -607,6 +634,7 @@ workflows:
- test-request-node
- test-utils
- test-multi-format
- test-payment-detection

# Release a next version package everytime we merge to master
- next-release:
Expand All @@ -627,6 +655,7 @@ workflows:
- test-types
- test-utils
- test-web3-signature
- test-payment-detection
context: protocol-release
filters:
branches:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"scripts": {
"build": "lerna run build",
"clean": "lerna run clean",
"build:tsc": "tsc -b packages/advanced-logic packages/data-access packages/data-format packages/epk-decryption packages/epk-signature packages/ethereum-storage packages/integration-test packages/multi-format packages/prototype-estimator packages/request-client.js packages/request-logic packages/request-node packages/smart-contracts packages/toolbox packages/transaction-manager packages/types packages/usage-examples packages/utils packages/web3-signature",
"build:tsc": "tsc -b packages/advanced-logic packages/data-access packages/data-format packages/epk-decryption packages/epk-signature packages/ethereum-storage packages/integration-test packages/multi-format packages/payment-detection packages/prototype-estimator packages/request-client.js packages/request-logic packages/request-node packages/smart-contracts packages/toolbox packages/transaction-manager packages/types packages/usage-examples packages/utils packages/web3-signature",
"lint": "lerna run lint",
"lint-staged": "lerna run lint-staged",
"lerna": "lerna",
Expand Down
1 change: 1 addition & 0 deletions packages/integration-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@requestnetwork/epk-signature": "0.5.7",
"@requestnetwork/ethereum-storage": "0.4.8",
"@requestnetwork/multi-format": "0.2.4",
"@requestnetwork/payment-detection": "0.12.0",
"@requestnetwork/request-client.js": "0.12.0",
"@requestnetwork/request-logic": "0.8.3",
"@requestnetwork/transaction-manager": "0.8.4",
Expand Down
7 changes: 4 additions & 3 deletions packages/integration-test/test/scheduled/erc20.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// tslint:disable: no-magic-numbers
// tslint:disable: no-invalid-this
import ERC20AddressedBased from '@requestnetwork/request-client.js/src/api/payment-network/erc20/address-based';
import ERC20AddressBasedInfoRetriever from '@requestnetwork/request-client.js/src/api/payment-network/erc20/address-based-info-retriever';
import { Erc20PaymentNetwork } from '@requestnetwork/payment-detection';
import ERC20AddressBasedInfoRetriever from '@requestnetwork/payment-detection/src/erc20/address-based-info-retriever';

import {
AdvancedLogicTypes,
ExtensionTypes,
Expand Down Expand Up @@ -59,7 +60,7 @@ describe('ERC20 detection test-suite', () => {
});

it('can getBalance on a mainnet request', async () => {
const erc20AddressedBased = new ERC20AddressedBased({
const erc20AddressedBased = new Erc20PaymentNetwork.AddressBased({
advancedLogic: mockAdvancedLogic,
});
const mockRequest = {
Expand Down
3 changes: 3 additions & 0 deletions packages/payment-detection/.lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"src/**/*.ts": ["tslint --project . --fix", "prettier --single-quote --write", "git add"]
}
22 changes: 22 additions & 0 deletions packages/payment-detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# @requestnetwork/payment-detection

`@requestnetwork/payment-detection` is a typescript library part of the [Request Network protocol](https://github.com/RequestNetwork/requestNetwork).
It contains client-side payment detection for all supported payment networks.

### Payment and Refund detections

If a payment network has been given to the request, the payment detection can be done.

From the information provided in payment network, the library will feed the property `balance` of the request with:

- `balance`: the sum of the amount of all payments minus the sum of amount of all refunds
- `events`: all the payments and refunds events with the amount, timestamp etc...

The payment networks available are:

- `Types.Payment.PAYMENT_NETWORK_ID.BITCOIN_ADDRESS_BASED` ('pn-bitcoin-address-based'): handle Bitcoin payments associated to a BTC address to the request, every transaction hitting this address will be consider as a payment. Optionally, the payer can provide a BTC address for the refunds. Note that **the addresses must be used only for one and only one request** otherwise one transaction will be considered as a payment for more than one request. (see [the specification](https://github.com/RequestNetwork/requestNetwork/blob/master/packages/advanced-logic/specs/payment-network-btc-address-based-0.1.0-DRAFT.md))
- `Types.Payment.PAYMENT_NETWORK_ID.TESTNET_BITCOIN_ADDRESS_BASED` ('pn-testnet-bitcoin-address-based'): Same as previous but for the bitcoin testnet (for test purpose)
- `Types.Payment.PAYMENT_NETWORK_ID.ERC20_ADDRESS_BASED`('pn-erc20-address-based'): Same as `BITCOIN_ADDRESS_BASED`, for ERC20 payments.
- `Types.Payment.PAYMENT_NETWORK_ID.ERC20_PROXY_CONTRACT`('pn-erc20-proxy-contract'): uses an intermediary contract to document which request is being paid, through the `PaymentReference`. (see [the specification](https://github.com/RequestNetwork/requestNetwork/blob/master/packages/advanced-logic/specs/payment-network-erc20-address-based-0.1.0-DRAFT.md))
- `Types.Payment.PAYMENT_NETWORK_ID.ETH_INPUT_DATA`('pn-eth-input-data'): uses the transaction input data to pass the `PaymentReference`. (see [the specification](https://github.com/RequestNetwork/requestNetwork/blob/master/packages/advanced-logic/specs/payment-network-eth-input-data-0.1.0-DRAFT.md))
- `Types.Payment.PAYMENT_NETWORK_ID.DECLARATIVE`('pn-any-declarative'): a manual alternative, where payer can declare a payment sent, and payee can declare it received, working for any currency. (see [the specification](https://github.com/RequestNetwork/requestNetwork/blob/master/packages/advanced-logic/specs/payment-network-any-declarative-0.1.0-DRAFT.md))
72 changes: 72 additions & 0 deletions packages/payment-detection/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "@requestnetwork/payment-detection",
"version": "0.12.0",
"publishConfig": {
"access": "public"
},
"description": "Payment detection using ethers.",
"keywords": [
"requestnetwork"
],
"repository": {
"type": "git",
"url": "git+https://github.com/RequestNetwork/requestNetwork.git"
},
"homepage": "https://github.com/RequestNetwork/requestNetwork/tree/master/packages/payment-detection#readme",
"bugs": {
"url": "https://github.com/RequestNetwork/requestNetwork/issues"
},
"license": "MIT",
"engines": {
"node": ">=8.0.0"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"directories": {
"lib": "src",
"test": "test"
},
"files": [
"dist"
],
"scripts": {
"build": "tsc -b",
"clean": "shx rm -rf dist tsconfig.tsbuildinfo",
"lint": "tslint --project . && eslint \"src/**/*.ts\"",
"lint-staged": "lint-staged",
"prepare": "yarn run build",
"test": "nyc mocha --extension ts --require ts-node/register --require source-map-support/register \"test/**/*.ts\"",
"test:watch": "yarn test --watch"
},
"dependencies": {
"@requestnetwork/types": "0.11.0",
"@requestnetwork/utils": "0.10.0",
"axios": "0.19.0",
"bn.js": "4.11.8",
"ethers": "4.0.43",
"node-fetch": "2.6.0",
"satoshi-bitcoin": "1.0.4"
},
"devDependencies": {
"@types/bn.js": "4.11.5",
"@types/chai": "4.1.7",
"@types/mocha": "5.2.7",
"@types/sinon": "7.5.0",
"@typescript-eslint/parser": "1.2.0",
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"chai-spies": "1.0.0",
"eslint": "5.13.0",
"eslint-plugin-spellcheck": "0.0.14",
"eslint-plugin-typescript": "0.14.0",
"lint-staged": "8.1.3",
"mocha": "6.2.2",
"nyc": "15.0.0",
"prettier": "1.16.4",
"shx": "0.3.2",
"sinon": "7.5.0",
"source-map-support": "0.5.13",
"tslint": "5.12.1",
"typescript": "3.7.2"
}
}
23 changes: 23 additions & 0 deletions packages/payment-detection/src/btc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import BtcMainnetPaymentNetwork from './mainnet-address-based';
import BtcTestnetPaymentNetwork from './testnet-address-based';

import DefaultBitcoinDetectionProvider from './default-bitcoin-detection-provider';

import BlockchainInfoProvider from './default-providers/blockchain-info';
import BlockcypherComProvider from './default-providers/blockcypher-com';
import BlockStreamInfoProvider from './default-providers/blockstream-info';
import ChainSoProvider from './default-providers/chain-so';

const providers = {
BlockStreamInfoProvider,
BlockchainInfoProvider,
BlockcypherComProvider,
ChainSoProvider,
};

export {
DefaultBitcoinDetectionProvider,
BtcMainnetPaymentNetwork,
BtcTestnetPaymentNetwork,
providers as Providers,
};
4 changes: 4 additions & 0 deletions packages/payment-detection/src/erc20/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import AddressBased from './address-based';
import ProxyContract from './proxy-contract';

export { AddressBased, ProxyContract };
3 changes: 3 additions & 0 deletions packages/payment-detection/src/eth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import InputData from './input-data';

export { InputData };
16 changes: 16 additions & 0 deletions packages/payment-detection/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import PaymentNetworkFactory from './payment-network-factory';
import PaymentReferenceCalculator from './payment-reference-calculator';

import * as BtcPaymentNetwork from './btc';
import DeclarativePaymentNetwork from './declarative';
import * as Erc20PaymentNetwork from './erc20';
import * as EthPaymentNetwork from './eth';

export {
PaymentNetworkFactory,
PaymentReferenceCalculator,
BtcPaymentNetwork,
DeclarativePaymentNetwork,
Erc20PaymentNetwork,
EthPaymentNetwork,
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,35 @@ import EthInputData from './eth/input-data';
const supportedPaymentNetwork: PaymentTypes.ISupportedPaymentNetworkByCurrency = {
BTC: {
mainnet: {
[ExtensionTypes.ID.PAYMENT_NETWORK_BITCOIN_ADDRESS_BASED as string]: BTCAddressedBased,
[ExtensionTypes.ID.PAYMENT_NETWORK_BITCOIN_ADDRESS_BASED]: BTCAddressedBased,
},
testnet: {
[ExtensionTypes.ID
.PAYMENT_NETWORK_TESTNET_BITCOIN_ADDRESS_BASED as string]: TestnetBTCAddressedBased,
[ExtensionTypes.ID.PAYMENT_NETWORK_TESTNET_BITCOIN_ADDRESS_BASED]: TestnetBTCAddressedBased,
},
},
ERC20: {
mainnet: {
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_ADDRESS_BASED as string]: ERC20AddressBased,
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_PROXY_CONTRACT as string]: ERC20ProxyContract,
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_ADDRESS_BASED]: ERC20AddressBased,
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_PROXY_CONTRACT]: ERC20ProxyContract,
},
private: {
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_ADDRESS_BASED as string]: ERC20AddressBased,
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_PROXY_CONTRACT as string]: ERC20ProxyContract,
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_ADDRESS_BASED]: ERC20AddressBased,
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_PROXY_CONTRACT]: ERC20ProxyContract,
},
rinkeby: {
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_ADDRESS_BASED as string]: ERC20AddressBased,
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_PROXY_CONTRACT as string]: ERC20ProxyContract,
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_ADDRESS_BASED]: ERC20AddressBased,
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_PROXY_CONTRACT]: ERC20ProxyContract,
},
},
ETH: {
mainnet: {
[ExtensionTypes.ID.PAYMENT_NETWORK_ETH_INPUT_DATA as string]: EthInputData,
[ExtensionTypes.ID.PAYMENT_NETWORK_ETH_INPUT_DATA]: EthInputData,
},
private: {
[ExtensionTypes.ID.PAYMENT_NETWORK_ETH_INPUT_DATA as string]: EthInputData,
[ExtensionTypes.ID.PAYMENT_NETWORK_ETH_INPUT_DATA]: EthInputData,
},
rinkeby: {
[ExtensionTypes.ID.PAYMENT_NETWORK_ETH_INPUT_DATA as string]: EthInputData,
[ExtensionTypes.ID.PAYMENT_NETWORK_ETH_INPUT_DATA]: EthInputData,
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ chai.use(chaiAsPromised);

import { PaymentTypes } from '@requestnetwork/types';

import DefaultBitcoinDetectionProvider from '../../../../src/api/payment-network/btc/default-bitcoin-detection-provider';
import DefaultBitcoinDetectionProvider from '../../src/btc/default-bitcoin-detection-provider';

const btcProviderMock0: PaymentTypes.IBitcoinDetectionProvider = {
getAddressBalanceWithEvents: async (): Promise<PaymentTypes.BTCBalanceWithEvents> => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PaymentTypes } from '@requestnetwork/types';
import BlockchainInfo from '../../../../../src/api/payment-network/btc/default-providers/blockchain-info';
import BlockchainInfo from '../../../src/btc/default-providers/blockchain-info';

import * as BlockchainInfoData from './blockchain-info-data';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PaymentTypes } from '@requestnetwork/types';
import BlockCypherCom from '../../../../../src/api/payment-network/btc/default-providers/blockcypher-com';
import BlockCypherCom from '../../../src/btc/default-providers/blockcypher-com';

import * as BlockCypherComData from './blockcypher-com-data';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PaymentTypes } from '@requestnetwork/types';
import Blockstream from '../../../../../src/api/payment-network/btc/default-providers/blockstream-info';

import Blockstream from '../../../src/btc/default-providers/blockstream-info';

import * as BlockstreamData from './blockstream-info-data';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PaymentTypes } from '@requestnetwork/types';
import ChainSo from '../../../../../src/api/payment-network/btc/default-providers/chain-so';

import ChainSo from '../../../src/btc/default-providers/chain-so';

import * as ChainSoData from './chain-so-data';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AdvancedLogicTypes } from '@requestnetwork/types';

import BTCAddressedBased from '../../../../src/api/payment-network/btc/mainnet-address-based';
import BTCAddressedBased from '../../src/btc/mainnet-address-based';

import 'chai';
import 'mocha';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AdvancedLogicTypes } from '@requestnetwork/types';

import AddressBasedTestnetBtc from '../../../../src/api/payment-network/btc/testnet-address-based';
import AddressBasedTestnetBtc from '../../src/btc/testnet-address-based';

import 'chai';
import 'mocha';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {
AdvancedLogicTypes,
ExtensionTypes,
IdentityTypes,
PaymentTypes,
RequestLogicTypes,
} from '@requestnetwork/types';

import Declarative from '../../../src/api/payment-network/declarative';

import * as Types from '../../../src/types';
import Declarative from '../src/declarative';

import 'chai';
import 'mocha';
Expand Down Expand Up @@ -55,7 +54,7 @@ const mockAdvancedLogic: AdvancedLogicTypes.IAdvancedLogic = {

const requestMock: RequestLogicTypes.IRequest = {
creator: {
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: '',
},
currency: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// tslint:disable: no-invalid-this
// tslint:disable: no-magic-numbers
import { PaymentTypes } from '@requestnetwork/types';
import ERC20InfoRetriever from '../../../../src/api/payment-network/erc20/address-based-info-retriever';
import ERC20InfoRetriever from '../../src/erc20/address-based-info-retriever';

import 'chai';
import 'mocha';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
PaymentTypes,
RequestLogicTypes,
} from '@requestnetwork/types';
import ERC20AddressedBased from '../../../../src/api/payment-network/erc20/address-based';
import ERC20AddressedBased from '../../src/erc20/address-based';

import 'chai';
import 'mocha';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AdvancedLogicTypes } from '@requestnetwork/types';
import ERC20ProxyContract from '../../../../src/api/payment-network/erc20/proxy-contract';
import ERC20ProxyContract from '../../src/erc20/proxy-contract';

import * as chai from 'chai';
import * as spies from 'chai-spies';
Expand Down
Loading

0 comments on commit 7f2e5c2

Please sign in to comment.