@rhea-finance/cross-chain-sdk 0.1.2 → 0.1.3
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 +11 -14
- package/dist/index.cjs +705 -292
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +210 -144
- package/dist/index.d.ts +210 -144
- package/dist/index.js +696 -291
- 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,
|
|
@@ -1372,221 +1373,8 @@ var getAssetsDetail = async () => {
|
|
|
1372
1373
|
methodName: ViewMethodsLogic[7 /* get_assets_paged_detailed */]
|
|
1373
1374
|
});
|
|
1374
1375
|
};
|
|
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
1376
|
|
|
1547
1377
|
// 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
1378
|
var getAccountAllPositions = async (account_id) => {
|
|
1591
1379
|
const accountDetailed = await view_on_near({
|
|
1592
1380
|
contractId: config_near.LOGIC_CONTRACT_NAME,
|
|
@@ -2155,58 +1943,563 @@ async function prepareBusinessDataOnClaim({
|
|
|
2155
1943
|
};
|
|
2156
1944
|
return businessMap;
|
|
2157
1945
|
}
|
|
2158
|
-
var
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
}
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
1946
|
+
var expandTokenDecimal = (value, decimals) => {
|
|
1947
|
+
return new Decimal3__default.default(value).mul(new Decimal3__default.default(10).pow(decimals));
|
|
1948
|
+
};
|
|
1949
|
+
var expandToken = (value, decimals, fixed) => {
|
|
1950
|
+
return expandTokenDecimal(value, decimals).toFixed(fixed);
|
|
1951
|
+
};
|
|
1952
|
+
var shrinkTokenDecimal = (value, decimals) => {
|
|
1953
|
+
return new Decimal3__default.default(value).div(new Decimal3__default.default(10).pow(decimals));
|
|
1954
|
+
};
|
|
1955
|
+
var shrinkToken = (value, decimals, fixed) => {
|
|
1956
|
+
if (!value) return "";
|
|
1957
|
+
return new Decimal3__default.default(value).div(new Decimal3__default.default(10).pow(decimals)).toFixed(fixed);
|
|
1958
|
+
};
|
|
1959
|
+
function decimalMax(a, b) {
|
|
1960
|
+
a = new Decimal3__default.default(a);
|
|
1961
|
+
b = new Decimal3__default.default(b);
|
|
1962
|
+
return a.gt(b) ? a : b;
|
|
1963
|
+
}
|
|
1964
|
+
function decimalMin(a, b) {
|
|
1965
|
+
a = new Decimal3__default.default(a);
|
|
1966
|
+
b = new Decimal3__default.default(b);
|
|
1967
|
+
return a.lt(b) ? a : b;
|
|
1968
|
+
}
|
|
1969
|
+
var hasZeroSharesFarmRewards = (farms) => {
|
|
1970
|
+
return farms.some(
|
|
1971
|
+
(farm) => farm.rewards.some((reward) => +reward.boosted_shares === 0)
|
|
2177
1972
|
);
|
|
2178
|
-
const sumResult = result?.reduce(sumReducerDecimal, new Decimal3__default.default(0));
|
|
2179
|
-
return sumResult || new Decimal3__default.default(0);
|
|
2180
1973
|
};
|
|
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
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
1974
|
+
var listFarmToMap = (list) => list.map((asset) => ({ [asset.token_id]: ramda.omit(["token_id"], asset) })).reduce((a, b) => ({ ...a, ...b }), {});
|
|
1975
|
+
var transformFarmRewards = (rewards) => rewards.reduce(
|
|
1976
|
+
(o, item) => ({
|
|
1977
|
+
...o,
|
|
1978
|
+
[item.reward_token_id]: {
|
|
1979
|
+
...ramda.pick(
|
|
1980
|
+
["boosted_shares", "unclaimed_amount", "asset_farm_reward"],
|
|
1981
|
+
item
|
|
1982
|
+
)
|
|
1983
|
+
}
|
|
1984
|
+
}),
|
|
1985
|
+
{}
|
|
1986
|
+
);
|
|
1987
|
+
var transformAccountFarms = (list) => {
|
|
1988
|
+
const farms = {
|
|
1989
|
+
supplied: {},
|
|
1990
|
+
borrowed: {},
|
|
1991
|
+
netTvl: {},
|
|
1992
|
+
tokennetbalance: {}
|
|
1993
|
+
};
|
|
1994
|
+
const netTvlFarms = list.find((f) => f.farm_id === "NetTvl");
|
|
1995
|
+
const restFarms = list.filter((f) => f.farm_id !== "NetTvl");
|
|
1996
|
+
restFarms.forEach((farm) => {
|
|
1997
|
+
const [action, token] = Object.entries(farm.farm_id).flat().map((s) => s.toLowerCase());
|
|
1998
|
+
farms[action] = {
|
|
1999
|
+
...farms[action],
|
|
2000
|
+
[token]: transformFarmRewards(farm.rewards)
|
|
2001
|
+
};
|
|
2002
|
+
});
|
|
2003
|
+
if (netTvlFarms) {
|
|
2004
|
+
farms.netTvl = transformFarmRewards(netTvlFarms.rewards);
|
|
2005
|
+
}
|
|
2006
|
+
return farms;
|
|
2007
|
+
};
|
|
2008
|
+
var transformAssetFarms = (list) => {
|
|
2009
|
+
const farms = {
|
|
2010
|
+
supplied: {},
|
|
2011
|
+
borrowed: {},
|
|
2012
|
+
tokennetbalance: {}
|
|
2013
|
+
};
|
|
2014
|
+
list.forEach((farm) => {
|
|
2015
|
+
const [action] = Object.entries(farm.farm_id).flat().map((s) => s.toLowerCase());
|
|
2016
|
+
farms[action] = {
|
|
2017
|
+
...farms[action],
|
|
2018
|
+
...farm.rewards
|
|
2019
|
+
};
|
|
2020
|
+
});
|
|
2021
|
+
return farms;
|
|
2022
|
+
};
|
|
2023
|
+
|
|
2024
|
+
// src/utils/transformers/account.ts
|
|
2025
|
+
var initialStaking = {
|
|
2026
|
+
staked_booster_amount: "0",
|
|
2027
|
+
unlock_timestamp: "0",
|
|
2028
|
+
x_booster_amount: "0"
|
|
2029
|
+
};
|
|
2030
|
+
var transformPortfolio = (portfolio) => {
|
|
2031
|
+
if (!portfolio) return void 0;
|
|
2032
|
+
const { booster_staking, booster_stakings, supplied, positions, farms } = portfolio;
|
|
2033
|
+
const hasNonFarmedAssets = portfolio.has_non_farmed_assets || hasZeroSharesFarmRewards(farms);
|
|
2034
|
+
Object.keys(positions).forEach((shadow_id) => {
|
|
2035
|
+
const { borrowed, collateral } = positions[shadow_id];
|
|
2036
|
+
positions[shadow_id].borrowed = listFarmToMap(borrowed);
|
|
2037
|
+
positions[shadow_id].collateral = listFarmToMap(collateral);
|
|
2038
|
+
});
|
|
2039
|
+
let collateralAll = {};
|
|
2040
|
+
const collaterals = [];
|
|
2041
|
+
const borrows = [];
|
|
2042
|
+
Object.entries(positions).forEach(([positionId, value]) => {
|
|
2043
|
+
if (value?.collateral) {
|
|
2044
|
+
collateralAll = {
|
|
2045
|
+
...collateralAll,
|
|
2046
|
+
...value.collateral
|
|
2047
|
+
};
|
|
2048
|
+
}
|
|
2049
|
+
Object.entries(value.borrowed).forEach(
|
|
2050
|
+
([tokenId, tokenObj]) => {
|
|
2051
|
+
borrows.push({
|
|
2052
|
+
...tokenObj,
|
|
2053
|
+
token_id: tokenId,
|
|
2054
|
+
positionId
|
|
2055
|
+
});
|
|
2056
|
+
}
|
|
2057
|
+
);
|
|
2058
|
+
Object.entries(value.collateral).forEach(
|
|
2059
|
+
([tokenId, tokenObj]) => {
|
|
2060
|
+
collaterals.push({
|
|
2061
|
+
...tokenObj,
|
|
2062
|
+
token_id: tokenId,
|
|
2063
|
+
positionId
|
|
2064
|
+
});
|
|
2065
|
+
}
|
|
2066
|
+
);
|
|
2067
|
+
});
|
|
2068
|
+
return {
|
|
2069
|
+
supplies: supplied,
|
|
2070
|
+
borrows,
|
|
2071
|
+
collaterals,
|
|
2072
|
+
supplied: listFarmToMap(supplied),
|
|
2073
|
+
borrowed: positions[DEFAULT_POSITION]?.borrowed || {},
|
|
2074
|
+
collateral: positions[DEFAULT_POSITION]?.collateral || {},
|
|
2075
|
+
collateralAll,
|
|
2076
|
+
positions,
|
|
2077
|
+
farms: transformAccountFarms(farms),
|
|
2078
|
+
staking: booster_staking || initialStaking,
|
|
2079
|
+
stakings: booster_stakings || {},
|
|
2080
|
+
hasNonFarmedAssets
|
|
2081
|
+
};
|
|
2082
|
+
};
|
|
2083
|
+
|
|
2084
|
+
// src/utils/transformers/asstets.ts
|
|
2085
|
+
function transformAssets(assets) {
|
|
2086
|
+
const data = Object.values(assets).reduce((map, asset) => {
|
|
2087
|
+
if (!asset.config) return map;
|
|
2088
|
+
map[asset.token_id] = {
|
|
2089
|
+
...asset,
|
|
2090
|
+
farms: transformAssetFarms(asset.farms)
|
|
2091
|
+
};
|
|
2092
|
+
return map;
|
|
2093
|
+
}, {});
|
|
2094
|
+
return data;
|
|
2095
|
+
}
|
|
2096
|
+
function transformFarms(allFarms) {
|
|
2097
|
+
const transformed = allFarms.reduce(
|
|
2098
|
+
(acc, cur) => {
|
|
2099
|
+
const [tokenData, farmData] = cur;
|
|
2100
|
+
const [[Type, tokenId]] = Object.entries(tokenData);
|
|
2101
|
+
if (Type === "Supplied") {
|
|
2102
|
+
acc.supplied[tokenId] = farmData.rewards;
|
|
2103
|
+
}
|
|
2104
|
+
if (Type === "Borrowed") {
|
|
2105
|
+
acc.borrowed[tokenId] = farmData.rewards;
|
|
2106
|
+
}
|
|
2107
|
+
if (Type === "TokenNetBalance") {
|
|
2108
|
+
acc.tokenNetBalance[tokenId] = farmData.rewards;
|
|
2109
|
+
}
|
|
2110
|
+
return acc;
|
|
2111
|
+
},
|
|
2112
|
+
{ supplied: {}, borrowed: {}, netTvl: {}, tokenNetBalance: {} }
|
|
2113
|
+
);
|
|
2114
|
+
return transformed;
|
|
2115
|
+
}
|
|
2116
|
+
var sumReducerDecimal = (sum, cur) => sum.add(cur);
|
|
2117
|
+
var getAdjustedSum = ({
|
|
2118
|
+
type,
|
|
2119
|
+
portfolio,
|
|
2120
|
+
assets
|
|
2121
|
+
}) => {
|
|
2122
|
+
const positionId = DEFAULT_POSITION;
|
|
2123
|
+
const result = Object.keys(portfolio.positions[positionId]?.[type] || {}).map(
|
|
2124
|
+
(id) => {
|
|
2125
|
+
const asset = assets[id];
|
|
2126
|
+
let pricedBalance;
|
|
2127
|
+
const price = asset?.price ? new Decimal3__default.default(asset.price.multiplier).div(
|
|
2128
|
+
new Decimal3__default.default(10).pow(asset.price.decimals)
|
|
2129
|
+
) : new Decimal3__default.default(0);
|
|
2130
|
+
pricedBalance = new Decimal3__default.default(
|
|
2131
|
+
portfolio.positions[positionId][type][id].balance
|
|
2132
|
+
).div(expandTokenDecimal(1, asset?.config?.extra_decimals || 0)).mul(price);
|
|
2133
|
+
return type === "borrowed" ? pricedBalance.div(asset?.config?.volatility_ratio || 1).mul(MAX_RATIO) : pricedBalance.mul(asset?.config?.volatility_ratio || 1).div(MAX_RATIO);
|
|
2134
|
+
}
|
|
2135
|
+
);
|
|
2136
|
+
const sumResult = result?.reduce(sumReducerDecimal, new Decimal3__default.default(0));
|
|
2137
|
+
return sumResult || new Decimal3__default.default(0);
|
|
2138
|
+
};
|
|
2139
|
+
|
|
2140
|
+
// src/healthFactor/recomputeHealthFactorAdjust.ts
|
|
2141
|
+
var recomputeHealthFactorAdjust = ({
|
|
2142
|
+
tokenId,
|
|
2143
|
+
amount,
|
|
2144
|
+
portfolio,
|
|
2145
|
+
assets
|
|
2146
|
+
}) => {
|
|
2147
|
+
if (_7__default.default.isEmpty(assets))
|
|
2148
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2149
|
+
if (!portfolio || !tokenId)
|
|
2150
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2151
|
+
const asset = assets[tokenId];
|
|
2152
|
+
const { metadata, config: config2 } = asset;
|
|
2153
|
+
const position = DEFAULT_POSITION;
|
|
2154
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2155
|
+
const newBalance = expandTokenDecimal(amount, decimals).toFixed();
|
|
2156
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2157
|
+
if (!clonedPortfolio.positions[position]) {
|
|
2158
|
+
clonedPortfolio.positions[position] = {
|
|
2159
|
+
collateral: {
|
|
2160
|
+
[tokenId]: {
|
|
2161
|
+
balance: newBalance,
|
|
2162
|
+
shares: newBalance,
|
|
2163
|
+
apr: "0"
|
|
2164
|
+
}
|
|
2165
|
+
},
|
|
2166
|
+
borrowed: {}
|
|
2167
|
+
};
|
|
2168
|
+
} else if (!clonedPortfolio.positions[position].collateral[tokenId]) {
|
|
2169
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2170
|
+
balance: newBalance,
|
|
2171
|
+
shares: newBalance,
|
|
2172
|
+
apr: "0"
|
|
2173
|
+
};
|
|
2174
|
+
}
|
|
2175
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2176
|
+
...clonedPortfolio.positions[position].collateral[tokenId],
|
|
2177
|
+
balance: newBalance,
|
|
2178
|
+
shares: newBalance
|
|
2179
|
+
};
|
|
2180
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2181
|
+
type: "collateral",
|
|
2182
|
+
portfolio: clonedPortfolio,
|
|
2183
|
+
assets
|
|
2184
|
+
});
|
|
2185
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2186
|
+
type: "borrowed",
|
|
2187
|
+
portfolio,
|
|
2188
|
+
assets
|
|
2189
|
+
});
|
|
2190
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2191
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2192
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2193
|
+
return { healthFactor, maxBorrowValue };
|
|
2194
|
+
};
|
|
2195
|
+
var recomputeHealthFactorBorrow = ({
|
|
2196
|
+
tokenId,
|
|
2197
|
+
amount,
|
|
2198
|
+
position,
|
|
2199
|
+
portfolio,
|
|
2200
|
+
assets
|
|
2201
|
+
}) => {
|
|
2202
|
+
if (_7__default.default.isEmpty(assets))
|
|
2203
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2204
|
+
if (!portfolio || !tokenId)
|
|
2205
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2206
|
+
const asset = assets[tokenId];
|
|
2207
|
+
const { metadata, config: config2 } = asset;
|
|
2208
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2209
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2210
|
+
if (!clonedPortfolio.positions[position || DEFAULT_POSITION]) {
|
|
2211
|
+
clonedPortfolio.positions[position || DEFAULT_POSITION] = {
|
|
2212
|
+
borrowed: {
|
|
2213
|
+
[tokenId]: {
|
|
2214
|
+
balance: "0",
|
|
2215
|
+
shares: "0",
|
|
2216
|
+
apr: "0"
|
|
2217
|
+
}
|
|
2218
|
+
},
|
|
2219
|
+
collateral: {}
|
|
2220
|
+
};
|
|
2221
|
+
} else if (!clonedPortfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId]) {
|
|
2222
|
+
clonedPortfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId] = {
|
|
2223
|
+
balance: "0",
|
|
2224
|
+
shares: "0",
|
|
2225
|
+
apr: "0"
|
|
2226
|
+
};
|
|
2227
|
+
}
|
|
2228
|
+
const newBalance = expandTokenDecimal(amount, decimals).plus(
|
|
2229
|
+
new Decimal3__default.default(
|
|
2230
|
+
clonedPortfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId]?.balance || 0
|
|
2231
|
+
)
|
|
2232
|
+
).toFixed();
|
|
2233
|
+
clonedPortfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId].balance = newBalance;
|
|
2234
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2235
|
+
type: "collateral",
|
|
2236
|
+
portfolio,
|
|
2237
|
+
assets
|
|
2238
|
+
});
|
|
2239
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2240
|
+
type: "borrowed",
|
|
2241
|
+
portfolio: clonedPortfolio,
|
|
2242
|
+
assets
|
|
2243
|
+
});
|
|
2244
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2245
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2246
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2247
|
+
return { healthFactor, maxBorrowValue };
|
|
2248
|
+
};
|
|
2249
|
+
var recomputeHealthFactorRepay = ({
|
|
2250
|
+
tokenId,
|
|
2251
|
+
amount,
|
|
2252
|
+
position,
|
|
2253
|
+
portfolio,
|
|
2254
|
+
assets
|
|
2255
|
+
}) => {
|
|
2256
|
+
if (_7__default.default.isEmpty(assets) || !portfolio || !tokenId || !portfolio?.positions?.[position || DEFAULT_POSITION]?.borrowed?.[tokenId])
|
|
2257
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2258
|
+
const asset = assets[tokenId];
|
|
2259
|
+
const { metadata, config: config2 } = asset;
|
|
2260
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2261
|
+
const borrowedBalance = new Decimal3__default.default(
|
|
2262
|
+
portfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId].balance
|
|
2263
|
+
);
|
|
2264
|
+
const newBalance = Decimal3__default.default.max(
|
|
2265
|
+
0,
|
|
2266
|
+
borrowedBalance.minus(expandTokenDecimal(amount, decimals))
|
|
2267
|
+
);
|
|
2268
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2269
|
+
clonedPortfolio.positions[position || DEFAULT_POSITION].borrowed[tokenId].balance = newBalance.toFixed();
|
|
2270
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2271
|
+
type: "collateral",
|
|
2272
|
+
portfolio,
|
|
2273
|
+
assets
|
|
2274
|
+
});
|
|
2275
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2276
|
+
type: "borrowed",
|
|
2277
|
+
portfolio: clonedPortfolio,
|
|
2278
|
+
assets
|
|
2279
|
+
});
|
|
2280
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2281
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2282
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2283
|
+
return { healthFactor, maxBorrowValue };
|
|
2284
|
+
};
|
|
2285
|
+
var recomputeHealthFactorRepayFromDeposits = ({
|
|
2286
|
+
tokenId,
|
|
2287
|
+
amount,
|
|
2288
|
+
portfolio,
|
|
2289
|
+
assets
|
|
2290
|
+
}) => {
|
|
2291
|
+
if (_7__default.default.isEmpty(assets) || !tokenId || !portfolio || !portfolio?.positions?.[DEFAULT_POSITION]?.borrowed?.[tokenId])
|
|
2292
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2293
|
+
const asset = assets[tokenId];
|
|
2294
|
+
const { metadata, config: config2 } = asset;
|
|
2295
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2296
|
+
const amountDecimal = expandTokenDecimal(amount, decimals);
|
|
2297
|
+
const position = DEFAULT_POSITION;
|
|
2298
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2299
|
+
const borrowedBalance = new Decimal3__default.default(
|
|
2300
|
+
clonedPortfolio.positions[position].borrowed[tokenId].balance
|
|
2301
|
+
);
|
|
2302
|
+
const newBorrowedBalance = decimalMax(
|
|
2303
|
+
0,
|
|
2304
|
+
borrowedBalance.minus(amountDecimal)
|
|
2305
|
+
);
|
|
2306
|
+
const collateralBalance = new Decimal3__default.default(
|
|
2307
|
+
clonedPortfolio.positions[position]?.collateral?.[tokenId]?.balance || 0
|
|
2308
|
+
);
|
|
2309
|
+
const suppliedBalance = new Decimal3__default.default(
|
|
2310
|
+
clonedPortfolio.supplied?.[tokenId]?.balance || 0
|
|
2311
|
+
);
|
|
2312
|
+
const newCollateralBalance = decimalMax(
|
|
2313
|
+
0,
|
|
2314
|
+
decimalMin(
|
|
2315
|
+
collateralBalance,
|
|
2316
|
+
collateralBalance.plus(suppliedBalance).minus(amountDecimal)
|
|
2317
|
+
)
|
|
2318
|
+
);
|
|
2319
|
+
if (newCollateralBalance.lt(collateralBalance)) {
|
|
2320
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2321
|
+
...clonedPortfolio.positions[position].collateral[tokenId],
|
|
2322
|
+
shares: newCollateralBalance.toFixed(),
|
|
2323
|
+
balance: newCollateralBalance.toFixed()
|
|
2324
|
+
};
|
|
2325
|
+
}
|
|
2326
|
+
clonedPortfolio.positions[position].borrowed[tokenId].balance = newBorrowedBalance.toFixed();
|
|
2327
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2328
|
+
type: "collateral",
|
|
2329
|
+
portfolio: clonedPortfolio,
|
|
2330
|
+
assets
|
|
2331
|
+
});
|
|
2332
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2333
|
+
type: "borrowed",
|
|
2334
|
+
portfolio: clonedPortfolio,
|
|
2335
|
+
assets
|
|
2336
|
+
});
|
|
2337
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2338
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2339
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2340
|
+
return { healthFactor, maxBorrowValue };
|
|
2341
|
+
};
|
|
2342
|
+
var recomputeHealthFactorSupply = ({
|
|
2343
|
+
tokenId,
|
|
2344
|
+
amount,
|
|
2345
|
+
portfolio,
|
|
2346
|
+
assets,
|
|
2347
|
+
useAsCollateral
|
|
2348
|
+
}) => {
|
|
2349
|
+
if (_7__default.default.isEmpty(assets) || !tokenId || !portfolio)
|
|
2350
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2351
|
+
const asset = assets[tokenId];
|
|
2352
|
+
const { metadata, config: config2 } = asset;
|
|
2353
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2354
|
+
const position = DEFAULT_POSITION;
|
|
2355
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2356
|
+
const amountDecimal = expandTokenDecimal(amount, decimals);
|
|
2357
|
+
if (!clonedPortfolio.positions[position]) {
|
|
2358
|
+
clonedPortfolio.positions[position] = {
|
|
2359
|
+
collateral: {
|
|
2360
|
+
[tokenId]: {
|
|
2361
|
+
balance: "0",
|
|
2362
|
+
shares: "0",
|
|
2363
|
+
apr: "0"
|
|
2364
|
+
}
|
|
2365
|
+
},
|
|
2366
|
+
borrowed: {}
|
|
2367
|
+
};
|
|
2368
|
+
} else if (!clonedPortfolio.positions[position].collateral[tokenId]) {
|
|
2369
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2370
|
+
balance: "0",
|
|
2371
|
+
shares: "0",
|
|
2372
|
+
apr: "0"
|
|
2373
|
+
};
|
|
2374
|
+
}
|
|
2375
|
+
const collateralBalance = new Decimal3__default.default(
|
|
2376
|
+
clonedPortfolio.positions[position].collateral[tokenId].balance
|
|
2377
|
+
);
|
|
2378
|
+
const newBalance = collateralBalance.plus(
|
|
2379
|
+
useAsCollateral ? amountDecimal : 0
|
|
2380
|
+
);
|
|
2381
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2382
|
+
...clonedPortfolio.positions[position].collateral[tokenId],
|
|
2383
|
+
shares: newBalance.toFixed(),
|
|
2384
|
+
balance: newBalance.toFixed()
|
|
2385
|
+
};
|
|
2386
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2387
|
+
type: "collateral",
|
|
2388
|
+
portfolio: clonedPortfolio,
|
|
2389
|
+
assets
|
|
2390
|
+
});
|
|
2391
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2392
|
+
type: "borrowed",
|
|
2393
|
+
portfolio,
|
|
2394
|
+
assets
|
|
2395
|
+
});
|
|
2396
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2397
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2398
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2399
|
+
return { healthFactor, maxBorrowValue };
|
|
2400
|
+
};
|
|
2401
|
+
var recomputeHealthFactorWithdraw = ({
|
|
2402
|
+
tokenId,
|
|
2403
|
+
amount,
|
|
2404
|
+
portfolio,
|
|
2405
|
+
assets
|
|
2406
|
+
}) => {
|
|
2407
|
+
if (_7__default.default.isEmpty(assets) || !tokenId || !portfolio) {
|
|
2408
|
+
return { healthFactor: 0, maxBorrowValue: new Decimal3__default.default(0) };
|
|
2409
|
+
}
|
|
2410
|
+
const asset = assets[tokenId];
|
|
2411
|
+
const { metadata, config: config2 } = asset;
|
|
2412
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2413
|
+
const position = DEFAULT_POSITION;
|
|
2414
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2415
|
+
if (!clonedPortfolio.positions[position]) {
|
|
2416
|
+
clonedPortfolio.positions[position] = {
|
|
2417
|
+
collateral: {
|
|
2418
|
+
[tokenId]: {
|
|
2419
|
+
balance: "0",
|
|
2420
|
+
shares: "0",
|
|
2421
|
+
apr: "0"
|
|
2422
|
+
}
|
|
2423
|
+
},
|
|
2424
|
+
borrowed: {}
|
|
2425
|
+
};
|
|
2426
|
+
} else if (!clonedPortfolio.positions[position].collateral[tokenId]) {
|
|
2427
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2428
|
+
balance: "0",
|
|
2429
|
+
shares: "0",
|
|
2430
|
+
apr: "0"
|
|
2431
|
+
};
|
|
2432
|
+
}
|
|
2433
|
+
if (!clonedPortfolio.supplied[tokenId]) {
|
|
2434
|
+
clonedPortfolio.supplied[tokenId] = {
|
|
2435
|
+
balance: "0",
|
|
2436
|
+
shares: "0",
|
|
2437
|
+
apr: "0"
|
|
2438
|
+
};
|
|
2439
|
+
}
|
|
2440
|
+
const collateralBalance = new Decimal3__default.default(
|
|
2441
|
+
clonedPortfolio.positions[position].collateral[tokenId].balance
|
|
2442
|
+
);
|
|
2443
|
+
const suppliedBalance = new Decimal3__default.default(
|
|
2444
|
+
clonedPortfolio.supplied[tokenId].balance
|
|
2445
|
+
);
|
|
2446
|
+
const amountDecimal = expandTokenDecimal(amount, decimals);
|
|
2447
|
+
const newCollateralBalance = decimalMax(
|
|
2448
|
+
0,
|
|
2449
|
+
decimalMin(
|
|
2450
|
+
collateralBalance,
|
|
2451
|
+
collateralBalance.plus(suppliedBalance).minus(amountDecimal)
|
|
2452
|
+
)
|
|
2453
|
+
);
|
|
2454
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2455
|
+
...clonedPortfolio.positions[position].collateral[tokenId],
|
|
2456
|
+
shares: newCollateralBalance.toFixed(),
|
|
2457
|
+
balance: newCollateralBalance.toFixed()
|
|
2458
|
+
};
|
|
2459
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2460
|
+
type: "collateral",
|
|
2461
|
+
portfolio: clonedPortfolio,
|
|
2462
|
+
assets
|
|
2463
|
+
});
|
|
2464
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2465
|
+
type: "borrowed",
|
|
2466
|
+
portfolio,
|
|
2467
|
+
assets
|
|
2468
|
+
});
|
|
2469
|
+
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2470
|
+
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2471
|
+
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2472
|
+
return { healthFactor, maxBorrowValue };
|
|
2473
|
+
};
|
|
2474
|
+
|
|
2475
|
+
// src/other/computeRelayerGas.ts
|
|
2476
|
+
var computeRelayerGas = ({
|
|
2477
|
+
nearStorageAmount,
|
|
2478
|
+
mca,
|
|
2479
|
+
relayerGasFees,
|
|
2480
|
+
assets,
|
|
2481
|
+
portfolio
|
|
2482
|
+
}) => {
|
|
2483
|
+
if (!mca || _7__default.default.isEmpty(assets) || _7__default.default.isEmpty(portfolio)) return;
|
|
2484
|
+
const { tokenId, amount, relayerFeeUsd } = searchMatchAssetId({
|
|
2485
|
+
portfolio,
|
|
2486
|
+
assets,
|
|
2487
|
+
nearStorageAmount,
|
|
2488
|
+
relayerGasFees
|
|
2489
|
+
});
|
|
2490
|
+
if (!tokenId) return;
|
|
2491
|
+
const asset = assets[tokenId];
|
|
2492
|
+
const { metadata, config: config2 } = asset;
|
|
2493
|
+
const position = DEFAULT_POSITION;
|
|
2494
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2495
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2496
|
+
if (!clonedPortfolio.positions[position]) {
|
|
2497
|
+
clonedPortfolio.positions[position] = {
|
|
2498
|
+
collateral: {
|
|
2499
|
+
[tokenId]: {
|
|
2500
|
+
balance: "0",
|
|
2501
|
+
shares: "0",
|
|
2502
|
+
apr: "0"
|
|
2210
2503
|
}
|
|
2211
2504
|
},
|
|
2212
2505
|
borrowed: {}
|
|
@@ -2276,19 +2569,27 @@ var computeRelayerGas = ({
|
|
|
2276
2569
|
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2277
2570
|
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
|
|
2278
2571
|
if (healthFactor > 105) {
|
|
2572
|
+
const amountToken = expandTokenDecimal(
|
|
2573
|
+
amount,
|
|
2574
|
+
asset?.metadata?.decimals || 0
|
|
2575
|
+
).toFixed(0, Decimal3__default.default.ROUND_DOWN);
|
|
2576
|
+
const amountBurrow = expandTokenDecimal(
|
|
2577
|
+
amount,
|
|
2578
|
+
(asset?.config?.extra_decimals || 0) + (asset?.metadata?.decimals || 0)
|
|
2579
|
+
).toFixed(0, Decimal3__default.default.ROUND_DOWN);
|
|
2279
2580
|
return {
|
|
2280
2581
|
portfolioMinusGas: clonedPortfolio,
|
|
2281
2582
|
tokenId,
|
|
2282
2583
|
amount,
|
|
2283
|
-
relayerFeeUsd,
|
|
2284
|
-
amountToken
|
|
2584
|
+
relayerFeeUsd: relayerFeeUsd || 0,
|
|
2585
|
+
amountToken,
|
|
2586
|
+
amountBurrow,
|
|
2587
|
+
simpleWithdrawData: {
|
|
2588
|
+
tokenId,
|
|
2285
2589
|
amount,
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
amount,
|
|
2290
|
-
(asset?.config?.extra_decimals || 0) + (asset?.metadata?.decimals || 0)
|
|
2291
|
-
).toFixed(0, Decimal3__default.default.ROUND_DOWN)
|
|
2590
|
+
amountToken,
|
|
2591
|
+
amountBurrow
|
|
2592
|
+
}
|
|
2292
2593
|
};
|
|
2293
2594
|
}
|
|
2294
2595
|
return;
|
|
@@ -2370,29 +2671,133 @@ function searchMatchAssetId({
|
|
|
2370
2671
|
}
|
|
2371
2672
|
return {};
|
|
2372
2673
|
}
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
var getSimpleWithdrawData = async ({
|
|
2376
|
-
nearStorageAmount,
|
|
2377
|
-
mca,
|
|
2378
|
-
relayerGasFees,
|
|
2674
|
+
var computeBorrowMaxAmount = ({
|
|
2675
|
+
tokenId,
|
|
2379
2676
|
assets,
|
|
2380
2677
|
portfolio
|
|
2381
2678
|
}) => {
|
|
2382
|
-
const
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2679
|
+
const asset = assets[tokenId];
|
|
2680
|
+
return Object.keys(portfolio.positions).map((position) => {
|
|
2681
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2682
|
+
type: "collateral",
|
|
2683
|
+
portfolio,
|
|
2684
|
+
assets
|
|
2685
|
+
});
|
|
2686
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2687
|
+
type: "borrowed",
|
|
2688
|
+
portfolio,
|
|
2689
|
+
assets
|
|
2690
|
+
});
|
|
2691
|
+
const volatiliyRatio = asset.config.volatility_ratio || 0;
|
|
2692
|
+
const price = asset.price?.usd || Infinity;
|
|
2693
|
+
const maxBorrowPricedForToken = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(volatiliyRatio).div(MAX_RATIO).mul(95).div(100);
|
|
2694
|
+
const maxBorrowAmountTemp = maxBorrowPricedForToken.div(price);
|
|
2695
|
+
const maxBorrowAmount = Decimal3__default.default(
|
|
2696
|
+
Math.max(0, maxBorrowAmountTemp.toNumber())
|
|
2697
|
+
);
|
|
2698
|
+
const maxBorrowPriced = adjustedCollateralSum.sub(adjustedBorrowedSum);
|
|
2699
|
+
return {
|
|
2700
|
+
[position]: {
|
|
2701
|
+
maxBorrowAmount: Math.max(maxBorrowAmount.toNumber(), 0),
|
|
2702
|
+
maxBorrowValue: Math.max(maxBorrowPriced.toNumber(), 0)
|
|
2703
|
+
}
|
|
2704
|
+
};
|
|
2705
|
+
}).reduce((acc, cur) => ({ ...acc, ...cur }), {});
|
|
2706
|
+
};
|
|
2707
|
+
var getBorrowMaxAmount = ({
|
|
2708
|
+
tokenId,
|
|
2709
|
+
portfolio,
|
|
2710
|
+
assets
|
|
2711
|
+
}) => {
|
|
2712
|
+
if (_7__default.default.isEmpty(assets) || !portfolio || !tokenId)
|
|
2713
|
+
return {
|
|
2714
|
+
[DEFAULT_POSITION]: { maxBorrowAmount: 0, maxBorrowValue: 0 }
|
|
2715
|
+
};
|
|
2716
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2717
|
+
const maxBorrowAmount = computeBorrowMaxAmount({
|
|
2718
|
+
tokenId,
|
|
2719
|
+
assets,
|
|
2720
|
+
portfolio: clonedPortfolio
|
|
2721
|
+
});
|
|
2722
|
+
return maxBorrowAmount;
|
|
2723
|
+
};
|
|
2724
|
+
var computeWithdrawMaxAmount = ({
|
|
2725
|
+
tokenId,
|
|
2726
|
+
assets,
|
|
2727
|
+
portfolio
|
|
2728
|
+
}) => {
|
|
2729
|
+
const asset = assets[tokenId];
|
|
2730
|
+
const position = DEFAULT_POSITION;
|
|
2731
|
+
const assetPrice = asset.price ? new Decimal3__default.default(asset.price.usd || "0") : new Decimal3__default.default(0);
|
|
2732
|
+
const suppliedBalance = new Decimal3__default.default(
|
|
2733
|
+
portfolio.supplied[tokenId]?.balance || 0
|
|
2734
|
+
);
|
|
2735
|
+
const collateralBalance = new Decimal3__default.default(
|
|
2736
|
+
portfolio.positions[position]?.collateral?.[tokenId]?.balance || 0
|
|
2737
|
+
);
|
|
2738
|
+
let maxAmount = suppliedBalance;
|
|
2739
|
+
if (portfolio.borrows.length > 0 && collateralBalance.gt(0)) {
|
|
2740
|
+
const clonedPortfolio = JSON.parse(JSON.stringify(portfolio));
|
|
2741
|
+
clonedPortfolio.positions[position].collateral[tokenId] = {
|
|
2742
|
+
apr: "0",
|
|
2743
|
+
balance: "0",
|
|
2744
|
+
shares: "0"
|
|
2745
|
+
};
|
|
2746
|
+
const _adjustedCollateralSum = getAdjustedSum({
|
|
2747
|
+
type: "collateral",
|
|
2748
|
+
portfolio: clonedPortfolio,
|
|
2749
|
+
assets
|
|
2750
|
+
});
|
|
2751
|
+
const adjustedBorrowedSum = getAdjustedSum({
|
|
2752
|
+
type: "borrowed",
|
|
2753
|
+
portfolio,
|
|
2754
|
+
assets
|
|
2755
|
+
});
|
|
2756
|
+
const healthFactor = _adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
|
|
2757
|
+
if (healthFactor <= 105) {
|
|
2758
|
+
const adjustedCollateralSum = getAdjustedSum({
|
|
2759
|
+
type: "collateral",
|
|
2760
|
+
portfolio,
|
|
2761
|
+
assets
|
|
2762
|
+
});
|
|
2763
|
+
const adjustedPricedDiff = decimalMax(
|
|
2764
|
+
0,
|
|
2765
|
+
adjustedCollateralSum.sub(adjustedBorrowedSum)
|
|
2766
|
+
);
|
|
2767
|
+
const safeAdjustedPricedDiff = adjustedPricedDiff.mul(999).div(1e3);
|
|
2768
|
+
const safePricedDiff = safeAdjustedPricedDiff.div(asset.config.volatility_ratio).mul(1e4);
|
|
2769
|
+
const safeDiff = safePricedDiff.div(assetPrice).mul(
|
|
2770
|
+
expandTokenDecimal(
|
|
2771
|
+
1,
|
|
2772
|
+
asset.config.extra_decimals + (asset?.metadata?.decimals || 0)
|
|
2773
|
+
)
|
|
2774
|
+
).trunc();
|
|
2775
|
+
maxAmount = maxAmount.add(decimalMin(safeDiff, collateralBalance));
|
|
2776
|
+
} else {
|
|
2777
|
+
maxAmount = suppliedBalance.plus(collateralBalance);
|
|
2778
|
+
}
|
|
2779
|
+
} else {
|
|
2780
|
+
maxAmount = suppliedBalance.plus(collateralBalance);
|
|
2781
|
+
}
|
|
2782
|
+
return {
|
|
2783
|
+
maxAmount,
|
|
2784
|
+
canWithdrawAll: maxAmount.eq(suppliedBalance.plus(collateralBalance))
|
|
2785
|
+
};
|
|
2786
|
+
};
|
|
2787
|
+
var getWithdrawMaxAmount = ({
|
|
2788
|
+
tokenId,
|
|
2789
|
+
assets,
|
|
2790
|
+
portfolio
|
|
2791
|
+
}) => {
|
|
2792
|
+
const asset = assets[tokenId];
|
|
2793
|
+
const { metadata, config: config2 } = asset;
|
|
2794
|
+
const decimals = (metadata?.decimals || 0) + config2.extra_decimals;
|
|
2795
|
+
const { maxAmount } = computeWithdrawMaxAmount({
|
|
2796
|
+
tokenId,
|
|
2386
2797
|
assets,
|
|
2387
2798
|
portfolio
|
|
2388
2799
|
});
|
|
2389
|
-
|
|
2390
|
-
tokenId: gasData.tokenId,
|
|
2391
|
-
amount: gasData.amount,
|
|
2392
|
-
amountToken: gasData.amountToken,
|
|
2393
|
-
amountBurrow: gasData.amountBurrow
|
|
2394
|
-
} : null;
|
|
2395
|
-
return simpleWithdrawData;
|
|
2800
|
+
return Number(shrinkToken(maxAmount.toFixed(), decimals));
|
|
2396
2801
|
};
|
|
2397
2802
|
|
|
2398
2803
|
exports.DEFAULT_POSITION = DEFAULT_POSITION;
|
|
@@ -2404,6 +2809,7 @@ exports.TGas = TGas;
|
|
|
2404
2809
|
exports.TOKEN_STORAGE_DEPOSIT_READ = TOKEN_STORAGE_DEPOSIT_READ;
|
|
2405
2810
|
exports.batchViews = batchViews;
|
|
2406
2811
|
exports.computeRelayerGas = computeRelayerGas;
|
|
2812
|
+
exports.computeWithdrawMaxAmount = computeWithdrawMaxAmount;
|
|
2407
2813
|
exports.config_btc = config_btc;
|
|
2408
2814
|
exports.config_evm = config_evm;
|
|
2409
2815
|
exports.config_near = config_near;
|
|
@@ -2418,12 +2824,14 @@ exports.fetchIntentsTransactionStatus = fetchIntentsTransactionStatus;
|
|
|
2418
2824
|
exports.format_wallet = format_wallet;
|
|
2419
2825
|
exports.getAccountAllPositions = getAccountAllPositions;
|
|
2420
2826
|
exports.getAccountBalance = getAccountBalance;
|
|
2827
|
+
exports.getAdjustedSum = getAdjustedSum;
|
|
2421
2828
|
exports.getAllFarms = getAllFarms;
|
|
2422
2829
|
exports.getAssets = getAssets;
|
|
2423
2830
|
exports.getAssetsDetail = getAssetsDetail;
|
|
2424
2831
|
exports.getAuthenticationHeaders = getAuthenticationHeaders;
|
|
2425
2832
|
exports.getBalance = getBalance;
|
|
2426
2833
|
exports.getBoosterTokens = getBoosterTokens;
|
|
2834
|
+
exports.getBorrowMaxAmount = getBorrowMaxAmount;
|
|
2427
2835
|
exports.getCeateMcaFee = getCeateMcaFee;
|
|
2428
2836
|
exports.getConfig = getConfig2;
|
|
2429
2837
|
exports.getCreateMcaCustomRecipientMsg = getCreateMcaCustomRecipientMsg;
|
|
@@ -2436,13 +2844,12 @@ exports.getMultichainLendingHistory = getMultichainLendingHistory;
|
|
|
2436
2844
|
exports.getMultichainTokensByChains = getMultichainTokensByChains;
|
|
2437
2845
|
exports.getNearValue = getNearValue;
|
|
2438
2846
|
exports.getNearValuesPaged = getNearValuesPaged;
|
|
2439
|
-
exports.getPortfolio = getPortfolio;
|
|
2440
2847
|
exports.getPrices = getPrices;
|
|
2441
2848
|
exports.getRepayCustomRecipientMsg = getRepayCustomRecipientMsg;
|
|
2442
2849
|
exports.getSignature = getSignature;
|
|
2443
|
-
exports.getSimpleWithdrawData = getSimpleWithdrawData;
|
|
2444
2850
|
exports.getSupplyCustomRecipientMsg = getSupplyCustomRecipientMsg;
|
|
2445
2851
|
exports.getTokenPythInfos = getTokenPythInfos;
|
|
2852
|
+
exports.getWithdrawMaxAmount = getWithdrawMaxAmount;
|
|
2446
2853
|
exports.get_all_tokens_metadata = get_all_tokens_metadata;
|
|
2447
2854
|
exports.get_interest_rate = get_interest_rate;
|
|
2448
2855
|
exports.get_liquidations = get_liquidations;
|
|
@@ -2476,6 +2883,12 @@ exports.process_signature_evm = process_signature_evm;
|
|
|
2476
2883
|
exports.process_signature_solana = process_signature_solana;
|
|
2477
2884
|
exports.query_account_register_token_tx = query_account_register_token_tx;
|
|
2478
2885
|
exports.query_intents_tansfer_txs = query_intents_tansfer_txs;
|
|
2886
|
+
exports.recomputeHealthFactorAdjust = recomputeHealthFactorAdjust;
|
|
2887
|
+
exports.recomputeHealthFactorBorrow = recomputeHealthFactorBorrow;
|
|
2888
|
+
exports.recomputeHealthFactorRepay = recomputeHealthFactorRepay;
|
|
2889
|
+
exports.recomputeHealthFactorRepayFromDeposits = recomputeHealthFactorRepayFromDeposits;
|
|
2890
|
+
exports.recomputeHealthFactorSupply = recomputeHealthFactorSupply;
|
|
2891
|
+
exports.recomputeHealthFactorWithdraw = recomputeHealthFactorWithdraw;
|
|
2479
2892
|
exports.serializationObj = serializationObj;
|
|
2480
2893
|
exports.setCustomNodeUrl = setCustomNodeUrl;
|
|
2481
2894
|
exports.shrinkToken = shrinkToken;
|