@gearbox-protocol/sdk 13.3.2 → 13.4.0-beta.1

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.
@@ -37,10 +37,6 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
37
37
  const cm = this.sdk.marketRegister.findCreditManager(
38
38
  targetContract.creditManager
39
39
  );
40
- const priceUpdatesCalls = targetContract.type === "creditAccount" ? await this.getPriceUpdatesForFacade({
41
- creditManager: targetContract.creditManager,
42
- creditAccount: targetContract
43
- }) : [];
44
40
  const permissions = defaultPermissions !== null ? defaultPermissions : await (0, import_viem.getContract)({
45
41
  address: botAddress,
46
42
  client: this.sdk.client,
@@ -64,7 +60,11 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
64
60
  args: [botAddress, permissions]
65
61
  })
66
62
  };
67
- const calls = [...priceUpdatesCalls, addBotCall];
63
+ const calls = targetContract.type === "creditAccount" ? await this.prependPriceUpdates(
64
+ targetContract.creditManager,
65
+ [addBotCall],
66
+ targetContract
67
+ ) : [addBotCall];
68
68
  const tx = targetContract.type === "creditAccount" ? cm.creditFacade.multicall(targetContract.creditAccount, calls) : void 0;
69
69
  return { tx, calls, creditFacade: cm.creditFacade };
70
70
  }
@@ -81,12 +81,7 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
81
81
  const cm = this.sdk.marketRegister.findCreditManager(
82
82
  creditAccount.creditManager
83
83
  );
84
- const priceUpdatesCalls = await this.getPriceUpdatesForFacade({
85
- creditManager: creditAccount.creditManager,
86
- creditAccount
87
- });
88
- const calls = [
89
- ...priceUpdatesCalls,
84
+ const operationCalls = [
90
85
  ...assetsToWithdraw.map(
91
86
  (a) => this.prepareWithdrawToken(
92
87
  creditAccount.creditFacade,
@@ -100,6 +95,11 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
100
95
  averageQuota
101
96
  })
102
97
  ];
98
+ const calls = await this.prependPriceUpdates(
99
+ creditAccount.creditManager,
100
+ operationCalls,
101
+ creditAccount
102
+ );
103
103
  const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
104
104
  return { tx, calls, creditFacade: cm.creditFacade };
105
105
  }
@@ -122,12 +122,7 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
122
122
  tokensToClaim,
123
123
  creditAccount: ca
124
124
  });
125
- const priceUpdates = await this.getPriceUpdatesForFacade({
126
- creditManager: ca.creditManager,
127
- creditAccount: ca
128
- });
129
- const calls = [
130
- ...operation === "close" ? [] : priceUpdates,
125
+ const operationCalls = [
131
126
  ...this.prepareAddCollateral(ca.creditFacade, addCollateral, permits),
132
127
  ...this.prepareDisableQuotas(ca),
133
128
  ...this.prepareDecreaseDebt(ca),
@@ -136,6 +131,7 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
136
131
  (t) => this.prepareWithdrawToken(ca.creditFacade, t.token, import_math.MAX_UINT256, to)
137
132
  )
138
133
  ];
134
+ const calls = operation === "close" ? operationCalls : await this.prependPriceUpdates(ca.creditManager, operationCalls, ca);
139
135
  const tx = operation === "close" ? cm.creditFacade.closeCreditAccount(ca.creditAccount, calls) : cm.creditFacade.multicall(ca.creditAccount, calls);
140
136
  return { tx, calls, creditFacade: cm.creditFacade };
141
137
  }
@@ -156,19 +152,19 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
156
152
  tokensToClaim,
157
153
  creditAccount: ca
158
154
  });
159
- const priceUpdates = await this.getPriceUpdatesForFacade({
160
- creditManager: ca.creditManager,
161
- creditAccount: ca
162
- });
163
155
  const addCollateral = collateralAssets.filter((a) => a.balance > 0);
164
- const calls = [
165
- ...priceUpdates,
156
+ const operationCalls = [
166
157
  ...this.prepareAddCollateral(ca.creditFacade, addCollateral, permits),
167
158
  ...claimPath.calls,
168
159
  ...assetsToWithdraw.map(
169
160
  (t) => this.prepareWithdrawToken(ca.creditFacade, t.token, import_math.MAX_UINT256, to)
170
161
  )
171
162
  ];
163
+ const calls = await this.prependPriceUpdates(
164
+ ca.creditManager,
165
+ operationCalls,
166
+ ca
167
+ );
172
168
  const tx = cm.creditFacade.liquidateCreditAccount(
173
169
  ca.creditAccount,
174
170
  to,
@@ -198,16 +194,15 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
198
194
  });
199
195
  }
200
196
  if (claimPath.calls.length === 0) throw new Error("No path to execute");
201
- const priceUpdatesCalls = await this.getPriceUpdatesForFacade({
202
- creditManager: ca.creditManager,
203
- creditAccount: ca,
204
- desiredQuotas: averageQuota
205
- });
206
- const calls = [
207
- ...priceUpdatesCalls,
197
+ const operationCalls = [
208
198
  ...claimPath.calls,
209
199
  ...this.prepareUpdateQuotas(ca.creditFacade, { minQuota, averageQuota })
210
200
  ];
201
+ const calls = await this.prependPriceUpdates(
202
+ ca.creditManager,
203
+ operationCalls,
204
+ ca
205
+ );
211
206
  const tx = cm.creditFacade.multicall(ca.creditAccount, calls);
212
207
  return { tx, calls, creditFacade: cm.creditFacade };
213
208
  }
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var multicall_utils_exports = {};
20
+ __export(multicall_utils_exports, {
21
+ extractPriceUpdates: () => extractPriceUpdates,
22
+ extractQuotaTokens: () => extractQuotaTokens,
23
+ mergePriceUpdates: () => mergePriceUpdates
24
+ });
25
+ module.exports = __toCommonJS(multicall_utils_exports);
26
+ var import_viem = require("viem");
27
+ var import_generated = require("../../abi/310/generated.js");
28
+ var import_AddressMap = require("../utils/AddressMap.js");
29
+ var import_AddressSet = require("../utils/AddressSet.js");
30
+ const ON_DEMAND_SELECTOR = (0, import_viem.toFunctionSelector)(
31
+ (0, import_viem.getAbiItem)({
32
+ abi: import_generated.iCreditFacadeMulticallV310Abi,
33
+ name: "onDemandPriceUpdates"
34
+ })
35
+ );
36
+ const UPDATE_QUOTA_SELECTOR = (0, import_viem.toFunctionSelector)(
37
+ (0, import_viem.getAbiItem)({
38
+ abi: import_generated.iCreditFacadeMulticallV310Abi,
39
+ name: "updateQuota"
40
+ })
41
+ );
42
+ function extractPriceUpdates(calls) {
43
+ const priceUpdates = [];
44
+ const remainingCalls = [];
45
+ for (const call of calls) {
46
+ if (isOnDemandPriceUpdateCall(call)) {
47
+ const decoded = (0, import_viem.decodeFunctionData)({
48
+ abi: import_generated.iCreditFacadeMulticallV310Abi,
49
+ data: call.callData
50
+ });
51
+ const updates = decoded.args[0];
52
+ priceUpdates.push(...updates);
53
+ } else {
54
+ remainingCalls.push(call);
55
+ }
56
+ }
57
+ return { priceUpdates, remainingCalls };
58
+ }
59
+ function extractQuotaTokens(calls) {
60
+ const tokens = new import_AddressSet.AddressSet();
61
+ for (const { callData } of calls) {
62
+ if (callData.slice(0, 10) !== UPDATE_QUOTA_SELECTOR) {
63
+ continue;
64
+ }
65
+ const decoded = (0, import_viem.decodeFunctionData)({
66
+ abi: import_generated.iCreditFacadeMulticallV310Abi,
67
+ data: callData
68
+ });
69
+ const [token, quotaChange] = decoded.args;
70
+ if (quotaChange > 0n) {
71
+ tokens.add(token);
72
+ }
73
+ }
74
+ return tokens;
75
+ }
76
+ function mergePriceUpdates(existing, generated) {
77
+ const seen = new import_AddressMap.AddressMap();
78
+ for (const u of [...generated, ...existing]) {
79
+ seen.upsert(u.priceFeed, u);
80
+ }
81
+ return seen.values();
82
+ }
83
+ function isOnDemandPriceUpdateCall(call) {
84
+ return call.callData.slice(0, 10) === ON_DEMAND_SELECTOR;
85
+ }
86
+ // Annotate the CommonJS export names for ESM import in node:
87
+ 0 && (module.exports = {
88
+ extractPriceUpdates,
89
+ extractQuotaTokens,
90
+ mergePriceUpdates
91
+ });
@@ -91,6 +91,12 @@ class CreditFacadeV310Contract extends import_CreditFacadeV310BaseContract.Credi
91
91
  args: [ca, calls]
92
92
  });
93
93
  }
94
+ botMulticall(ca, calls) {
95
+ return this.createRawTx({
96
+ functionName: "botMulticall",
97
+ args: [ca, calls]
98
+ });
99
+ }
94
100
  openCreditAccount(to, calls, referralCode) {
95
101
  return this.createRawTx({
96
102
  functionName: "openCreditAccount",
@@ -1,102 +1,12 @@
1
- import { getAddress, getContract } from "viem";
2
- import { iFarmingPoolAbi } from "../../abi/iFarmingPool.js";
1
+ import { getAddress } from "viem";
3
2
  import { BigIntMath } from "../../common-utils/index.js";
4
3
  import { chains, toBigInt } from "../../sdk/index.js";
5
4
  import {
6
5
  MerkleXYZApi
7
6
  } from "./merkl-api.js";
8
- class GearboxRewardsApi {
7
+ class RewardAmountAPI {
9
8
  constructor() {
10
9
  }
11
- static async getLmRewardsV3({
12
- pools,
13
- tokensList,
14
- provider,
15
- account,
16
- reportError
17
- }) {
18
- const poolByStakedDiesel = Object.values(pools).reduce((acc, p) => {
19
- p.stakedDieselToken.forEach((t) => {
20
- if (t) acc[t] = p.address;
21
- });
22
- p.stakedDieselToken_old.forEach((t) => {
23
- if (t) acc[t] = p.address;
24
- });
25
- return acc;
26
- }, {});
27
- const stakedDieselTokens = Object.keys(poolByStakedDiesel);
28
- const farmInfoCalls = stakedDieselTokens.map((address) => ({
29
- address,
30
- abi: iFarmingPoolAbi,
31
- functionName: "farmInfo",
32
- args: []
33
- }));
34
- const rewardTokenCalls = stakedDieselTokens.map((address) => ({
35
- address,
36
- abi: POOL_REWARDS_ABI,
37
- functionName: "rewardsToken",
38
- args: []
39
- }));
40
- const farmedCalls = stakedDieselTokens.map((address) => ({
41
- address,
42
- abi: iFarmingPoolAbi,
43
- functionName: "farmed",
44
- args: [account]
45
- }));
46
- const [response] = await Promise.allSettled([
47
- provider.multicall({
48
- allowFailure: false,
49
- contracts: [...farmInfoCalls, ...rewardTokenCalls, ...farmedCalls],
50
- batchSize: 0
51
- })
52
- ]);
53
- const safeResponse = GearboxRewardsApi.extractFulfilled(response, reportError, "v3Rewards") || [];
54
- const farmInfoCallsEnd = farmInfoCalls.length;
55
- const farmInfo = safeResponse.slice(
56
- 0,
57
- farmInfoCallsEnd
58
- );
59
- const rewardTokenCallsEnd = farmInfoCallsEnd + rewardTokenCalls.length;
60
- const rewardTokens = safeResponse.slice(
61
- farmInfoCallsEnd,
62
- rewardTokenCallsEnd
63
- );
64
- const farmedCallsEnd = rewardTokenCallsEnd + farmedCalls.length;
65
- const farmedList = safeResponse.slice(
66
- rewardTokenCallsEnd,
67
- farmedCallsEnd
68
- );
69
- const gearboxLmRewards = stakedDieselTokens.map(
70
- (address, i) => {
71
- const pool = poolByStakedDiesel[address];
72
- const info = farmInfo[i];
73
- const rewardToken = rewardTokens[i]?.toLowerCase();
74
- const farmed = farmedList[i];
75
- if (!pool || !info || !rewardToken) return void 0;
76
- return {
77
- pool,
78
- poolToken: address,
79
- rewardToken,
80
- rewardTokenDecimals: tokensList[rewardToken]?.decimals || 18,
81
- rewardTokenSymbol: tokensList[rewardToken]?.symbol || "unknown",
82
- amount: farmed ?? 0n,
83
- type: "stakedV3"
84
- };
85
- }
86
- );
87
- const { zero, nonZero } = gearboxLmRewards.reduce(
88
- (acc, r) => {
89
- if (r && r.amount > 0n) {
90
- acc.nonZero.push(r);
91
- } else if (r) {
92
- acc.zero.push(r);
93
- }
94
- return acc;
95
- },
96
- { nonZero: [], zero: [] }
97
- );
98
- return [...nonZero, zero];
99
- }
100
10
  static async getLmRewardsMerkle({
101
11
  pools,
102
12
  account,
@@ -113,7 +23,7 @@ class GearboxRewardsApi {
113
23
  })
114
24
  )
115
25
  ]);
116
- const merkleXYZLm = GearboxRewardsApi.extractFulfilled(
26
+ const merkleXYZLm = RewardAmountAPI.extractFulfilled(
117
27
  merkleXYZLMResponse,
118
28
  reportError,
119
29
  "merkleXYZLm"
@@ -168,37 +78,7 @@ class GearboxRewardsApi {
168
78
  return void 0;
169
79
  }
170
80
  }
171
- static async claimLmRewardsV3({
172
- reward,
173
- signer,
174
- account
175
- }) {
176
- const pool = getContract({
177
- address: reward.poolToken,
178
- abi: iFarmingPoolAbi,
179
- client: signer
180
- });
181
- return pool.write.claim({
182
- account,
183
- chain: signer.chain
184
- });
185
- }
186
81
  }
187
- const POOL_REWARDS_ABI = [
188
- {
189
- inputs: [],
190
- name: "rewardsToken",
191
- outputs: [
192
- {
193
- internalType: "contract IERC20",
194
- name: "",
195
- type: "address"
196
- }
197
- ],
198
- stateMutability: "view",
199
- type: "function"
200
- }
201
- ];
202
82
  export {
203
- GearboxRewardsApi
83
+ RewardAmountAPI
204
84
  };
@@ -17,7 +17,7 @@ function getKeyForPoolPointsInfo(i) {
17
17
  i.condition
18
18
  ].join("-");
19
19
  }
20
- class GearboxRewardsExtraApy {
20
+ class PoolPointsAPI {
21
21
  constructor() {
22
22
  }
23
23
  static async getTotalTokensOnProtocol({
@@ -27,9 +27,7 @@ class GearboxRewardsExtraApy {
27
27
  }) {
28
28
  const list = [...new Set(tokensToCheck)];
29
29
  const res = await Promise.allSettled(
30
- list.map(
31
- (t) => GearboxRewardsExtraApy.getTokenTotal(t, network, tokensList)
32
- )
30
+ list.map((t) => PoolPointsAPI.getTokenTotal(t, network, tokensList))
33
31
  );
34
32
  return res.map((r, i) => [
35
33
  list[i],
@@ -59,7 +57,7 @@ class GearboxRewardsExtraApy {
59
57
  (acc2, pointsInfo2) => {
60
58
  const { address: tokenAddress } = tokensList[pointsInfo2.token] || {};
61
59
  const tokenBalance = totalTokenBalances[tokenAddress || ""];
62
- const points = GearboxRewardsExtraApy.getPoolTokenPoints(
60
+ const points = PoolPointsAPI.getPoolTokenPoints(
63
61
  tokenBalance,
64
62
  p,
65
63
  tokensList,
@@ -96,6 +94,6 @@ class GearboxRewardsExtraApy {
96
94
  }
97
95
  }
98
96
  export {
99
- GearboxRewardsExtraApy,
97
+ PoolPointsAPI,
100
98
  getKeyForPoolPointsInfo
101
99
  };
@@ -1,4 +1,2 @@
1
1
  export * from "./api.js";
2
- export * from "./apy.js";
3
2
  export * from "./extra-apy.js";
4
- export * from "./merkl-api.js";