@gearbox-protocol/sdk 14.12.0-next.66 → 14.12.0-next.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/common-utils/index.js +2 -2
- package/dist/cjs/common-utils/utils/apy/calculate-safe-borrow-rate.js +3 -3
- package/dist/cjs/common-utils/utils/assets-math.js +7 -7
- package/dist/cjs/common-utils/utils/creditAccount/calc-health-factor.js +2 -2
- package/dist/cjs/common-utils/utils/creditAccount/debt.js +2 -2
- package/dist/cjs/common-utils/utils/creditAccount/quota-utils.js +5 -5
- package/dist/cjs/common-utils/utils/index.js +2 -2
- package/dist/cjs/common-utils/utils/strategies/leverage/calculate-max-leverage-factor.js +2 -2
- package/dist/cjs/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.js +5 -5
- package/dist/cjs/common-utils/utils/strategies/strategy-info/calculate-total-points.js +2 -2
- package/dist/cjs/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.js +2 -2
- package/dist/cjs/common-utils/utils/validation/validate-open-account-pool-status.js +4 -4
- package/dist/cjs/model/history.js +28 -0
- package/dist/cjs/model/history.schema.js +84 -0
- package/dist/cjs/model/index.js +64 -0
- package/dist/cjs/model/liquidations.js +23 -0
- package/dist/cjs/model/liquidations.schema.js +91 -0
- package/dist/cjs/model/opportunities.js +55 -0
- package/dist/cjs/model/opportunities.schema.js +220 -0
- package/dist/cjs/model/package.json +1 -0
- package/dist/cjs/model/positions.js +1 -0
- package/dist/cjs/model/primitives.js +1 -0
- package/dist/cjs/model/primitives.schema.js +85 -0
- package/dist/cjs/new-sdk/AbstractNamespace.js +158 -0
- package/dist/cjs/new-sdk/GearboxSDK.js +122 -0
- package/dist/cjs/new-sdk/index.js +11 -0
- package/dist/cjs/new-sdk/opportunities/OpportunitiesNamespace.js +122 -0
- package/dist/cjs/new-sdk/opportunities/index.js +4 -0
- package/dist/cjs/new-sdk/opportunities/types.js +1 -0
- package/dist/cjs/new-sdk/package.json +1 -0
- package/dist/cjs/new-sdk/types.js +22 -0
- package/dist/cjs/new-sdk/utils/history.js +1 -0
- package/dist/cjs/new-sdk/utils/index.js +2 -0
- package/dist/cjs/offchain/GearboxAPI.js +33 -0
- package/dist/cjs/offchain/index.js +8 -0
- package/dist/cjs/offchain/opportunities/OffchainOpportunities.js +97 -0
- package/dist/cjs/offchain/opportunities/index.js +4 -0
- package/dist/cjs/offchain/package.json +1 -0
- package/dist/cjs/offchain/types.js +1 -0
- package/dist/cjs/preview/preview/buildDelayedPreview.js +5 -5
- package/dist/cjs/rewards/rewards/api.js +2 -2
- package/dist/cjs/rewards/rewards/extra-apy.js +2 -2
- package/dist/cjs/sdk/MultichainSDK.js +18 -2
- package/dist/cjs/sdk/OnchainSDK.js +8 -1
- package/dist/cjs/sdk/accounts/index.js +2 -0
- package/dist/cjs/sdk/accounts/liquidations/LiquidationsService.js +150 -106
- package/dist/cjs/sdk/accounts/liquidations/MultichainLiquidationsService.js +32 -38
- package/dist/cjs/sdk/accounts/liquidations/constants.js +13 -0
- package/dist/cjs/sdk/accounts/liquidations/index.js +2 -0
- package/dist/cjs/sdk/base/MultichainConstruct.js +80 -0
- package/dist/cjs/sdk/base/TokensMeta.js +51 -0
- package/dist/cjs/sdk/base/index.js +2 -0
- package/dist/cjs/sdk/chain/chains.js +76 -0
- package/dist/cjs/sdk/chain/index.js +4 -0
- package/dist/cjs/sdk/core/errors.js +13 -0
- package/dist/cjs/sdk/core/index.js +2 -1
- package/dist/cjs/sdk/index.js +25 -1
- package/dist/cjs/sdk/market/MarketConfiguratorContract.js +14 -0
- package/dist/cjs/sdk/market/MarketSuite.js +163 -0
- package/dist/cjs/sdk/market/credit/CreditManagerV310Contract.js +26 -0
- package/dist/cjs/sdk/market/credit/CreditSuite.js +102 -0
- package/dist/cjs/sdk/market/math.js +104 -0
- package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +52 -0
- package/dist/cjs/sdk/market/pool/LinearInterestRateModelContract.js +30 -0
- package/dist/cjs/sdk/market/pool/PoolQuotaKeeperV310Contract.js +26 -0
- package/dist/cjs/sdk/market/pool/PoolSuite.js +28 -0
- package/dist/cjs/sdk/market/pool/PoolV310Contract.js +27 -0
- package/dist/cjs/sdk/market/pool/math.js +44 -0
- package/dist/cjs/sdk/market/pricefeeds/AbstractPriceFeed.js +17 -0
- package/dist/cjs/sdk/opportunities/MultichainOpportunitiesService.js +60 -0
- package/dist/cjs/sdk/opportunities/OpportunitiesService.js +103 -0
- package/dist/cjs/sdk/opportunities/index.js +12 -0
- package/dist/cjs/sdk/types/multichain.js +1 -0
- package/dist/cjs/{common-utils → sdk}/utils/bigint-math.js +11 -11
- package/dist/cjs/sdk/utils/index.js +3 -0
- package/dist/cjs/sdk/utils/zod.js +12 -0
- package/dist/esm/common-utils/index.js +1 -1
- package/dist/esm/common-utils/utils/apy/calculate-safe-borrow-rate.js +1 -1
- package/dist/esm/common-utils/utils/assets-math.js +1 -1
- package/dist/esm/common-utils/utils/creditAccount/calc-health-factor.js +1 -1
- package/dist/esm/common-utils/utils/creditAccount/debt.js +1 -1
- package/dist/esm/common-utils/utils/creditAccount/quota-utils.js +1 -1
- package/dist/esm/common-utils/utils/index.js +1 -1
- package/dist/esm/common-utils/utils/strategies/leverage/calculate-max-leverage-factor.js +1 -1
- package/dist/esm/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.js +1 -1
- package/dist/esm/common-utils/utils/strategies/strategy-info/calculate-total-points.js +1 -1
- package/dist/esm/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.js +1 -1
- package/dist/esm/common-utils/utils/validation/validate-open-account-pool-status.js +1 -1
- package/dist/esm/model/history.js +26 -0
- package/dist/esm/model/history.schema.js +76 -0
- package/dist/esm/model/index.js +9 -0
- package/dist/esm/model/liquidations.js +22 -0
- package/dist/esm/model/liquidations.schema.js +83 -0
- package/dist/esm/model/opportunities.js +51 -0
- package/dist/esm/model/opportunities.schema.js +198 -0
- package/dist/esm/model/package.json +1 -0
- package/dist/esm/model/positions.js +1 -0
- package/dist/esm/model/primitives.js +1 -0
- package/dist/esm/model/primitives.schema.js +75 -0
- package/dist/esm/new-sdk/AbstractNamespace.js +157 -0
- package/dist/esm/new-sdk/GearboxSDK.js +121 -0
- package/dist/esm/new-sdk/index.js +7 -0
- package/dist/esm/new-sdk/opportunities/OpportunitiesNamespace.js +121 -0
- package/dist/esm/new-sdk/opportunities/index.js +3 -0
- package/dist/esm/new-sdk/opportunities/types.js +1 -0
- package/dist/esm/new-sdk/package.json +1 -0
- package/dist/esm/new-sdk/types.js +21 -0
- package/dist/esm/new-sdk/utils/history.js +1 -0
- package/dist/esm/new-sdk/utils/index.js +2 -0
- package/dist/esm/offchain/GearboxAPI.js +32 -0
- package/dist/esm/offchain/index.js +5 -0
- package/dist/esm/offchain/opportunities/OffchainOpportunities.js +95 -0
- package/dist/esm/offchain/opportunities/index.js +2 -0
- package/dist/esm/offchain/package.json +1 -0
- package/dist/esm/offchain/types.js +1 -0
- package/dist/esm/preview/preview/buildDelayedPreview.js +1 -1
- package/dist/esm/rewards/rewards/api.js +1 -1
- package/dist/esm/rewards/rewards/extra-apy.js +1 -1
- package/dist/esm/sdk/MultichainSDK.js +18 -2
- package/dist/esm/sdk/OnchainSDK.js +8 -1
- package/dist/esm/sdk/accounts/index.js +2 -2
- package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +152 -108
- package/dist/esm/sdk/accounts/liquidations/MultichainLiquidationsService.js +32 -38
- package/dist/esm/sdk/accounts/liquidations/constants.js +12 -1
- package/dist/esm/sdk/accounts/liquidations/index.js +2 -2
- package/dist/esm/sdk/base/MultichainConstruct.js +79 -0
- package/dist/esm/sdk/base/TokensMeta.js +51 -0
- package/dist/esm/sdk/base/index.js +2 -1
- package/dist/esm/sdk/chain/chains.js +74 -2
- package/dist/esm/sdk/chain/index.js +2 -2
- package/dist/esm/sdk/core/errors.js +13 -1
- package/dist/esm/sdk/core/index.js +2 -2
- package/dist/esm/sdk/index.js +11 -5
- package/dist/esm/sdk/market/MarketConfiguratorContract.js +14 -0
- package/dist/esm/sdk/market/MarketSuite.js +163 -0
- package/dist/esm/sdk/market/credit/CreditManagerV310Contract.js +26 -0
- package/dist/esm/sdk/market/credit/CreditSuite.js +102 -0
- package/dist/esm/sdk/market/math.js +98 -0
- package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +52 -0
- package/dist/esm/sdk/market/pool/LinearInterestRateModelContract.js +30 -0
- package/dist/esm/sdk/market/pool/PoolQuotaKeeperV310Contract.js +26 -0
- package/dist/esm/sdk/market/pool/PoolSuite.js +28 -0
- package/dist/esm/sdk/market/pool/PoolV310Contract.js +27 -0
- package/dist/esm/sdk/market/pool/math.js +41 -0
- package/dist/esm/sdk/market/pricefeeds/AbstractPriceFeed.js +17 -0
- package/dist/esm/sdk/opportunities/MultichainOpportunitiesService.js +59 -0
- package/dist/esm/sdk/opportunities/OpportunitiesService.js +102 -0
- package/dist/esm/sdk/opportunities/index.js +4 -0
- package/dist/esm/sdk/types/multichain.js +1 -0
- package/dist/esm/{common-utils → sdk}/utils/bigint-math.js +11 -11
- package/dist/esm/sdk/utils/index.js +3 -2
- package/dist/esm/sdk/utils/zod.js +13 -2
- package/dist/types/common-utils/index.d.ts +1 -1
- package/dist/types/common-utils/utils/index.d.ts +1 -1
- package/dist/types/model/history.d.ts +105 -0
- package/dist/types/model/history.schema.d.ts +66 -0
- package/dist/types/model/index.d.ts +9 -0
- package/dist/types/model/liquidations.d.ts +230 -0
- package/dist/types/model/liquidations.schema.d.ts +292 -0
- package/dist/types/model/opportunities.d.ts +543 -0
- package/dist/types/model/opportunities.schema.d.ts +1066 -0
- package/dist/types/model/positions.d.ts +11 -0
- package/dist/types/model/primitives.d.ts +193 -0
- package/dist/types/model/primitives.schema.d.ts +81 -0
- package/dist/types/new-sdk/AbstractNamespace.d.ts +119 -0
- package/dist/types/new-sdk/GearboxSDK.d.ts +78 -0
- package/dist/types/new-sdk/index.d.ts +9 -0
- package/dist/types/new-sdk/opportunities/OpportunitiesNamespace.d.ts +69 -0
- package/dist/types/new-sdk/opportunities/index.d.ts +3 -0
- package/dist/types/new-sdk/opportunities/types.d.ts +66 -0
- package/dist/types/new-sdk/types.d.ts +149 -0
- package/dist/types/new-sdk/utils/history.d.ts +44 -0
- package/dist/types/new-sdk/utils/index.d.ts +2 -0
- package/dist/types/offchain/GearboxAPI.d.ts +31 -0
- package/dist/types/offchain/index.d.ts +5 -0
- package/dist/types/offchain/opportunities/OffchainOpportunities.d.ts +69 -0
- package/dist/types/offchain/opportunities/index.d.ts +2 -0
- package/dist/types/offchain/types.d.ts +50 -0
- package/dist/types/sdk/MultichainSDK.d.ts +15 -2
- package/dist/types/sdk/OnchainSDK.d.ts +9 -3
- package/dist/types/sdk/accounts/index.d.ts +3 -3
- package/dist/types/sdk/accounts/liquidations/LiquidationsService.d.ts +24 -16
- package/dist/types/sdk/accounts/liquidations/MultichainLiquidationsService.d.ts +26 -20
- package/dist/types/sdk/accounts/liquidations/constants.d.ts +12 -1
- package/dist/types/sdk/accounts/liquidations/index.d.ts +3 -3
- package/dist/types/sdk/accounts/liquidations/types.d.ts +34 -309
- package/dist/types/sdk/base/MultichainConstruct.d.ts +57 -0
- package/dist/types/sdk/base/TokensMeta.d.ts +26 -0
- package/dist/types/sdk/base/index.d.ts +2 -1
- package/dist/types/sdk/chain/chains.d.ts +68 -1
- package/dist/types/sdk/chain/index.d.ts +2 -2
- package/dist/types/sdk/core/errors.d.ts +10 -1
- package/dist/types/sdk/core/index.d.ts +2 -2
- package/dist/types/sdk/index.d.ts +15 -8
- package/dist/types/sdk/market/MarketConfiguratorContract.d.ts +9 -0
- package/dist/types/sdk/market/MarketSuite.d.ts +109 -1
- package/dist/types/sdk/market/credit/CreditManagerV310Contract.d.ts +14 -0
- package/dist/types/sdk/market/credit/CreditSuite.d.ts +47 -0
- package/dist/types/sdk/market/credit/types.d.ts +22 -0
- package/dist/types/sdk/market/index.d.ts +2 -2
- package/dist/types/sdk/market/math.d.ts +74 -0
- package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +23 -0
- package/dist/types/sdk/market/oracle/types.d.ts +46 -0
- package/dist/types/sdk/market/pool/LinearInterestRateModelContract.d.ts +12 -0
- package/dist/types/sdk/market/pool/PoolQuotaKeeperV310Contract.d.ts +22 -1
- package/dist/types/sdk/market/pool/PoolSuite.d.ts +23 -0
- package/dist/types/sdk/market/pool/PoolV310Contract.d.ts +18 -0
- package/dist/types/sdk/market/pool/math.d.ts +57 -0
- package/dist/types/sdk/market/pool/types.d.ts +23 -0
- package/dist/types/sdk/market/pricefeeds/AbstractPriceFeed.d.ts +6 -0
- package/dist/types/sdk/market/pricefeeds/types.d.ts +10 -0
- package/dist/types/sdk/opportunities/MultichainOpportunitiesService.d.ts +40 -0
- package/dist/types/sdk/opportunities/OpportunitiesService.d.ts +47 -0
- package/dist/types/sdk/opportunities/index.d.ts +4 -0
- package/dist/types/sdk/types/index.d.ts +2 -1
- package/dist/types/sdk/types/multichain.d.ts +65 -0
- package/dist/types/sdk/types/state.d.ts +1 -1
- package/dist/types/sdk/types/transactions.d.ts +3 -0
- package/dist/types/{common-utils → sdk}/utils/bigint-math.d.ts +7 -11
- package/dist/types/sdk/utils/index.d.ts +3 -2
- package/dist/types/sdk/utils/zod.d.ts +6 -2
- package/package.json +16 -1
- package/dist/cjs/sdk/accounts/liquidations/helpers.js +0 -201
- package/dist/esm/sdk/accounts/liquidations/helpers.js +0 -192
- package/dist/types/sdk/accounts/liquidations/helpers.d.ts +0 -140
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { ZodAddress } from "../sdk/utils/zod.js";
|
|
2
|
+
import { amountSchema, assetTypeSchema, bpsSchema, chainIdSchema, curatorSchema, leverageSchema, timestampSchema, tokenSchema } from "./primitives.schema.js";
|
|
3
|
+
import { z } from "zod/v4";
|
|
4
|
+
//#region src/model/opportunities.schema.ts
|
|
5
|
+
/**
|
|
6
|
+
* Runtime schemas for {@link ./opportunities.js}, see the note in
|
|
7
|
+
* `primitives.schema.ts` on why they are written by hand.
|
|
8
|
+
**/
|
|
9
|
+
/**
|
|
10
|
+
* {@link OpportunityKind}
|
|
11
|
+
**/
|
|
12
|
+
const opportunityKindSchema = z.union([z.literal("pool"), z.literal("strategy")]);
|
|
13
|
+
/**
|
|
14
|
+
* {@link PointsProgram}
|
|
15
|
+
**/
|
|
16
|
+
const pointsProgramSchema = z.object({
|
|
17
|
+
id: z.string(),
|
|
18
|
+
name: z.string(),
|
|
19
|
+
multiplier: z.number().nullable()
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* {@link TokenRewards}
|
|
23
|
+
**/
|
|
24
|
+
const tokenRewardsSchema = z.object({
|
|
25
|
+
kind: z.literal("token"),
|
|
26
|
+
token: tokenSchema,
|
|
27
|
+
supplyApr: bpsSchema.optional(),
|
|
28
|
+
borrowApr: bpsSchema.optional()
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* {@link PointRewards}
|
|
32
|
+
**/
|
|
33
|
+
const pointRewardsSchema = z.object({
|
|
34
|
+
kind: z.literal("point"),
|
|
35
|
+
points: z.array(pointsProgramSchema)
|
|
36
|
+
});
|
|
37
|
+
/**
|
|
38
|
+
* {@link Rewards}
|
|
39
|
+
**/
|
|
40
|
+
const rewardsSchema = z.discriminatedUnion("kind", [tokenRewardsSchema, pointRewardsSchema]);
|
|
41
|
+
/**
|
|
42
|
+
* {@link ApyBreakdown}
|
|
43
|
+
**/
|
|
44
|
+
const apyBreakdownSchema = z.object({
|
|
45
|
+
totalApy: bpsSchema,
|
|
46
|
+
organicApy: bpsSchema,
|
|
47
|
+
rewards: z.array(rewardsSchema)
|
|
48
|
+
});
|
|
49
|
+
/**
|
|
50
|
+
* {@link OpportunityBase}
|
|
51
|
+
**/
|
|
52
|
+
const opportunityBaseSchema = z.object({
|
|
53
|
+
chainId: chainIdSchema,
|
|
54
|
+
title: z.string(),
|
|
55
|
+
curator: curatorSchema,
|
|
56
|
+
underlyingToken: tokenSchema,
|
|
57
|
+
totalSupply: amountSchema,
|
|
58
|
+
totalBorrow: amountSchema,
|
|
59
|
+
utilization: bpsSchema,
|
|
60
|
+
supplyApy: apyBreakdownSchema.optional(),
|
|
61
|
+
collateralTokens: z.array(tokenSchema),
|
|
62
|
+
paused: z.boolean(),
|
|
63
|
+
rwa: z.boolean(),
|
|
64
|
+
sunset: z.boolean()
|
|
65
|
+
});
|
|
66
|
+
/**
|
|
67
|
+
* {@link PoolOpportunity}
|
|
68
|
+
**/
|
|
69
|
+
const poolOpportunitySchema = z.object({
|
|
70
|
+
...opportunityBaseSchema.shape,
|
|
71
|
+
kind: z.literal("pool"),
|
|
72
|
+
pool: ZodAddress()
|
|
73
|
+
});
|
|
74
|
+
/**
|
|
75
|
+
* {@link StrategyOpportunity}
|
|
76
|
+
**/
|
|
77
|
+
const strategyOpportunitySchema = z.object({
|
|
78
|
+
...opportunityBaseSchema.shape,
|
|
79
|
+
kind: z.literal("strategy"),
|
|
80
|
+
creditManager: ZodAddress(),
|
|
81
|
+
targetCollateral: tokenSchema,
|
|
82
|
+
liquidationThreshold: bpsSchema,
|
|
83
|
+
liquidationPremium: bpsSchema,
|
|
84
|
+
liquidationFee: bpsSchema,
|
|
85
|
+
expirationDate: timestampSchema.nullable(),
|
|
86
|
+
collateralApy: apyBreakdownSchema.optional(),
|
|
87
|
+
borrowApy: bpsSchema.optional(),
|
|
88
|
+
additionalBorrowApy: bpsSchema.optional(),
|
|
89
|
+
maxBorrowAmount: amountSchema,
|
|
90
|
+
maxLeverage: leverageSchema
|
|
91
|
+
});
|
|
92
|
+
/**
|
|
93
|
+
* {@link Opportunity}
|
|
94
|
+
**/
|
|
95
|
+
const opportunitySchema = z.discriminatedUnion("kind", [poolOpportunitySchema, strategyOpportunitySchema]);
|
|
96
|
+
/**
|
|
97
|
+
* {@link OpportunityFilter}
|
|
98
|
+
**/
|
|
99
|
+
const opportunityFilterSchema = z.object({
|
|
100
|
+
kind: opportunityKindSchema.optional(),
|
|
101
|
+
chainIds: z.array(chainIdSchema).optional(),
|
|
102
|
+
underlyingType: assetTypeSchema.optional(),
|
|
103
|
+
paused: z.boolean().optional(),
|
|
104
|
+
sunset: z.boolean().optional(),
|
|
105
|
+
rwa: z.boolean().optional()
|
|
106
|
+
});
|
|
107
|
+
/**
|
|
108
|
+
* {@link RateCurvePoint}
|
|
109
|
+
**/
|
|
110
|
+
const rateCurvePointSchema = z.object({
|
|
111
|
+
utilization: bpsSchema,
|
|
112
|
+
supplyApy: bpsSchema,
|
|
113
|
+
borrowApy: bpsSchema
|
|
114
|
+
});
|
|
115
|
+
/**
|
|
116
|
+
* {@link RateCurve}
|
|
117
|
+
**/
|
|
118
|
+
const rateCurveSchema = z.object({
|
|
119
|
+
points: z.array(rateCurvePointSchema),
|
|
120
|
+
borrowingLimitUtilization: bpsSchema.nullable()
|
|
121
|
+
});
|
|
122
|
+
/**
|
|
123
|
+
* {@link QuotaAsset}
|
|
124
|
+
**/
|
|
125
|
+
const quotaAssetSchema = z.object({
|
|
126
|
+
token: tokenSchema,
|
|
127
|
+
quotaRate: bpsSchema,
|
|
128
|
+
limit: amountSchema,
|
|
129
|
+
used: amountSchema
|
|
130
|
+
});
|
|
131
|
+
/**
|
|
132
|
+
* {@link PriceFeedData}. Recursive: a composite feed lists the feeds it reads.
|
|
133
|
+
**/
|
|
134
|
+
const priceFeedDataSchema = z.object({
|
|
135
|
+
name: z.string(),
|
|
136
|
+
type: z.string(),
|
|
137
|
+
feedAddress: ZodAddress(),
|
|
138
|
+
get dependencies() {
|
|
139
|
+
return z.array(priceFeedDataSchema);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
/**
|
|
143
|
+
* {@link PriceFeedSummary}
|
|
144
|
+
**/
|
|
145
|
+
const priceFeedSummarySchema = z.object({
|
|
146
|
+
underlyingPriceInUsd: z.number(),
|
|
147
|
+
collateralPriceInUsd: z.number(),
|
|
148
|
+
collateralPriceInUnderlying: z.number(),
|
|
149
|
+
underlyingFeed: priceFeedDataSchema,
|
|
150
|
+
collateralFeed: priceFeedDataSchema
|
|
151
|
+
});
|
|
152
|
+
/**
|
|
153
|
+
* {@link PoolOpportunityDetail}
|
|
154
|
+
**/
|
|
155
|
+
const poolOpportunityDetailSchema = z.object({
|
|
156
|
+
...poolOpportunitySchema.shape,
|
|
157
|
+
rateCurve: rateCurveSchema,
|
|
158
|
+
quotaAssets: z.array(quotaAssetSchema)
|
|
159
|
+
});
|
|
160
|
+
/**
|
|
161
|
+
* {@link StrategyOpportunityDetail}
|
|
162
|
+
**/
|
|
163
|
+
const strategyOpportunityDetailSchema = z.object({
|
|
164
|
+
...strategyOpportunitySchema.shape,
|
|
165
|
+
rateCurve: rateCurveSchema,
|
|
166
|
+
priceFeeds: priceFeedSummarySchema
|
|
167
|
+
});
|
|
168
|
+
/**
|
|
169
|
+
* {@link OpportunityDetail}
|
|
170
|
+
**/
|
|
171
|
+
const opportunityDetailSchema = z.discriminatedUnion("kind", [poolOpportunityDetailSchema, strategyOpportunityDetailSchema]);
|
|
172
|
+
/**
|
|
173
|
+
* {@link PoolOpportunityKey}
|
|
174
|
+
**/
|
|
175
|
+
const poolOpportunityKeySchema = z.object({
|
|
176
|
+
chainId: chainIdSchema,
|
|
177
|
+
pool: ZodAddress()
|
|
178
|
+
});
|
|
179
|
+
/**
|
|
180
|
+
* {@link StrategyOpportunityKey}
|
|
181
|
+
**/
|
|
182
|
+
const strategyOpportunityKeySchema = z.object({
|
|
183
|
+
chainId: chainIdSchema,
|
|
184
|
+
creditManager: ZodAddress(),
|
|
185
|
+
targetCollateral: ZodAddress()
|
|
186
|
+
});
|
|
187
|
+
/**
|
|
188
|
+
* {@link OpportunityKey}
|
|
189
|
+
**/
|
|
190
|
+
const opportunityKeySchema = z.discriminatedUnion("kind", [z.object({
|
|
191
|
+
kind: z.literal("pool"),
|
|
192
|
+
...poolOpportunityKeySchema.shape
|
|
193
|
+
}), z.object({
|
|
194
|
+
kind: z.literal("strategy"),
|
|
195
|
+
...strategyOpportunityKeySchema.shape
|
|
196
|
+
})]);
|
|
197
|
+
//#endregion
|
|
198
|
+
export { apyBreakdownSchema, opportunityBaseSchema, opportunityDetailSchema, opportunityFilterSchema, opportunityKeySchema, opportunityKindSchema, opportunitySchema, pointRewardsSchema, pointsProgramSchema, poolOpportunityDetailSchema, poolOpportunityKeySchema, poolOpportunitySchema, priceFeedDataSchema, priceFeedSummarySchema, quotaAssetSchema, rateCurvePointSchema, rateCurveSchema, rewardsSchema, strategyOpportunityDetailSchema, strategyOpportunityKeySchema, strategyOpportunitySchema, tokenRewardsSchema };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "module","sideEffects":false}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ZodAddress, ZodHex } from "../sdk/utils/zod.js";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
//#region src/model/primitives.schema.ts
|
|
4
|
+
/**
|
|
5
|
+
* Runtime schemas for {@link ./primitives.js}.
|
|
6
|
+
*
|
|
7
|
+
* The hand-written types are the contract; these schemas exist so the offchain
|
|
8
|
+
* client can reject a backend response that no longer matches it. The two are
|
|
9
|
+
* kept in sync by the type-level tests in `model.test-d.ts`, not by inferring
|
|
10
|
+
* one from the other.
|
|
11
|
+
**/
|
|
12
|
+
/**
|
|
13
|
+
* {@link ChainId}
|
|
14
|
+
**/
|
|
15
|
+
const chainIdSchema = z.number().int().positive();
|
|
16
|
+
/**
|
|
17
|
+
* {@link Timestamp}
|
|
18
|
+
**/
|
|
19
|
+
const timestampSchema = z.number().int().nonnegative();
|
|
20
|
+
/**
|
|
21
|
+
* {@link Bps}. Integer, not bounded to `0..10000`: rates may exceed 100%.
|
|
22
|
+
**/
|
|
23
|
+
const bpsSchema = z.number().int();
|
|
24
|
+
/**
|
|
25
|
+
* {@link AssetType}
|
|
26
|
+
**/
|
|
27
|
+
const assetTypeSchema = z.union([
|
|
28
|
+
z.literal("Stable"),
|
|
29
|
+
z.literal("ETH"),
|
|
30
|
+
z.literal("BTC")
|
|
31
|
+
]);
|
|
32
|
+
/**
|
|
33
|
+
* {@link Leverage}
|
|
34
|
+
**/
|
|
35
|
+
const leverageSchema = z.number().positive();
|
|
36
|
+
/**
|
|
37
|
+
* {@link Amount}
|
|
38
|
+
**/
|
|
39
|
+
const amountSchema = z.object({
|
|
40
|
+
value: z.bigint(),
|
|
41
|
+
valueUsd: z.number().nullable()
|
|
42
|
+
});
|
|
43
|
+
/**
|
|
44
|
+
* {@link Token}
|
|
45
|
+
**/
|
|
46
|
+
const tokenSchema = z.object({
|
|
47
|
+
chainId: chainIdSchema,
|
|
48
|
+
address: ZodAddress(),
|
|
49
|
+
symbol: z.string(),
|
|
50
|
+
name: z.string(),
|
|
51
|
+
decimals: z.number().int().nonnegative(),
|
|
52
|
+
assetType: assetTypeSchema.optional()
|
|
53
|
+
});
|
|
54
|
+
/**
|
|
55
|
+
* {@link TokenAmount}
|
|
56
|
+
**/
|
|
57
|
+
const tokenAmountSchema = amountSchema.extend({ token: tokenSchema });
|
|
58
|
+
/**
|
|
59
|
+
* {@link TxCall}
|
|
60
|
+
**/
|
|
61
|
+
const txCallSchema = z.object({
|
|
62
|
+
to: ZodAddress(),
|
|
63
|
+
callData: ZodHex(),
|
|
64
|
+
value: z.bigint().optional()
|
|
65
|
+
});
|
|
66
|
+
/**
|
|
67
|
+
* {@link Curator}
|
|
68
|
+
**/
|
|
69
|
+
const curatorSchema = z.object({
|
|
70
|
+
address: ZodAddress(),
|
|
71
|
+
name: z.string(),
|
|
72
|
+
url: z.string().nullable()
|
|
73
|
+
});
|
|
74
|
+
//#endregion
|
|
75
|
+
export { amountSchema, assetTypeSchema, bpsSchema, chainIdSchema, curatorSchema, leverageSchema, timestampSchema, tokenAmountSchema, tokenSchema, txCallSchema };
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { getNetworkType } from "../sdk/chain/chains.js";
|
|
2
|
+
import { AllSourcesFailedError } from "./types.js";
|
|
3
|
+
//#region src/new-sdk/AbstractNamespace.ts
|
|
4
|
+
/**
|
|
5
|
+
* Shared machinery of every {@link GearboxSDK} namespace.
|
|
6
|
+
*
|
|
7
|
+
* A namespace is a stateless router: it asks the sources its mode allows, in
|
|
8
|
+
* parallel, merges what came back and reports what did not. This class owns
|
|
9
|
+
* that routing — per-chain failure capture, backend degradation, the response
|
|
10
|
+
* envelope and the all-sources-failed rule — so a namespace holds nothing but
|
|
11
|
+
* its reads and its merge policy.
|
|
12
|
+
*
|
|
13
|
+
* The sources are whatever it was handed, and their readiness is their owner's
|
|
14
|
+
* business: a source that is not ready to answer fails the read it was given,
|
|
15
|
+
* and that failure is reported like any other.
|
|
16
|
+
*
|
|
17
|
+
* All protocol knowledge lives in the sources: `MultichainSDK` for the chain,
|
|
18
|
+
* `GearboxAPI` for the backend. None of it is repeated here or in a subclass.
|
|
19
|
+
*
|
|
20
|
+
* @typeParam ListRow - Row type of the namespace's list read.
|
|
21
|
+
**/
|
|
22
|
+
var AbstractNamespace = class {
|
|
23
|
+
logger;
|
|
24
|
+
#onchain;
|
|
25
|
+
#offchain;
|
|
26
|
+
constructor(name, onchain, offchain, logger) {
|
|
27
|
+
this.#onchain = onchain;
|
|
28
|
+
this.#offchain = offchain;
|
|
29
|
+
this.logger = logger?.child?.({ name }) ?? logger;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Asks both sources in parallel and combines whatever answered.
|
|
33
|
+
*
|
|
34
|
+
* The action name is only used in diagnostics and in the error raised when
|
|
35
|
+
* every source the read had failed.
|
|
36
|
+
**/
|
|
37
|
+
async read(action, fromChain, fromBackend, combine) {
|
|
38
|
+
const [onchain, offchain] = await Promise.all([this.fromChain(fromChain), this.fromBackend(fromBackend)]);
|
|
39
|
+
const meta = toMeta(onchain, offchain);
|
|
40
|
+
this.assertAnySourceSucceeded(action, meta);
|
|
41
|
+
return {
|
|
42
|
+
result: combine(onchain.value, offchain.value),
|
|
43
|
+
meta
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Reads the namespace's list from both sources, unioned by
|
|
48
|
+
* {@link AbstractNamespace.mergeList}.
|
|
49
|
+
**/
|
|
50
|
+
async readList(action, fromChain, fromBackend) {
|
|
51
|
+
return this.read(action, fromChain, fromBackend, (onchain, offchain) => this.mergeList(onchain ?? [], offchain ?? []));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Reads one row from both sources and merges them under
|
|
55
|
+
* {@link AbstractNamespace.mergeOne}. The key names the chain, so the
|
|
56
|
+
* on-chain leg queries exactly one.
|
|
57
|
+
**/
|
|
58
|
+
async readOne(action, chainId, fromChain, fromBackend) {
|
|
59
|
+
return this.read(action, async (sdk) => {
|
|
60
|
+
const network = this.networkOf(chainId);
|
|
61
|
+
if (!network) return { chains: [] };
|
|
62
|
+
return this.onOneChain(action, network, () => fromChain(sdk));
|
|
63
|
+
}, fromBackend, (onchain, offchain) => {
|
|
64
|
+
if (onchain && offchain) return this.mergeOne(onchain, offchain);
|
|
65
|
+
return onchain ?? offchain;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Reads something only the backend can answer.
|
|
70
|
+
*
|
|
71
|
+
* The fallback stands in for a backend that answered with nothing, which is
|
|
72
|
+
* a different thing from a backend that failed and was dropped.
|
|
73
|
+
**/
|
|
74
|
+
async readOffchain(action, fromBackend, fallback) {
|
|
75
|
+
const offchain = await this.fromBackend(fromBackend);
|
|
76
|
+
const meta = toMeta({ chains: [] }, offchain);
|
|
77
|
+
this.assertAnySourceSucceeded(action, meta);
|
|
78
|
+
return {
|
|
79
|
+
result: offchain.value ?? fallback,
|
|
80
|
+
meta
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Runs a read against the chain, or reports the chain as absent when the
|
|
85
|
+
* namespace has no on-chain source at all.
|
|
86
|
+
**/
|
|
87
|
+
async fromChain(run) {
|
|
88
|
+
return this.#onchain ? run(this.#onchain) : { chains: [] };
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Runs a read against the backend, turning any rejection into metadata so
|
|
92
|
+
* that a dead backend degrades a `both`-mode read instead of failing it.
|
|
93
|
+
**/
|
|
94
|
+
async fromBackend(run) {
|
|
95
|
+
if (!this.#offchain) return {};
|
|
96
|
+
try {
|
|
97
|
+
const { result, meta } = await run(this.#offchain);
|
|
98
|
+
return {
|
|
99
|
+
value: meta.status === "success" ? result : void 0,
|
|
100
|
+
status: meta
|
|
101
|
+
};
|
|
102
|
+
} catch (error) {
|
|
103
|
+
this.logger?.warn(error, "offchain read failed");
|
|
104
|
+
return { status: {
|
|
105
|
+
status: "error",
|
|
106
|
+
error
|
|
107
|
+
} };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Runs a read against one chain, turning a rejection into that chain's
|
|
112
|
+
* metadata entry.
|
|
113
|
+
**/
|
|
114
|
+
async onOneChain(action, network, run) {
|
|
115
|
+
try {
|
|
116
|
+
return {
|
|
117
|
+
value: await run(),
|
|
118
|
+
chains: [{
|
|
119
|
+
network,
|
|
120
|
+
status: "success"
|
|
121
|
+
}]
|
|
122
|
+
};
|
|
123
|
+
} catch (error) {
|
|
124
|
+
this.logger?.warn(error, `failed to ${action} on ${network}`);
|
|
125
|
+
return { chains: [{
|
|
126
|
+
network,
|
|
127
|
+
status: "error",
|
|
128
|
+
error
|
|
129
|
+
}] };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* A read that reached at least one source returns what it has; a read where
|
|
134
|
+
* every source it had failed throws, because an empty result would otherwise
|
|
135
|
+
* be indistinguishable from "there is nothing here".
|
|
136
|
+
**/
|
|
137
|
+
assertAnySourceSucceeded(action, meta) {
|
|
138
|
+
const asked = meta.chains.length > 0 || meta.offchain !== void 0;
|
|
139
|
+
const answered = meta.chains.some((c) => c.status === "success") || meta.offchain?.status === "success";
|
|
140
|
+
if (asked && !answered) throw new AllSourcesFailedError(action, meta);
|
|
141
|
+
}
|
|
142
|
+
networkOf(chainId) {
|
|
143
|
+
try {
|
|
144
|
+
return getNetworkType(chainId);
|
|
145
|
+
} catch {
|
|
146
|
+
this.logger?.debug(`chain ${chainId} is not a Gearbox network, skipping the onchain source`);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
function toMeta(onchain, offchain) {
|
|
152
|
+
const meta = { chains: onchain.chains };
|
|
153
|
+
if (offchain.status) meta.offchain = offchain.status;
|
|
154
|
+
return meta;
|
|
155
|
+
}
|
|
156
|
+
//#endregion
|
|
157
|
+
export { AbstractNamespace };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { MultichainSDK } from "../sdk/MultichainSDK.js";
|
|
2
|
+
import "../sdk/index.js";
|
|
3
|
+
import { GearboxAPI } from "../offchain/GearboxAPI.js";
|
|
4
|
+
import "../offchain/index.js";
|
|
5
|
+
import { OpportunitiesNamespace } from "./opportunities/OpportunitiesNamespace.js";
|
|
6
|
+
import "./opportunities/index.js";
|
|
7
|
+
//#region src/new-sdk/GearboxSDK.ts
|
|
8
|
+
/**
|
|
9
|
+
* Combined entry point over both sources of Gearbox data: the chain and the
|
|
10
|
+
* Gearbox backend.
|
|
11
|
+
*
|
|
12
|
+
* The {@link Mode} is fixed at construction and decides which methods exist,
|
|
13
|
+
* not what they return, so a screen written against `both` cannot silently
|
|
14
|
+
* degrade to a subset when someone reconfigures the SDK:
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const sdk = new GearboxSDK({
|
|
18
|
+
* mode: "both",
|
|
19
|
+
* networks: ["Mainnet"],
|
|
20
|
+
* onchain: { chains: { Mainnet: { rpcURLs } } },
|
|
21
|
+
* offchain: { baseUrl: "https://api.gearbox.fi" },
|
|
22
|
+
* });
|
|
23
|
+
* await sdk.attach();
|
|
24
|
+
*
|
|
25
|
+
* const { result, meta } = await sdk.opportunities.list({ kind: "strategy" });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* Every read answers with `{ result, meta }`: `meta` says which chains and
|
|
29
|
+
* which backend answered, because a partial answer is the normal case for a
|
|
30
|
+
* multi-chain read.
|
|
31
|
+
*
|
|
32
|
+
* @typeParam M - Mode the instance was built in.
|
|
33
|
+
**/
|
|
34
|
+
var GearboxSDK = class {
|
|
35
|
+
/**
|
|
36
|
+
* Sources this instance reads from.
|
|
37
|
+
**/
|
|
38
|
+
mode;
|
|
39
|
+
/**
|
|
40
|
+
* Chains this instance covers. Authoritative even when an injected on-chain
|
|
41
|
+
* SDK covers a different set.
|
|
42
|
+
**/
|
|
43
|
+
networks;
|
|
44
|
+
/**
|
|
45
|
+
* Namespace for pool and strategy opportunities.
|
|
46
|
+
**/
|
|
47
|
+
opportunities;
|
|
48
|
+
#attachOptions;
|
|
49
|
+
#onchain;
|
|
50
|
+
#offchain;
|
|
51
|
+
/**
|
|
52
|
+
* Whether this instance built the on-chain SDK, and therefore attaches it.
|
|
53
|
+
**/
|
|
54
|
+
#ownsOnchain = false;
|
|
55
|
+
#attached;
|
|
56
|
+
constructor(options) {
|
|
57
|
+
const { mode, networks, onchain, offchain, attach, logger } = options;
|
|
58
|
+
this.mode = mode;
|
|
59
|
+
this.networks = [...networks];
|
|
60
|
+
this.#attachOptions = attach;
|
|
61
|
+
const needsOnchain = mode === "onchain" || mode === "both";
|
|
62
|
+
const needsOffchain = mode === "offchain" || mode === "both";
|
|
63
|
+
if (needsOnchain && !onchain) throw new Error(`GearboxSDK in ${mode} mode needs an onchain source`);
|
|
64
|
+
if (needsOffchain && !offchain) throw new Error(`GearboxSDK in ${mode} mode needs an offchain source`);
|
|
65
|
+
if (!needsOnchain && onchain) logger?.warn(`GearboxSDK in ${mode} mode ignores the onchain source it was given`);
|
|
66
|
+
if (!needsOffchain && offchain) logger?.warn(`GearboxSDK in ${mode} mode ignores the offchain source it was given`);
|
|
67
|
+
if (needsOnchain && onchain) if (onchain instanceof MultichainSDK) this.#onchain = onchain;
|
|
68
|
+
else {
|
|
69
|
+
this.#onchain = new MultichainSDK({
|
|
70
|
+
logger,
|
|
71
|
+
...onchain
|
|
72
|
+
});
|
|
73
|
+
this.#ownsOnchain = true;
|
|
74
|
+
}
|
|
75
|
+
if (needsOffchain && offchain) this.#offchain = offchain instanceof GearboxAPI ? offchain : new GearboxAPI({
|
|
76
|
+
logger,
|
|
77
|
+
...offchain
|
|
78
|
+
});
|
|
79
|
+
this.#attached = !this.#ownsOnchain;
|
|
80
|
+
this.opportunities = new OpportunitiesNamespace(this.#onchain, this.#offchain, logger);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Attaches the on-chain SDK when this instance owns one.
|
|
84
|
+
*
|
|
85
|
+
* Reads issued before this resolves still reach the chain and fail there, so
|
|
86
|
+
* in `both` mode they are served from the backend alone and report the chain
|
|
87
|
+
* as failed in their meta, rather than blocking.
|
|
88
|
+
*
|
|
89
|
+
* A no-op in `offchain` mode and when an already-attached SDK was injected.
|
|
90
|
+
**/
|
|
91
|
+
async attach() {
|
|
92
|
+
if (this.#attached || !this.#ownsOnchain || !this.#onchain) return;
|
|
93
|
+
await this.#onchain.attach(this.#attachOptions);
|
|
94
|
+
this.#attached = true;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Whether the on-chain source is ready to be read from. Always `true` in
|
|
98
|
+
* `offchain` mode, where there is nothing to wait for.
|
|
99
|
+
**/
|
|
100
|
+
get attached() {
|
|
101
|
+
return this.mode === "offchain" ? true : this.#attached;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The underlying on-chain SDK, for everything this facade does not expose
|
|
105
|
+
* yet: markets, credit accounts, transaction building.
|
|
106
|
+
*
|
|
107
|
+
* `undefined` in `offchain` mode. Otherwise it exists from construction on,
|
|
108
|
+
* but only answers reads once {@link attach} has resolved.
|
|
109
|
+
**/
|
|
110
|
+
get onchain() {
|
|
111
|
+
return this.#onchain;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The underlying backend client. `undefined` in `onchain` mode.
|
|
115
|
+
**/
|
|
116
|
+
get offchain() {
|
|
117
|
+
return this.#offchain;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
//#endregion
|
|
121
|
+
export { GearboxSDK };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AllSourcesFailedError } from "./types.js";
|
|
2
|
+
import { AbstractNamespace } from "./AbstractNamespace.js";
|
|
3
|
+
import { OpportunitiesNamespace } from "./opportunities/OpportunitiesNamespace.js";
|
|
4
|
+
import "./opportunities/index.js";
|
|
5
|
+
import { GearboxSDK } from "./GearboxSDK.js";
|
|
6
|
+
import "./utils/index.js";
|
|
7
|
+
export { AbstractNamespace, AllSourcesFailedError, GearboxSDK, OpportunitiesNamespace };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { opportunityId } from "../../model/opportunities.js";
|
|
2
|
+
import "../../model/index.js";
|
|
3
|
+
import { AbstractNamespace } from "../AbstractNamespace.js";
|
|
4
|
+
//#region src/new-sdk/opportunities/OpportunitiesNamespace.ts
|
|
5
|
+
/**
|
|
6
|
+
* Fields the backend owns even when the chain also fills them.
|
|
7
|
+
*
|
|
8
|
+
* Empty of on-chain conflicts today — every yield group here is off-chain only,
|
|
9
|
+
* so onchain-first would already yield the backend's value. It is written out
|
|
10
|
+
* anyway so that adding an on-chain estimate of one of them is a deliberate
|
|
11
|
+
* change to this list rather than a silent regression.
|
|
12
|
+
**/
|
|
13
|
+
const OFFCHAIN_OWNED_FIELDS = /* @__PURE__ */ new Set(["supplyApy", "collateralApy"]);
|
|
14
|
+
/**
|
|
15
|
+
* The `opportunities` namespace of the combined SDK.
|
|
16
|
+
*
|
|
17
|
+
* A stateless router over the two sources, see {@link AbstractNamespace} for
|
|
18
|
+
* the routing itself. What is specific to opportunities is the reads below and
|
|
19
|
+
* the merge policy at the bottom of the class.
|
|
20
|
+
*
|
|
21
|
+
* The class implements the methods of every mode; {@link GearboxSDK} exposes it
|
|
22
|
+
* as its mode's slice of {@link OpportunitiesByMode}, so calling a method the
|
|
23
|
+
* mode does not have is a compile error rather than a runtime one.
|
|
24
|
+
**/
|
|
25
|
+
var OpportunitiesNamespace = class extends AbstractNamespace {
|
|
26
|
+
constructor(onchain, offchain, logger) {
|
|
27
|
+
super("Opportunities", onchain, offchain, logger);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* {@inheritDoc OpportunitiesBase.list}
|
|
31
|
+
**/
|
|
32
|
+
async list(filter) {
|
|
33
|
+
return this.readList("list opportunities", async (sdk) => {
|
|
34
|
+
const { result, meta } = await sdk.opportunities.list(filter);
|
|
35
|
+
return {
|
|
36
|
+
value: result,
|
|
37
|
+
chains: meta
|
|
38
|
+
};
|
|
39
|
+
}, (api) => api.opportunities.list(filter));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* {@inheritDoc OpportunitiesBase.getPool}
|
|
43
|
+
**/
|
|
44
|
+
async getPool(key) {
|
|
45
|
+
return this.readOne("get pool opportunity", key.chainId, (sdk) => sdk.opportunities.getPool(key), (api) => api.opportunities.getPool(key));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* {@inheritDoc OpportunitiesBase.getStrategy}
|
|
49
|
+
**/
|
|
50
|
+
async getStrategy(key) {
|
|
51
|
+
return this.readOne("get strategy opportunity", key.chainId, (sdk) => sdk.opportunities.getStrategy(key), (api) => api.opportunities.getStrategy(key));
|
|
52
|
+
}
|
|
53
|
+
history(key) {
|
|
54
|
+
return { chart: (metric, range) => this.#chart(key, metric, range) };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Reads one chart of one opportunity from the backend.
|
|
58
|
+
*
|
|
59
|
+
* The metric a caller may name is gated by the reader's type, so the kind of
|
|
60
|
+
* the key is not re-checked here.
|
|
61
|
+
**/
|
|
62
|
+
async #chart(key, metric, range) {
|
|
63
|
+
const { result, meta } = await this.readOffchain(`get ${metric} history`, (api) => api.opportunities.getHistory({
|
|
64
|
+
opportunity: key,
|
|
65
|
+
range,
|
|
66
|
+
metric
|
|
67
|
+
}), {
|
|
68
|
+
metric,
|
|
69
|
+
points: [],
|
|
70
|
+
metadata: {}
|
|
71
|
+
});
|
|
72
|
+
return {
|
|
73
|
+
data: result.points,
|
|
74
|
+
metadata: {
|
|
75
|
+
...result.metadata,
|
|
76
|
+
source: meta
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Merges the two versions of one opportunity.
|
|
82
|
+
*
|
|
83
|
+
* The chain wins every field it fills; the backend fills the rest and owns
|
|
84
|
+
* {@link OFFCHAIN_OWNED_FIELDS}.
|
|
85
|
+
*
|
|
86
|
+
* The rule is onchain-first, field-wise, and never deeper than one level: a
|
|
87
|
+
* group like `totalSupply` or `supplyApy` is taken whole from one source, so
|
|
88
|
+
* a row never mixes an on-chain token amount with a backend dollar value
|
|
89
|
+
* derived from a different block.
|
|
90
|
+
**/
|
|
91
|
+
mergeOne(onchain, offchain) {
|
|
92
|
+
const merged = { ...onchain };
|
|
93
|
+
for (const [field, value] of Object.entries(offchain)) {
|
|
94
|
+
if (value === void 0) continue;
|
|
95
|
+
if (OFFCHAIN_OWNED_FIELDS.has(field) || merged[field] === void 0) merged[field] = value;
|
|
96
|
+
}
|
|
97
|
+
return merged;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Unions the two lists by canonical opportunity id.
|
|
101
|
+
*
|
|
102
|
+
* Rows present in both are merged by {@link OpportunitiesNamespace.mergeOne}.
|
|
103
|
+
* Rows only the backend knows are appended: a chain the SDK does not cover,
|
|
104
|
+
* or a market it has not loaded, is a reason to show more rather than fewer
|
|
105
|
+
* opportunities.
|
|
106
|
+
**/
|
|
107
|
+
mergeList(onchain, offchain) {
|
|
108
|
+
const byId = /* @__PURE__ */ new Map();
|
|
109
|
+
for (const row of onchain) byId.set(opportunityId(row), row);
|
|
110
|
+
const extra = [];
|
|
111
|
+
for (const row of offchain) {
|
|
112
|
+
const id = opportunityId(row);
|
|
113
|
+
const existing = byId.get(id);
|
|
114
|
+
if (existing) byId.set(id, this.mergeOne(existing, row));
|
|
115
|
+
else extra.push(row);
|
|
116
|
+
}
|
|
117
|
+
return [...byId.values(), ...extra];
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
//#endregion
|
|
121
|
+
export { OpportunitiesNamespace };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "module","sideEffects":false}
|