@rhea-finance/cross-chain-sdk 0.1.2 → 0.1.4
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/README.md +12 -15
- package/dist/index.cjs +709 -290
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +214 -144
- package/dist/index.d.ts +214 -144
- package/dist/index.js +698 -289
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -23,7 +23,7 @@ var setCustomNodeUrl = (nodeUrl) => {
|
|
|
23
23
|
function getConfig() {
|
|
24
24
|
let NEAR_CONFIG = {
|
|
25
25
|
networkId: "mainnet",
|
|
26
|
-
nodeUrl: customNodeUrl || "https://
|
|
26
|
+
nodeUrl: customNodeUrl || "https://free.rpc.fastnear.com",
|
|
27
27
|
explorerUrl: "https://nearblocks.io",
|
|
28
28
|
LOGIC_CONTRACT_NAME: "contract.main.burrow.near",
|
|
29
29
|
AM_CONTRACT: "multica.near",
|
|
@@ -48,7 +48,8 @@ function getConfig() {
|
|
|
48
48
|
"token.burrow.near",
|
|
49
49
|
"45.contract.portalbridge.near",
|
|
50
50
|
"shadow_ref_v1-4179"
|
|
51
|
-
]
|
|
51
|
+
],
|
|
52
|
+
HHHH: "xieqian"
|
|
52
53
|
};
|
|
53
54
|
return {
|
|
54
55
|
NEAR: NEAR_CONFIG,
|
|
@@ -238,7 +239,7 @@ var DEFAULT_POSITION = "REGULAR";
|
|
|
238
239
|
var lpTokenPrefix = "shadow_ref_v1";
|
|
239
240
|
|
|
240
241
|
// src/chains/near.ts
|
|
241
|
-
async function
|
|
242
|
+
async function getNearConnection() {
|
|
242
243
|
let keyStore;
|
|
243
244
|
if (typeof globalThis["window"] === "undefined") {
|
|
244
245
|
keyStore = new nearApiJs.keyStores.InMemoryKeyStore();
|
|
@@ -250,6 +251,10 @@ async function getAccountConnection(accountId) {
|
|
|
250
251
|
networkId: config_near.networkId,
|
|
251
252
|
nodeUrl: config_near.nodeUrl
|
|
252
253
|
});
|
|
254
|
+
return connection;
|
|
255
|
+
}
|
|
256
|
+
async function getAccountConnection(accountId) {
|
|
257
|
+
const connection = await getNearConnection();
|
|
253
258
|
const account = await connection.account(
|
|
254
259
|
accountId || config_near.LOGIC_CONTRACT_NAME
|
|
255
260
|
);
|
|
@@ -1372,221 +1377,8 @@ var getAssetsDetail = async () => {
|
|
|
1372
1377
|
methodName: ViewMethodsLogic[7 /* get_assets_paged_detailed */]
|
|
1373
1378
|
});
|
|
1374
1379
|
};
|
|
1375
|
-
var expandTokenDecimal = (value, decimals) => {
|
|
1376
|
-
return new Decimal3__default.default(value).mul(new Decimal3__default.default(10).pow(decimals));
|
|
1377
|
-
};
|
|
1378
|
-
var expandToken = (value, decimals, fixed) => {
|
|
1379
|
-
return expandTokenDecimal(value, decimals).toFixed(fixed);
|
|
1380
|
-
};
|
|
1381
|
-
var shrinkTokenDecimal = (value, decimals) => {
|
|
1382
|
-
return new Decimal3__default.default(value).div(new Decimal3__default.default(10).pow(decimals));
|
|
1383
|
-
};
|
|
1384
|
-
var shrinkToken = (value, decimals, fixed) => {
|
|
1385
|
-
if (!value) return "";
|
|
1386
|
-
return new Decimal3__default.default(value).div(new Decimal3__default.default(10).pow(decimals)).toFixed(fixed);
|
|
1387
|
-
};
|
|
1388
|
-
function decimalMax(a, b) {
|
|
1389
|
-
a = new Decimal3__default.default(a);
|
|
1390
|
-
b = new Decimal3__default.default(b);
|
|
1391
|
-
return a.gt(b) ? a : b;
|
|
1392
|
-
}
|
|
1393
|
-
function decimalMin(a, b) {
|
|
1394
|
-
a = new Decimal3__default.default(a);
|
|
1395
|
-
b = new Decimal3__default.default(b);
|
|
1396
|
-
return a.lt(b) ? a : b;
|
|
1397
|
-
}
|
|
1398
|
-
var hasZeroSharesFarmRewards = (farms) => {
|
|
1399
|
-
return farms.some(
|
|
1400
|
-
(farm) => farm.rewards.some((reward) => +reward.boosted_shares === 0)
|
|
1401
|
-
);
|
|
1402
|
-
};
|
|
1403
|
-
var listFarmToMap = (list) => list.map((asset) => ({ [asset.token_id]: ramda.omit(["token_id"], asset) })).reduce((a, b) => ({ ...a, ...b }), {});
|
|
1404
|
-
var transformFarmRewards = (rewards) => rewards.reduce(
|
|
1405
|
-
(o, item) => ({
|
|
1406
|
-
...o,
|
|
1407
|
-
[item.reward_token_id]: {
|
|
1408
|
-
...ramda.pick(
|
|
1409
|
-
["boosted_shares", "unclaimed_amount", "asset_farm_reward"],
|
|
1410
|
-
item
|
|
1411
|
-
)
|
|
1412
|
-
}
|
|
1413
|
-
}),
|
|
1414
|
-
{}
|
|
1415
|
-
);
|
|
1416
|
-
var transformAccountFarms = (list) => {
|
|
1417
|
-
const farms = {
|
|
1418
|
-
supplied: {},
|
|
1419
|
-
borrowed: {},
|
|
1420
|
-
netTvl: {},
|
|
1421
|
-
tokennetbalance: {}
|
|
1422
|
-
};
|
|
1423
|
-
const netTvlFarms = list.find((f) => f.farm_id === "NetTvl");
|
|
1424
|
-
const restFarms = list.filter((f) => f.farm_id !== "NetTvl");
|
|
1425
|
-
restFarms.forEach((farm) => {
|
|
1426
|
-
const [action, token] = Object.entries(farm.farm_id).flat().map((s) => s.toLowerCase());
|
|
1427
|
-
farms[action] = {
|
|
1428
|
-
...farms[action],
|
|
1429
|
-
[token]: transformFarmRewards(farm.rewards)
|
|
1430
|
-
};
|
|
1431
|
-
});
|
|
1432
|
-
if (netTvlFarms) {
|
|
1433
|
-
farms.netTvl = transformFarmRewards(netTvlFarms.rewards);
|
|
1434
|
-
}
|
|
1435
|
-
return farms;
|
|
1436
|
-
};
|
|
1437
|
-
var transformAssetFarms = (list) => {
|
|
1438
|
-
const farms = {
|
|
1439
|
-
supplied: {},
|
|
1440
|
-
borrowed: {},
|
|
1441
|
-
tokennetbalance: {}
|
|
1442
|
-
};
|
|
1443
|
-
list.forEach((farm) => {
|
|
1444
|
-
const [action] = Object.entries(farm.farm_id).flat().map((s) => s.toLowerCase());
|
|
1445
|
-
farms[action] = {
|
|
1446
|
-
...farms[action],
|
|
1447
|
-
...farm.rewards
|
|
1448
|
-
};
|
|
1449
|
-
});
|
|
1450
|
-
return farms;
|
|
1451
|
-
};
|
|
1452
|
-
|
|
1453
|
-
// src/utils/transformers/account.ts
|
|
1454
|
-
var initialStaking = {
|
|
1455
|
-
staked_booster_amount: "0",
|
|
1456
|
-
unlock_timestamp: "0",
|
|
1457
|
-
x_booster_amount: "0"
|
|
1458
|
-
};
|
|
1459
|
-
var transformPortfolio = (account) => {
|
|
1460
|
-
const { portfolio } = account;
|
|
1461
|
-
if (!portfolio) return void 0;
|
|
1462
|
-
const { booster_staking, booster_stakings, supplied, positions, farms } = portfolio;
|
|
1463
|
-
const hasNonFarmedAssets = account.portfolio.has_non_farmed_assets || hasZeroSharesFarmRewards(farms);
|
|
1464
|
-
Object.keys(positions).forEach((shadow_id) => {
|
|
1465
|
-
const { borrowed, collateral } = positions[shadow_id];
|
|
1466
|
-
positions[shadow_id].borrowed = listFarmToMap(borrowed);
|
|
1467
|
-
positions[shadow_id].collateral = listFarmToMap(collateral);
|
|
1468
|
-
});
|
|
1469
|
-
let collateralAll = {};
|
|
1470
|
-
const collaterals = [];
|
|
1471
|
-
const borrows = [];
|
|
1472
|
-
Object.entries(positions).forEach(([positionId, value]) => {
|
|
1473
|
-
if (value?.collateral) {
|
|
1474
|
-
collateralAll = {
|
|
1475
|
-
...collateralAll,
|
|
1476
|
-
...value.collateral
|
|
1477
|
-
};
|
|
1478
|
-
}
|
|
1479
|
-
Object.entries(value.borrowed).forEach(
|
|
1480
|
-
([tokenId, tokenObj]) => {
|
|
1481
|
-
borrows.push({
|
|
1482
|
-
...tokenObj,
|
|
1483
|
-
token_id: tokenId,
|
|
1484
|
-
positionId
|
|
1485
|
-
});
|
|
1486
|
-
}
|
|
1487
|
-
);
|
|
1488
|
-
Object.entries(value.collateral).forEach(
|
|
1489
|
-
([tokenId, tokenObj]) => {
|
|
1490
|
-
collaterals.push({
|
|
1491
|
-
...tokenObj,
|
|
1492
|
-
token_id: tokenId,
|
|
1493
|
-
positionId
|
|
1494
|
-
});
|
|
1495
|
-
}
|
|
1496
|
-
);
|
|
1497
|
-
});
|
|
1498
|
-
return {
|
|
1499
|
-
supplies: supplied,
|
|
1500
|
-
borrows,
|
|
1501
|
-
collaterals,
|
|
1502
|
-
supplied: listFarmToMap(supplied),
|
|
1503
|
-
borrowed: positions[DEFAULT_POSITION]?.borrowed || {},
|
|
1504
|
-
collateral: positions[DEFAULT_POSITION]?.collateral || {},
|
|
1505
|
-
collateralAll,
|
|
1506
|
-
positions,
|
|
1507
|
-
farms: transformAccountFarms(farms),
|
|
1508
|
-
staking: booster_staking || initialStaking,
|
|
1509
|
-
stakings: booster_stakings || {},
|
|
1510
|
-
hasNonFarmedAssets
|
|
1511
|
-
};
|
|
1512
|
-
};
|
|
1513
|
-
|
|
1514
|
-
// src/utils/transformers/asstets.ts
|
|
1515
|
-
function transformAssets(assets) {
|
|
1516
|
-
const data = Object.values(assets).reduce((map, asset) => {
|
|
1517
|
-
if (!asset.config) return map;
|
|
1518
|
-
map[asset.token_id] = {
|
|
1519
|
-
...asset,
|
|
1520
|
-
farms: transformAssetFarms(asset.farms)
|
|
1521
|
-
};
|
|
1522
|
-
return map;
|
|
1523
|
-
}, {});
|
|
1524
|
-
return data;
|
|
1525
|
-
}
|
|
1526
|
-
function transformFarms(allFarms) {
|
|
1527
|
-
const transformed = allFarms.reduce(
|
|
1528
|
-
(acc, cur) => {
|
|
1529
|
-
const [tokenData, farmData] = cur;
|
|
1530
|
-
const [[Type, tokenId]] = Object.entries(tokenData);
|
|
1531
|
-
if (Type === "Supplied") {
|
|
1532
|
-
acc.supplied[tokenId] = farmData.rewards;
|
|
1533
|
-
}
|
|
1534
|
-
if (Type === "Borrowed") {
|
|
1535
|
-
acc.borrowed[tokenId] = farmData.rewards;
|
|
1536
|
-
}
|
|
1537
|
-
if (Type === "TokenNetBalance") {
|
|
1538
|
-
acc.tokenNetBalance[tokenId] = farmData.rewards;
|
|
1539
|
-
}
|
|
1540
|
-
return acc;
|
|
1541
|
-
},
|
|
1542
|
-
{ supplied: {}, borrowed: {}, netTvl: {}, tokenNetBalance: {} }
|
|
1543
|
-
);
|
|
1544
|
-
return transformed;
|
|
1545
|
-
}
|
|
1546
1380
|
|
|
1547
1381
|
// src/view/get-portfolio.ts
|
|
1548
|
-
var convertAssetArrayToObject = (assets) => {
|
|
1549
|
-
const result = {};
|
|
1550
|
-
for (const asset of assets) {
|
|
1551
|
-
result[asset.token_id] = {
|
|
1552
|
-
apr: asset.apr,
|
|
1553
|
-
balance: asset.balance,
|
|
1554
|
-
shares: asset.shares
|
|
1555
|
-
};
|
|
1556
|
-
}
|
|
1557
|
-
return result;
|
|
1558
|
-
};
|
|
1559
|
-
var convertPositions = (positionsOrigin) => {
|
|
1560
|
-
const result = {};
|
|
1561
|
-
for (const shadowId in positionsOrigin) {
|
|
1562
|
-
const position = positionsOrigin[shadowId];
|
|
1563
|
-
result[shadowId] = {
|
|
1564
|
-
collateral: convertAssetArrayToObject(position.collateral),
|
|
1565
|
-
borrowed: convertAssetArrayToObject(position.borrowed)
|
|
1566
|
-
};
|
|
1567
|
-
}
|
|
1568
|
-
return result;
|
|
1569
|
-
};
|
|
1570
|
-
var getPortfolio = (accountPositions) => {
|
|
1571
|
-
const supplied = convertAssetArrayToObject(accountPositions.supplied);
|
|
1572
|
-
const positions = convertPositions(accountPositions.positions);
|
|
1573
|
-
const defaultPosition = positions[DEFAULT_POSITION] || {
|
|
1574
|
-
collateral: {},
|
|
1575
|
-
borrowed: {}
|
|
1576
|
-
};
|
|
1577
|
-
const collateral = defaultPosition.collateral;
|
|
1578
|
-
const borrowed = defaultPosition.borrowed;
|
|
1579
|
-
return {
|
|
1580
|
-
supplied,
|
|
1581
|
-
collateral,
|
|
1582
|
-
borrowed,
|
|
1583
|
-
positions,
|
|
1584
|
-
farms: accountPositions.farms,
|
|
1585
|
-
staking: accountPositions.booster_staking,
|
|
1586
|
-
stakings: accountPositions.booster_stakings,
|
|
1587
|
-
hasNonFarmedAssets: accountPositions.has_non_farmed_assets || hasZeroSharesFarmRewards(accountPositions.farms)
|
|
1588
|
-
};
|
|
1589
|
-
};
|
|
1590
1382
|
var getAccountAllPositions = async (account_id) => {
|
|
1591
1383
|
const accountDetailed = await view_on_near({
|
|
1592
1384
|
contractId: config_near.LOGIC_CONTRACT_NAME,
|
|
@@ -2155,55 +1947,560 @@ async function prepareBusinessDataOnClaim({
|
|
|
2155
1947
|
};
|
|
2156
1948
|
return businessMap;
|
|
2157
1949
|
}
|
|
2158
|
-
var
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
}
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
1950
|
+
var expandTokenDecimal = (value, decimals) => {
|
|
1951
|
+
return new Decimal3__default.default(value).mul(new Decimal3__default.default(10).pow(decimals));
|
|
1952
|
+
};
|
|
1953
|
+
var expandToken = (value, decimals, fixed) => {
|
|
1954
|
+
return expandTokenDecimal(value, decimals).toFixed(fixed);
|
|
1955
|
+
};
|
|
1956
|
+
var shrinkTokenDecimal = (value, decimals) => {
|
|
1957
|
+
return new Decimal3__default.default(value).div(new Decimal3__default.default(10).pow(decimals));
|
|
1958
|
+
};
|
|
1959
|
+
var shrinkToken = (value, decimals, fixed) => {
|
|
1960
|
+
if (!value) return "";
|
|
1961
|
+
return new Decimal3__default.default(value).div(new Decimal3__default.default(10).pow(decimals)).toFixed(fixed);
|
|
1962
|
+
};
|
|
1963
|
+
function decimalMax(a, b) {
|
|
1964
|
+
a = new Decimal3__default.default(a);
|
|
1965
|
+
b = new Decimal3__default.default(b);
|
|
1966
|
+
return a.gt(b) ? a : b;
|
|
1967
|
+
}
|
|
1968
|
+
function decimalMin(a, b) {
|
|
1969
|
+
a = new Decimal3__default.default(a);
|
|
1970
|
+
b = new Decimal3__default.default(b);
|
|
1971
|
+
return a.lt(b) ? a : b;
|
|
1972
|
+
}
|
|
1973
|
+
var hasZeroSharesFarmRewards = (farms) => {
|
|
1974
|
+
return farms.some(
|
|
1975
|
+
(farm) => farm.rewards.some((reward) => +reward.boosted_shares === 0)
|
|
2177
1976
|
);
|
|
2178
|
-
const sumResult = result?.reduce(sumReducerDecimal, new Decimal3__default.default(0));
|
|
2179
|
-
return sumResult || new Decimal3__default.default(0);
|
|
2180
1977
|
};
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
const
|
|
2202
|
-
const
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
1978
|
+
var listFarmToMap = (list) => list.map((asset) => ({ [asset.token_id]: ramda.omit(["token_id"], asset) })).reduce((a, b) => ({ ...a, ...b }), {});
|
|
1979
|
+
var transformFarmRewards = (rewards) => rewards.reduce(
|
|
1980
|
+
(o, item) => ({
|
|
1981
|
+
...o,
|
|
1982
|
+
[item.reward_token_id]: {
|
|
1983
|
+
...ramda.pick(
|
|
1984
|
+
["boosted_shares", "unclaimed_amount", "asset_farm_reward"],
|
|
1985
|
+
item
|
|
1986
|
+
)
|
|
1987
|
+
}
|
|
1988
|
+
}),
|
|
1989
|
+
{}
|
|
1990
|
+
);
|
|
1991
|
+
var transformAccountFarms = (list) => {
|
|
1992
|
+
const farms = {
|
|
1993
|
+
supplied: {},
|
|
1994
|
+
borrowed: {},
|
|
1995
|
+
netTvl: {},
|
|
1996
|
+
tokennetbalance: {}
|
|
1997
|
+
};
|
|
1998
|
+
const netTvlFarms = list.find((f) => f.farm_id === "NetTvl");
|
|
1999
|
+
const restFarms = list.filter((f) => f.farm_id !== "NetTvl");
|
|
2000
|
+
restFarms.forEach((farm) => {
|
|
2001
|
+
const [action, token] = Object.entries(farm.farm_id).flat().map((s) => s.toLowerCase());
|
|
2002
|
+
farms[action] = {
|
|
2003
|
+
...farms[action],
|
|
2004
|
+
[token]: transformFarmRewards(farm.rewards)
|
|
2005
|
+
};
|
|
2006
|
+
});
|
|
2007
|
+
if (netTvlFarms) {
|
|
2008
|
+
farms.netTvl = transformFarmRewards(netTvlFarms.rewards);
|
|
2009
|
+
}
|
|
2010
|
+
return farms;
|
|
2011
|
+
};
|
|
2012
|
+
var transformAssetFarms = (list) => {
|
|
2013
|
+
const farms = {
|
|
2014
|
+
supplied: {},
|
|
2015
|
+
borrowed: {},
|
|
2016
|
+
tokennetbalance: {}
|
|
2017
|
+
};
|
|
2018
|
+
list.forEach((farm) => {
|
|
2019
|
+
const [action] = Object.entries(farm.farm_id).flat().map((s) => s.toLowerCase());
|
|
2020
|
+
farms[action] = {
|
|
2021
|
+
...farms[action],
|
|
2022
|
+
...farm.rewards
|
|
2023
|
+
};
|
|
2024
|
+
});
|
|
2025
|
+
return farms;
|
|
2026
|
+
};
|
|
2027
|
+
|
|
2028
|
+
// src/utils/transformers/account.ts
|
|
2029
|
+
var initialStaking = {
|
|
2030
|
+
staked_booster_amount: "0",
|
|
2031
|
+
unlock_timestamp: "0",
|
|
2032
|
+
x_booster_amount: "0"
|
|
2033
|
+
};
|
|
2034
|
+
var transformPortfolio = (portfolio) => {
|
|
2035
|
+
if (!portfolio) return void 0;
|
|
2036
|
+
const { booster_staking, booster_stakings, supplied, positions, farms } = portfolio;
|
|
2037
|
+
const hasNonFarmedAssets = portfolio.has_non_farmed_assets || hasZeroSharesFarmRewards(farms);
|
|
2038
|
+
Object.keys(positions).forEach((shadow_id) => {
|
|
2039
|
+
const { borrowed, collateral } = positions[shadow_id];
|
|
2040
|
+
positions[shadow_id].borrowed = listFarmToMap(borrowed);
|
|
2041
|
+
positions[shadow_id].collateral = listFarmToMap(collateral);
|
|
2042
|
+
});
|
|
2043
|
+
let collateralAll = {};
|
|
2044
|
+
const collaterals = [];
|
|
2045
|
+
const borrows = [];
|
|
2046
|
+
Object.entries(positions).forEach(([positionId, value]) => {
|
|
2047
|
+
if (value?.collateral) {
|
|
2048
|
+
collateralAll = {
|
|
2049
|
+
...collateralAll,
|
|
2050
|
+
...value.collateral
|
|
2051
|
+
};
|
|
2052
|
+
}
|
|
2053
|
+
Object.entries(value.borrowed).forEach(
|
|
2054
|
+
([tokenId, tokenObj]) => {
|
|
2055
|
+
borrows.push({
|
|
2056
|
+
...tokenObj,
|
|
2057
|
+
token_id: tokenId,
|
|
2058
|
+
positionId
|
|
2059
|
+
});
|
|
2060
|
+
}
|
|
2061
|
+
);
|
|
2062
|
+
Object.entries(value.collateral).forEach(
|
|
2063
|
+
([tokenId, tokenObj]) => {
|
|
2064
|
+
collaterals.push({
|
|
2065
|
+
...tokenObj,
|
|
2066
|
+
token_id: tokenId,
|
|
2067
|
+
positionId
|
|
2068
|
+
});
|
|
2069
|
+
}
|
|
2070
|
+
);
|
|
2071
|
+
});
|
|
2072
|
+
return {
|
|
2073
|
+
supplies: supplied,
|
|
2074
|
+
borrows,
|
|
2075
|
+
collaterals,
|
|
2076
|
+
supplied: listFarmToMap(supplied),
|
|
2077
|
+
borrowed: positions[DEFAULT_POSITION]?.borrowed || {},
|
|
2078
|
+
collateral: positions[DEFAULT_POSITION]?.collateral || {},
|
|
2079
|
+
collateralAll,
|
|
2080
|
+
positions,
|
|
2081
|
+
farms: transformAccountFarms(farms),
|
|
2082
|
+
staking: booster_staking || initialStaking,
|
|
2083
|
+
stakings: booster_stakings || {},
|
|
2084
|
+
hasNonFarmedAssets
|
|
2085
|
+
};
|
|
2086
|
+
};
|
|
2087
|
+
|
|
2088
|
+
// src/utils/transformers/asstets.ts
|
|
2089
|
+
function transformAssets(assets) {
|
|
2090
|
+
const data = Object.values(assets).reduce((map, asset) => {
|
|
2091
|
+
if (!asset.config) return map;
|
|
2092
|
+
map[asset.token_id] = {
|
|
2093
|
+
...asset,
|
|
2094
|
+
farms: transformAssetFarms(asset.farms)
|
|
2095
|
+
};
|
|
2096
|
+
return map;
|
|
2097
|
+
}, {});
|
|
2098
|
+
return data;
|
|
2099
|
+
}
|
|
2100
|
+
function transformFarms(allFarms) {
|
|
2101
|
+
const transformed = allFarms.reduce(
|
|
2102
|
+
(acc, cur) => {
|
|
2103
|
+
const [tokenData, farmData] = cur;
|
|
2104
|
+
const [[Type, tokenId]] = Object.entries(tokenData);
|
|
2105
|
+
if (Type === "Supplied") {
|
|
2106
|
+
acc.supplied[tokenId] = farmData.rewards;
|
|
2107
|
+
}
|
|
2108
|
+
if (Type === "Borrowed") {
|
|
2109
|
+
acc.borrowed[tokenId] = farmData.rewards;
|
|
2110
|
+
}
|
|
2111
|
+
if (Type === "TokenNetBalance") {
|
|
2112
|
+
acc.tokenNetBalance[tokenId] = farmData.rewards;
|
|
2113
|
+
}
|
|
2114
|
+
return acc;
|
|
2115
|
+
},
|
|
2116
|
+
{ supplied: {}, borrowed: {}, netTvl: {}, tokenNetBalance: {} }
|
|
2117
|
+
);
|
|
2118
|
+
return transformed;
|
|
2119
|
+
}
|
|
2120
|
+
var sumReducerDecimal = (sum, cur) => sum.add(cur);
|
|
2121
|
+
var getAdjustedSum = ({
|
|
2122
|
+
type,
|
|
2123
|
+
portfolio,
|
|
2124
|
+
assets
|
|
2125
|
+
}) => {
|
|
2126
|
+
const positionId = DEFAULT_POSITION;
|
|
2127
|
+
const result = Object.keys(portfolio.positions[positionId]?.[type] || {}).map(
|
|
2128
|
+
(id) => {
|
|
2129
|
+
const asset = assets[id];
|
|
2130
|
+
let pricedBalance;
|
|
2131
|
+
const price = asset?.price ? new Decimal3__default.default(asset.price.multiplier).div(
|
|
2132
|
+
new Decimal3__default.default(10).pow(asset.price.decimals)
|
|
2133
|
+
) : new Decimal3__default.default(0);
|
|
2134
|
+
pricedBalance = new Decimal3__default.default(
|
|
2135
|
+
portfolio.positions[positionId][type][id].balance
|
|
2136
|
+
).div(expandTokenDecimal(1, asset?.config?.extra_decimals || 0)).mul(price);
|
|
2137
|
+
return type === "borrowed" ? pricedBalance.div(asset?.config?.volatility_ratio || 1).mul(MAX_RATIO) : pricedBalance.mul(asset?.config?.volatility_ratio || 1).div(MAX_RATIO);
|
|
2138
|
+
}
|
|
2139
|
+
);
|
|
2140
|
+
const sumResult = result?.reduce(sumReducerDecimal, new Decimal3__default.default(0));
|
|
2141
|
+
return sumResult || new Decimal3__default.default(0);
|
|
2142
|
+
};
|
|
2143
|
+
|
|
2144
|
+
// src/healthFactor/recomputeHealthFactorAdjust.ts
|
|
2145
|
+
var recomputeHealthFactorAdjust = ({
|
|
2146
|
+
tokenId,
|
|
2147
|
+
amount,
|
|
2148
|
+
portfolio,
|
|
2149
|
+
assets
|
|
2150
|
+
}) => {
|
|
2151
|
+
if (_7__default.default.isEmpty(assets))
|
|
2152
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2153
|
+
if (!portfolio || !tokenId)
|
|
2154
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2155
|
+
const asset = assets[tokenId];
|
|
2156
|
+
const { metadata, config: config2 } = asset;
|
|
2157
|
+
const position = DEFAULT_POSITION;
|
|
2158
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2159
|
+
const newBalance = expandTokenDecimal(amount, decimals).toFixed();
|
|
2160
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2161
|
+
if (!clonedPortfolio.positions[position]) {
|
|
2162
|
+
clonedPortfolio.positions[position] = {
|
|
2163
|
+
collateral: {
|
|
2164
|
+
[tokenId]: {
|
|
2165
|
+
balance: newBalance,
|
|
2166
|
+
shares: newBalance,
|
|
2167
|
+
apr: "0"
|
|
2168
|
+
}
|
|
2169
|
+
},
|
|
2170
|
+
borrowed: {}
|
|
2171
|
+
};
|
|
2172
|
+
} else if (!clonedPortfolio.positions[position].collateral[tokenId]) {
|
|
2173
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2174
|
+
balance: newBalance,
|
|
2175
|
+
shares: newBalance,
|
|
2176
|
+
apr: "0"
|
|
2177
|
+
};
|
|
2178
|
+
}
|
|
2179
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2180
|
+
...clonedPortfolio.positions[position].collateral[tokenId],
|
|
2181
|
+
balance: newBalance,
|
|
2182
|
+
shares: newBalance
|
|
2183
|
+
};
|
|
2184
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2185
|
+
type: "collateral",
|
|
2186
|
+
portfolio: clonedPortfolio,
|
|
2187
|
+
assets
|
|
2188
|
+
});
|
|
2189
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2190
|
+
type: "borrowed",
|
|
2191
|
+
portfolio,
|
|
2192
|
+
assets
|
|
2193
|
+
});
|
|
2194
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2195
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2196
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2197
|
+
return { healthFactor, maxBorrowValue };
|
|
2198
|
+
};
|
|
2199
|
+
var recomputeHealthFactorBorrow = ({
|
|
2200
|
+
tokenId,
|
|
2201
|
+
amount,
|
|
2202
|
+
position,
|
|
2203
|
+
portfolio,
|
|
2204
|
+
assets
|
|
2205
|
+
}) => {
|
|
2206
|
+
if (_7__default.default.isEmpty(assets))
|
|
2207
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2208
|
+
if (!portfolio || !tokenId)
|
|
2209
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2210
|
+
const asset = assets[tokenId];
|
|
2211
|
+
const { metadata, config: config2 } = asset;
|
|
2212
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2213
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2214
|
+
if (!clonedPortfolio.positions[position || DEFAULT_POSITION]) {
|
|
2215
|
+
clonedPortfolio.positions[position || DEFAULT_POSITION] = {
|
|
2216
|
+
borrowed: {
|
|
2217
|
+
[tokenId]: {
|
|
2218
|
+
balance: "0",
|
|
2219
|
+
shares: "0",
|
|
2220
|
+
apr: "0"
|
|
2221
|
+
}
|
|
2222
|
+
},
|
|
2223
|
+
collateral: {}
|
|
2224
|
+
};
|
|
2225
|
+
} else if (!clonedPortfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId]) {
|
|
2226
|
+
clonedPortfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId] = {
|
|
2227
|
+
balance: "0",
|
|
2228
|
+
shares: "0",
|
|
2229
|
+
apr: "0"
|
|
2230
|
+
};
|
|
2231
|
+
}
|
|
2232
|
+
const newBalance = expandTokenDecimal(amount, decimals).plus(
|
|
2233
|
+
new Decimal3__default.default(
|
|
2234
|
+
clonedPortfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId]?.balance || 0
|
|
2235
|
+
)
|
|
2236
|
+
).toFixed();
|
|
2237
|
+
clonedPortfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId].balance = newBalance;
|
|
2238
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2239
|
+
type: "collateral",
|
|
2240
|
+
portfolio,
|
|
2241
|
+
assets
|
|
2242
|
+
});
|
|
2243
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2244
|
+
type: "borrowed",
|
|
2245
|
+
portfolio: clonedPortfolio,
|
|
2246
|
+
assets
|
|
2247
|
+
});
|
|
2248
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2249
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2250
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2251
|
+
return { healthFactor, maxBorrowValue };
|
|
2252
|
+
};
|
|
2253
|
+
var recomputeHealthFactorRepay = ({
|
|
2254
|
+
tokenId,
|
|
2255
|
+
amount,
|
|
2256
|
+
position,
|
|
2257
|
+
portfolio,
|
|
2258
|
+
assets
|
|
2259
|
+
}) => {
|
|
2260
|
+
if (_7__default.default.isEmpty(assets) || !portfolio || !tokenId || !portfolio?.positions?.[position || DEFAULT_POSITION]?.borrowed?.[tokenId])
|
|
2261
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2262
|
+
const asset = assets[tokenId];
|
|
2263
|
+
const { metadata, config: config2 } = asset;
|
|
2264
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2265
|
+
const borrowedBalance = new Decimal3__default.default(
|
|
2266
|
+
portfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId].balance
|
|
2267
|
+
);
|
|
2268
|
+
const newBalance = Decimal3__default.default.max(
|
|
2269
|
+
0,
|
|
2270
|
+
borrowedBalance.minus(expandTokenDecimal(amount, decimals))
|
|
2271
|
+
);
|
|
2272
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2273
|
+
clonedPortfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId].balance = newBalance.toFixed();
|
|
2274
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2275
|
+
type: "collateral",
|
|
2276
|
+
portfolio,
|
|
2277
|
+
assets
|
|
2278
|
+
});
|
|
2279
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2280
|
+
type: "borrowed",
|
|
2281
|
+
portfolio: clonedPortfolio,
|
|
2282
|
+
assets
|
|
2283
|
+
});
|
|
2284
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2285
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2286
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2287
|
+
return { healthFactor, maxBorrowValue };
|
|
2288
|
+
};
|
|
2289
|
+
var recomputeHealthFactorRepayFromDeposits = ({
|
|
2290
|
+
tokenId,
|
|
2291
|
+
amount,
|
|
2292
|
+
portfolio,
|
|
2293
|
+
assets
|
|
2294
|
+
}) => {
|
|
2295
|
+
if (_7__default.default.isEmpty(assets) || !tokenId || !portfolio || !portfolio?.positions?.[DEFAULT_POSITION]?.borrowed?.[tokenId])
|
|
2296
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2297
|
+
const asset = assets[tokenId];
|
|
2298
|
+
const { metadata, config: config2 } = asset;
|
|
2299
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2300
|
+
const amountDecimal = expandTokenDecimal(amount, decimals);
|
|
2301
|
+
const position = DEFAULT_POSITION;
|
|
2302
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2303
|
+
const borrowedBalance = new Decimal3__default.default(
|
|
2304
|
+
clonedPortfolio.positions[position].borrowed[tokenId].balance
|
|
2305
|
+
);
|
|
2306
|
+
const newBorrowedBalance = decimalMax(
|
|
2307
|
+
0,
|
|
2308
|
+
borrowedBalance.minus(amountDecimal)
|
|
2309
|
+
);
|
|
2310
|
+
const collateralBalance = new Decimal3__default.default(
|
|
2311
|
+
clonedPortfolio.positions[position]?.collateral?.[tokenId]?.balance || 0
|
|
2312
|
+
);
|
|
2313
|
+
const suppliedBalance = new Decimal3__default.default(
|
|
2314
|
+
clonedPortfolio.supplied?.[tokenId]?.balance || 0
|
|
2315
|
+
);
|
|
2316
|
+
const newCollateralBalance = decimalMax(
|
|
2317
|
+
0,
|
|
2318
|
+
decimalMin(
|
|
2319
|
+
collateralBalance,
|
|
2320
|
+
collateralBalance.plus(suppliedBalance).minus(amountDecimal)
|
|
2321
|
+
)
|
|
2322
|
+
);
|
|
2323
|
+
if (newCollateralBalance.lt(collateralBalance)) {
|
|
2324
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2325
|
+
...clonedPortfolio.positions[position].collateral[tokenId],
|
|
2326
|
+
shares: newCollateralBalance.toFixed(),
|
|
2327
|
+
balance: newCollateralBalance.toFixed()
|
|
2328
|
+
};
|
|
2329
|
+
}
|
|
2330
|
+
clonedPortfolio.positions[position].borrowed[tokenId].balance = newBorrowedBalance.toFixed();
|
|
2331
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2332
|
+
type: "collateral",
|
|
2333
|
+
portfolio: clonedPortfolio,
|
|
2334
|
+
assets
|
|
2335
|
+
});
|
|
2336
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2337
|
+
type: "borrowed",
|
|
2338
|
+
portfolio: clonedPortfolio,
|
|
2339
|
+
assets
|
|
2340
|
+
});
|
|
2341
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2342
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2343
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2344
|
+
return { healthFactor, maxBorrowValue };
|
|
2345
|
+
};
|
|
2346
|
+
var recomputeHealthFactorSupply = ({
|
|
2347
|
+
tokenId,
|
|
2348
|
+
amount,
|
|
2349
|
+
portfolio,
|
|
2350
|
+
assets,
|
|
2351
|
+
useAsCollateral
|
|
2352
|
+
}) => {
|
|
2353
|
+
if (_7__default.default.isEmpty(assets) || !tokenId || !portfolio)
|
|
2354
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2355
|
+
const asset = assets[tokenId];
|
|
2356
|
+
const { metadata, config: config2 } = asset;
|
|
2357
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2358
|
+
const position = DEFAULT_POSITION;
|
|
2359
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2360
|
+
const amountDecimal = expandTokenDecimal(amount, decimals);
|
|
2361
|
+
if (!clonedPortfolio.positions[position]) {
|
|
2362
|
+
clonedPortfolio.positions[position] = {
|
|
2363
|
+
collateral: {
|
|
2364
|
+
[tokenId]: {
|
|
2365
|
+
balance: "0",
|
|
2366
|
+
shares: "0",
|
|
2367
|
+
apr: "0"
|
|
2368
|
+
}
|
|
2369
|
+
},
|
|
2370
|
+
borrowed: {}
|
|
2371
|
+
};
|
|
2372
|
+
} else if (!clonedPortfolio.positions[position].collateral[tokenId]) {
|
|
2373
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2374
|
+
balance: "0",
|
|
2375
|
+
shares: "0",
|
|
2376
|
+
apr: "0"
|
|
2377
|
+
};
|
|
2378
|
+
}
|
|
2379
|
+
const collateralBalance = new Decimal3__default.default(
|
|
2380
|
+
clonedPortfolio.positions[position].collateral[tokenId].balance
|
|
2381
|
+
);
|
|
2382
|
+
const newBalance = collateralBalance.plus(
|
|
2383
|
+
useAsCollateral ? amountDecimal : 0
|
|
2384
|
+
);
|
|
2385
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2386
|
+
...clonedPortfolio.positions[position].collateral[tokenId],
|
|
2387
|
+
shares: newBalance.toFixed(),
|
|
2388
|
+
balance: newBalance.toFixed()
|
|
2389
|
+
};
|
|
2390
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2391
|
+
type: "collateral",
|
|
2392
|
+
portfolio: clonedPortfolio,
|
|
2393
|
+
assets
|
|
2394
|
+
});
|
|
2395
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2396
|
+
type: "borrowed",
|
|
2397
|
+
portfolio,
|
|
2398
|
+
assets
|
|
2399
|
+
});
|
|
2400
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2401
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2402
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2403
|
+
return { healthFactor, maxBorrowValue };
|
|
2404
|
+
};
|
|
2405
|
+
var recomputeHealthFactorWithdraw = ({
|
|
2406
|
+
tokenId,
|
|
2407
|
+
amount,
|
|
2408
|
+
portfolio,
|
|
2409
|
+
assets
|
|
2410
|
+
}) => {
|
|
2411
|
+
if (_7__default.default.isEmpty(assets) || !tokenId || !portfolio) {
|
|
2412
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2413
|
+
}
|
|
2414
|
+
const asset = assets[tokenId];
|
|
2415
|
+
const { metadata, config: config2 } = asset;
|
|
2416
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2417
|
+
const position = DEFAULT_POSITION;
|
|
2418
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2419
|
+
if (!clonedPortfolio.positions[position]) {
|
|
2420
|
+
clonedPortfolio.positions[position] = {
|
|
2421
|
+
collateral: {
|
|
2422
|
+
[tokenId]: {
|
|
2423
|
+
balance: "0",
|
|
2424
|
+
shares: "0",
|
|
2425
|
+
apr: "0"
|
|
2426
|
+
}
|
|
2427
|
+
},
|
|
2428
|
+
borrowed: {}
|
|
2429
|
+
};
|
|
2430
|
+
} else if (!clonedPortfolio.positions[position].collateral[tokenId]) {
|
|
2431
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2432
|
+
balance: "0",
|
|
2433
|
+
shares: "0",
|
|
2434
|
+
apr: "0"
|
|
2435
|
+
};
|
|
2436
|
+
}
|
|
2437
|
+
if (!clonedPortfolio.supplied[tokenId]) {
|
|
2438
|
+
clonedPortfolio.supplied[tokenId] = {
|
|
2439
|
+
balance: "0",
|
|
2440
|
+
shares: "0",
|
|
2441
|
+
apr: "0"
|
|
2442
|
+
};
|
|
2443
|
+
}
|
|
2444
|
+
const collateralBalance = new Decimal3__default.default(
|
|
2445
|
+
clonedPortfolio.positions[position].collateral[tokenId].balance
|
|
2446
|
+
);
|
|
2447
|
+
const suppliedBalance = new Decimal3__default.default(
|
|
2448
|
+
clonedPortfolio.supplied[tokenId].balance
|
|
2449
|
+
);
|
|
2450
|
+
const amountDecimal = expandTokenDecimal(amount, decimals);
|
|
2451
|
+
const newCollateralBalance = decimalMax(
|
|
2452
|
+
0,
|
|
2453
|
+
decimalMin(
|
|
2454
|
+
collateralBalance,
|
|
2455
|
+
collateralBalance.plus(suppliedBalance).minus(amountDecimal)
|
|
2456
|
+
)
|
|
2457
|
+
);
|
|
2458
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2459
|
+
...clonedPortfolio.positions[position].collateral[tokenId],
|
|
2460
|
+
shares: newCollateralBalance.toFixed(),
|
|
2461
|
+
balance: newCollateralBalance.toFixed()
|
|
2462
|
+
};
|
|
2463
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2464
|
+
type: "collateral",
|
|
2465
|
+
portfolio: clonedPortfolio,
|
|
2466
|
+
assets
|
|
2467
|
+
});
|
|
2468
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2469
|
+
type: "borrowed",
|
|
2470
|
+
portfolio,
|
|
2471
|
+
assets
|
|
2472
|
+
});
|
|
2473
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2474
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2475
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2476
|
+
return { healthFactor, maxBorrowValue };
|
|
2477
|
+
};
|
|
2478
|
+
|
|
2479
|
+
// src/other/computeRelayerGas.ts
|
|
2480
|
+
var computeRelayerGas = ({
|
|
2481
|
+
nearStorageAmount,
|
|
2482
|
+
mca,
|
|
2483
|
+
relayerGasFees,
|
|
2484
|
+
assets,
|
|
2485
|
+
portfolio
|
|
2486
|
+
}) => {
|
|
2487
|
+
if (!mca || _7__default.default.isEmpty(assets) || _7__default.default.isEmpty(portfolio)) return;
|
|
2488
|
+
const { tokenId, amount, relayerFeeUsd } = searchMatchAssetId({
|
|
2489
|
+
portfolio,
|
|
2490
|
+
assets,
|
|
2491
|
+
nearStorageAmount,
|
|
2492
|
+
relayerGasFees
|
|
2493
|
+
});
|
|
2494
|
+
if (!tokenId) return;
|
|
2495
|
+
const asset = assets[tokenId];
|
|
2496
|
+
const { metadata, config: config2 } = asset;
|
|
2497
|
+
const position = DEFAULT_POSITION;
|
|
2498
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2499
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2500
|
+
if (!clonedPortfolio.positions[position]) {
|
|
2501
|
+
clonedPortfolio.positions[position] = {
|
|
2502
|
+
collateral: {
|
|
2503
|
+
[tokenId]: {
|
|
2207
2504
|
balance: "0",
|
|
2208
2505
|
shares: "0",
|
|
2209
2506
|
apr: "0"
|
|
@@ -2276,19 +2573,27 @@ var computeRelayerGas = ({
|
|
|
2276
2573
|
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2277
2574
|
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2278
2575
|
if (healthFactor > 105) {
|
|
2576
|
+
const amountToken = expandTokenDecimal(
|
|
2577
|
+
amount,
|
|
2578
|
+
asset?.metadata?.decimals || 0
|
|
2579
|
+
).toFixed(0, Decimal3__default.default.ROUND_DOWN);
|
|
2580
|
+
const amountBurrow = expandTokenDecimal(
|
|
2581
|
+
amount,
|
|
2582
|
+
(asset?.config?.extra_decimals || 0) + (asset?.metadata?.decimals || 0)
|
|
2583
|
+
).toFixed(0, Decimal3__default.default.ROUND_DOWN);
|
|
2279
2584
|
return {
|
|
2280
2585
|
portfolioMinusGas: clonedPortfolio,
|
|
2281
2586
|
tokenId,
|
|
2282
2587
|
amount,
|
|
2283
|
-
relayerFeeUsd,
|
|
2284
|
-
amountToken
|
|
2588
|
+
relayerFeeUsd: relayerFeeUsd || 0,
|
|
2589
|
+
amountToken,
|
|
2590
|
+
amountBurrow,
|
|
2591
|
+
simpleWithdrawData: {
|
|
2592
|
+
tokenId,
|
|
2285
2593
|
amount,
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
amount,
|
|
2290
|
-
(asset?.config?.extra_decimals || 0) + (asset?.metadata?.decimals || 0)
|
|
2291
|
-
).toFixed(0, Decimal3__default.default.ROUND_DOWN)
|
|
2594
|
+
amountToken,
|
|
2595
|
+
amountBurrow
|
|
2596
|
+
}
|
|
2292
2597
|
};
|
|
2293
2598
|
}
|
|
2294
2599
|
return;
|
|
@@ -2370,29 +2675,133 @@ function searchMatchAssetId({
|
|
|
2370
2675
|
}
|
|
2371
2676
|
return {};
|
|
2372
2677
|
}
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
var getSimpleWithdrawData = async ({
|
|
2376
|
-
nearStorageAmount,
|
|
2377
|
-
mca,
|
|
2378
|
-
relayerGasFees,
|
|
2678
|
+
var computeBorrowMaxAmount = ({
|
|
2679
|
+
tokenId,
|
|
2379
2680
|
assets,
|
|
2380
2681
|
portfolio
|
|
2381
2682
|
}) => {
|
|
2382
|
-
const
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2683
|
+
const asset = assets[tokenId];
|
|
2684
|
+
return Object.keys(portfolio.positions).map((position) => {
|
|
2685
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2686
|
+
type: "collateral",
|
|
2687
|
+
portfolio,
|
|
2688
|
+
assets
|
|
2689
|
+
});
|
|
2690
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2691
|
+
type: "borrowed",
|
|
2692
|
+
portfolio,
|
|
2693
|
+
assets
|
|
2694
|
+
});
|
|
2695
|
+
const volatiliyRatio = asset.config.volatility_ratio || 0;
|
|
2696
|
+
const price = asset.price?.usd || Infinity;
|
|
2697
|
+
const maxBorrowPricedForToken = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(volatiliyRatio).div(MAX_RATIO).mul(95).div(100);
|
|
2698
|
+
const maxBorrowAmountTemp = maxBorrowPricedForToken.div(price);
|
|
2699
|
+
const maxBorrowAmount = Decimal3__default.default(
|
|
2700
|
+
Math.max(0, maxBorrowAmountTemp.toNumber())
|
|
2701
|
+
);
|
|
2702
|
+
const maxBorrowPriced = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2703
|
+
return {
|
|
2704
|
+
[position]: {
|
|
2705
|
+
maxBorrowAmount: Math.max(maxBorrowAmount.toNumber(), 0),
|
|
2706
|
+
maxBorrowValue: Math.max(maxBorrowPriced.toNumber(), 0)
|
|
2707
|
+
}
|
|
2708
|
+
};
|
|
2709
|
+
}).reduce((acc, cur) => ({ ...acc, ...cur }), {});
|
|
2710
|
+
};
|
|
2711
|
+
var getBorrowMaxAmount = ({
|
|
2712
|
+
tokenId,
|
|
2713
|
+
portfolio,
|
|
2714
|
+
assets
|
|
2715
|
+
}) => {
|
|
2716
|
+
if (_7__default.default.isEmpty(assets) || !portfolio || !tokenId)
|
|
2717
|
+
return {
|
|
2718
|
+
[DEFAULT_POSITION]: { maxBorrowAmount: 0, maxBorrowValue: 0 }
|
|
2719
|
+
};
|
|
2720
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2721
|
+
const maxBorrowAmount = computeBorrowMaxAmount({
|
|
2722
|
+
tokenId,
|
|
2723
|
+
assets,
|
|
2724
|
+
portfolio: clonedPortfolio
|
|
2725
|
+
});
|
|
2726
|
+
return maxBorrowAmount;
|
|
2727
|
+
};
|
|
2728
|
+
var computeWithdrawMaxAmount = ({
|
|
2729
|
+
tokenId,
|
|
2730
|
+
assets,
|
|
2731
|
+
portfolio
|
|
2732
|
+
}) => {
|
|
2733
|
+
const asset = assets[tokenId];
|
|
2734
|
+
const position = DEFAULT_POSITION;
|
|
2735
|
+
const assetPrice = asset.price ? new Decimal3__default.default(asset.price.usd || "0") : new Decimal3__default.default(0);
|
|
2736
|
+
const suppliedBalance = new Decimal3__default.default(
|
|
2737
|
+
portfolio.supplied[tokenId]?.balance || 0
|
|
2738
|
+
);
|
|
2739
|
+
const collateralBalance = new Decimal3__default.default(
|
|
2740
|
+
portfolio.positions[position]?.collateral?.[tokenId]?.balance || 0
|
|
2741
|
+
);
|
|
2742
|
+
let maxAmount = suppliedBalance;
|
|
2743
|
+
if (portfolio.borrows.length > 0 && collateralBalance.gt(0)) {
|
|
2744
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2745
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2746
|
+
apr: "0",
|
|
2747
|
+
balance: "0",
|
|
2748
|
+
shares: "0"
|
|
2749
|
+
};
|
|
2750
|
+
const _adjustedCollateralSum = getAdjustedSum({
|
|
2751
|
+
type: "collateral",
|
|
2752
|
+
portfolio: clonedPortfolio,
|
|
2753
|
+
assets
|
|
2754
|
+
});
|
|
2755
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2756
|
+
type: "borrowed",
|
|
2757
|
+
portfolio,
|
|
2758
|
+
assets
|
|
2759
|
+
});
|
|
2760
|
+
const healthFactor = _adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2761
|
+
if (healthFactor <= 105) {
|
|
2762
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2763
|
+
type: "collateral",
|
|
2764
|
+
portfolio,
|
|
2765
|
+
assets
|
|
2766
|
+
});
|
|
2767
|
+
const adjustedPricedDiff = decimalMax(
|
|
2768
|
+
0,
|
|
2769
|
+
adjustedCollateralSum.sub(adjustedBorrowedSum)
|
|
2770
|
+
);
|
|
2771
|
+
const safeAdjustedPricedDiff = adjustedPricedDiff.mul(999).div(1e3);
|
|
2772
|
+
const safePricedDiff = safeAdjustedPricedDiff.div(asset.config.volatility_ratio).mul(1e4);
|
|
2773
|
+
const safeDiff = safePricedDiff.div(assetPrice).mul(
|
|
2774
|
+
expandTokenDecimal(
|
|
2775
|
+
1,
|
|
2776
|
+
asset.config.extra_decimals + (asset?.metadata?.decimals || 0)
|
|
2777
|
+
)
|
|
2778
|
+
).trunc();
|
|
2779
|
+
maxAmount = maxAmount.add(decimalMin(safeDiff, collateralBalance));
|
|
2780
|
+
} else {
|
|
2781
|
+
maxAmount = suppliedBalance.plus(collateralBalance);
|
|
2782
|
+
}
|
|
2783
|
+
} else {
|
|
2784
|
+
maxAmount = suppliedBalance.plus(collateralBalance);
|
|
2785
|
+
}
|
|
2786
|
+
return {
|
|
2787
|
+
maxAmount,
|
|
2788
|
+
canWithdrawAll: maxAmount.eq(suppliedBalance.plus(collateralBalance))
|
|
2789
|
+
};
|
|
2790
|
+
};
|
|
2791
|
+
var getWithdrawMaxAmount = ({
|
|
2792
|
+
tokenId,
|
|
2793
|
+
assets,
|
|
2794
|
+
portfolio
|
|
2795
|
+
}) => {
|
|
2796
|
+
const asset = assets[tokenId];
|
|
2797
|
+
const { metadata, config: config2 } = asset;
|
|
2798
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2799
|
+
const { maxAmount } = computeWithdrawMaxAmount({
|
|
2800
|
+
tokenId,
|
|
2386
2801
|
assets,
|
|
2387
2802
|
portfolio
|
|
2388
2803
|
});
|
|
2389
|
-
|
|
2390
|
-
tokenId: gasData.tokenId,
|
|
2391
|
-
amount: gasData.amount,
|
|
2392
|
-
amountToken: gasData.amountToken,
|
|
2393
|
-
amountBurrow: gasData.amountBurrow
|
|
2394
|
-
} : null;
|
|
2395
|
-
return simpleWithdrawData;
|
|
2804
|
+
return Number(shrinkToken(maxAmount.toFixed(), decimals));
|
|
2396
2805
|
};
|
|
2397
2806
|
|
|
2398
2807
|
exports.DEFAULT_POSITION = DEFAULT_POSITION;
|
|
@@ -2404,6 +2813,7 @@ exports.TGas = TGas;
|
|
|
2404
2813
|
exports.TOKEN_STORAGE_DEPOSIT_READ = TOKEN_STORAGE_DEPOSIT_READ;
|
|
2405
2814
|
exports.batchViews = batchViews;
|
|
2406
2815
|
exports.computeRelayerGas = computeRelayerGas;
|
|
2816
|
+
exports.computeWithdrawMaxAmount = computeWithdrawMaxAmount;
|
|
2407
2817
|
exports.config_btc = config_btc;
|
|
2408
2818
|
exports.config_evm = config_evm;
|
|
2409
2819
|
exports.config_near = config_near;
|
|
@@ -2418,12 +2828,15 @@ exports.fetchIntentsTransactionStatus = fetchIntentsTransactionStatus;
|
|
|
2418
2828
|
exports.format_wallet = format_wallet;
|
|
2419
2829
|
exports.getAccountAllPositions = getAccountAllPositions;
|
|
2420
2830
|
exports.getAccountBalance = getAccountBalance;
|
|
2831
|
+
exports.getAccountConnection = getAccountConnection;
|
|
2832
|
+
exports.getAdjustedSum = getAdjustedSum;
|
|
2421
2833
|
exports.getAllFarms = getAllFarms;
|
|
2422
2834
|
exports.getAssets = getAssets;
|
|
2423
2835
|
exports.getAssetsDetail = getAssetsDetail;
|
|
2424
2836
|
exports.getAuthenticationHeaders = getAuthenticationHeaders;
|
|
2425
2837
|
exports.getBalance = getBalance;
|
|
2426
2838
|
exports.getBoosterTokens = getBoosterTokens;
|
|
2839
|
+
exports.getBorrowMaxAmount = getBorrowMaxAmount;
|
|
2427
2840
|
exports.getCeateMcaFee = getCeateMcaFee;
|
|
2428
2841
|
exports.getConfig = getConfig2;
|
|
2429
2842
|
exports.getCreateMcaCustomRecipientMsg = getCreateMcaCustomRecipientMsg;
|
|
@@ -2434,15 +2847,15 @@ exports.getMultichainLendingConfig = getMultichainLendingConfig;
|
|
|
2434
2847
|
exports.getMultichainLendingData = getMultichainLendingData;
|
|
2435
2848
|
exports.getMultichainLendingHistory = getMultichainLendingHistory;
|
|
2436
2849
|
exports.getMultichainTokensByChains = getMultichainTokensByChains;
|
|
2850
|
+
exports.getNearConnection = getNearConnection;
|
|
2437
2851
|
exports.getNearValue = getNearValue;
|
|
2438
2852
|
exports.getNearValuesPaged = getNearValuesPaged;
|
|
2439
|
-
exports.getPortfolio = getPortfolio;
|
|
2440
2853
|
exports.getPrices = getPrices;
|
|
2441
2854
|
exports.getRepayCustomRecipientMsg = getRepayCustomRecipientMsg;
|
|
2442
2855
|
exports.getSignature = getSignature;
|
|
2443
|
-
exports.getSimpleWithdrawData = getSimpleWithdrawData;
|
|
2444
2856
|
exports.getSupplyCustomRecipientMsg = getSupplyCustomRecipientMsg;
|
|
2445
2857
|
exports.getTokenPythInfos = getTokenPythInfos;
|
|
2858
|
+
exports.getWithdrawMaxAmount = getWithdrawMaxAmount;
|
|
2446
2859
|
exports.get_all_tokens_metadata = get_all_tokens_metadata;
|
|
2447
2860
|
exports.get_interest_rate = get_interest_rate;
|
|
2448
2861
|
exports.get_liquidations = get_liquidations;
|
|
@@ -2476,6 +2889,12 @@ exports.process_signature_evm = process_signature_evm;
|
|
|
2476
2889
|
exports.process_signature_solana = process_signature_solana;
|
|
2477
2890
|
exports.query_account_register_token_tx = query_account_register_token_tx;
|
|
2478
2891
|
exports.query_intents_tansfer_txs = query_intents_tansfer_txs;
|
|
2892
|
+
exports.recomputeHealthFactorAdjust = recomputeHealthFactorAdjust;
|
|
2893
|
+
exports.recomputeHealthFactorBorrow = recomputeHealthFactorBorrow;
|
|
2894
|
+
exports.recomputeHealthFactorRepay = recomputeHealthFactorRepay;
|
|
2895
|
+
exports.recomputeHealthFactorRepayFromDeposits = recomputeHealthFactorRepayFromDeposits;
|
|
2896
|
+
exports.recomputeHealthFactorSupply = recomputeHealthFactorSupply;
|
|
2897
|
+
exports.recomputeHealthFactorWithdraw = recomputeHealthFactorWithdraw;
|
|
2479
2898
|
exports.serializationObj = serializationObj;
|
|
2480
2899
|
exports.setCustomNodeUrl = setCustomNodeUrl;
|
|
2481
2900
|
exports.shrinkToken = shrinkToken;
|