-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuseAllowances.ts
182 lines (170 loc) · 5.58 KB
/
useAllowances.ts
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { useCallback, useEffect, useMemo, useState } from 'react';
import { Address, erc20ABI, erc4626ABI, usePublicClient } from 'wagmi';
import WAD from '@exactly/lib/esm/fixed-point-math/WAD';
import MAX_UINT256 from '@exactly/lib/esm/fixed-point-math/MAX_UINT256';
import useDebtManager from './useDebtManager';
import useAccountData from './useAccountData';
import useETHRouter from './useETHRouter';
import { useWeb3 } from './useWeb3';
import useAssets from './useAssets';
import useContract from './useContract';
import { installmentsRouterABI } from 'types/abi';
export type Allowance = {
allowance: bigint;
allowanceUSD: bigint;
symbol: string;
token: Address;
decimals: number;
spenderAddress: Address;
spenderName: string;
unlimited: boolean;
};
export type AllowancesState = {
data?: Allowance[];
loading: boolean;
update: () => Promise<void>;
};
type AllowanceDescriptor = {
symbol: string;
} & (
| {
type: 'directAsset' | 'shareAsset';
spenderAddress: Address;
spenderName: string;
}
| {
type: 'marketSpender';
}
);
export const useAllowances = (): AllowancesState => {
const [allowances, setAllowances] = useState<Allowance[]>();
const [loading, setLoading] = useState(true);
const { getMarketAccount } = useAccountData();
const { walletAddress, chain } = useWeb3();
const client = usePublicClient({ chainId: chain.id });
const assetSymbols = useAssets();
const debtManager = useDebtManager();
const ethRouter = useETHRouter();
const installmentsRouter = useContract('InstallmentsRouter', installmentsRouterABI);
const allowanceDescriptors: AllowanceDescriptor[] = useMemo(() => {
const debtManagerDescriptor = (asset: string) =>
debtManager
? ([
{
symbol: asset,
spenderAddress: debtManager.address,
spenderName: 'DebtManager',
type: 'directAsset',
},
{
symbol: asset,
spenderAddress: debtManager.address,
spenderName: 'DebtManager',
type: 'shareAsset',
},
] as const)
: [];
const installmentsRouterDescriptor = (asset: string) =>
installmentsRouter
? ([
{
symbol: asset,
spenderAddress: installmentsRouter.address,
spenderName: 'InstallmentsRouter',
type: 'shareAsset',
},
] as const)
: [];
const assetDescriptors = assetSymbols.flatMap((asset) => [
...debtManagerDescriptor(asset),
...installmentsRouterDescriptor(asset),
{
symbol: asset,
type: 'marketSpender',
} as const,
]);
const ethRouterDescriptor = ethRouter
? ([{ symbol: 'WETH', spenderAddress: ethRouter.address, spenderName: 'ETHRouter', type: 'shareAsset' }] as const)
: [];
return [...assetDescriptors, ...ethRouterDescriptor];
}, [assetSymbols, debtManager, ethRouter, installmentsRouter]);
const descriptorToAllowance = useCallback(
async (descriptor: AllowanceDescriptor, owner: Address) => {
const marketAccount = getMarketAccount(descriptor.symbol);
if (!marketAccount) return;
const { asset, market, usdPrice, decimals } = marketAccount;
let spenderAddress, spenderName, token, symbol, allowanceUSD;
switch (descriptor.type) {
case 'directAsset':
spenderAddress = descriptor.spenderAddress;
spenderName = descriptor.spenderName;
token = asset;
symbol = descriptor.symbol;
break;
case 'shareAsset':
spenderAddress = descriptor.spenderAddress;
spenderName = descriptor.spenderName;
token = market;
symbol = `exa${descriptor.symbol}`;
break;
case 'marketSpender':
spenderAddress = market;
spenderName = `Market${descriptor.symbol}`;
token = asset;
symbol = descriptor.symbol;
break;
}
const allowance = await client.readContract({
abi: erc4626ABI,
address: token,
functionName: 'allowance',
args: [owner, spenderAddress],
});
const totalSupply = await client.readContract({
abi: erc20ABI,
address: token,
functionName: 'totalSupply',
});
const unlimited = allowance > totalSupply;
try {
allowanceUSD =
((descriptor.type === 'shareAsset'
? await client.readContract({
abi: erc4626ABI,
address: token,
functionName: 'convertToAssets',
args: [allowance],
})
: allowance) *
usdPrice) /
WAD;
} catch {
allowanceUSD = 0n;
}
return {
symbol,
spenderAddress,
spenderName,
token,
decimals,
unlimited,
allowance,
allowanceUSD: unlimited ? MAX_UINT256 : allowanceUSD,
};
},
[client, getMarketAccount],
);
const update = useCallback(async () => {
if (!walletAddress) return;
const allowancesData = (await Promise.all(allowanceDescriptors.map((d) => descriptorToAllowance(d, walletAddress))))
.filter((a): a is Allowance => a !== undefined && a.allowance !== 0n)
.sort((a1, a2) => (a1.allowanceUSD > a2.allowanceUSD ? -1 : 1));
setAllowances(allowancesData);
}, [allowanceDescriptors, descriptorToAllowance, walletAddress]);
useEffect(() => {
setLoading(true);
update();
setLoading(false);
}, [update]);
return { data: allowances, loading: loading, update };
};