-
Notifications
You must be signed in to change notification settings - Fork 422
/
user.ts
340 lines (307 loc) · 10.5 KB
/
user.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { CoinPretty, Dec, IntPretty, PricePretty } from "@keplr-wallet/unit";
import { AssetList, Chain } from "@osmosis-labs/types";
import { aggregateRawCoinsByDenom } from "@osmosis-labs/utils";
import {
calcSumCoinsValue,
mapRawCoinToPretty,
} from "../../../queries/complex/assets";
import { DEFAULT_VS_CURRENCY } from "../../../queries/complex/assets/config";
import {
getCachedPoolIncentivesMap,
PoolIncentives,
} from "../../../queries/complex/pools/incentives";
import { queryBalances } from "../../../queries/cosmos";
import {
StablePoolRawResponse,
WeightedPoolRawResponse,
} from "../../../queries/osmosis";
import {
LiquidityPosition,
queryAccountPositions,
} from "../../../queries/osmosis/concentratedliquidity";
import { timeout } from "../../../utils/async";
import { captureErrorAndReturn } from "../../../utils/error";
import { getUserLocks } from "../osmosis/lockup";
import { getPools } from "./index";
import { getGammShareUnderlyingCoins, makeShareCoin } from "./share";
import { getSuperfluidPoolIds } from "./superfluid";
/** Gets info for all user pools of all types (excluding cosmwasm pools). */
export async function getUserPools(params: {
assetLists: AssetList[];
chainList: Chain[];
bech32Address: string;
}) {
const { assetLists } = params;
const [accountPositions, poolIncentives, superfluidPoolIds] =
await Promise.all([
timeout(
() =>
queryAccountPositions(params)
.then(({ positions }) => positions)
.catch(() => [] as LiquidityPosition[]),
10_000, // 10 seconds
"queryCLPositions"
)().catch(() => [] as LiquidityPosition[]),
timeout(
() =>
getCachedPoolIncentivesMap().catch(
() => new Map<string, PoolIncentives>()
),
10_000, // 10 seconds
"getCachedPoolIncentivesMap"
)().catch(() => new Map<string, PoolIncentives>()),
timeout(
() => getSuperfluidPoolIds(params).catch(() => [] as string[]),
10_000, // 10 seconds
"getSuperfluidPoolIds"
)().catch(() => [] as string[]),
]);
const { locked: lockedShares, poolIds } = await getUserShareRawCoins(params);
const userUniquePoolIds = new Set(poolIds);
accountPositions
.map(({ position: { pool_id } }) => pool_id)
.forEach((poolId) => userUniquePoolIds.add(poolId));
const eventualPools = await timeout(
() => getPools({ ...params, poolIds: Array.from(userUniquePoolIds) }),
10_000, // 10 seconds
"getPools"
)();
return await Promise.all(
eventualPools.map(async (pool) => {
const { id, reserveCoins, totalFiatValueLocked, type } = pool;
let userValue: PricePretty = new PricePretty(
DEFAULT_VS_CURRENCY,
new Dec(0)
);
if (type === "concentrated") {
const positions = accountPositions.filter(
({ position: { pool_id } }) => pool_id === id
);
if (positions.length === 0) {
throw new Error(
`Positions for pool id ${id} not found. It should exist if the pool id is available in the userPoolIds set.`
);
}
const aggregatedRawCoins = aggregateRawCoinsByDenom(
positions.flatMap(({ asset0, asset1 }) => [asset0, asset1])
);
const coinsToCalculateValue = mapRawCoinToPretty(
assetLists,
aggregatedRawCoins
);
userValue = new PricePretty(
DEFAULT_VS_CURRENCY,
await calcSumCoinsValue({ ...params, coins: coinsToCalculateValue })
);
} else if (type === "weighted" || type === "stable") {
const totalShareAmount = new Dec(
(
pool.raw as WeightedPoolRawResponse | StablePoolRawResponse
).total_shares.amount
);
const rawShare = aggregateRawCoinsByDenom(
lockedShares
.filter((coin) => coin.denom === `gamm/pool/${id}`)
.concat()
)[0];
if (rawShare) {
const userValueAmount = totalShareAmount.isZero()
? new Dec(0)
: totalFiatValueLocked
.mul(
new IntPretty(new Dec(rawShare.amount).quo(totalShareAmount))
)
.trim(true);
userValue = new PricePretty(DEFAULT_VS_CURRENCY, userValueAmount);
}
}
return {
id,
type,
reserveCoins,
apr: poolIncentives.get(id)?.aprBreakdown?.total,
poolLiquidity: totalFiatValueLocked,
isSuperfluid: superfluidPoolIds.some(
(superfluidPoolId) => superfluidPoolId === id
),
/** Note: if it's a share pool it is just locked shares value. */
userValue,
};
})
);
}
export async function getUserSharePools(params: {
assetLists: AssetList[];
chainList: Chain[];
bech32Address: string;
poolIds?: string[];
}) {
const [userRawCoins, userLocks, specifiedPools] = await Promise.all([
getUserShareRawCoins(params),
getUserLocks(params),
params.poolIds ? getPools(params) : null,
]);
const {
available: availableRaw,
locked: lockedRaw,
unlocking: unlockingRaw,
total: totalRaw,
poolIds: ownedPoolIds,
} = userRawCoins;
const userSharePools =
specifiedPools ?? (await getPools({ ...params, poolIds: ownedPoolIds }));
const eventualUserSharePools = userSharePools.map(async (sharePool) => {
// get aggregate of raw shares of each variation
const available = availableRaw.length
? aggregateRawCoinsByDenom(
availableRaw.filter(
(coin) => coin.denom === `gamm/pool/${sharePool.id}`
)
)[0]
: null;
const locked = lockedRaw.length
? aggregateRawCoinsByDenom(
lockedRaw.filter((coin) => coin.denom === `gamm/pool/${sharePool.id}`)
)[0]
: null;
const unlocking = unlockingRaw.length
? aggregateRawCoinsByDenom(
unlockingRaw.filter(
(coin) => coin.denom === `gamm/pool/${sharePool.id}`
)
)[0]
: null;
const total = totalRaw.length
? aggregateRawCoinsByDenom(
totalRaw.filter((coin) => coin.denom === `gamm/pool/${sharePool.id}`)
)[0]
: null;
const availableShares = available ? makeShareCoin(available) : null;
const lockedShares = locked ? makeShareCoin(locked) : null;
const unlockingShares = unlocking ? makeShareCoin(unlocking) : null;
const totalShares = total ? makeShareCoin(total) : null;
// underlying assets behind all shares
// when catching: likely shares balance is too small for precision
const underlyingAvailableCoins: CoinPretty[] = available
? await getGammShareUnderlyingCoins({ ...params, ...available }).catch(
(e) => captureErrorAndReturn(e, [])
)
: [];
const underlyingLockedCoins: CoinPretty[] = locked
? await getGammShareUnderlyingCoins({ ...params, ...locked }).catch((e) =>
captureErrorAndReturn(e, [])
)
: [];
const underlyingUnlockingCoins: CoinPretty[] = unlocking
? await getGammShareUnderlyingCoins({ ...params, ...unlocking }).catch(
(e) => captureErrorAndReturn(e, [])
)
: [];
const totalCoins: CoinPretty[] = total
? await getGammShareUnderlyingCoins({ ...params, ...total }).catch((e) =>
captureErrorAndReturn(e, [])
)
: [];
// value of all shares
const availableValue = await calcSumCoinsValue({
...params,
coins: underlyingAvailableCoins,
});
const lockedValue = await calcSumCoinsValue({
...params,
coins: underlyingLockedCoins,
});
const unlockingValue = await calcSumCoinsValue({
...params,
coins: underlyingUnlockingCoins,
});
const totalValue = await calcSumCoinsValue({
...params,
coins: totalCoins,
});
// get locks containing this pool's shares
const lockedLocks = userLocks.filter(
({ coins, isCurrentlyUnlocking }) =>
coins.some((coin) => coin.denom === `gamm/pool/${sharePool.id}`) &&
!isCurrentlyUnlocking
);
const unlockingLocks = userLocks.filter(
({ coins, isCurrentlyUnlocking }) =>
coins.some((coin) => coin.denom === `gamm/pool/${sharePool.id}`) &&
isCurrentlyUnlocking
);
return {
// pool
...sharePool,
// narrow the type
type: sharePool.type as "weighted" | "stable",
raw: sharePool.raw as Omit<
WeightedPoolRawResponse | StablePoolRawResponse,
"@type"
>,
// user data
availableShares,
underlyingAvailableCoins,
availableValue: new PricePretty(DEFAULT_VS_CURRENCY, availableValue ?? 0),
lockedShares,
underlyingLockedCoins,
lockedLocks,
lockedValue: new PricePretty(DEFAULT_VS_CURRENCY, lockedValue ?? 0),
unlockingShares,
unlockingLocks,
underlyingUnlockingCoins,
unlockingValue: new PricePretty(DEFAULT_VS_CURRENCY, unlockingValue ?? 0),
totalShares,
totalCoins,
totalValue: new PricePretty(DEFAULT_VS_CURRENCY, totalValue ?? 0),
};
});
// needed per pool
// - user total coinpretty and value
// - user available coinpretty and value
// - user locked coinpretty and value
// - user unlocking coinpretty and value
return Promise.all(eventualUserSharePools);
}
async function getUserShareRawCoins(params: {
chainList: Chain[];
bech32Address: string;
}) {
const [userBalances, userLocks] = await Promise.all([
queryBalances(params),
getUserLocks(params),
]);
const available = userBalances.balances.filter(
({ denom }) => denom && denom.startsWith("gamm/pool/")
);
const locked = userLocks
.filter((userLock) => !userLock.isCurrentlyUnlocking)
.filter((userLock) =>
userLock.coins.some((coin) => coin.denom.startsWith("gamm/pool"))
)
.flatMap((lock) =>
lock.coins.filter((coin) => coin.denom.startsWith("gamm/pool"))
);
const unlocking = userLocks
.filter((userLock) => userLock.isCurrentlyUnlocking)
.filter((userLock) =>
userLock.coins.some((coin) => coin.denom.startsWith("gamm/pool"))
)
.flatMap((lock) =>
lock.coins.filter((coin) => coin.denom.startsWith("gamm/pool"))
);
const total = [...available, ...locked, ...unlocking];
const poolIds = new Set<string>();
total.forEach(({ denom }) => {
const poolId = denom.replace("gamm/pool/", "");
poolIds.add(poolId);
});
return {
available,
locked,
unlocking,
/** Unaggregated total of all share balances. */
total: [...available, ...locked, ...unlocking],
poolIds: Array.from(poolIds),
};
}