Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: payment processor batch payment native token #1213

Merged
merged 16 commits into from
Oct 27, 2023
Merged
6 changes: 3 additions & 3 deletions packages/currency/src/aggregators/private.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"0x775eb53d00dd0acd3ec1696472105d579b9b386b": {
"0x38cf23c52bb4b13f051aec09580a2de845a7fa35": 1,
"0x17b4158805772ced11225e77339f90beb5aae968": 1,
"0xf5af88e117747e87fc5929f2ff87221b1447652e": 1
"0xa65ded58a0afee8241e788c5115ca53ef3925fd2": 1
},
"0x17b4158805772ced11225e77339f90beb5aae968": {
"0x775eb53d00dd0acd3ec1696472105d579b9b386b": 1
},
"0xf5af88e117747e87fc5929f2ff87221b1447652e": {
"0xa65ded58a0afee8241e788c5115ca53ef3925fd2": {
"0x775eb53d00dd0acd3ec1696472105d579b9b386b": 1,
"0x8acee021a27779d8e98b9650722676b850b25e11": 1
},
"0x8acee021a27779d8e98b9650722676b850b25e11": {
"0xf5af88e117747e87fc5929f2ff87221b1447652e": 1
"0xa65ded58a0afee8241e788c5115ca53ef3925fd2": 1
}
}
6 changes: 6 additions & 0 deletions packages/currency/src/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ type NativeBtcCurrency = NamedNativeCurrency & { network: CurrencyTypes.BtcChain
export const nativeCurrencies: Record<RequestLogicTypes.CURRENCY.ETH, NativeEthCurrency[]> &
Record<RequestLogicTypes.CURRENCY.BTC, NativeBtcCurrency[]> = {
[RequestLogicTypes.CURRENCY.ETH]: [
{
symbol: 'ETH-private',
decimals: 18,
name: 'Ether',
network: 'private',
},
{
symbol: 'ETH',
decimals: 18,
Expand Down
15 changes: 3 additions & 12 deletions packages/payment-processor/src/payment/any-to-erc20-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,9 @@ export function encodePayAnyToErc20ProxyRequest(
* @param amount Optionally, the amount to pay. Defaults to remaining amount of the request.
* @param feeAmountOverride Optionally, the fee amount to pay. Defaults to the fee amount of the request.
*/
export function checkRequestAndGetPathAndCurrency(
export function getConversionPathForErc20Request(
request: ClientTypes.IRequestData,
paymentSettings: IConversionPaymentSettings,
amount?: BigNumberish,
feeAmountOverride?: BigNumberish,
): { path: string[]; requestCurrency: CurrencyDefinition<unknown> } {
if (!paymentSettings.currency) {
throw new Error('currency must be provided in the paymentSettings');
Expand Down Expand Up @@ -130,9 +128,6 @@ export function checkRequestAndGetPathAndCurrency(
`Impossible to find a conversion path between from ${requestCurrency.symbol} (${requestCurrency.hash}) to ${paymentCurrency.symbol} (${paymentCurrency.hash})`,
);
}

// Check request
validateConversionFeeProxyRequest(request, path, amount, feeAmountOverride);
return { path, requestCurrency };
}

Expand All @@ -157,12 +152,8 @@ function prepareAnyToErc20Arguments(
amountToPay: BigNumber;
feeToPay: BigNumber;
} {
const { path, requestCurrency } = checkRequestAndGetPathAndCurrency(
request,
paymentSettings,
amount,
feeAmountOverride,
);
const { path, requestCurrency } = getConversionPathForErc20Request(request, paymentSettings);
validateConversionFeeProxyRequest(request, path, amount, feeAmountOverride);

const { paymentReference, paymentAddress, feeAddress, feeAmount, maxRateTimespan } =
getRequestPaymentValues(request);
Expand Down
79 changes: 47 additions & 32 deletions packages/payment-processor/src/payment/any-to-eth-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { constants, ContractTransaction, Signer, providers, BigNumberish, BigNumber } from 'ethers';

import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';
import {
CurrencyDefinition,
CurrencyManager,
UnsupportedCurrencyError,
} from '@requestnetwork/currency';
import { AnyToEthFeeProxyPaymentDetector } from '@requestnetwork/payment-detection';
import { EthConversionProxy__factory } from '@requestnetwork/smart-contracts/types';
import { ClientTypes, RequestLogicTypes } from '@requestnetwork/types';
Expand Down Expand Up @@ -54,40 +58,11 @@ export function encodePayAnyToEthProxyRequest(
amount?: BigNumberish,
feeAmountOverride?: BigNumberish,
): string {
const currencyManager = paymentSettings.currencyManager || CurrencyManager.getDefault();

if (!request.currencyInfo) {
throw new Error(`currency not specified`);
}
const { path, requestCurrency } = getConversionPathForEthRequest(request, paymentSettings);

const requestCurrency = currencyManager.fromStorageCurrency(request.currencyInfo);
if (!requestCurrency) {
throw new UnsupportedCurrencyError(request.currencyInfo);
}

const { paymentReference, paymentAddress, feeAddress, feeAmount, maxRateTimespan, network } =
const { paymentReference, paymentAddress, feeAddress, feeAmount, maxRateTimespan } =
getRequestPaymentValues(request);

if (!network) {
throw new Error(`missing network`);
}

const paymentCurrency = currencyManager.getNativeCurrency(
RequestLogicTypes.CURRENCY.ETH,
network,
);
if (!paymentCurrency) {
throw new UnsupportedCurrencyError({ value: 'ETH', network });
}

// Compute the path automatically
const path = currencyManager.getConversionPath(requestCurrency, paymentCurrency, network);
if (!path) {
throw new Error(
`Impossible to find a conversion path between from ${requestCurrency.symbol} (${requestCurrency.hash}) to ${paymentCurrency.symbol} (${paymentCurrency.hash})`,
);
}

const amountToPay = padAmountForChainlink(getAmountToPay(request, amount), requestCurrency);
const feeToPay = padAmountForChainlink(feeAmountOverride || feeAmount || 0, requestCurrency);

Expand Down Expand Up @@ -125,3 +100,43 @@ export function prepareAnyToEthProxyPaymentTransaction(
value: BigNumber.from(paymentSettings.maxToSpend),
};
}

export function getConversionPathForEthRequest(
request: ClientTypes.IRequestData,
paymentSettings: IConversionPaymentSettings,
): { path: string[]; requestCurrency: CurrencyDefinition<unknown> } {
const currencyManager = paymentSettings.currencyManager || CurrencyManager.getDefault();

if (!request.currencyInfo) {
throw new Error(`currency not specified`);
}

const requestCurrency = currencyManager.fromStorageCurrency(request.currencyInfo);
if (!requestCurrency) {
throw new UnsupportedCurrencyError(request.currencyInfo);
}

const { network } = getRequestPaymentValues(request);

if (!network) {
throw new Error(`missing network`);
}

const paymentCurrency = currencyManager.getNativeCurrency(
RequestLogicTypes.CURRENCY.ETH,
network,
);
if (!paymentCurrency) {
throw new UnsupportedCurrencyError({ value: 'ETH', network });
}

// Compute the path automatically
const path = currencyManager.getConversionPath(requestCurrency, paymentCurrency, network);
if (!path) {
throw new Error(
`Impossible to find a conversion path between from ${requestCurrency.symbol} (${requestCurrency.hash}) to ${paymentCurrency.symbol} (${paymentCurrency.hash})`,
);
}

return { path, requestCurrency };
}
Loading
Loading