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: conversion payments #141

Merged
merged 60 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
ff814d3
feat: add conversion pairs configuration
rodrigopavezi Sep 24, 2024
c7d3c8c
feat: create conversion payment request
rodrigopavezi Sep 25, 2024
160fefa
feat: add payments and other fixes
rodrigopavezi Sep 26, 2024
737c582
Merge branch 'main' into feat/conversion
rodrigopavezi Sep 26, 2024
4257ed6
Update shared/utils/currencyConversionPairs.ts
rodrigopavezi Sep 26, 2024
d119054
fix: typings
rodrigopavezi Sep 26, 2024
be5ba80
fix: potencial array empty issue
rodrigopavezi Sep 26, 2024
a8a77f1
fix: coderabbitai issues
rodrigopavezi Sep 26, 2024
3df13e7
Update shared/utils/currencyConversionPairs.ts
rodrigopavezi Sep 26, 2024
c936aa6
Update shared/utils/currencyConversionPairs.ts
rodrigopavezi Sep 26, 2024
e6a2bef
Update packages/create-invoice-form/src/lib/utils/prepareRequest.ts
rodrigopavezi Sep 26, 2024
ad5ba2d
Update packages/invoice-dashboard/src/utils/conversion.ts
rodrigopavezi Sep 26, 2024
678fb4c
Update packages/invoice-dashboard/src/utils/conversion.ts
rodrigopavezi Sep 26, 2024
871b408
Update shared/utils/initCurrencyManager.ts
rodrigopavezi Sep 26, 2024
728c511
fix: typings
rodrigopavezi Sep 26, 2024
0c5ac5d
Update packages/invoice-dashboard/src/utils/getConversionPaymentValue…
rodrigopavezi Sep 26, 2024
777fe4a
fix: coderabbitai mistake
rodrigopavezi Sep 26, 2024
6d2e57b
Merge branch 'feat/conversion' of https://github.com/RequestNetwork/w…
rodrigopavezi Sep 26, 2024
eac0486
fix: coderabitai issue
rodrigopavezi Sep 26, 2024
b460a35
fix: dropdown placeholders
rodrigopavezi Oct 2, 2024
4cceeea
Merge branch 'main' into feat/conversion
rodrigopavezi Oct 4, 2024
08405a2
fix: per coderabbitai review
rodrigopavezi Oct 4, 2024
02fec1b
Merge branch 'main' into feat/conversion
MantisClone Oct 22, 2024
98a86b7
Merge branch 'main' into feat/conversion
MantisClone Nov 7, 2024
c71891d
fix: build
MantisClone Nov 7, 2024
a8b5aa3
feat: add 'clean' npm script that deletes all the dist/ directories
MantisClone Nov 7, 2024
51ffdf3
Remove "Invoice Type" section from Invoice View and PDF
MantisClone Nov 7, 2024
6838eac
Trim trailing whitespace
MantisClone Nov 7, 2024
755e49d
Remove redundant loadScript definition
MantisClone Nov 7, 2024
2642d47
Improve exportToPDF parameter types
MantisClone Nov 8, 2024
70f4e16
Ensure safe access to paymentCurrencies[0] to prevent runtime errors
MantisClone Nov 8, 2024
d342f40
Revert "Remove redundant loadScript definition"
MantisClone Nov 8, 2024
4f5cb73
Remove redundant loadScript function
MantisClone Nov 8, 2024
6f6c8e8
Format document
MantisClone Nov 8, 2024
0f523a2
Remove unused `signer` parameter
MantisClone Nov 8, 2024
e150934
format file
MantisClone Nov 8, 2024
7515be6
Allow 0 discount values
MantisClone Nov 8, 2024
d4c2d74
fix return type of extractUniqueNetworkNames()
MantisClone Nov 8, 2024
6029eb7
Remove redundant optional chaining
MantisClone Nov 8, 2024
5f5c43c
Define explicit type for paymentNetworkExtension
MantisClone Nov 8, 2024
f8f386b
Trim whitespace
MantisClone Nov 8, 2024
6873909
Format document
MantisClone Nov 8, 2024
6bb683a
Streamline the checkApproval and approve functions
MantisClone Nov 8, 2024
6b85a21
Safely access paymentCurrencies[0]
MantisClone Nov 9, 2024
232709c
refactor: fully enumerate all PAYMENT_NETWORK_ID handling, else error
MantisClone Nov 9, 2024
7657f59
Reformat
MantisClone Nov 9, 2024
f32b263
Update payment network handling. Remove hardcoded "mainnet"
MantisClone Nov 9, 2024
5342712
Reformat
MantisClone Nov 9, 2024
9238561
refactor : specify a more precise type for `network`
MantisClone Nov 9, 2024
6f924b3
Handle undefined `network` in currency filtering
MantisClone Nov 9, 2024
0aeb2be
format document
MantisClone Nov 9, 2024
de54662
Ensure invoiceCurrency.hash is defined before use
MantisClone Nov 9, 2024
d4ceaed
Handle potential undefined values in currency labels
MantisClone Nov 9, 2024
db532cf
Fixes per CodeRabbit comments
MantisClone Nov 18, 2024
ad0385f
Merge branch 'main' into feat/conversion
MantisClone Nov 18, 2024
601b9ff
Fix script so it ignores node_modules directory
MantisClone Nov 18, 2024
17d3f69
Fix clean script output to not include "No such file or directory"
MantisClone Nov 18, 2024
bc3f9cb
fix: Add factors to canSubmit conditional
MantisClone Nov 19, 2024
450453d
refactor: format
MantisClone Nov 19, 2024
fee1377
Merge branch 'main' into feat/conversion
MantisClone Nov 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "find . -path './node_modules' -prune -o -name 'dist' -type d -exec rm -rf '{}' \\; -printf '%p deleted\\n' 2>/dev/null",
"deep-clean": "find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \\;",
"build": "turbo run build",
"build:form": "turbo run build --filter=@requestnetwork/create-invoice-form",
Expand Down
78 changes: 63 additions & 15 deletions packages/create-invoice-form/src/lib/create-invoice-form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
import type { IConfig } from "@requestnetwork/shared-types";
import { APP_STATUS } from "@requestnetwork/shared-types/enums";
import type { RequestNetwork } from "@requestnetwork/request-client.js";
import { Types } from "@requestnetwork/request-client.js";
import { CurrencyTypes } from "@requestnetwork/types";
// Utils
import { getInitialFormData, prepareRequestParams } from "./utils";
import { config as defaultConfig } from "@requestnetwork/shared-utils/config";
import { calculateInvoiceTotals } from "@requestnetwork/shared-utils/invoiceTotals";
import { initializeCurrencyManager } from "@requestnetwork/shared-utils/initCurrencyManager";
import {
getCurrencySupportedNetworksForConversion,
initializeCurrencyManager,
} from "@requestnetwork/shared-utils/initCurrencyManager";
// Components
import { InvoiceForm, InvoiceView } from "./invoice";
import Button from "@requestnetwork/shared-components/button.svelte";
Expand All @@ -34,26 +39,34 @@
const extractUniqueNetworkNames = (): string[] => {
const networkSet = new Set<string>();

currencyManager.knownCurrencies.forEach((currency: any) => {
networkSet.add(currency.network);
});
currencyManager.knownCurrencies.forEach(
(currency: CurrencyTypes.CurrencyDefinition) => {
if (currency.network) {
networkSet.add(currency.network);
}
}
);

return Array.from(networkSet);
};

let networks = extractUniqueNetworkNames();
let networks: string[] = extractUniqueNetworkNames();

let network = networks[0];
let network: string | undefined = undefined;
let currency: CurrencyTypes.CurrencyDefinition | undefined = undefined;
let invoiceCurrency: CurrencyTypes.CurrencyDefinition | undefined = undefined;

const handleNetworkChange = (network: string) => {
if (network) {
const handleNetworkChange = (newNetwork: string) => {
if (newNetwork) {
const newCurrencies = currencyManager.knownCurrencies.filter(
(currency: any) => currency.network === network
(currency: CurrencyTypes.CurrencyDefinition) =>
currency.type === Types.RequestLogic.CURRENCY.ISO4217 ||
currency.network === newNetwork
);

network = network;
network = newNetwork;
defaultCurrencies = newCurrencies;
currency = newCurrencies[0];
currency = undefined;
}
};

Expand All @@ -62,12 +75,33 @@
let appStatus: APP_STATUS[] = [];
let formData = getInitialFormData();
let defaultCurrencies = currencyManager.knownCurrencies.filter(
(currency: any) => currency.network === network
(currency: CurrencyTypes.CurrencyDefinition) =>
currency.type === Types.RequestLogic.CURRENCY.ISO4217 || network
? currency.network === network
: true
);

let currency = defaultCurrencies[0];
const handleInvoiceCurrencyChange = (
value: CurrencyTypes.CurrencyDefinition
) => {
invoiceCurrency = value;
network = undefined;
currency = undefined;

if (
invoiceCurrency &&
invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217
) {
networks = getCurrencySupportedNetworksForConversion(
invoiceCurrency.hash,
currencyManager
);
} else {
networks = extractUniqueNetworkNames();
}
};

const handleCurrencyChange = (value: string) => {
const handleCurrencyChange = (value: CurrencyTypes.CurrencyDefinition) => {
currency = value;
};

Expand All @@ -93,7 +127,14 @@

$: {
const basicDetailsFilled =
formData.payeeAddress && formData.payerAddress && formData.dueDate;
formData.payeeAddress &&
formData.payerAddress &&
formData.dueDate &&
formData.invoiceNumber &&
formData.issuedOn &&
invoiceCurrency &&
currency &&
formData.issuedOn;
const hasItems =
formData.invoiceItems.length > 0 &&
formData.invoiceItems.every(isValidItem);
Expand Down Expand Up @@ -132,6 +173,7 @@
const requestCreateParameters = prepareRequestParams({
address: account?.address,
formData,
invoiceCurrency,
currency,
invoiceTotals,
});
Expand Down Expand Up @@ -172,13 +214,19 @@
bind:formData
config={activeConfig}
bind:defaultCurrencies
{handleInvoiceCurrencyChange}
{handleCurrencyChange}
{handleNetworkChange}
{networks}
{currencyManager}
{invoiceCurrency}
{currency}
{network}
/>
<div class="invoice-view-wrapper">
<InvoiceView
config={activeConfig}
{invoiceCurrency}
{currency}
bind:formData
bind:canSubmit
Expand Down
15 changes: 8 additions & 7 deletions packages/create-invoice-form/src/lib/invoice/form-view.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// Types
import type { IConfig, CustomFormData } from "@requestnetwork/shared-types";
import { CurrencyTypes } from "@requestnetwork/types";

// Utils
import { config as defaultConfig } from "@requestnetwork/shared-utils/config";
Expand All @@ -18,7 +19,8 @@
export let config: IConfig;
export let canSubmit = false;
export let formData: CustomFormData;
export let currency = defaultCurrencies[0];
export let currency: CurrencyTypes.CurrencyDefinition | undefined;
export let invoiceCurrency: CurrencyTypes.CurrencyDefinition | undefined;
export let submitForm: (e: Event) => Promise<void>;
export let invoiceTotals = {
amountWithoutTax: 0,
Expand Down Expand Up @@ -164,16 +166,15 @@
</div>
<p class="invoice-section-title">
<span>Payment Chain</span>
{currency.network[0].toUpperCase() + currency.network.slice(1)}
{currency?.network ? currency.network.charAt(0).toUpperCase() + currency.network.slice(1).toLowerCase() : ""}
</p>
<p class="invoice-section-title">
<span>Invoice Currency</span>
{currency.symbol}
({currency.network})
{invoiceCurrency ? invoiceCurrency.symbol: ""}
</p>
<p class="invoice-section-title">
<span>Invoice Type</span>
Regular Invoice
<span>Settlement Currency</span>
{currency ? `${currency.symbol} (${currency.network})` : ""}
</p>
<div class="invoice-table-wrapper">
<table class="invoice-table">
Expand Down Expand Up @@ -221,7 +222,7 @@
>
<span>Due: </span>
<span
>{currency.symbol}
>{currency ? currency.symbol : ""}
{" "}
{invoiceTotals.totalAmount.toFixed(2)}</span
>
Expand Down
71 changes: 58 additions & 13 deletions packages/create-invoice-form/src/lib/invoice/form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@
import { calculateItemTotal } from "@requestnetwork/shared-utils/invoiceTotals";
import { checkAddress } from "@requestnetwork/shared-utils/checkEthAddress";
import { inputDateFormat } from "@requestnetwork/shared-utils/formatDate";
import { Types } from "@requestnetwork/request-client.js";
import { CurrencyTypes } from "@requestnetwork/types";
import isEmail from "validator/es/lib/isEmail";

export let config: IConfig;
export const invoiceNumber: number = 1;
export let formData: CustomFormData;
export let handleInvoiceCurrencyChange: (value: string) => void;
export let handleCurrencyChange: (value: string) => void;

export let handleNetworkChange: (chainId: string) => void;
export let networks;
export let defaultCurrencies: any = [];
export let currencyManager: any;
export let invoiceCurrency: CurrencyTypes.CurrencyDefinition | undefined;
export let currency:
| CurrencyTypes.ERC20Currency
| CurrencyTypes.NativeCurrency
| undefined;
export let network: any;

let validationErrors = {
payeeAddress: false,
Expand Down Expand Up @@ -110,6 +120,21 @@
}
};

const filterSettlementCurrencies = (
currency: CurrencyTypes.CurrencyDefinition
) => {
return invoiceCurrency
? invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217
? currency.type !== Types.RequestLogic.CURRENCY.ISO4217 &&
currencyManager?.getConversionPath(
invoiceCurrency,
currency,
currency.network
)?.length > 0
: invoiceCurrency.hash === currency.hash
: false;
};

const addInvoiceItem = () => {
const newItem = {
name: "",
Expand Down Expand Up @@ -333,25 +358,45 @@
</div>
</Accordion>
</div>
<Dropdown
{config}
placeholder="Select payment chain"
options={networks.map((network) => {
return {
value: network,
label: network[0].toUpperCase() + network.slice(1),
};
})}
onchange={handleNetworkChange}
/>

<Dropdown
{config}
placeholder="Select a currency"
selectedValue={invoiceCurrency
? `${invoiceCurrency.symbol} ${invoiceCurrency?.network ? `(${invoiceCurrency?.network})` : ""}`
: undefined}
placeholder="Invoice currency (labeling)"
options={defaultCurrencies.map((currency) => ({
value: currency,
label: `${currency.symbol} (${currency.network})`,
label: `${currency.symbol} ${currency?.network ? `(${currency?.network})` : ""}`,
}))}
onchange={handleInvoiceCurrencyChange}
/>
<Dropdown
{config}
placeholder="Payment chain"
selectedValue={network}
options={networks
.filter((networkItem) => networkItem)
.map((networkItem) => {
return {
value: networkItem,
label: networkItem[0]?.toUpperCase() + networkItem?.slice(1),
};
})}
onchange={handleNetworkChange}
/>
<Dropdown
{config}
placeholder="Settlement currency"
selectedValue={currency
? `${currency.symbol ?? 'Unknown'} (${currency?.network ?? 'Unknown'})`
: undefined}
options={defaultCurrencies
.filter((currency) => filterSettlementCurrencies(currency))
.map((currency) => ({
value: currency,
label: `${currency.symbol ?? 'Unknown'} (${currency?.network ?? 'Unknown'})`,
}))}
onchange={handleCurrencyChange}
/>
<Input
Expand Down
Loading