Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into erc20SwapToPay_with_fees
- Loading branch information
Showing
27 changed files
with
408 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ const supportedNetworks = [ | |
'fantom', | ||
'bsctest', | ||
'bsc', | ||
'arbitrum-rinkeby', | ||
]; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,65 @@ | ||
# @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. | ||
It contains the implementation of request-related events interpretations, typically onchain payments of requests. | ||
|
||
### Payment and Refund detections | ||
The interpretation of events is specified by the payment extension added to the request. [Cf. advanced-logic specifications](../advanced-logic/specs/). | ||
|
||
# Balance and events | ||
|
||
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: | ||
Based on information found in the payment network state, and included manual payment declarations, the library will perform queries and feed the property `balance` of the request with: | ||
|
||
- `balance`: the detected amount paid on the request, in request currency | ||
- `events`: all the payments, payment declarations, refunds, and other balance-related events with the amount, timestamp etc... | ||
|
||
# Retrievers and detectors | ||
|
||
This library relies on two concepts: | ||
|
||
- **Retrievers** perform RPC or TheGraph calls and fetch relevant events. Balance-impacting events are fetched with `InfoRetrievers`, implementing the `getTransferEvents()` method (cf. [IPaymentRetriever](./src/types.ts)) | ||
- **Payment detectors** implement the interface **PaymentTypes.IPaymentNetwork**, with the method `getBalance()`, which calls retrievers and interpret events according to the payment network (cf. [Abstract PaymentDetectorBase](./src/payment-detector-base.ts)). `getBalance()` returns the balance as well as events: payments, refunds, and possibly other events (declarations, escrow events...) | ||
|
||
## PaymentDetectorBase | ||
|
||
A good part of the logic is implemented in the abstract class `PaymentDetectorBase`: | ||
|
||
```typescript | ||
export abstract class PaymentDetectorBase< | ||
TExtension extends ExtensionTypes.IExtension, | ||
TPaymentEventParameters | ||
> implements PaymentTypes.IPaymentNetwork<TPaymentEventParameters> { | ||
public async getBalance( | ||
request: RequestLogicTypes.IRequest, | ||
): Promise<PaymentTypes.IBalanceWithEvents<TPaymentEventParameters>> { | ||
// ... | ||
|
||
// getEvents() should be implemented by children payment detectors, and use appropriate retrievers | ||
// For example: RPC or The Graph based retriever | ||
const rawEvents = await this.getEvents(request); | ||
// ... | ||
// computeBalance() sums up all payment events and deduces all refunds. | ||
const balance = this.computeBalance(events).toString(); | ||
|
||
return { | ||
balance, | ||
events, | ||
}; | ||
} | ||
} | ||
``` | ||
|
||
[cf. full implementation](./src/payment-detector-base.ts) | ||
|
||
## Subgraph-based payment retrievers | ||
|
||
For TheGraph-based information retrieval, a client can be retrieved using `getTheGraphClient()` in `./src/thegraph/index.ts`. It provides a strongly typed interface, generated based on the queries in `/src/thegraph/queries`. | ||
|
||
- `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 automated type generation is configured within files `./codegen.yml` (for EVM chains) and `./codegen-near.yml` (for Near) and output in `./src/thegraph/generated`. It depends on the deployed subgraphes schema and on the queries. | ||
|
||
The payment networks available are: | ||
The code generation is included in the pre-build script and can be run manually: | ||
|
||
- `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.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.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.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.md)) | ||
``` | ||
yarn codegen | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { PaymentTypes } from '@requestnetwork/types'; | ||
|
||
/** Generic info retriever interface */ | ||
export interface IPaymentRetriever< | ||
TPaymentNetworkEvent extends PaymentTypes.IPaymentNetworkEvent<unknown, TEventNames>, | ||
TEventNames = PaymentTypes.EVENTS_NAMES | ||
> { | ||
getTransferEvents(): Promise<TPaymentNetworkEvent[]>; | ||
} | ||
|
||
/** Generic info retriever interface without transfers */ | ||
export interface IEventRetriever< | ||
TPaymentNetworkEvent extends PaymentTypes.IPaymentNetworkBaseEvent<TEventNames>, | ||
TEventNames = PaymentTypes.EVENTS_NAMES | ||
> { | ||
getContractEvents(): Promise<TPaymentNetworkEvent[]>; | ||
} | ||
|
||
/** Object interface to list the payment network module by id */ | ||
export interface IPaymentNetworkModuleByType { | ||
[type: string]: any; | ||
} | ||
|
||
/** Object interface to list the payment network module by network */ | ||
export interface ISupportedPaymentNetworkByNetwork { | ||
[network: string]: IPaymentNetworkModuleByType; | ||
} | ||
|
||
/** Object interface to list the payment network id and its module by currency */ | ||
export interface ISupportedPaymentNetworkByCurrency { | ||
[currency: string]: ISupportedPaymentNetworkByNetwork; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.