@gearbox-protocol/sdk 3.0.0-vfour.283 → 3.0.0-vfour.284

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.
@@ -22,6 +22,7 @@ __export(RouterV310Contract_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(RouterV310Contract_exports);
24
24
  var import_routerV310 = require("../../abi/routerV310.js");
25
+ var import_AddressMap = require("../utils/AddressMap.js");
25
26
  var import_AbstractRouterContract = require("./AbstractRouterContract.js");
26
27
  var import_helpers = require("./helpers.js");
27
28
  const abi = import_routerV310.iGearboxRouterV310Abi;
@@ -45,14 +46,37 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
45
46
  * @returns
46
47
  */
47
48
  async findOneTokenPath(props) {
49
+ const {
50
+ creditAccount,
51
+ creditManager,
52
+ amount,
53
+ slippage,
54
+ tokenIn,
55
+ tokenOut
56
+ } = props;
57
+ const numSplits = this.#numSplitsGetter(
58
+ creditManager,
59
+ creditAccount.tokens
60
+ )(tokenIn);
61
+ this.logger?.debug(
62
+ {
63
+ creditAccount: creditAccount.creditAccount,
64
+ creditManager: this.labelAddress(creditManager.address),
65
+ tokenIn: this.labelAddress(tokenIn),
66
+ target: this.labelAddress(tokenOut),
67
+ amount,
68
+ slippage,
69
+ numSplits
70
+ },
71
+ "calling routeOneToOne"
72
+ );
48
73
  const { result } = await this.contract.simulate.routeOneToOne([
49
- props.creditAccount.creditAccount,
50
- props.tokenIn,
51
- props.amount,
52
- props.tokenOut,
53
- BigInt(props.slippage),
54
- 4n
55
- // TODO:? how many 4n or 0n for underlying
74
+ creditAccount.creditAccount,
75
+ tokenIn,
76
+ amount,
77
+ tokenOut,
78
+ BigInt(slippage),
79
+ numSplits
56
80
  ]);
57
81
  return {
58
82
  amount: result.amount,
@@ -83,15 +107,24 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
83
107
  (0, import_helpers.balancesMap)(expectedBalances),
84
108
  (0, import_helpers.balancesMap)(leftoverBalances)
85
109
  ];
110
+ const getNumSplits = this.#numSplitsGetter(cm, expectedBalances);
86
111
  const tData = cm.collateralTokens.map(
87
112
  (token) => ({
88
113
  token,
89
114
  balance: expectedMap.get(token) ?? 0n,
90
115
  leftoverBalance: leftoverMap.get(token) ?? 0n,
91
- numSplits: 4n
92
- // TODO:? how many 4n or 0n for underlying
116
+ numSplits: getNumSplits(token)
93
117
  })
94
118
  );
119
+ this.logger?.debug(
120
+ {
121
+ creditManager: this.labelAddress(cm.address),
122
+ target: this.labelAddress(target),
123
+ slippage,
124
+ tData: this.#debugTokenData(tData)
125
+ },
126
+ "calling routeOpenManyToOne"
127
+ );
95
128
  const { result } = await this.contract.simulate.routeOpenManyToOne([
96
129
  cm.address,
97
130
  target,
@@ -128,16 +161,26 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
128
161
  leftoverBalances: (0, import_helpers.assetsMap)(balances.leftoverBalances)
129
162
  } : void 0
130
163
  );
164
+ const getNumSplits = this.#numSplitsGetter(cm, expectedBalances.values());
131
165
  const tData = [];
132
166
  for (const token of cm.collateralTokens) {
133
167
  tData.push({
134
168
  token,
135
169
  balance: expectedBalances.get(token)?.balance || 0n,
136
170
  leftoverBalance: leftoverBalances.get(token)?.balance || 0n,
137
- numSplits: 4n
138
- // TODO:? how many 4n or 0n for underlying
171
+ numSplits: getNumSplits(token)
139
172
  });
140
173
  }
174
+ this.logger?.debug(
175
+ {
176
+ creditAccount: ca.creditAccount,
177
+ creditManager: this.labelAddress(cm.address),
178
+ target: this.labelAddress(ca.underlying),
179
+ slippage,
180
+ tData: this.#debugTokenData(tData)
181
+ },
182
+ "calling routeManyToOne"
183
+ );
141
184
  const { result } = await this.contract.simulate.routeManyToOne([
142
185
  cm.address,
143
186
  ca.underlying,
@@ -152,6 +195,35 @@ class RouterV310Contract extends import_AbstractRouterContract.AbstractRouterCon
152
195
  calls: [...result.calls]
153
196
  };
154
197
  }
198
+ #numSplitsGetter(creditManager, assets) {
199
+ const { priceOracle } = this.sdk.marketRegister.findByCreditManager(
200
+ creditManager.address
201
+ );
202
+ const inUSD = assets.filter(({ token, balance }) => {
203
+ const decimals = this.sdk.tokensMeta.decimals(token);
204
+ const minBalance = 10n ** BigInt(Math.max(8, decimals) - 8);
205
+ return balance >= minBalance;
206
+ }).map(({ token, balance }) => {
207
+ return {
208
+ token,
209
+ balance: priceOracle.convertToUSD(token, balance)
210
+ };
211
+ }).sort((a, b) => {
212
+ return b.balance > a.balance ? -1 : 1;
213
+ });
214
+ const map = new import_AddressMap.AddressMap(
215
+ inUSD.map(({ token }, i) => [token, i === 0 ? 4n : 1n])
216
+ );
217
+ return (token) => map.get(token) ?? 1n;
218
+ }
219
+ #debugTokenData(tData) {
220
+ return tData.map((t) => ({
221
+ token: this.labelAddress(t.token),
222
+ balance: this.sdk.tokensMeta.formatBN(t.token, t.balance),
223
+ leftoverBalance: this.sdk.tokensMeta.formatBN(t.token, t.leftoverBalance),
224
+ numSplits: t.numSplits
225
+ }));
226
+ }
155
227
  findAllSwaps(props) {
156
228
  throw ERR_NOT_IMPLEMENTED;
157
229
  }
@@ -1,4 +1,5 @@
1
1
  import { iGearboxRouterV310Abi } from "../../abi/routerV310.js";
2
+ import { AddressMap } from "../utils/AddressMap.js";
2
3
  import { AbstractRouterContract } from "./AbstractRouterContract.js";
3
4
  import { assetsMap, balancesMap } from "./helpers.js";
4
5
  const abi = iGearboxRouterV310Abi;
@@ -22,14 +23,37 @@ class RouterV310Contract extends AbstractRouterContract {
22
23
  * @returns
23
24
  */
24
25
  async findOneTokenPath(props) {
26
+ const {
27
+ creditAccount,
28
+ creditManager,
29
+ amount,
30
+ slippage,
31
+ tokenIn,
32
+ tokenOut
33
+ } = props;
34
+ const numSplits = this.#numSplitsGetter(
35
+ creditManager,
36
+ creditAccount.tokens
37
+ )(tokenIn);
38
+ this.logger?.debug(
39
+ {
40
+ creditAccount: creditAccount.creditAccount,
41
+ creditManager: this.labelAddress(creditManager.address),
42
+ tokenIn: this.labelAddress(tokenIn),
43
+ target: this.labelAddress(tokenOut),
44
+ amount,
45
+ slippage,
46
+ numSplits
47
+ },
48
+ "calling routeOneToOne"
49
+ );
25
50
  const { result } = await this.contract.simulate.routeOneToOne([
26
- props.creditAccount.creditAccount,
27
- props.tokenIn,
28
- props.amount,
29
- props.tokenOut,
30
- BigInt(props.slippage),
31
- 4n
32
- // TODO:? how many 4n or 0n for underlying
51
+ creditAccount.creditAccount,
52
+ tokenIn,
53
+ amount,
54
+ tokenOut,
55
+ BigInt(slippage),
56
+ numSplits
33
57
  ]);
34
58
  return {
35
59
  amount: result.amount,
@@ -60,15 +84,24 @@ class RouterV310Contract extends AbstractRouterContract {
60
84
  balancesMap(expectedBalances),
61
85
  balancesMap(leftoverBalances)
62
86
  ];
87
+ const getNumSplits = this.#numSplitsGetter(cm, expectedBalances);
63
88
  const tData = cm.collateralTokens.map(
64
89
  (token) => ({
65
90
  token,
66
91
  balance: expectedMap.get(token) ?? 0n,
67
92
  leftoverBalance: leftoverMap.get(token) ?? 0n,
68
- numSplits: 4n
69
- // TODO:? how many 4n or 0n for underlying
93
+ numSplits: getNumSplits(token)
70
94
  })
71
95
  );
96
+ this.logger?.debug(
97
+ {
98
+ creditManager: this.labelAddress(cm.address),
99
+ target: this.labelAddress(target),
100
+ slippage,
101
+ tData: this.#debugTokenData(tData)
102
+ },
103
+ "calling routeOpenManyToOne"
104
+ );
72
105
  const { result } = await this.contract.simulate.routeOpenManyToOne([
73
106
  cm.address,
74
107
  target,
@@ -105,16 +138,26 @@ class RouterV310Contract extends AbstractRouterContract {
105
138
  leftoverBalances: assetsMap(balances.leftoverBalances)
106
139
  } : void 0
107
140
  );
141
+ const getNumSplits = this.#numSplitsGetter(cm, expectedBalances.values());
108
142
  const tData = [];
109
143
  for (const token of cm.collateralTokens) {
110
144
  tData.push({
111
145
  token,
112
146
  balance: expectedBalances.get(token)?.balance || 0n,
113
147
  leftoverBalance: leftoverBalances.get(token)?.balance || 0n,
114
- numSplits: 4n
115
- // TODO:? how many 4n or 0n for underlying
148
+ numSplits: getNumSplits(token)
116
149
  });
117
150
  }
151
+ this.logger?.debug(
152
+ {
153
+ creditAccount: ca.creditAccount,
154
+ creditManager: this.labelAddress(cm.address),
155
+ target: this.labelAddress(ca.underlying),
156
+ slippage,
157
+ tData: this.#debugTokenData(tData)
158
+ },
159
+ "calling routeManyToOne"
160
+ );
118
161
  const { result } = await this.contract.simulate.routeManyToOne([
119
162
  cm.address,
120
163
  ca.underlying,
@@ -129,6 +172,35 @@ class RouterV310Contract extends AbstractRouterContract {
129
172
  calls: [...result.calls]
130
173
  };
131
174
  }
175
+ #numSplitsGetter(creditManager, assets) {
176
+ const { priceOracle } = this.sdk.marketRegister.findByCreditManager(
177
+ creditManager.address
178
+ );
179
+ const inUSD = assets.filter(({ token, balance }) => {
180
+ const decimals = this.sdk.tokensMeta.decimals(token);
181
+ const minBalance = 10n ** BigInt(Math.max(8, decimals) - 8);
182
+ return balance >= minBalance;
183
+ }).map(({ token, balance }) => {
184
+ return {
185
+ token,
186
+ balance: priceOracle.convertToUSD(token, balance)
187
+ };
188
+ }).sort((a, b) => {
189
+ return b.balance > a.balance ? -1 : 1;
190
+ });
191
+ const map = new AddressMap(
192
+ inUSD.map(({ token }, i) => [token, i === 0 ? 4n : 1n])
193
+ );
194
+ return (token) => map.get(token) ?? 1n;
195
+ }
196
+ #debugTokenData(tData) {
197
+ return tData.map((t) => ({
198
+ token: this.labelAddress(t.token),
199
+ balance: this.sdk.tokensMeta.formatBN(t.token, t.balance),
200
+ leftoverBalance: this.sdk.tokensMeta.formatBN(t.token, t.leftoverBalance),
201
+ numSplits: t.numSplits
202
+ }));
203
+ }
132
204
  findAllSwaps(props) {
133
205
  throw ERR_NOT_IMPLEMENTED;
134
206
  }
@@ -350,6 +350,7 @@ declare const abi: readonly [{
350
350
  }];
351
351
  type abi = typeof abi;
352
352
  export declare class RouterV310Contract extends AbstractRouterContract<abi> implements IRouterContract {
353
+ #private;
353
354
  constructor(sdk: GearboxSDK, address: Address);
354
355
  /**
355
356
  * Finds best path to swap all Normal tokens and tokens "on the way" to target one and vice versa
@@ -59,6 +59,9 @@ export interface FindAllSwapsProps {
59
59
  tokenOut: Address;
60
60
  amount: bigint;
61
61
  leftoverAmount: bigint;
62
+ /**
63
+ * Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
64
+ */
62
65
  slippage: number | bigint;
63
66
  }
64
67
  export interface FindClosePathInput {
@@ -73,6 +76,9 @@ export interface FindOneTokenPathProps {
73
76
  tokenIn: Address;
74
77
  tokenOut: Address;
75
78
  amount: bigint;
79
+ /**
80
+ * Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
81
+ */
76
82
  slippage: number | bigint;
77
83
  }
78
84
  export interface FindOpenStrategyPathProps {
@@ -80,16 +86,30 @@ export interface FindOpenStrategyPathProps {
80
86
  expectedBalances: Array<Asset>;
81
87
  leftoverBalances: Array<Asset>;
82
88
  target: Address;
89
+ /**
90
+ * Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
91
+ */
83
92
  slippage: number | bigint;
84
93
  }
85
94
  export interface FindBestClosePathProps {
86
95
  creditAccount: RouterCASlice;
87
96
  creditManager: RouterCMSlice;
97
+ /**
98
+ * Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
99
+ */
88
100
  slippage: bigint | number;
89
101
  balances?: ClosePathBalances;
90
102
  }
91
103
  export interface ClosePathBalances {
104
+ /**
105
+ * Current balances or expected balances after some actions (add/withdraw collateral, for example),
106
+ * if these actions are before router calls
107
+ */
92
108
  expectedBalances: Array<Asset>;
109
+ /**
110
+ * Balances to keep on account after all actions.
111
+ * If the final balance is 0, we set 1 for gas optimization, except for forbidden tokens
112
+ */
93
113
  leftoverBalances: Array<Asset>;
94
114
  }
95
115
  export interface IRouterContract extends IBaseContract {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "3.0.0-vfour.283",
3
+ "version": "3.0.0-vfour.284",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",