-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathfetchFundingBalances.ts
More file actions
134 lines (116 loc) · 4.17 KB
/
fetchFundingBalances.ts
File metadata and controls
134 lines (116 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {
Checkout, ItemBalance, WrappedBrowserProvider, TokenBalance, TransactionRequirement,
} from '@imtbl/checkout-sdk';
import { Environment } from '@imtbl/config';
import { compareStr, isNativeToken } from '../../../lib/utils';
import {
OrderQuoteCurrency,
FundingBalance,
FundingBalanceResult,
} from '../types';
import {
getAlternativeFundingSteps,
getERC20ItemRequirement,
getNativeItemRequirement,
getFnToPushAndSortFundingBalances,
getFundingBalances,
getGasEstimate,
processGasFreeBalances,
wrapPromisesWithOnResolve,
} from './fetchFundingBalancesUtils';
const isTokenFee = (balance: ItemBalance): balance is TokenBalance => 'token' in balance && balance.token !== undefined;
export type FundingBalanceParams = {
provider: WrappedBrowserProvider;
checkout: Checkout;
currencies: OrderQuoteCurrency[];
baseCurrency: OrderQuoteCurrency;
routingOptions: { bridge: boolean; onRamp: boolean; swap: boolean };
getAmountByCurrency: (currency: OrderQuoteCurrency) => string;
getIsGasless: () => boolean;
onFundingBalance: (balances: FundingBalance[]) => void;
onFundingRequirement: (
fundingItemRequirement: TransactionRequirement
) => void;
onComplete?: (balances: FundingBalance[]) => void;
onUpdateGasFees?: (fees: TokenBalance) => void;
};
export const fetchFundingBalances = async (
params: FundingBalanceParams,
): Promise<FundingBalanceResult[]> => {
const {
provider,
checkout,
currencies,
baseCurrency,
onFundingBalance,
getAmountByCurrency,
getIsGasless,
onComplete,
onFundingRequirement,
onUpdateGasFees,
} = params;
const signer = await provider?.getSigner();
const spenderAddress = (await signer?.getAddress()) || '';
const environment = checkout.config.environment as Environment;
const pushToFoundBalances = getFnToPushAndSortFundingBalances(baseCurrency);
const updateFundingBalances = (balances: FundingBalance[] | null) => {
if (Array.isArray(balances) && balances.length > 0) {
onFundingBalance(
pushToFoundBalances(processGasFreeBalances(balances, provider)),
);
}
};
const isBaseCurrency = (name: string) => compareStr(name, baseCurrency.name);
const balancePromises: Promise<FundingBalanceResult>[] = currencies
.map(async (currency) => {
const amount = getAmountByCurrency(currency);
if (!amount) {
return null;
}
const itemRequirements = isNativeToken(currency.address)
? getNativeItemRequirement(amount)
: getERC20ItemRequirement(amount, spenderAddress, currency.address);
const transactionOrGasAmount = getIsGasless()
? undefined
: getGasEstimate();
const handleOnComplete = () => {
onComplete?.(pushToFoundBalances([]));
};
const handleOnFundingRoute = (route) => {
updateFundingBalances(getAlternativeFundingSteps([route], environment));
};
const smartCheckoutResult = await checkout.smartCheckout({
provider,
itemRequirements,
transactionOrGasAmount,
routingOptions: { bridge: false, onRamp: false, swap: true },
fundingRouteFullAmount: true,
onComplete: isBaseCurrency(currency.name)
? handleOnComplete
: undefined,
onFundingRoute: isBaseCurrency(currency.name)
? handleOnFundingRoute
: undefined,
});
return { currency, smartCheckoutResult };
})
.filter(Boolean) as Promise<FundingBalanceResult>[];
return await wrapPromisesWithOnResolve(
balancePromises,
({ currency, smartCheckoutResult }) => {
if (isBaseCurrency(currency.name)) {
const fundingItemRequirement = smartCheckoutResult.transactionRequirements[0];
onFundingRequirement(fundingItemRequirement);
}
if (smartCheckoutResult.sufficient) {
updateFundingBalances(
getFundingBalances(smartCheckoutResult, environment),
);
const feeRequirement = smartCheckoutResult.transactionRequirements.find((requirement) => requirement.isFee);
if (feeRequirement && isTokenFee(feeRequirement.required) && onUpdateGasFees) {
onUpdateGasFees(feeRequirement.required);
}
}
},
);
};