@evaafi/sdk 0.3.2 → 0.4.0
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/api/parser.d.ts +3 -3
- package/dist/api/parser.js +18 -12
- package/dist/constants.d.ts +22 -4
- package/dist/constants.js +34 -14
- package/dist/contracts/MasterContract.d.ts +5 -0
- package/dist/contracts/MasterContract.js +17 -2
- package/dist/contracts/UserContract.d.ts +9 -2
- package/dist/contracts/UserContract.js +27 -8
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -3
- package/dist/types/Master.d.ts +1 -0
- package/dist/utils/userJettonWallet.js +22 -9
- package/package.json +1 -1
- package/src/api/parser.ts +287 -279
- package/src/constants.ts +75 -15
- package/src/contracts/MasterContract.ts +409 -387
- package/src/contracts/UserContract.ts +160 -125
- package/src/index.ts +76 -78
- package/src/types/Master.ts +72 -71
- package/src/utils/userJettonWallet.ts +24 -11
package/src/api/parser.ts
CHANGED
|
@@ -1,279 +1,287 @@
|
|
|
1
|
-
import { beginCell, Cell, Dictionary, DictionaryValue, Slice } from '@ton/core';
|
|
2
|
-
import { AssetConfig, AssetData, ExtendedAssetData, MasterData } from '../types/Master';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
bigIntMax,
|
|
6
|
-
bigIntMin,
|
|
7
|
-
calculateAssetData,
|
|
8
|
-
calculateLiquidationData,
|
|
9
|
-
calculatePresentValue,
|
|
10
|
-
getAvailableToBorrow,
|
|
11
|
-
presentValue,
|
|
12
|
-
} from './math';
|
|
13
|
-
import { loadMaybeMyRef, loadMyRef } from './helpers';
|
|
14
|
-
import { BalanceType, UserBalance, UserData, UserLiteData } from '../types/User';
|
|
15
|
-
|
|
16
|
-
export function createAssetData(): DictionaryValue<AssetData> {
|
|
17
|
-
return {
|
|
18
|
-
serialize: (src: any, buidler: any) => {
|
|
19
|
-
buidler.storeUint(src.
|
|
20
|
-
buidler.storeUint(src.
|
|
21
|
-
buidler.storeUint(src.totalSupply, 64);
|
|
22
|
-
buidler.storeUint(src.totalBorrow, 64);
|
|
23
|
-
buidler.storeUint(src.lastAccural, 32);
|
|
24
|
-
buidler.storeUint(src.balance, 64);
|
|
25
|
-
},
|
|
26
|
-
parse: (src: Slice) => {
|
|
27
|
-
const sRate = BigInt(src.loadInt(64));
|
|
28
|
-
const bRate = BigInt(src.loadInt(64));
|
|
29
|
-
const totalSupply = BigInt(src.loadInt(64));
|
|
30
|
-
const totalBorrow = BigInt(src.loadInt(64));
|
|
31
|
-
const lastAccural = BigInt(src.loadInt(32));
|
|
32
|
-
const balance = BigInt(src.loadInt(64));
|
|
33
|
-
return { sRate, bRate, totalSupply, totalBorrow, lastAccural, balance };
|
|
34
|
-
},
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function createAssetConfig(): DictionaryValue<AssetConfig> {
|
|
39
|
-
return {
|
|
40
|
-
serialize: (src: any, builder: any) => {
|
|
41
|
-
builder.storeUint(src.oracle, 256);
|
|
42
|
-
builder.storeUint(src.decimals, 8);
|
|
43
|
-
const refBuild = beginCell();
|
|
44
|
-
refBuild.storeUint(src.collateralFactor, 16);
|
|
45
|
-
refBuild.storeUint(src.liquidationThreshold, 16);
|
|
46
|
-
refBuild.storeUint(src.
|
|
47
|
-
refBuild.storeUint(src.baseBorrowRate, 64);
|
|
48
|
-
refBuild.storeUint(src.borrowRateSlopeLow, 64);
|
|
49
|
-
refBuild.storeUint(src.borrowRateSlopeHigh, 64);
|
|
50
|
-
refBuild.storeUint(src.supplyRateSlopeLow, 64);
|
|
51
|
-
refBuild.storeUint(src.supplyRateSlopeHigh, 64);
|
|
52
|
-
refBuild.storeUint(src.targetUtilization, 64);
|
|
53
|
-
refBuild.storeUint(src.originationFee, 64);
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
const
|
|
70
|
-
const
|
|
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
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
const
|
|
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
|
-
const
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
const
|
|
171
|
-
const
|
|
172
|
-
userSlice.
|
|
173
|
-
|
|
174
|
-
const
|
|
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
|
-
const
|
|
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
|
-
|
|
1
|
+
import { beginCell, Cell, Dictionary, DictionaryValue, Slice } from '@ton/core';
|
|
2
|
+
import { AssetConfig, AssetData, ExtendedAssetData, MasterData } from '../types/Master';
|
|
3
|
+
import { MAINNET_ASSETS_ID, MASTER_CONSTANTS, TESTNET_ASSETS_ID } from '../constants';
|
|
4
|
+
import {
|
|
5
|
+
bigIntMax,
|
|
6
|
+
bigIntMin,
|
|
7
|
+
calculateAssetData,
|
|
8
|
+
calculateLiquidationData,
|
|
9
|
+
calculatePresentValue,
|
|
10
|
+
getAvailableToBorrow,
|
|
11
|
+
presentValue,
|
|
12
|
+
} from './math';
|
|
13
|
+
import { loadMaybeMyRef, loadMyRef } from './helpers';
|
|
14
|
+
import { BalanceType, UserBalance, UserData, UserLiteData } from '../types/User';
|
|
15
|
+
|
|
16
|
+
export function createAssetData(): DictionaryValue<AssetData> {
|
|
17
|
+
return {
|
|
18
|
+
serialize: (src: any, buidler: any) => {
|
|
19
|
+
buidler.storeUint(src.sRate, 64);
|
|
20
|
+
buidler.storeUint(src.bRate, 64);
|
|
21
|
+
buidler.storeUint(src.totalSupply, 64);
|
|
22
|
+
buidler.storeUint(src.totalBorrow, 64);
|
|
23
|
+
buidler.storeUint(src.lastAccural, 32);
|
|
24
|
+
buidler.storeUint(src.balance, 64);
|
|
25
|
+
},
|
|
26
|
+
parse: (src: Slice) => {
|
|
27
|
+
const sRate = BigInt(src.loadInt(64));
|
|
28
|
+
const bRate = BigInt(src.loadInt(64));
|
|
29
|
+
const totalSupply = BigInt(src.loadInt(64));
|
|
30
|
+
const totalBorrow = BigInt(src.loadInt(64));
|
|
31
|
+
const lastAccural = BigInt(src.loadInt(32));
|
|
32
|
+
const balance = BigInt(src.loadInt(64));
|
|
33
|
+
return { sRate, bRate, totalSupply, totalBorrow, lastAccural, balance };
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function createAssetConfig(): DictionaryValue<AssetConfig> {
|
|
39
|
+
return {
|
|
40
|
+
serialize: (src: any, builder: any) => {
|
|
41
|
+
builder.storeUint(src.oracle, 256);
|
|
42
|
+
builder.storeUint(src.decimals, 8);
|
|
43
|
+
const refBuild = beginCell();
|
|
44
|
+
refBuild.storeUint(src.collateralFactor, 16);
|
|
45
|
+
refBuild.storeUint(src.liquidationThreshold, 16);
|
|
46
|
+
refBuild.storeUint(src.liquidationBonus, 16);
|
|
47
|
+
refBuild.storeUint(src.baseBorrowRate, 64);
|
|
48
|
+
refBuild.storeUint(src.borrowRateSlopeLow, 64);
|
|
49
|
+
refBuild.storeUint(src.borrowRateSlopeHigh, 64);
|
|
50
|
+
refBuild.storeUint(src.supplyRateSlopeLow, 64);
|
|
51
|
+
refBuild.storeUint(src.supplyRateSlopeHigh, 64);
|
|
52
|
+
refBuild.storeUint(src.targetUtilization, 64);
|
|
53
|
+
refBuild.storeUint(src.originationFee, 64);
|
|
54
|
+
refBuild.storeUint(src.maxTotalSupply, 64);
|
|
55
|
+
builder.storeRef(refBuild.endCell());
|
|
56
|
+
},
|
|
57
|
+
parse: (src: Slice) => {
|
|
58
|
+
const oracle = src.loadUintBig(256);
|
|
59
|
+
const decimals = BigInt(src.loadUint(8));
|
|
60
|
+
const ref = src.loadRef().beginParse();
|
|
61
|
+
const collateralFactor = ref.loadUintBig(16);
|
|
62
|
+
const liquidationThreshold = ref.loadUintBig(16);
|
|
63
|
+
const liquidationBonus = ref.loadUintBig(16);
|
|
64
|
+
const baseBorrowRate = ref.loadUintBig(64);
|
|
65
|
+
const borrowRateSlopeLow = ref.loadUintBig(64);
|
|
66
|
+
const borrowRateSlopeHigh = ref.loadUintBig(64);
|
|
67
|
+
const supplyRateSlopeLow = ref.loadUintBig(64);
|
|
68
|
+
const supplyRateSlopeHigh = ref.loadUintBig(64);
|
|
69
|
+
const targetUtilization = ref.loadUintBig(64);
|
|
70
|
+
const originationFee = ref.loadUintBig(64);
|
|
71
|
+
const dust = ref.loadUintBig(64);
|
|
72
|
+
const maxTotalSupply = ref.loadUintBig(64);
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
oracle,
|
|
76
|
+
decimals,
|
|
77
|
+
collateralFactor,
|
|
78
|
+
liquidationThreshold,
|
|
79
|
+
liquidationBonus,
|
|
80
|
+
baseBorrowRate,
|
|
81
|
+
borrowRateSlopeLow,
|
|
82
|
+
borrowRateSlopeHigh,
|
|
83
|
+
supplyRateSlopeLow,
|
|
84
|
+
supplyRateSlopeHigh,
|
|
85
|
+
targetUtilization,
|
|
86
|
+
originationFee,
|
|
87
|
+
dust,
|
|
88
|
+
maxTotalSupply,
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function parseMasterData(masterDataBOC: string, testnet: boolean = false): MasterData {
|
|
95
|
+
const ASSETS_ID = testnet ? TESTNET_ASSETS_ID : MAINNET_ASSETS_ID;
|
|
96
|
+
const masterSlice = Cell.fromBase64(masterDataBOC).beginParse();
|
|
97
|
+
const meta = masterSlice.loadRef().beginParse().loadStringTail();
|
|
98
|
+
const upgradeConfigParser = masterSlice.loadRef().beginParse();
|
|
99
|
+
|
|
100
|
+
const upgradeConfig = {
|
|
101
|
+
masterCodeVersion: Number(upgradeConfigParser.loadCoins()),
|
|
102
|
+
userCodeVersion: Number(upgradeConfigParser.loadCoins()),
|
|
103
|
+
timeout: upgradeConfigParser.loadUint(32),
|
|
104
|
+
updateTime: upgradeConfigParser.loadUint(64),
|
|
105
|
+
freezeTime: upgradeConfigParser.loadUint(64),
|
|
106
|
+
userCode: loadMyRef(upgradeConfigParser),
|
|
107
|
+
blankCode: loadMyRef(upgradeConfigParser),
|
|
108
|
+
newMasterCode: loadMaybeMyRef(upgradeConfigParser),
|
|
109
|
+
newUserCode: loadMaybeMyRef(upgradeConfigParser),
|
|
110
|
+
};
|
|
111
|
+
upgradeConfigParser.endParse();
|
|
112
|
+
|
|
113
|
+
const masterConfigSlice = masterSlice.loadRef().beginParse();
|
|
114
|
+
|
|
115
|
+
const assetsConfigDict = masterConfigSlice.loadDict(Dictionary.Keys.BigUint(256), createAssetConfig());
|
|
116
|
+
const assetsDataDict = masterSlice.loadDict(Dictionary.Keys.BigUint(256), createAssetData());
|
|
117
|
+
const assetsExtendedData = Dictionary.empty<bigint, ExtendedAssetData>();
|
|
118
|
+
const assetsReserves = Dictionary.empty<bigint, bigint>();
|
|
119
|
+
const apy = {
|
|
120
|
+
supply: Dictionary.empty<bigint, number>(),
|
|
121
|
+
borrow: Dictionary.empty<bigint, number>(),
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
for (const [tokenSymbol, assetID] of Object.entries(ASSETS_ID)) {
|
|
125
|
+
const assetData = calculateAssetData(assetsConfigDict, assetsDataDict, assetID);
|
|
126
|
+
assetsExtendedData.set(assetID, assetData);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const masterConfig = {
|
|
130
|
+
ifActive: masterConfigSlice.loadInt(8),
|
|
131
|
+
admin: masterConfigSlice.loadAddress(),
|
|
132
|
+
adminPK: masterConfigSlice.loadUintBig(256),
|
|
133
|
+
tokenKeys: loadMaybeMyRef(masterConfigSlice),
|
|
134
|
+
walletToMaster: loadMaybeMyRef(masterConfigSlice),
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
masterConfigSlice.endParse();
|
|
138
|
+
|
|
139
|
+
for (const [_, assetID] of Object.entries(ASSETS_ID)) {
|
|
140
|
+
const assetData = assetsExtendedData.get(assetID) as ExtendedAssetData;
|
|
141
|
+
const totalSupply = calculatePresentValue(assetData.sRate, assetData.totalSupply);
|
|
142
|
+
const totalBorrow = calculatePresentValue(assetData.bRate, assetData.totalBorrow);
|
|
143
|
+
assetsReserves.set(assetID, assetData.balance - totalSupply + totalBorrow);
|
|
144
|
+
|
|
145
|
+
apy.supply.set(assetID, (1 + (Number(assetData.supplyInterest) / 1e12) * 24 * 3600) ** 365 - 1);
|
|
146
|
+
apy.borrow.set(assetID, (1 + (Number(assetData.borrowInterest) / 1e12) * 24 * 3600) ** 365 - 1);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
meta: meta,
|
|
151
|
+
upgradeConfig: upgradeConfig,
|
|
152
|
+
masterConfig: masterConfig,
|
|
153
|
+
assetsConfig: assetsConfigDict,
|
|
154
|
+
assetsData: assetsExtendedData,
|
|
155
|
+
assetsReserves: assetsReserves,
|
|
156
|
+
apy: apy,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function parseUserLiteData(
|
|
161
|
+
userDataBOC: string,
|
|
162
|
+
assetsData: Dictionary<bigint, ExtendedAssetData>,
|
|
163
|
+
assetsConfig: Dictionary<bigint, AssetConfig>,
|
|
164
|
+
testnet: boolean = false,
|
|
165
|
+
): UserLiteData {
|
|
166
|
+
const ASSETS_ID = testnet ? TESTNET_ASSETS_ID : MAINNET_ASSETS_ID;
|
|
167
|
+
const userSlice = Cell.fromBase64(userDataBOC).beginParse();
|
|
168
|
+
|
|
169
|
+
const codeVersion = userSlice.loadCoins();
|
|
170
|
+
const masterAddress = userSlice.loadAddress();
|
|
171
|
+
const userAddress = userSlice.loadAddress();
|
|
172
|
+
const principalsDict = userSlice.loadDict(Dictionary.Keys.BigUint(256), Dictionary.Values.BigInt(64));
|
|
173
|
+
const userState = userSlice.loadInt(64);
|
|
174
|
+
const trackingSupplyIndex = userSlice.loadUintBig(64);
|
|
175
|
+
const trackingBorrowIndex = userSlice.loadUintBig(64);
|
|
176
|
+
const dutchAuctionStart = userSlice.loadUint(32);
|
|
177
|
+
const backupCell = loadMyRef(userSlice);
|
|
178
|
+
userSlice.endParse();
|
|
179
|
+
|
|
180
|
+
const userBalances = Dictionary.empty<bigint, UserBalance>();
|
|
181
|
+
|
|
182
|
+
for (const [_, assetID] of Object.entries(ASSETS_ID)) {
|
|
183
|
+
const assetData = assetsData.get(assetID) as ExtendedAssetData;
|
|
184
|
+
const assetConfig = assetsConfig.get(assetID) as AssetConfig;
|
|
185
|
+
const balance = presentValue(assetData.sRate, assetData.bRate, principalsDict.get(assetID) || 0n);
|
|
186
|
+
userBalances.set(assetID, balance);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
type: 'active',
|
|
191
|
+
codeVersion: Number(codeVersion),
|
|
192
|
+
masterAddress: masterAddress,
|
|
193
|
+
ownerAddress: userAddress,
|
|
194
|
+
principals: principalsDict,
|
|
195
|
+
state: userState,
|
|
196
|
+
balances: userBalances,
|
|
197
|
+
trackingSupplyIndex: trackingSupplyIndex,
|
|
198
|
+
trackingBorrowIndex: trackingBorrowIndex,
|
|
199
|
+
dutchAuctionStart: dutchAuctionStart,
|
|
200
|
+
backupCell: backupCell,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function parseUserData(
|
|
205
|
+
userLiteData: UserLiteData,
|
|
206
|
+
assetsData: Dictionary<bigint, ExtendedAssetData>,
|
|
207
|
+
assetsConfig: Dictionary<bigint, AssetConfig>,
|
|
208
|
+
prices: Dictionary<bigint, bigint>,
|
|
209
|
+
testnet: boolean = false,
|
|
210
|
+
): UserData {
|
|
211
|
+
const ASSETS_ID = testnet ? TESTNET_ASSETS_ID : MAINNET_ASSETS_ID;
|
|
212
|
+
const withdrawalLimits = Dictionary.empty<bigint, bigint>();
|
|
213
|
+
const borrowLimits = Dictionary.empty<bigint, bigint>();
|
|
214
|
+
|
|
215
|
+
let supplyBalance = 0n;
|
|
216
|
+
let borrowBalance = 0n;
|
|
217
|
+
for (const [_, assetID] of Object.entries(ASSETS_ID)) {
|
|
218
|
+
const assetData = assetsData.get(assetID) as ExtendedAssetData;
|
|
219
|
+
const assetConfig = assetsConfig.get(assetID) as AssetConfig;
|
|
220
|
+
const balance = presentValue(assetData.sRate, assetData.bRate, userLiteData.principals.get(assetID) || 0n);
|
|
221
|
+
userLiteData.balances.set(assetID, balance);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
for (const [_, assetID] of Object.entries(ASSETS_ID)) {
|
|
225
|
+
const assetConfig = assetsConfig.get(assetID) as AssetConfig;
|
|
226
|
+
const balance = userLiteData.balances.get(assetID) as UserBalance;
|
|
227
|
+
|
|
228
|
+
if (balance.type === BalanceType.supply) {
|
|
229
|
+
supplyBalance += (balance.amount * prices.get(assetID)!) / 10n ** assetConfig.decimals;
|
|
230
|
+
}
|
|
231
|
+
if (balance.type === BalanceType.borrow) {
|
|
232
|
+
borrowBalance += (balance.amount * prices.get(assetID)!) / 10n ** assetConfig.decimals;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const availableToBorrow = getAvailableToBorrow(assetsConfig, assetsData, userLiteData.principals, prices);
|
|
237
|
+
for (const [_, assetID] of Object.entries(ASSETS_ID)) {
|
|
238
|
+
const assetConfig = assetsConfig.get(assetID) as AssetConfig;
|
|
239
|
+
const assetData = assetsData.get(assetID) as ExtendedAssetData;
|
|
240
|
+
const balance = userLiteData.balances.get(assetID) as UserBalance;
|
|
241
|
+
|
|
242
|
+
if (balance.type === BalanceType.supply) {
|
|
243
|
+
withdrawalLimits.set(
|
|
244
|
+
assetID,
|
|
245
|
+
bigIntMax(
|
|
246
|
+
bigIntMin(
|
|
247
|
+
assetData.balance,
|
|
248
|
+
((supplyBalance -
|
|
249
|
+
(borrowBalance * MASTER_CONSTANTS.ASSET_COEFFICIENT_SCALE) / assetConfig.collateralFactor) *
|
|
250
|
+
10n ** assetConfig.decimals) /
|
|
251
|
+
prices.get(assetID)! -
|
|
252
|
+
5n,
|
|
253
|
+
balance.amount,
|
|
254
|
+
),
|
|
255
|
+
0n,
|
|
256
|
+
),
|
|
257
|
+
);
|
|
258
|
+
} else {
|
|
259
|
+
borrowLimits.set(
|
|
260
|
+
assetID,
|
|
261
|
+
bigIntMin((availableToBorrow * 10n ** assetConfig.decimals) / prices.get(assetID)!, assetData.balance),
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const limitUsed = borrowBalance + availableToBorrow;
|
|
267
|
+
const limitUsedPercent =
|
|
268
|
+
limitUsed === 0n
|
|
269
|
+
? 0
|
|
270
|
+
: Number(BigInt(1e9) - (availableToBorrow * BigInt(1e9)) / (borrowBalance + availableToBorrow)) / 1e7;
|
|
271
|
+
|
|
272
|
+
const liquidationData = calculateLiquidationData(assetsConfig, assetsData, userLiteData.principals, prices);
|
|
273
|
+
const healthFactor = 1 - Number(liquidationData.totalDebt) / Number(liquidationData.totalLimit);
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
...userLiteData,
|
|
277
|
+
withdrawalLimits: withdrawalLimits,
|
|
278
|
+
borrowLimits: borrowLimits,
|
|
279
|
+
supplyBalance: supplyBalance,
|
|
280
|
+
borrowBalance: borrowBalance,
|
|
281
|
+
availableToBorrow: availableToBorrow,
|
|
282
|
+
limitUsedPercent: limitUsedPercent,
|
|
283
|
+
limitUsed: limitUsed,
|
|
284
|
+
liquidationData: liquidationData,
|
|
285
|
+
healthFactor: healthFactor,
|
|
286
|
+
};
|
|
287
|
+
}
|