@evaafi/sdk 0.5.4 → 0.5.6

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.
Files changed (48) hide show
  1. package/dist/api/helpers.js +2 -3
  2. package/dist/api/math.d.ts +24 -2
  3. package/dist/api/math.js +189 -62
  4. package/dist/api/parser.d.ts +3 -3
  5. package/dist/api/parser.js +37 -25
  6. package/dist/api/prices.d.ts +1 -1
  7. package/dist/api/prices.js +3 -3
  8. package/dist/config.d.ts +2 -0
  9. package/dist/config.js +5 -0
  10. package/dist/constants/assets.d.ts +2 -3
  11. package/dist/constants/assets.js +10 -2
  12. package/dist/constants/general.d.ts +3 -2
  13. package/dist/constants/general.js +6 -5
  14. package/dist/constants/pools.d.ts +0 -1
  15. package/dist/constants/pools.js +1 -14
  16. package/dist/constants.d.ts +5 -34
  17. package/dist/constants.js +90 -37
  18. package/dist/contracts/MasterContract.d.ts +13 -45
  19. package/dist/contracts/MasterContract.js +10 -12
  20. package/dist/contracts/UserContract.js +7 -7
  21. package/dist/index.d.ts +6 -5
  22. package/dist/index.js +5 -3
  23. package/dist/types/Master.d.ts +8 -7
  24. package/dist/types/User.d.ts +5 -5
  25. package/dist/utils/merkleProof.d.ts +4 -0
  26. package/dist/utils/merkleProof.js +107 -0
  27. package/dist/utils/priceUtils.d.ts +55 -0
  28. package/dist/utils/priceUtils.js +117 -0
  29. package/dist/utils/sha256BigInt.js +1 -2
  30. package/dist/utils/tonConnectSender.js +2 -3
  31. package/dist/utils/userJettonWallet.d.ts +2 -2
  32. package/dist/utils/userJettonWallet.js +5 -2
  33. package/dist/utils/utils.d.ts +2 -0
  34. package/dist/utils/utils.js +6 -0
  35. package/package.json +2 -2
  36. package/src/api/math.ts +229 -50
  37. package/src/api/parser.ts +52 -37
  38. package/src/api/prices.ts +2 -1
  39. package/src/constants/assets.ts +11 -3
  40. package/src/constants/general.ts +6 -4
  41. package/src/constants/pools.ts +1 -15
  42. package/src/contracts/MasterContract.ts +25 -67
  43. package/src/contracts/UserContract.ts +8 -10
  44. package/src/index.ts +4 -7
  45. package/src/types/Master.ts +9 -8
  46. package/src/types/User.ts +5 -5
  47. package/src/utils/userJettonWallet.ts +6 -2
  48. package/src/utils/utils.ts +6 -0
@@ -16,10 +16,10 @@ import {
16
16
  import { Maybe } from '@ton/core/dist/utils/maybe';
17
17
  import { EvaaUser } from './UserContract';
18
18
  import { parseMasterData } from '../api/parser';
19
- import { MasterData, PoolAssetConfig, PoolConfig, PoolJettonAssetConfig, PoolTonAssetConfig } from '../types/Master';
19
+ import { MasterData, PoolAssetConfig, PoolConfig} from '../types/Master';
20
20
  import { JettonWallet } from './JettonWallet';
21
21
  import { getUserJettonWallet } from '../utils/userJettonWallet';
22
- import { getPricesByNft, MAINNET_POOL_CONFIG } from '..';
22
+ import { getPrices, isTonAsset, MAINNET_POOL_CONFIG } from '..';
23
23
 
24
24
  /**
25
25
  * Parameters for the Evaa contract
@@ -31,51 +31,26 @@ export type EvaaParameters = {
31
31
  debug?: boolean;
32
32
  };
33
33
 
34
- /**
35
- * Parameters for the Jetton message
36
- * @property responseAddress - address to send excesses
37
- * @property forwardAmount - amount to forward to the destination address
38
- */
39
- export type JettonMessageParameters = {
40
- responseAddress?: Address;
41
- forwardAmount?: bigint;
42
- };
43
-
44
34
  /**
45
35
  * Base parameters for supply
46
36
  * @property queryID - unique query ID
47
37
  * @property includeUserCode - true to include user code for update (needed when user contract code version is outdated)
48
38
  * @property amount - amount to supply
49
39
  * @property userAddress - user address
50
- * @property assetID - asset ID
40
+ * @property asset
51
41
  */
52
- export type SupplyBaseParameters = {
42
+ export type SupplyParameters = {
43
+ asset: PoolAssetConfig
53
44
  queryID: bigint;
54
45
  includeUserCode: boolean;
55
46
  amount: bigint;
56
47
  userAddress: Address;
48
+ responseAddress?: Address;
49
+ forwardAmount?: bigint;
57
50
  /* Will be in v6
58
51
  amountToTransfer: bigint;
59
52
  payload: Cell; */
60
- };
61
- /**
62
- * Parameters for the TON supply message
63
- * @property type - 'ton'
64
- */
65
- export type TonSupplyParameters = SupplyBaseParameters & {
66
- asset: PoolAssetConfig;
67
- };
68
- /**
69
- * Parameters for the jetton supply message
70
- * @property type - 'jetton'
71
- */
72
- export type JettonSupplyParameters = SupplyBaseParameters &
73
- JettonMessageParameters & {
74
- asset: PoolJettonAssetConfig & PoolAssetConfig;
75
- };
76
-
77
- export type SupplyParameters = TonSupplyParameters | JettonSupplyParameters;
78
-
53
+ }
79
54
 
80
55
  /**
81
56
  * Parameters for the withdraw message
@@ -124,29 +99,16 @@ export type LiquidationBaseData = {
124
99
  * @property includeUserCode - true to include user code for update (needed when user contract code version is outdated)
125
100
  * @property priceData - price data cell. Can be obtained from the getPrices function
126
101
  */
127
- export type LiquidationBaseParameters = LiquidationBaseData & {
102
+ export type LiquidationParameters = LiquidationBaseData & {
103
+ asset: PoolAssetConfig;
128
104
  queryID: bigint;
129
105
  liquidatorAddress: Address;
130
106
  includeUserCode: boolean;
131
107
  priceData: Cell;
108
+ responseAddress?: Address;
109
+ forwardAmount?: bigint;
132
110
  };
133
111
 
134
- /**
135
- * Parameters for the TON liquidation message
136
- * @property type - 'ton'
137
- */
138
- export type TonLiquidationParameters = LiquidationBaseParameters & {
139
- asset: PoolAssetConfig;
140
- };
141
- /**
142
- * Parameters for the jetton liquidation message
143
- * @property type - 'jetton'
144
- */
145
- export type JettonLiquidationParameters = LiquidationBaseParameters &
146
- JettonMessageParameters & {
147
- asset: PoolAssetConfig & PoolJettonAssetConfig
148
- };
149
-
150
112
  /**
151
113
  * Evaa master contract wrapper
152
114
  */
@@ -172,17 +134,15 @@ export class Evaa implements Contract {
172
134
  * @returns supply message as a cell
173
135
  */
174
136
  createSupplyMessage(parameters: SupplyParameters): Cell {
175
- if ('jettonMasterAddress' in parameters.asset) {
176
- const jettonParams = parameters as JettonSupplyParameters;
177
-
137
+ if (!isTonAsset(parameters.asset)) {
178
138
  return beginCell()
179
139
  .storeUint(OPCODES.JETTON_TRANSFER, 32)
180
140
  .storeUint(parameters.queryID, 64)
181
141
  .storeCoins(parameters.amount)
182
142
  .storeAddress(this.address)
183
- .storeAddress(jettonParams.responseAddress ?? parameters.userAddress)
143
+ .storeAddress(parameters.responseAddress ?? parameters.userAddress)
184
144
  .storeBit(0)
185
- .storeCoins(jettonParams.forwardAmount ?? FEES.SUPPLY_JETTON_FWD)
145
+ .storeCoins(parameters.forwardAmount ?? FEES.SUPPLY_JETTON_FWD)
186
146
  .storeBit(1)
187
147
  .storeRef(
188
148
  beginCell()
@@ -232,18 +192,16 @@ export class Evaa implements Contract {
232
192
  * Create liquidation message
233
193
  * @returns liquidation message as a cell
234
194
  */
235
- createLiquidationMessage(parameters: TonLiquidationParameters | JettonLiquidationParameters): Cell {
236
- if ('jettonMasterAddress' in parameters.asset) {
237
- const jettonParams = parameters as JettonLiquidationParameters;
238
-
195
+ createLiquidationMessage(parameters: LiquidationParameters): Cell {
196
+ if (!isTonAsset(parameters.asset)) {
239
197
  return beginCell()
240
198
  .storeUint(OPCODES.JETTON_TRANSFER, 32)
241
199
  .storeUint(parameters.queryID, 64)
242
200
  .storeCoins(parameters.liquidationAmount)
243
201
  .storeAddress(this.address)
244
- .storeAddress(jettonParams.responseAddress ?? parameters.liquidatorAddress)
202
+ .storeAddress(parameters.responseAddress ?? parameters.liquidatorAddress)
245
203
  .storeBit(0)
246
- .storeCoins(jettonParams.forwardAmount ?? FEES.LIQUIDATION_JETTON_FWD)
204
+ .storeCoins(parameters.forwardAmount ?? FEES.LIQUIDATION_JETTON_FWD)
247
205
  .storeBit(1)
248
206
  .storeRef(
249
207
  beginCell()
@@ -323,11 +281,11 @@ export class Evaa implements Contract {
323
281
  provider: ContractProvider,
324
282
  via: Sender,
325
283
  value: bigint,
326
- parameters: TonSupplyParameters | JettonSupplyParameters,
284
+ parameters: SupplyParameters,
327
285
  ) {
328
286
  const message = this.createSupplyMessage(parameters);
329
287
 
330
- if ('jettonMasterAddress' in parameters.asset) {
288
+ if (!isTonAsset(parameters.asset)) {
331
289
  if (!via.address) {
332
290
  throw Error('Via address is required for jetton supply');
333
291
  }
@@ -360,11 +318,11 @@ export class Evaa implements Contract {
360
318
  provider: ContractProvider,
361
319
  via: Sender,
362
320
  value: bigint,
363
- parameters: TonLiquidationParameters | JettonLiquidationParameters,
321
+ parameters: LiquidationParameters,
364
322
  ) {
365
323
  const message = this.createLiquidationMessage(parameters);
366
324
 
367
- if ('jettonMasterAddress' in parameters.asset) {
325
+ if (!isTonAsset(parameters.asset)) {
368
326
  if (!via.address) {
369
327
  throw Error('Via address is required for jetton liquidation');
370
328
  }
@@ -424,9 +382,9 @@ export class Evaa implements Contract {
424
382
 
425
383
  async getPrices(provider: ContractProvider, endpoints?: string[]) {
426
384
  if ((endpoints?.length ?? 0) > 0) {
427
- return await getPricesByNft(this.poolConfig.nftId, endpoints);
385
+ return await getPrices(endpoints, this.poolConfig.nftId);
428
386
  } else {
429
- return await getPricesByNft(this.poolConfig.nftId);
387
+ return await getPrices(undefined, this.poolConfig.nftId);
430
388
  }
431
389
  }
432
390
  }
@@ -1,7 +1,7 @@
1
1
  import { Address, beginCell, Cell, Contract, ContractProvider, Dictionary, Sender, SendMode } from '@ton/core';
2
2
  import { UserData, UserLiteData } from '../types/User';
3
3
  import { parseUserData, parseUserLiteData } from '../api/parser';
4
- import { AssetConfig, ExtendedAssetData, ExtendedAssetsConfig, ExtendedAssetsData, PoolConfig } from '../types/Master';
4
+ import { ExtendedAssetsConfig, ExtendedAssetsData, PoolConfig } from '../types/Master';
5
5
  import { LiquidationBaseData } from './MasterContract';
6
6
  import { OPCODES } from '../constants/general';
7
7
  import { MAINNET_POOL_CONFIG } from '../constants/pools';
@@ -41,8 +41,7 @@ export class EvaaUser implements Contract {
41
41
  state.data!.toString('base64'),
42
42
  assetsData,
43
43
  assetsConfig,
44
- this.poolConfig.poolAssetsConfig,
45
- this.poolConfig.masterConstants
44
+ this.poolConfig
46
45
  );
47
46
  this.lastSync = Math.floor(Date.now() / 1000);
48
47
  } else {
@@ -64,7 +63,7 @@ export class EvaaUser implements Contract {
64
63
  prices: Dictionary<bigint, bigint>,
65
64
  ): boolean {
66
65
  if (this._liteData) {
67
- this._data = parseUserData(this._liteData, assetsData, assetsConfig, prices, this.poolConfig.poolAssetsConfig, this.poolConfig.masterConstants);
66
+ this._data = parseUserData(this._liteData, assetsData, assetsConfig, prices, this.poolConfig);
68
67
  return true;
69
68
  }
70
69
  return false;
@@ -104,10 +103,9 @@ export class EvaaUser implements Contract {
104
103
  state.data!.toString('base64'),
105
104
  assetsData,
106
105
  assetsConfig,
107
- this.poolConfig.poolAssetsConfig,
108
- this.poolConfig.masterConstants
106
+ this.poolConfig
109
107
  );
110
- this._data = parseUserData(this._liteData, assetsData, assetsConfig, prices, this.poolConfig.poolAssetsConfig, this.poolConfig.masterConstants);
108
+ this._data = parseUserData(this._liteData, assetsData, assetsConfig, prices, this.poolConfig);
111
109
  this.lastSync = Math.floor(Date.now() / 1000);
112
110
  } else {
113
111
  this._data = { type: 'inactive' };
@@ -153,11 +151,11 @@ export class EvaaUser implements Contract {
153
151
 
154
152
  return {
155
153
  borrowerAddress: this._data.ownerAddress,
156
- loanAsset: this._data.liquidationData.greatestLoanAsset,
157
- collateralAsset: this._data.liquidationData.greatestCollateralAsset,
154
+ loanAsset: this._data.liquidationData.greatestLoanAsset.assetId,
155
+ collateralAsset: this._data.liquidationData.greatestCollateralAsset.assetId,
158
156
  minCollateralAmount: this._data.liquidationData.minCollateralAmount,
159
157
  liquidationAmount: this._data.liquidationData.liquidationAmount,
160
- tonLiquidation: this._data.liquidationData.greatestLoanAsset === TON_MAINNET.assetId,
158
+ tonLiquidation: this._data.liquidationData.greatestLoanAsset.assetId === TON_MAINNET.assetId,
161
159
  };
162
160
  }
163
161
  }
package/src/index.ts CHANGED
@@ -9,6 +9,7 @@ export {
9
9
  calculateAssetData,
10
10
  calculateAssetInterest,
11
11
  getAvailableToBorrow,
12
+ calculateMaximumWithdrawAmount,
12
13
  presentValue,
13
14
  calculateLiquidationData,
14
15
  } from './api/math';
@@ -17,19 +18,14 @@ export {
17
18
  export { createAssetData, createAssetConfig, parseMasterData, parseUserData, parseUserLiteData } from './api/parser';
18
19
 
19
20
  // Prices
20
- export { getPricesByNft } from './api/prices';
21
+ export { getPrices } from './api/prices';
21
22
 
22
23
  // Contracts' wrappers
23
24
  export { JettonWallet } from './contracts/JettonWallet';
24
25
  export {
25
26
  EvaaParameters,
26
- JettonMessageParameters,
27
- TonSupplyParameters,
28
- JettonSupplyParameters,
29
27
  WithdrawParameters,
30
28
  LiquidationBaseData,
31
- TonLiquidationParameters,
32
- JettonLiquidationParameters,
33
29
  Evaa,
34
30
  } from './contracts/MasterContract';
35
31
  export { EvaaUser } from './contracts/UserContract';
@@ -80,10 +76,10 @@ export {
80
76
  MAINNET_POOL_CONFIG,
81
77
  TESTNET_POOL_CONFIG,
82
78
  MAINNET_LP_POOL_CONFIG,
83
- TESTNET_LP_POOL_CONFIG
84
79
  } from './constants/pools';
85
80
 
86
81
  export {
82
+ UNDEFINED_ASSET,
87
83
  TON_MAINNET,
88
84
  USDT_MAINNET,
89
85
  TONUSDT_DEDUST_MAINNET,
@@ -99,6 +95,7 @@ export {
99
95
  } from './constants/assets';
100
96
 
101
97
  export * from './constants/assets'
98
+ export * from './utils/utils'
102
99
 
103
100
  // Utils
104
101
  export { getLastSentBoc, getTonConnectSender } from './utils/tonConnectSender';
@@ -6,19 +6,15 @@ export type MasterConstants = {
6
6
  ASSET_PRICE_SCALE: bigint,
7
7
  ASSET_RESERVE_FACTOR_SCALE: bigint,
8
8
  ASSET_LIQUIDATION_RESERVE_FACTOR_SCALE: bigint,
9
+ ASSET_LIQUIDATION_THRESHOLD_SCALE: bigint,
10
+ ASSET_LIQUIDATION_BONUS_SCALE: bigint,
9
11
  ASSET_ORIGINATION_FEE_SCALE: bigint
10
12
  };
11
13
 
12
- export type PoolAssetConfig = (PoolTonAssetConfig | PoolJettonAssetConfig) & {
13
- name: string;
14
- };
15
14
  export type PoolAssetsConfig = PoolAssetConfig[];
16
15
 
17
- export type PoolTonAssetConfig = {
18
- assetId: bigint;
19
- }
20
-
21
- export type PoolJettonAssetConfig = {
16
+ export type PoolAssetConfig = {
17
+ name: string;
22
18
  assetId: bigint;
23
19
  jettonMasterAddress: Address;
24
20
  jettonWalletCode: Cell;
@@ -115,3 +111,8 @@ export type MasterData = {
115
111
  borrow: Dictionary<bigint, number>;
116
112
  };
117
113
  };
114
+
115
+ export type AgregatedBalances = {
116
+ totalBorrow: bigint;
117
+ totalSupply: bigint;
118
+ }
package/src/types/User.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Address, Cell, Dictionary } from '@ton/core';
2
- import { AssetConfig, ExtendedAssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConfig, MasterConstants } from './Master';
2
+ import { AssetConfig, ExtendedAssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConfig, MasterConstants, PoolAssetConfig, PoolConfig } from './Master';
3
3
 
4
4
  export enum BalanceType {
5
5
  supply = 'supply',
@@ -13,9 +13,9 @@ export type UserBalance = {
13
13
 
14
14
  export type UserLiqudationData = {
15
15
  greatestCollateralValue: bigint;
16
- greatestCollateralAsset: bigint;
16
+ greatestCollateralAsset: PoolAssetConfig;
17
17
  greatestLoanValue: bigint;
18
- greatestLoanAsset: bigint;
18
+ greatestLoanAsset: PoolAssetConfig;
19
19
  totalDebt: bigint;
20
20
  totalLimit: bigint;
21
21
  };
@@ -86,9 +86,9 @@ export type PredictHealthFactorArgs = {
86
86
  balanceChangeType: BalanceChangeType;
87
87
  amount: bigint; // always positive
88
88
  tokenSymbol: string;
89
- balances: Dictionary<bigint, bigint>;
89
+ principals: Dictionary<bigint, bigint>;
90
90
  prices: Dictionary<bigint, bigint>;
91
91
  assetsData: ExtendedAssetsData;
92
92
  assetsConfig: ExtendedAssetsConfig;
93
- masterConstants: MasterConstants;
93
+ poolConfig: PoolConfig;
94
94
  };
@@ -1,7 +1,11 @@
1
1
  import { Address, beginCell, Cell, storeStateInit } from '@ton/core';
2
- import { PoolAssetConfig, PoolJettonAssetConfig } from '../types/Master';
2
+ import { PoolAssetConfig } from '../types/Master';
3
+ import { UNDEFINED_ASSET } from '../constants/assets';
3
4
 
4
- export function getUserJettonWallet(ownerAddress: Address, poolAssetConfig: PoolAssetConfig & PoolJettonAssetConfig) {
5
+ export function getUserJettonWallet(ownerAddress: Address, poolAssetConfig: PoolAssetConfig) {
6
+ if (poolAssetConfig.name == 'TON' || poolAssetConfig.assetId === UNDEFINED_ASSET.assetId) {
7
+ throw new Error(`Cant getUserJettonWallet for ${poolAssetConfig.name} asset`)
8
+ }
5
9
  const jettonMasterAddress = poolAssetConfig.jettonMasterAddress;
6
10
  const jettonWalletCode = poolAssetConfig.jettonWalletCode;
7
11
 
@@ -0,0 +1,6 @@
1
+ import { PoolAssetConfig } from "../types/Master";
2
+
3
+ export function isTonAsset(asset: PoolAssetConfig) {
4
+ return asset.name === 'TON';
5
+ }
6
+