Skip to content

Commit d119054

Browse files
committed
fix: typings
1 parent 4257ed6 commit d119054

File tree

12 files changed

+65
-61
lines changed

12 files changed

+65
-61
lines changed

packages/create-invoice-form/src/lib/create-invoice-form.svelte

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import { getInitialFormData, prepareRequestParams } from "./utils";
2020
import type { RequestNetwork } from "@requestnetwork/request-client.js";
2121
import { Types } from "@requestnetwork/request-client.js";
22+
import { CurrencyTypes } from "@requestnetwork/types";
2223
2324
export let config: IConfig;
2425
export let signer: string = "";
@@ -34,7 +35,7 @@
3435
const extractUniqueNetworkNames = (): string[] => {
3536
const networkSet = new Set<string>();
3637
37-
currencyManager.knownCurrencies.forEach((currency: any) => {
38+
currencyManager.knownCurrencies.forEach((currency: CurrencyTypes.CurrencyDefinition) => {
3839
if(currency.network) {
3940
networkSet.add(currency.network);
4041
}
@@ -46,13 +47,13 @@
4647
let networks: (string | undefined)[] = extractUniqueNetworkNames();
4748
4849
let network: any = undefined;
49-
let currency: any = undefined;
50-
let invoiceCurrency: any = undefined;
50+
let currency: CurrencyTypes.CurrencyDefinition | undefined = undefined;
51+
let invoiceCurrency: CurrencyTypes.CurrencyDefinition | undefined = undefined;
5152
5253
const handleNetworkChange = (newNetwork: string) => {
5354
if (newNetwork) {
5455
const newCurrencies = currencyManager.knownCurrencies.filter(
55-
(currency: any) => currency.type === Types.RequestLogic.CURRENCY.ISO4217 || currency.network === newNetwork
56+
(currency: CurrencyTypes.CurrencyDefinition) => currency.type === Types.RequestLogic.CURRENCY.ISO4217 || currency.network === newNetwork
5657
);
5758
5859
network = newNetwork;
@@ -66,10 +67,10 @@
6667
let appStatus: APP_STATUS[] = [];
6768
let formData = getInitialFormData();
6869
let defaultCurrencies = currencyManager.knownCurrencies.filter(
69-
(currency: any) => currency.type === Types.RequestLogic.CURRENCY.ISO4217 || currency.network === network
70+
(currency: CurrencyTypes.CurrencyDefinition) => currency.type === Types.RequestLogic.CURRENCY.ISO4217 || currency.network === network
7071
);
7172
72-
const handleInvoiceCurrencyChange = (value: string) => {
73+
const handleInvoiceCurrencyChange = (value: CurrencyTypes.CurrencyDefinition) => {
7374
invoiceCurrency = value;
7475
network = undefined;
7576
currency = undefined;
@@ -82,7 +83,7 @@
8283
}
8384
};
8485
85-
const handleCurrencyChange = (value: string) => {
86+
const handleCurrencyChange = (value: CurrencyTypes.CurrencyDefinition) => {
8687
currency = value;
8788
};
8889

packages/create-invoice-form/src/lib/invoice/form-view.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
99
// Types
1010
import type { IConfig, CustomFormData } from "@requestnetwork/shared-types";
11+
import { CurrencyTypes } from "@requestnetwork/types";
1112
1213
// Utils
1314
import { config as defaultConfig } from "@requestnetwork/shared-utils/config";
1415
import { calculateItemTotal } from "@requestnetwork/shared-utils/invoiceTotals";
1516
import { formatDate } from "@requestnetwork/shared-utils/formatDate";
16-
import { Types } from '@requestnetwork/request-client.js';
1717
1818
export let defaultCurrencies;
1919
export let config: IConfig;
2020
export let canSubmit = false;
2121
export let formData: CustomFormData;
22-
export let currency: any;
23-
export let invoiceCurrency: any;
22+
export let currency: CurrencyTypes.CurrencyDefinition | undefined;
23+
export let invoiceCurrency: CurrencyTypes.CurrencyDefinition | undefined;
2424
export let submitForm: (e: Event) => Promise<void>;
2525
export let invoiceTotals = {
2626
amountWithoutTax: 0,
@@ -166,7 +166,7 @@
166166
</div>
167167
<p class="invoice-section-title">
168168
<span>Payment Chain</span>
169-
{currency? currency?.network[0]?.toUpperCase() + currency?.network?.slice(1): ""}
169+
{currency?.network ? currency.network.charAt(0).toUpperCase() + currency.network.slice(1).toLowerCase() : ""}
170170
</p>
171171
<p class="invoice-section-title">
172172
<span>Invoice Currency</span>

packages/create-invoice-form/src/lib/invoice/form.svelte

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import { checkAddress } from "@requestnetwork/shared-utils/checkEthAddress";
1919
import { inputDateFormat } from "@requestnetwork/shared-utils/formatDate";
2020
import { Types } from '@requestnetwork/request-client.js';
21+
import { CurrencyTypes } from "@requestnetwork/types";
2122
2223
export let config: IConfig;
2324
export const invoiceNumber: number = 1;
@@ -31,8 +32,8 @@
3132
export let payeeAddressError = false;
3233
export let clientAddressError = false;
3334
export let currencyManager: any;
34-
export let invoiceCurrency: any;
35-
export let currency: any;
35+
export let invoiceCurrency: CurrencyTypes.CurrencyDefinition | undefined;
36+
export let currency: CurrencyTypes.CurrencyDefinition | undefined;
3637
export let network: any;
3738
3839
let creatorId = "";
@@ -102,7 +103,7 @@
102103
}
103104
};
104105
105-
const filterSettlementCurrencies = (currency: any) => {
106+
const filterSettlementCurrencies = (currency: CurrencyTypes.CurrencyDefinition) => {
106107
return invoiceCurrency? (
107108
invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217 ?
108109
currency.type !== Types.RequestLogic.CURRENCY.ISO4217 && currencyManager?.getConversionPath(invoiceCurrency, currency, currency.network)?.length > 0 :

packages/create-invoice-form/src/lib/utils/prepareRequest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Types, Utils } from "@requestnetwork/request-client.js";
22
import type { CustomFormData } from "@requestnetwork/shared-types";
3-
import { get } from 'svelte/store'
43
import { parseUnits, zeroAddress } from "viem";
4+
import { CurrencyTypes } from "@requestnetwork/types";
55

66
interface IRequestParams {
7-
invoiceCurrency: any;
8-
currency: any;
7+
invoiceCurrency: CurrencyTypes.CurrencyDefinition;
8+
currency: CurrencyTypes.CurrencyDefinition;
99
formData: CustomFormData;
1010
invoiceTotals: {
1111
amountWithoutTax: number;
@@ -15,7 +15,7 @@ interface IRequestParams {
1515
signer: string;
1616
}
1717

18-
const getPaymentNetwork = (invoiceCurrency: any, currency: any, formData: CustomFormData) => {
18+
const getPaymentNetwork = (invoiceCurrency: CurrencyTypes.CurrencyDefinition, currency: CurrencyTypes.CurrencyDefinition, formData: CustomFormData) => {
1919
if (
2020
invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217 &&
2121
currency.type === Types.RequestLogic.CURRENCY.ETH

packages/invoice-dashboard/src/lib/dashboard/invoice-view.svelte

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Types,
1212
type RequestNetwork,
1313
} from "@requestnetwork/request-client.js";
14+
import { CurrencyTypes } from "@requestnetwork/types";
1415
import { toast } from "svelte-sonner";
1516
// Components
1617
import Accordion from "@requestnetwork/shared-components/accordion.svelte";
@@ -38,10 +39,10 @@
3839
export let currencyManager: any;
3940
export let isRequestPayed: boolean;
4041
41-
let network = request?.currencyInfo?.network || "mainnet";
42+
let network: string | undefined = request?.currencyInfo?.network || "mainnet";
4243
// FIXME: Use a non deprecated function
43-
let currency = getCurrencyFromManager(request.currencyInfo, currencyManager);
44-
let paymentCurrencies: any = [];
44+
let currency: CurrencyTypes.CurrencyDefinition | undefined = getCurrencyFromManager(request.currencyInfo, currencyManager);
45+
let paymentCurrencies: (CurrencyTypes.CurrencyDefinition | undefined)[] = [];
4546
let statuses: any = [];
4647
let isPaid = false;
4748
let loading = false;
@@ -118,7 +119,7 @@
118119
paymentNetworkExtension = getPaymentNetworkExtension(requestData);
119120
120121
if (paymentNetworkExtension?.id === Types.Extension.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY) {
121-
paymentCurrencies = paymentNetworkExtension?.values?.acceptedTokens.map(
122+
paymentCurrencies = paymentNetworkExtension?.values?.acceptedTokens?.map(
122123
(token: any) => currencyManager.fromAddress(token, paymentNetworkExtension?.values?.network)
123124
);
124125
} else if( paymentNetworkExtension?.id === Types.Extension.PAYMENT_NETWORK_ID.ANY_TO_ETH_PROXY) {
@@ -132,7 +133,7 @@
132133
133134
network = paymentCurrencies[0]?.network || "mainnet";
134135
135-
if (paymentCurrencies[0].type === Types.RequestLogic.CURRENCY.ERC20) {
136+
if (paymentCurrencies[0]?.type === Types.RequestLogic.CURRENCY.ERC20) {
136137
approved = await checkApproval(requestData, paymentCurrencies, signer);
137138
} else {
138139
approved = true;
@@ -204,7 +205,7 @@
204205
return await hasErc20Approval(requestData!, address!, signer)
205206
} else if(paymentNetworkExtension?.id ===
206207
Types.Extension.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY) {
207-
return await hasErc20ApprovalForProxyConversion(requestData!, address!, paymentCurrencies[0].address, signer, requestData.expectedAmount);
208+
return await hasErc20ApprovalForProxyConversion(requestData!, address!, paymentCurrencies[0]?.address, signer, requestData.expectedAmount);
208209
}
209210
210211
return false;
@@ -223,7 +224,7 @@
223224
approved = true;
224225
} else if(paymentNetworkExtension?.id ===
225226
Types.Extension.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY) {
226-
const approvalTx = await approveErc20ForProxyConversion(requestData!, paymentCurrencies[0].address, signer);
227+
const approvalTx = await approveErc20ForProxyConversion(requestData!, paymentCurrencies[0]?.address, signer);
227228
await approvalTx.wait(2);
228229
approved = true;
229230
}
@@ -499,7 +500,7 @@
499500
type="button"
500501
text="Switch Network"
501502
padding="px-[12px] py-[6px]"
502-
onClick={() => switchNetworkIfNeeded(network)}
503+
onClick={() => switchNetworkIfNeeded(network || "mainnet")}
503504
/>
504505
{:else if !approved && !isPaid && !isPayee && !unsupportedNetwork}
505506
<Button

packages/invoice-dashboard/src/lib/react/InvoiceDashboard.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import React from "react";
22
import type { IConfig } from "@requestnetwork/shared-types";
33
import type { WalletState } from "@requestnetwork/shared-types/web3Onboard";
44
import type { RequestNetwork } from "@requestnetwork/request-client.js";
5-
import { Currency } from "../types";
6-
5+
import { CurrencyTypes } from "@requestnetwork/types";
76
export interface InvoiceDashboardProps {
87
config: IConfig;
98
wallet: WalletState | null;
109
requestNetwork: RequestNetwork | null | undefined;
11-
currencies: Currency[];
10+
currencies: CurrencyTypes.CurrencyDefinition[];
1211
}
1312
/**
1413
* InvoiceDashboard is a React component that integrates with the Request Network to manage and display invoices.

packages/invoice-dashboard/src/lib/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export interface Currency {
55
address: string;
66
network: string;
77
decimals: number;
8+
value: string;
89
type: Types.RequestLogic.CURRENCY;
910
}

packages/invoice-dashboard/src/lib/view-requests.svelte

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import { getCurrencyFromManager } from "../utils/getCurrency";
3636
import { Drawer, InvoiceView } from "./dashboard";
3737
import { getPaymentNetworkExtension } from '@requestnetwork/payment-detection';
38+
import { CurrencyTypes } from "@requestnetwork/types";
3839
3940
export let config: IConfig;
4041
export let wallet: WalletState;
@@ -220,7 +221,7 @@
220221
): Types.IRequestDataWithEvents & {
221222
formattedAmount: string;
222223
currencySymbol: string;
223-
paymentCurrencies: any[];
224+
paymentCurrencies: (CurrencyTypes.CurrencyDefinition | undefined)[];
224225
} => {
225226
const currencyInfo = getCurrencyFromManager(
226227
request.currencyInfo,
@@ -231,13 +232,15 @@
231232
let paymentCurrencies = [currencyInfo];
232233
233234
if (paymentNetworkExtension?.id === Types.Extension.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY) {
234-
paymentCurrencies = paymentNetworkExtension?.values?.acceptedTokens.map(
235+
paymentCurrencies = paymentNetworkExtension?.values?.acceptedTokens?.map(
235236
(token: any) => currencyManager.fromAddress(token, paymentNetworkExtension?.values?.network)
236237
);
237238
} else if( paymentNetworkExtension?.id === Types.Extension.PAYMENT_NETWORK_ID.ANY_TO_ETH_PROXY) {
238-
paymentCurrencies = [currencyManager.getNativeCurrency(
239-
Types.RequestLogic.CURRENCY.ETH,
240-
paymentNetworkExtension?.values?.network
239+
const network = paymentNetworkExtension?.values?.network ?? "mainnet";
240+
241+
paymentCurrencies = [currencyManager.getNativeCurrency(
242+
Types.RequestLogic.CURRENCY.ETH,
243+
network
241244
)];
242245
}
243246

packages/invoice-dashboard/src/utils/chainlink.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@ export const getChainlinkRate = async (
1616
currencyManager: CurrencyTypes.ICurrencyManager;
1717
},
1818
) => {
19-
const chainlink = chainlinkConversionPath.connect(network, provider);
20-
const path = currencyManager.getConversionPath(from, to, network);
21-
if (!path) return null;
22-
const result = await chainlink.getRate(path);
23-
if (!result) return null;
19+
try {
20+
const chainlink = chainlinkConversionPath.connect(network, provider);
21+
const path = currencyManager.getConversionPath(from, to, network);
22+
if (!path) return null;
23+
const result = await chainlink.getRate(path);
24+
if (!result) return null;
2425

25-
// ChainlinkConversionPath uses 8 decimals for fiat.
26-
const fromDecimals = isISO4217Currency(from) ? 8 : from.decimals;
27-
const toDecimals = isISO4217Currency(to) ? 8 : to.decimals;
28-
const value = result.rate
29-
.mul(BigNumber.from(10).pow(fromDecimals))
30-
.div(BigNumber.from(10).pow(toDecimals));
31-
return {
32-
value,
33-
decimals: result.decimals.toString().length - 1,
34-
};
26+
// ChainlinkConversionPath uses 8 decimals for fiat.
27+
const fromDecimals = isISO4217Currency(from) ? 8 : from.decimals;
28+
const toDecimals = isISO4217Currency(to) ? 8 : to.decimals;
29+
const value = result.rate
30+
.mul(BigNumber.from(10).pow(fromDecimals))
31+
.div(BigNumber.from(10).pow(toDecimals));
32+
return {
33+
value,
34+
decimals: result.decimals.toString().length - 1,
35+
};
36+
} catch (e) {
37+
return null;
38+
}
3539
};

packages/invoice-dashboard/src/utils/conversion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const MAX_SLIPPAGE_DEFAULT = 1.03;
1111
const MAX_SLIPPAGE_LOW_VOLATILITY = 1.01;
1212
export const lowVolatilityTokens = ["DAI", "USDC", "USDT"];
1313

14-
export const getSlippageMargin = (currency: any) => {
14+
export const getSlippageMargin = (currency: CurrencyTypes.CurrencyInput) => {
1515
return lowVolatilityTokens.includes(currency.symbol)
1616
? MAX_SLIPPAGE_LOW_VOLATILITY
1717
: MAX_SLIPPAGE_DEFAULT;

0 commit comments

Comments
 (0)