@defisaver/positions-sdk 0.0.98 → 0.0.99

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.
@@ -4457,6 +4457,32 @@ export namespace USDCPriceFeed {
4457
4457
  export namespace MorphoBlueView {
4458
4458
  let abi_66: ({
4459
4459
  inputs: {
4460
+ components: ({
4461
+ components: {
4462
+ internalType: string;
4463
+ name: string;
4464
+ type: string;
4465
+ }[];
4466
+ internalType: string;
4467
+ name: string;
4468
+ type: string;
4469
+ } | {
4470
+ internalType: string;
4471
+ name: string;
4472
+ type: string;
4473
+ components?: undefined;
4474
+ })[];
4475
+ internalType: string;
4476
+ name: string;
4477
+ type: string;
4478
+ }[];
4479
+ name: string;
4480
+ outputs: ({
4481
+ internalType: string;
4482
+ name: string;
4483
+ type: string;
4484
+ components?: undefined;
4485
+ } | {
4460
4486
  components: {
4461
4487
  internalType: string;
4462
4488
  name: string;
@@ -4465,7 +4491,25 @@ export namespace MorphoBlueView {
4465
4491
  internalType: string;
4466
4492
  name: string;
4467
4493
  type: string;
4468
- }[];
4494
+ })[];
4495
+ stateMutability: string;
4496
+ type: string;
4497
+ } | {
4498
+ inputs: ({
4499
+ internalType: string;
4500
+ name: string;
4501
+ type: string;
4502
+ components?: undefined;
4503
+ } | {
4504
+ components: {
4505
+ internalType: string;
4506
+ name: string;
4507
+ type: string;
4508
+ }[];
4509
+ internalType: string;
4510
+ name: string;
4511
+ type: string;
4512
+ })[];
4469
4513
  name: string;
4470
4514
  outputs: {
4471
4515
  internalType: string;
@@ -819,13 +819,13 @@ module.exports = {
819
819
  }
820
820
  },
821
821
  "MorphoBlueView": {
822
- "abi": [{ "inputs": [], "name": "MORPHO_BLUE_ADDRESS", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }], "name": "getMarketId", "outputs": [{ "internalType": "Id", "name": "id", "type": "bytes32" }], "stateMutability": "pure", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }], "name": "getMarketInfo", "outputs": [{ "components": [{ "internalType": "Id", "name": "id", "type": "bytes32" }, { "internalType": "uint128", "name": "totalSupplyAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalSupplyShares", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowShares", "type": "uint128" }, { "internalType": "uint256", "name": "lastUpdate", "type": "uint256" }, { "internalType": "uint256", "name": "fee", "type": "uint256" }, { "internalType": "uint256", "name": "borrowRate", "type": "uint256" }, { "internalType": "uint256", "name": "oracle", "type": "uint256" }], "internalType": "struct MorphoBlueView.MarketInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "name": "getMarketInfoNotTuple", "outputs": [{ "components": [{ "internalType": "Id", "name": "id", "type": "bytes32" }, { "internalType": "uint128", "name": "totalSupplyAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalSupplyShares", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowShares", "type": "uint128" }, { "internalType": "uint256", "name": "lastUpdate", "type": "uint256" }, { "internalType": "uint256", "name": "fee", "type": "uint256" }, { "internalType": "uint256", "name": "borrowRate", "type": "uint256" }, { "internalType": "uint256", "name": "oracle", "type": "uint256" }], "internalType": "struct MorphoBlueView.MarketInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }, { "internalType": "address", "name": "owner", "type": "address" }], "name": "getUserInfo", "outputs": [{ "components": [{ "internalType": "uint256", "name": "supplyShares", "type": "uint256" }, { "internalType": "uint256", "name": "suppliedInAssets", "type": "uint256" }, { "internalType": "uint256", "name": "borrowShares", "type": "uint256" }, { "internalType": "uint256", "name": "borrowedInAssets", "type": "uint256" }, { "internalType": "uint256", "name": "collateral", "type": "uint256" }], "internalType": "struct MorphoBlueView.PositionInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "morphoBlue", "outputs": [{ "internalType": "contract IMorphoBlue", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }],
822
+ "abi": [{ "inputs": [], "name": "MORPHO_BLUE_ADDRESS", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "components": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }, { "internalType": "bool", "name": "isBorrowOperation", "type": "bool" }, { "internalType": "uint256", "name": "liquidityAdded", "type": "uint256" }, { "internalType": "uint256", "name": "liquidityRemoved", "type": "uint256" }], "internalType": "struct MorphoBlueView.LiquidityChangeParams", "name": "params", "type": "tuple" }], "name": "getApyAfterValuesEstimation", "outputs": [{ "internalType": "uint256", "name": "borrowRate", "type": "uint256" }, { "components": [{ "internalType": "uint128", "name": "totalSupplyAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalSupplyShares", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowShares", "type": "uint128" }, { "internalType": "uint128", "name": "lastUpdate", "type": "uint128" }, { "internalType": "uint128", "name": "fee", "type": "uint128" }], "internalType": "struct Market", "name": "market", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }], "name": "getMarketId", "outputs": [{ "internalType": "Id", "name": "id", "type": "bytes32" }], "stateMutability": "pure", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }], "name": "getMarketInfo", "outputs": [{ "components": [{ "internalType": "Id", "name": "id", "type": "bytes32" }, { "internalType": "uint128", "name": "totalSupplyAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalSupplyShares", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowShares", "type": "uint128" }, { "internalType": "uint256", "name": "lastUpdate", "type": "uint256" }, { "internalType": "uint256", "name": "fee", "type": "uint256" }, { "internalType": "uint256", "name": "borrowRate", "type": "uint256" }, { "internalType": "uint256", "name": "oracle", "type": "uint256" }], "internalType": "struct MorphoBlueView.MarketInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "name": "getMarketInfoNotTuple", "outputs": [{ "components": [{ "internalType": "Id", "name": "id", "type": "bytes32" }, { "internalType": "uint128", "name": "totalSupplyAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalSupplyShares", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowShares", "type": "uint128" }, { "internalType": "uint256", "name": "lastUpdate", "type": "uint256" }, { "internalType": "uint256", "name": "fee", "type": "uint256" }, { "internalType": "uint256", "name": "borrowRate", "type": "uint256" }, { "internalType": "uint256", "name": "oracle", "type": "uint256" }], "internalType": "struct MorphoBlueView.MarketInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "Id", "name": "marketId", "type": "bytes32" }, { "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }, { "internalType": "address", "name": "owner", "type": "address" }], "name": "getRatio", "outputs": [{ "internalType": "uint256", "name": "ratio", "type": "uint256" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "Id", "name": "marketId", "type": "bytes32" }, { "internalType": "address", "name": "owner", "type": "address" }], "name": "getRatioUsingId", "outputs": [{ "internalType": "uint256", "name": "ratio", "type": "uint256" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }, { "internalType": "address", "name": "owner", "type": "address" }], "name": "getRatioUsingParams", "outputs": [{ "internalType": "uint256", "name": "ratio", "type": "uint256" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }, { "internalType": "address", "name": "owner", "type": "address" }], "name": "getUserInfo", "outputs": [{ "components": [{ "internalType": "uint256", "name": "supplyShares", "type": "uint256" }, { "internalType": "uint256", "name": "suppliedInAssets", "type": "uint256" }, { "internalType": "uint256", "name": "borrowShares", "type": "uint256" }, { "internalType": "uint256", "name": "borrowedInAssets", "type": "uint256" }, { "internalType": "uint256", "name": "collateral", "type": "uint256" }], "internalType": "struct MorphoBlueView.PositionInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "morphoBlue", "outputs": [{ "internalType": "contract IMorphoBlue", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }],
823
823
  "networks": {
824
824
  "1": {
825
- "address": "0xE29175a86B60138403a9534A391acaDb19f1E9a6",
825
+ "address": "0x10B621823D4f3E85fBDF759e252598e4e097C1fd",
826
826
  },
827
827
  "8453": {
828
- "address": "0xDBCACF59C5063da8B15481F88E7D70E13c92f2a1",
828
+ "address": "0x53c0E962bd0AC53928ca04703238b2ec2894195B",
829
829
  }
830
830
  }
831
831
  },
@@ -1,7 +1,14 @@
1
- import { MMUsedAssets } from '../../types/common';
2
- import { MorphoBlueAggregatedPositionData, MorphoBlueAssetsData, MorphoBlueMarketInfo } from '../../types';
1
+ import Web3 from 'web3';
2
+ import { MMUsedAssets, NetworkNumber } from '../../types/common';
3
+ import { MorphoBlueAggregatedPositionData, MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo } from '../../types';
3
4
  export declare const getMorphoBlueAggregatedPositionData: ({ usedAssets, assetsData, marketInfo }: {
4
5
  usedAssets: MMUsedAssets;
5
6
  assetsData: MorphoBlueAssetsData;
6
7
  marketInfo: MorphoBlueMarketInfo;
7
8
  }) => MorphoBlueAggregatedPositionData;
9
+ export declare const getSupplyRate: (totalSupplyAssets: string, totalBorrowAssets: string, borrowRate: string, fee: string) => string;
10
+ export declare const getBorrowRate: (borrowRate: string, totalBorrowShares: string) => string;
11
+ export declare const getApyAfterValuesEstimation: (selectedMarket: MorphoBlueMarketData, action: string, amount: string, asset: string, web3: Web3, network: NetworkNumber) => Promise<{
12
+ borrowRate: string;
13
+ supplyRate: string;
14
+ }>;
@@ -1,12 +1,24 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
13
  };
5
14
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getMorphoBlueAggregatedPositionData = void 0;
15
+ exports.getApyAfterValuesEstimation = exports.getBorrowRate = exports.getSupplyRate = exports.getMorphoBlueAggregatedPositionData = void 0;
7
16
  const decimal_js_1 = __importDefault(require("decimal.js"));
17
+ const tokens_1 = require("@defisaver/tokens");
8
18
  const moneymarket_1 = require("../../moneymarket");
9
19
  const staking_1 = require("../../staking");
20
+ const constants_1 = require("../../constants");
21
+ const contracts_1 = require("../../contracts");
10
22
  const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, marketInfo }) => {
11
23
  var _a, _b, _c, _d, _e, _f;
12
24
  const payload = {};
@@ -46,3 +58,56 @@ const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, marketInf
46
58
  return payload;
47
59
  };
48
60
  exports.getMorphoBlueAggregatedPositionData = getMorphoBlueAggregatedPositionData;
61
+ const compound = (ratePerSeconds) => {
62
+ const compounding = new decimal_js_1.default(ratePerSeconds).mul(constants_1.SECONDS_PER_YEAR).toString();
63
+ const apyNumber = Math.expm1(new decimal_js_1.default(compounding).div(constants_1.WAD).toNumber());
64
+ return new decimal_js_1.default(apyNumber).mul(constants_1.WAD).floor().toString();
65
+ };
66
+ const getSupplyRate = (totalSupplyAssets, totalBorrowAssets, borrowRate, fee) => {
67
+ if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
68
+ return '0';
69
+ }
70
+ const utillization = new decimal_js_1.default(totalBorrowAssets).mul(constants_1.WAD).div(totalSupplyAssets).ceil()
71
+ .toString();
72
+ const supplyRate = new decimal_js_1.default(utillization).mul(borrowRate).div(constants_1.WAD).ceil()
73
+ .toString();
74
+ const ratePerSecond = new decimal_js_1.default(supplyRate).mul(new decimal_js_1.default(constants_1.WAD).minus(fee)).div(constants_1.WAD).ceil()
75
+ .toString();
76
+ return new decimal_js_1.default(compound(ratePerSecond)).div(1e18).mul(100).toString();
77
+ };
78
+ exports.getSupplyRate = getSupplyRate;
79
+ const getBorrowRate = (borrowRate, totalBorrowShares) => {
80
+ if (totalBorrowShares === '0') {
81
+ return '0';
82
+ }
83
+ return new decimal_js_1.default(compound(borrowRate)).div(1e18).mul(100).toString();
84
+ };
85
+ exports.getBorrowRate = getBorrowRate;
86
+ const borrowOperations = ['borrow', 'payback'];
87
+ const getApyAfterValuesEstimation = (selectedMarket, action, amount, asset, web3, network) => __awaiter(void 0, void 0, void 0, function* () {
88
+ const morphoBlueViewContract = (0, contracts_1.MorphoBlueViewContract)(web3, network);
89
+ const lltvInWei = (0, tokens_1.assetAmountInWei)(selectedMarket.lltv, 'ETH');
90
+ const marketData = [selectedMarket.loanToken, selectedMarket.collateralToken, selectedMarket.oracle, selectedMarket.irm, lltvInWei];
91
+ const isBorrowOperation = borrowOperations.includes(action);
92
+ const amountInWei = (0, tokens_1.assetAmountInWei)(amount, asset);
93
+ let liquidityAdded;
94
+ let liquidityRemoved;
95
+ if (isBorrowOperation) {
96
+ liquidityAdded = action === 'payback' ? amountInWei : '0';
97
+ liquidityRemoved = action === 'borrow' ? amountInWei : '0';
98
+ }
99
+ else {
100
+ liquidityAdded = action === 'collateral' ? amountInWei : '0';
101
+ liquidityRemoved = action === 'withdraw' ? amountInWei : '0';
102
+ }
103
+ const data = yield morphoBlueViewContract.methods.getApyAfterValuesEstimation([
104
+ marketData,
105
+ isBorrowOperation,
106
+ liquidityAdded,
107
+ liquidityRemoved,
108
+ ]).call();
109
+ const borrowRate = (0, exports.getBorrowRate)(data.borrowRate, data.market.totalBorrowShares);
110
+ const supplyRate = (0, exports.getSupplyRate)(data.market.totalSupplyAssets, data.market.totalBorrowAssets, data.borrowRate, data.market.fee);
111
+ return { borrowRate, supplyRate };
112
+ });
113
+ exports.getApyAfterValuesEstimation = getApyAfterValuesEstimation;
@@ -21,29 +21,6 @@ const constants_1 = require("../constants");
21
21
  const staking_1 = require("../staking");
22
22
  const utils_1 = require("../services/utils");
23
23
  const morphoBlueHelpers_1 = require("../helpers/morphoBlueHelpers");
24
- const compound = (ratePerSeconds) => {
25
- const compounding = new decimal_js_1.default(ratePerSeconds).mul(constants_1.SECONDS_PER_YEAR).toString();
26
- const apyNumber = Math.expm1(new decimal_js_1.default(compounding).div(constants_1.WAD).toNumber());
27
- return new decimal_js_1.default(apyNumber).mul(constants_1.WAD).floor().toString();
28
- };
29
- const getSupplyRate = (totalSupplyAssets, totalBorrowAssets, borrowRate, fee) => {
30
- if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
31
- return 0;
32
- }
33
- const utillization = new decimal_js_1.default(totalBorrowAssets).mul(constants_1.WAD).div(totalSupplyAssets).ceil()
34
- .toString();
35
- const supplyRate = new decimal_js_1.default(utillization).mul(borrowRate).div(constants_1.WAD).ceil()
36
- .toString();
37
- const ratePerSecond = new decimal_js_1.default(supplyRate).mul(new decimal_js_1.default(constants_1.WAD).minus(fee)).div(constants_1.WAD).ceil()
38
- .toString();
39
- return compound(ratePerSecond);
40
- };
41
- const getBorrowRate = (borrowRate, totalBorrowShares) => {
42
- if (totalBorrowShares === '0') {
43
- return 0;
44
- }
45
- return compound(borrowRate);
46
- };
47
24
  function getMorphoBlueMarketData(web3, network, selectedMarket, mainnetWeb3) {
48
25
  return __awaiter(this, void 0, void 0, function* () {
49
26
  const { loanToken, collateralToken, oracle, irm, lltv, oracleType, } = selectedMarket;
@@ -61,8 +38,8 @@ function getMorphoBlueMarketData(web3, network, selectedMarket, mainnetWeb3) {
61
38
  loanTokenInfo.symbol === 'USDA' ? '100000000' : feedRegistryContract.methods.latestAnswer(loanTokenFeedAddress, constants_1.USD_QUOTE).call(),
62
39
  morphoBlueViewContract.methods.getMarketInfoNotTuple(loanToken, collateralToken, oracle, irm, lltvInWei).call(),
63
40
  ]);
64
- const supplyRate = getSupplyRate(marketInfo.totalSupplyAssets, marketInfo.totalBorrowAssets, marketInfo.borrowRate, marketInfo.fee);
65
- const compoundedBorrowRate = getBorrowRate(marketInfo.borrowRate, marketInfo.totalBorrowShares);
41
+ const supplyRate = (0, morphoBlueHelpers_1.getSupplyRate)(marketInfo.totalSupplyAssets, marketInfo.totalBorrowAssets, marketInfo.borrowRate, marketInfo.fee);
42
+ const compoundedBorrowRate = (0, morphoBlueHelpers_1.getBorrowRate)(marketInfo.borrowRate, marketInfo.totalBorrowShares);
66
43
  const utillization = new decimal_js_1.default(marketInfo.totalBorrowAssets).div(marketInfo.totalSupplyAssets).mul(100).toString();
67
44
  const oracleScaleFactor = new decimal_js_1.default(36).add(loanTokenInfo.decimals).sub(collateralTokenInfo.decimals).toString();
68
45
  const oracleScale = new decimal_js_1.default(10).pow(oracleScaleFactor).toString();
@@ -73,8 +50,8 @@ function getMorphoBlueMarketData(web3, network, selectedMarket, mainnetWeb3) {
73
50
  symbol: (0, utils_1.wethToEth)(loanTokenInfo.symbol),
74
51
  address: loanToken,
75
52
  price: new decimal_js_1.default(loanTokenPrice).div(1e8).toString(),
76
- supplyRate: new decimal_js_1.default(supplyRate).div(constants_1.WAD).mul(100).toString(),
77
- borrowRate: new decimal_js_1.default(compoundedBorrowRate).div(constants_1.WAD).mul(100).toString(),
53
+ supplyRate,
54
+ borrowRate: compoundedBorrowRate,
78
55
  totalSupply: new decimal_js_1.default(marketInfo.totalSupplyAssets).div(scale).toString(),
79
56
  totalBorrow: new decimal_js_1.default(marketInfo.totalBorrowAssets).div(scale).toString(),
80
57
  canBeSupplied: true,
@@ -31,7 +31,58 @@ export type MarketParamsStructOutputStruct = {
31
31
  lltv: string;
32
32
  };
33
33
  export type MarketParamsStructOutput = MarketParamsStructOutputArray & MarketParamsStructOutputStruct;
34
+ export type MarketStruct = [
35
+ number | string | BN,
36
+ number | string | BN,
37
+ number | string | BN,
38
+ number | string | BN,
39
+ number | string | BN,
40
+ number | string | BN
41
+ ] | {
42
+ totalSupplyAssets: number | string | BN;
43
+ totalSupplyShares: number | string | BN;
44
+ totalBorrowAssets: number | string | BN;
45
+ totalBorrowShares: number | string | BN;
46
+ lastUpdate: number | string | BN;
47
+ fee: number | string | BN;
48
+ };
49
+ export type MarketStructOutputArray = [
50
+ string,
51
+ string,
52
+ string,
53
+ string,
54
+ string,
55
+ string
56
+ ];
57
+ export type MarketStructOutputStruct = {
58
+ totalSupplyAssets: string;
59
+ totalSupplyShares: string;
60
+ totalBorrowAssets: string;
61
+ totalBorrowShares: string;
62
+ lastUpdate: string;
63
+ fee: string;
64
+ };
65
+ export type MarketStructOutput = MarketStructOutputArray & MarketStructOutputStruct;
34
66
  export declare namespace MorphoBlueView {
67
+ type LiquidityChangeParamsStruct = [MarketParamsStruct, boolean, number | string | BN, number | string | BN] | {
68
+ marketParams: MarketParamsStruct;
69
+ isBorrowOperation: boolean;
70
+ liquidityAdded: number | string | BN;
71
+ liquidityRemoved: number | string | BN;
72
+ };
73
+ type LiquidityChangeParamsStructOutputArray = [
74
+ MarketParamsStructOutput,
75
+ boolean,
76
+ string,
77
+ string
78
+ ];
79
+ type LiquidityChangeParamsStructOutputStruct = {
80
+ marketParams: MarketParamsStructOutput;
81
+ isBorrowOperation: boolean;
82
+ liquidityAdded: string;
83
+ liquidityRemoved: string;
84
+ };
85
+ type LiquidityChangeParamsStructOutput = LiquidityChangeParamsStructOutputArray & LiquidityChangeParamsStructOutputStruct;
35
86
  type MarketInfoStruct = [
36
87
  string | number[],
37
88
  number | string | BN,
@@ -110,9 +161,19 @@ export interface MorphoBlueView extends BaseContract {
110
161
  clone(): MorphoBlueView;
111
162
  methods: {
112
163
  MORPHO_BLUE_ADDRESS(): NonPayableTransactionObject<string>;
164
+ getApyAfterValuesEstimation(params: MorphoBlueView.LiquidityChangeParamsStruct): NonPayableTransactionObject<[
165
+ string,
166
+ MarketStructOutput
167
+ ] & {
168
+ borrowRate: string;
169
+ market: MarketStructOutput;
170
+ }>;
113
171
  getMarketId(marketParams: MarketParamsStruct): NonPayableTransactionObject<string>;
114
172
  getMarketInfo(marketParams: MarketParamsStruct): NonPayableTransactionObject<MorphoBlueView.MarketInfoStructOutput>;
115
173
  getMarketInfoNotTuple(loanToken: string, collToken: string, oracle: string, irm: string, lltv: number | string | BN): NonPayableTransactionObject<MorphoBlueView.MarketInfoStructOutput>;
174
+ getRatio(marketId: string | number[], marketParams: MarketParamsStruct, owner: string): NonPayableTransactionObject<string>;
175
+ getRatioUsingId(marketId: string | number[], owner: string): NonPayableTransactionObject<string>;
176
+ getRatioUsingParams(marketParams: MarketParamsStruct, owner: string): NonPayableTransactionObject<string>;
116
177
  getUserInfo(marketParams: MarketParamsStruct, owner: string): NonPayableTransactionObject<MorphoBlueView.PositionInfoStructOutput>;
117
178
  morphoBlue(): NonPayableTransactionObject<string>;
118
179
  };
@@ -4457,6 +4457,32 @@ export namespace USDCPriceFeed {
4457
4457
  export namespace MorphoBlueView {
4458
4458
  let abi_66: ({
4459
4459
  inputs: {
4460
+ components: ({
4461
+ components: {
4462
+ internalType: string;
4463
+ name: string;
4464
+ type: string;
4465
+ }[];
4466
+ internalType: string;
4467
+ name: string;
4468
+ type: string;
4469
+ } | {
4470
+ internalType: string;
4471
+ name: string;
4472
+ type: string;
4473
+ components?: undefined;
4474
+ })[];
4475
+ internalType: string;
4476
+ name: string;
4477
+ type: string;
4478
+ }[];
4479
+ name: string;
4480
+ outputs: ({
4481
+ internalType: string;
4482
+ name: string;
4483
+ type: string;
4484
+ components?: undefined;
4485
+ } | {
4460
4486
  components: {
4461
4487
  internalType: string;
4462
4488
  name: string;
@@ -4465,7 +4491,25 @@ export namespace MorphoBlueView {
4465
4491
  internalType: string;
4466
4492
  name: string;
4467
4493
  type: string;
4468
- }[];
4494
+ })[];
4495
+ stateMutability: string;
4496
+ type: string;
4497
+ } | {
4498
+ inputs: ({
4499
+ internalType: string;
4500
+ name: string;
4501
+ type: string;
4502
+ components?: undefined;
4503
+ } | {
4504
+ components: {
4505
+ internalType: string;
4506
+ name: string;
4507
+ type: string;
4508
+ }[];
4509
+ internalType: string;
4510
+ name: string;
4511
+ type: string;
4512
+ })[];
4469
4513
  name: string;
4470
4514
  outputs: {
4471
4515
  internalType: string;
@@ -818,13 +818,13 @@ module.exports = {
818
818
  }
819
819
  },
820
820
  "MorphoBlueView": {
821
- "abi": [{ "inputs": [], "name": "MORPHO_BLUE_ADDRESS", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }], "name": "getMarketId", "outputs": [{ "internalType": "Id", "name": "id", "type": "bytes32" }], "stateMutability": "pure", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }], "name": "getMarketInfo", "outputs": [{ "components": [{ "internalType": "Id", "name": "id", "type": "bytes32" }, { "internalType": "uint128", "name": "totalSupplyAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalSupplyShares", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowShares", "type": "uint128" }, { "internalType": "uint256", "name": "lastUpdate", "type": "uint256" }, { "internalType": "uint256", "name": "fee", "type": "uint256" }, { "internalType": "uint256", "name": "borrowRate", "type": "uint256" }, { "internalType": "uint256", "name": "oracle", "type": "uint256" }], "internalType": "struct MorphoBlueView.MarketInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "name": "getMarketInfoNotTuple", "outputs": [{ "components": [{ "internalType": "Id", "name": "id", "type": "bytes32" }, { "internalType": "uint128", "name": "totalSupplyAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalSupplyShares", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowShares", "type": "uint128" }, { "internalType": "uint256", "name": "lastUpdate", "type": "uint256" }, { "internalType": "uint256", "name": "fee", "type": "uint256" }, { "internalType": "uint256", "name": "borrowRate", "type": "uint256" }, { "internalType": "uint256", "name": "oracle", "type": "uint256" }], "internalType": "struct MorphoBlueView.MarketInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }, { "internalType": "address", "name": "owner", "type": "address" }], "name": "getUserInfo", "outputs": [{ "components": [{ "internalType": "uint256", "name": "supplyShares", "type": "uint256" }, { "internalType": "uint256", "name": "suppliedInAssets", "type": "uint256" }, { "internalType": "uint256", "name": "borrowShares", "type": "uint256" }, { "internalType": "uint256", "name": "borrowedInAssets", "type": "uint256" }, { "internalType": "uint256", "name": "collateral", "type": "uint256" }], "internalType": "struct MorphoBlueView.PositionInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "morphoBlue", "outputs": [{ "internalType": "contract IMorphoBlue", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }],
821
+ "abi": [{ "inputs": [], "name": "MORPHO_BLUE_ADDRESS", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "components": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }, { "internalType": "bool", "name": "isBorrowOperation", "type": "bool" }, { "internalType": "uint256", "name": "liquidityAdded", "type": "uint256" }, { "internalType": "uint256", "name": "liquidityRemoved", "type": "uint256" }], "internalType": "struct MorphoBlueView.LiquidityChangeParams", "name": "params", "type": "tuple" }], "name": "getApyAfterValuesEstimation", "outputs": [{ "internalType": "uint256", "name": "borrowRate", "type": "uint256" }, { "components": [{ "internalType": "uint128", "name": "totalSupplyAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalSupplyShares", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowShares", "type": "uint128" }, { "internalType": "uint128", "name": "lastUpdate", "type": "uint128" }, { "internalType": "uint128", "name": "fee", "type": "uint128" }], "internalType": "struct Market", "name": "market", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }], "name": "getMarketId", "outputs": [{ "internalType": "Id", "name": "id", "type": "bytes32" }], "stateMutability": "pure", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }], "name": "getMarketInfo", "outputs": [{ "components": [{ "internalType": "Id", "name": "id", "type": "bytes32" }, { "internalType": "uint128", "name": "totalSupplyAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalSupplyShares", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowShares", "type": "uint128" }, { "internalType": "uint256", "name": "lastUpdate", "type": "uint256" }, { "internalType": "uint256", "name": "fee", "type": "uint256" }, { "internalType": "uint256", "name": "borrowRate", "type": "uint256" }, { "internalType": "uint256", "name": "oracle", "type": "uint256" }], "internalType": "struct MorphoBlueView.MarketInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "name": "getMarketInfoNotTuple", "outputs": [{ "components": [{ "internalType": "Id", "name": "id", "type": "bytes32" }, { "internalType": "uint128", "name": "totalSupplyAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalSupplyShares", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowAssets", "type": "uint128" }, { "internalType": "uint128", "name": "totalBorrowShares", "type": "uint128" }, { "internalType": "uint256", "name": "lastUpdate", "type": "uint256" }, { "internalType": "uint256", "name": "fee", "type": "uint256" }, { "internalType": "uint256", "name": "borrowRate", "type": "uint256" }, { "internalType": "uint256", "name": "oracle", "type": "uint256" }], "internalType": "struct MorphoBlueView.MarketInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "Id", "name": "marketId", "type": "bytes32" }, { "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }, { "internalType": "address", "name": "owner", "type": "address" }], "name": "getRatio", "outputs": [{ "internalType": "uint256", "name": "ratio", "type": "uint256" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "Id", "name": "marketId", "type": "bytes32" }, { "internalType": "address", "name": "owner", "type": "address" }], "name": "getRatioUsingId", "outputs": [{ "internalType": "uint256", "name": "ratio", "type": "uint256" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }, { "internalType": "address", "name": "owner", "type": "address" }], "name": "getRatioUsingParams", "outputs": [{ "internalType": "uint256", "name": "ratio", "type": "uint256" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "components": [{ "internalType": "address", "name": "loanToken", "type": "address" }, { "internalType": "address", "name": "collateralToken", "type": "address" }, { "internalType": "address", "name": "oracle", "type": "address" }, { "internalType": "address", "name": "irm", "type": "address" }, { "internalType": "uint256", "name": "lltv", "type": "uint256" }], "internalType": "struct MarketParams", "name": "marketParams", "type": "tuple" }, { "internalType": "address", "name": "owner", "type": "address" }], "name": "getUserInfo", "outputs": [{ "components": [{ "internalType": "uint256", "name": "supplyShares", "type": "uint256" }, { "internalType": "uint256", "name": "suppliedInAssets", "type": "uint256" }, { "internalType": "uint256", "name": "borrowShares", "type": "uint256" }, { "internalType": "uint256", "name": "borrowedInAssets", "type": "uint256" }, { "internalType": "uint256", "name": "collateral", "type": "uint256" }], "internalType": "struct MorphoBlueView.PositionInfo", "name": "", "type": "tuple" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "morphoBlue", "outputs": [{ "internalType": "contract IMorphoBlue", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }],
822
822
  "networks": {
823
823
  "1": {
824
- "address": "0xE29175a86B60138403a9534A391acaDb19f1E9a6",
824
+ "address": "0x10B621823D4f3E85fBDF759e252598e4e097C1fd",
825
825
  },
826
826
  "8453": {
827
- "address": "0xDBCACF59C5063da8B15481F88E7D70E13c92f2a1",
827
+ "address": "0x53c0E962bd0AC53928ca04703238b2ec2894195B",
828
828
  }
829
829
  }
830
830
  },
@@ -1,7 +1,14 @@
1
- import { MMUsedAssets } from '../../types/common';
2
- import { MorphoBlueAggregatedPositionData, MorphoBlueAssetsData, MorphoBlueMarketInfo } from '../../types';
1
+ import Web3 from 'web3';
2
+ import { MMUsedAssets, NetworkNumber } from '../../types/common';
3
+ import { MorphoBlueAggregatedPositionData, MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo } from '../../types';
3
4
  export declare const getMorphoBlueAggregatedPositionData: ({ usedAssets, assetsData, marketInfo }: {
4
5
  usedAssets: MMUsedAssets;
5
6
  assetsData: MorphoBlueAssetsData;
6
7
  marketInfo: MorphoBlueMarketInfo;
7
8
  }) => MorphoBlueAggregatedPositionData;
9
+ export declare const getSupplyRate: (totalSupplyAssets: string, totalBorrowAssets: string, borrowRate: string, fee: string) => string;
10
+ export declare const getBorrowRate: (borrowRate: string, totalBorrowShares: string) => string;
11
+ export declare const getApyAfterValuesEstimation: (selectedMarket: MorphoBlueMarketData, action: string, amount: string, asset: string, web3: Web3, network: NetworkNumber) => Promise<{
12
+ borrowRate: string;
13
+ supplyRate: string;
14
+ }>;
@@ -1,6 +1,18 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import Dec from 'decimal.js';
11
+ import { assetAmountInWei } from '@defisaver/tokens';
2
12
  import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
3
13
  import { calculateNetApy } from '../../staking';
14
+ import { SECONDS_PER_YEAR, WAD } from '../../constants';
15
+ import { MorphoBlueViewContract } from '../../contracts';
4
16
  export const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, marketInfo }) => {
5
17
  var _a, _b, _c, _d, _e, _f;
6
18
  const payload = {};
@@ -39,3 +51,53 @@ export const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, ma
39
51
  }
40
52
  return payload;
41
53
  };
54
+ const compound = (ratePerSeconds) => {
55
+ const compounding = new Dec(ratePerSeconds).mul(SECONDS_PER_YEAR).toString();
56
+ const apyNumber = Math.expm1(new Dec(compounding).div(WAD).toNumber());
57
+ return new Dec(apyNumber).mul(WAD).floor().toString();
58
+ };
59
+ export const getSupplyRate = (totalSupplyAssets, totalBorrowAssets, borrowRate, fee) => {
60
+ if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
61
+ return '0';
62
+ }
63
+ const utillization = new Dec(totalBorrowAssets).mul(WAD).div(totalSupplyAssets).ceil()
64
+ .toString();
65
+ const supplyRate = new Dec(utillization).mul(borrowRate).div(WAD).ceil()
66
+ .toString();
67
+ const ratePerSecond = new Dec(supplyRate).mul(new Dec(WAD).minus(fee)).div(WAD).ceil()
68
+ .toString();
69
+ return new Dec(compound(ratePerSecond)).div(1e18).mul(100).toString();
70
+ };
71
+ export const getBorrowRate = (borrowRate, totalBorrowShares) => {
72
+ if (totalBorrowShares === '0') {
73
+ return '0';
74
+ }
75
+ return new Dec(compound(borrowRate)).div(1e18).mul(100).toString();
76
+ };
77
+ const borrowOperations = ['borrow', 'payback'];
78
+ export const getApyAfterValuesEstimation = (selectedMarket, action, amount, asset, web3, network) => __awaiter(void 0, void 0, void 0, function* () {
79
+ const morphoBlueViewContract = MorphoBlueViewContract(web3, network);
80
+ const lltvInWei = assetAmountInWei(selectedMarket.lltv, 'ETH');
81
+ const marketData = [selectedMarket.loanToken, selectedMarket.collateralToken, selectedMarket.oracle, selectedMarket.irm, lltvInWei];
82
+ const isBorrowOperation = borrowOperations.includes(action);
83
+ const amountInWei = assetAmountInWei(amount, asset);
84
+ let liquidityAdded;
85
+ let liquidityRemoved;
86
+ if (isBorrowOperation) {
87
+ liquidityAdded = action === 'payback' ? amountInWei : '0';
88
+ liquidityRemoved = action === 'borrow' ? amountInWei : '0';
89
+ }
90
+ else {
91
+ liquidityAdded = action === 'collateral' ? amountInWei : '0';
92
+ liquidityRemoved = action === 'withdraw' ? amountInWei : '0';
93
+ }
94
+ const data = yield morphoBlueViewContract.methods.getApyAfterValuesEstimation([
95
+ marketData,
96
+ isBorrowOperation,
97
+ liquidityAdded,
98
+ liquidityRemoved,
99
+ ]).call();
100
+ const borrowRate = getBorrowRate(data.borrowRate, data.market.totalBorrowShares);
101
+ const supplyRate = getSupplyRate(data.market.totalSupplyAssets, data.market.totalBorrowAssets, data.borrowRate, data.market.fee);
102
+ return { borrowRate, supplyRate };
103
+ });
@@ -11,33 +11,10 @@ import Dec from 'decimal.js';
11
11
  import { assetAmountInEth, getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
12
12
  import { NetworkNumber, } from '../types/common';
13
13
  import { FeedRegistryContract, MorphoBlueViewContract, } from '../contracts';
14
- import { WAD, SECONDS_PER_YEAR, USD_QUOTE } from '../constants';
14
+ import { WAD, USD_QUOTE } from '../constants';
15
15
  import { getStakingApy, STAKING_ASSETS } from '../staking';
16
16
  import { wethToEth } from '../services/utils';
17
- import { getMorphoBlueAggregatedPositionData } from '../helpers/morphoBlueHelpers';
18
- const compound = (ratePerSeconds) => {
19
- const compounding = new Dec(ratePerSeconds).mul(SECONDS_PER_YEAR).toString();
20
- const apyNumber = Math.expm1(new Dec(compounding).div(WAD).toNumber());
21
- return new Dec(apyNumber).mul(WAD).floor().toString();
22
- };
23
- const getSupplyRate = (totalSupplyAssets, totalBorrowAssets, borrowRate, fee) => {
24
- if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
25
- return 0;
26
- }
27
- const utillization = new Dec(totalBorrowAssets).mul(WAD).div(totalSupplyAssets).ceil()
28
- .toString();
29
- const supplyRate = new Dec(utillization).mul(borrowRate).div(WAD).ceil()
30
- .toString();
31
- const ratePerSecond = new Dec(supplyRate).mul(new Dec(WAD).minus(fee)).div(WAD).ceil()
32
- .toString();
33
- return compound(ratePerSecond);
34
- };
35
- const getBorrowRate = (borrowRate, totalBorrowShares) => {
36
- if (totalBorrowShares === '0') {
37
- return 0;
38
- }
39
- return compound(borrowRate);
40
- };
17
+ import { getBorrowRate, getMorphoBlueAggregatedPositionData, getSupplyRate } from '../helpers/morphoBlueHelpers';
41
18
  export function getMorphoBlueMarketData(web3, network, selectedMarket, mainnetWeb3) {
42
19
  return __awaiter(this, void 0, void 0, function* () {
43
20
  const { loanToken, collateralToken, oracle, irm, lltv, oracleType, } = selectedMarket;
@@ -67,8 +44,8 @@ export function getMorphoBlueMarketData(web3, network, selectedMarket, mainnetWe
67
44
  symbol: wethToEth(loanTokenInfo.symbol),
68
45
  address: loanToken,
69
46
  price: new Dec(loanTokenPrice).div(1e8).toString(),
70
- supplyRate: new Dec(supplyRate).div(WAD).mul(100).toString(),
71
- borrowRate: new Dec(compoundedBorrowRate).div(WAD).mul(100).toString(),
47
+ supplyRate,
48
+ borrowRate: compoundedBorrowRate,
72
49
  totalSupply: new Dec(marketInfo.totalSupplyAssets).div(scale).toString(),
73
50
  totalBorrow: new Dec(marketInfo.totalBorrowAssets).div(scale).toString(),
74
51
  canBeSupplied: true,
@@ -31,7 +31,58 @@ export type MarketParamsStructOutputStruct = {
31
31
  lltv: string;
32
32
  };
33
33
  export type MarketParamsStructOutput = MarketParamsStructOutputArray & MarketParamsStructOutputStruct;
34
+ export type MarketStruct = [
35
+ number | string | BN,
36
+ number | string | BN,
37
+ number | string | BN,
38
+ number | string | BN,
39
+ number | string | BN,
40
+ number | string | BN
41
+ ] | {
42
+ totalSupplyAssets: number | string | BN;
43
+ totalSupplyShares: number | string | BN;
44
+ totalBorrowAssets: number | string | BN;
45
+ totalBorrowShares: number | string | BN;
46
+ lastUpdate: number | string | BN;
47
+ fee: number | string | BN;
48
+ };
49
+ export type MarketStructOutputArray = [
50
+ string,
51
+ string,
52
+ string,
53
+ string,
54
+ string,
55
+ string
56
+ ];
57
+ export type MarketStructOutputStruct = {
58
+ totalSupplyAssets: string;
59
+ totalSupplyShares: string;
60
+ totalBorrowAssets: string;
61
+ totalBorrowShares: string;
62
+ lastUpdate: string;
63
+ fee: string;
64
+ };
65
+ export type MarketStructOutput = MarketStructOutputArray & MarketStructOutputStruct;
34
66
  export declare namespace MorphoBlueView {
67
+ type LiquidityChangeParamsStruct = [MarketParamsStruct, boolean, number | string | BN, number | string | BN] | {
68
+ marketParams: MarketParamsStruct;
69
+ isBorrowOperation: boolean;
70
+ liquidityAdded: number | string | BN;
71
+ liquidityRemoved: number | string | BN;
72
+ };
73
+ type LiquidityChangeParamsStructOutputArray = [
74
+ MarketParamsStructOutput,
75
+ boolean,
76
+ string,
77
+ string
78
+ ];
79
+ type LiquidityChangeParamsStructOutputStruct = {
80
+ marketParams: MarketParamsStructOutput;
81
+ isBorrowOperation: boolean;
82
+ liquidityAdded: string;
83
+ liquidityRemoved: string;
84
+ };
85
+ type LiquidityChangeParamsStructOutput = LiquidityChangeParamsStructOutputArray & LiquidityChangeParamsStructOutputStruct;
35
86
  type MarketInfoStruct = [
36
87
  string | number[],
37
88
  number | string | BN,
@@ -110,9 +161,19 @@ export interface MorphoBlueView extends BaseContract {
110
161
  clone(): MorphoBlueView;
111
162
  methods: {
112
163
  MORPHO_BLUE_ADDRESS(): NonPayableTransactionObject<string>;
164
+ getApyAfterValuesEstimation(params: MorphoBlueView.LiquidityChangeParamsStruct): NonPayableTransactionObject<[
165
+ string,
166
+ MarketStructOutput
167
+ ] & {
168
+ borrowRate: string;
169
+ market: MarketStructOutput;
170
+ }>;
113
171
  getMarketId(marketParams: MarketParamsStruct): NonPayableTransactionObject<string>;
114
172
  getMarketInfo(marketParams: MarketParamsStruct): NonPayableTransactionObject<MorphoBlueView.MarketInfoStructOutput>;
115
173
  getMarketInfoNotTuple(loanToken: string, collToken: string, oracle: string, irm: string, lltv: number | string | BN): NonPayableTransactionObject<MorphoBlueView.MarketInfoStructOutput>;
174
+ getRatio(marketId: string | number[], marketParams: MarketParamsStruct, owner: string): NonPayableTransactionObject<string>;
175
+ getRatioUsingId(marketId: string | number[], owner: string): NonPayableTransactionObject<string>;
176
+ getRatioUsingParams(marketParams: MarketParamsStruct, owner: string): NonPayableTransactionObject<string>;
116
177
  getUserInfo(marketParams: MarketParamsStruct, owner: string): NonPayableTransactionObject<MorphoBlueView.PositionInfoStructOutput>;
117
178
  morphoBlue(): NonPayableTransactionObject<string>;
118
179
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/positions-sdk",
3
- "version": "0.0.98",
3
+ "version": "0.0.99",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -818,13 +818,13 @@ module.exports = {
818
818
  }
819
819
  },
820
820
  "MorphoBlueView": {
821
- "abi": [{"inputs":[],"name":"MORPHO_BLUE_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collateralToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"internalType":"struct MarketParams","name":"marketParams","type":"tuple"}],"name":"getMarketId","outputs":[{"internalType":"Id","name":"id","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collateralToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"internalType":"struct MarketParams","name":"marketParams","type":"tuple"}],"name":"getMarketInfo","outputs":[{"components":[{"internalType":"Id","name":"id","type":"bytes32"},{"internalType":"uint128","name":"totalSupplyAssets","type":"uint128"},{"internalType":"uint128","name":"totalSupplyShares","type":"uint128"},{"internalType":"uint128","name":"totalBorrowAssets","type":"uint128"},{"internalType":"uint128","name":"totalBorrowShares","type":"uint128"},{"internalType":"uint256","name":"lastUpdate","type":"uint256"},{"internalType":"uint256","name":"fee","type":"uint256"},{"internalType":"uint256","name":"borrowRate","type":"uint256"},{"internalType":"uint256","name":"oracle","type":"uint256"}],"internalType":"struct MorphoBlueView.MarketInfo","name":"","type":"tuple"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"name":"getMarketInfoNotTuple","outputs":[{"components":[{"internalType":"Id","name":"id","type":"bytes32"},{"internalType":"uint128","name":"totalSupplyAssets","type":"uint128"},{"internalType":"uint128","name":"totalSupplyShares","type":"uint128"},{"internalType":"uint128","name":"totalBorrowAssets","type":"uint128"},{"internalType":"uint128","name":"totalBorrowShares","type":"uint128"},{"internalType":"uint256","name":"lastUpdate","type":"uint256"},{"internalType":"uint256","name":"fee","type":"uint256"},{"internalType":"uint256","name":"borrowRate","type":"uint256"},{"internalType":"uint256","name":"oracle","type":"uint256"}],"internalType":"struct MorphoBlueView.MarketInfo","name":"","type":"tuple"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collateralToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"internalType":"struct MarketParams","name":"marketParams","type":"tuple"},{"internalType":"address","name":"owner","type":"address"}],"name":"getUserInfo","outputs":[{"components":[{"internalType":"uint256","name":"supplyShares","type":"uint256"},{"internalType":"uint256","name":"suppliedInAssets","type":"uint256"},{"internalType":"uint256","name":"borrowShares","type":"uint256"},{"internalType":"uint256","name":"borrowedInAssets","type":"uint256"},{"internalType":"uint256","name":"collateral","type":"uint256"}],"internalType":"struct MorphoBlueView.PositionInfo","name":"","type":"tuple"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"morphoBlue","outputs":[{"internalType":"contract IMorphoBlue","name":"","type":"address"}],"stateMutability":"view","type":"function"}],
821
+ "abi": [{"inputs":[],"name":"MORPHO_BLUE_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"components":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collateralToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"internalType":"struct MarketParams","name":"marketParams","type":"tuple"},{"internalType":"bool","name":"isBorrowOperation","type":"bool"},{"internalType":"uint256","name":"liquidityAdded","type":"uint256"},{"internalType":"uint256","name":"liquidityRemoved","type":"uint256"}],"internalType":"struct MorphoBlueView.LiquidityChangeParams","name":"params","type":"tuple"}],"name":"getApyAfterValuesEstimation","outputs":[{"internalType":"uint256","name":"borrowRate","type":"uint256"},{"components":[{"internalType":"uint128","name":"totalSupplyAssets","type":"uint128"},{"internalType":"uint128","name":"totalSupplyShares","type":"uint128"},{"internalType":"uint128","name":"totalBorrowAssets","type":"uint128"},{"internalType":"uint128","name":"totalBorrowShares","type":"uint128"},{"internalType":"uint128","name":"lastUpdate","type":"uint128"},{"internalType":"uint128","name":"fee","type":"uint128"}],"internalType":"struct Market","name":"market","type":"tuple"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collateralToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"internalType":"struct MarketParams","name":"marketParams","type":"tuple"}],"name":"getMarketId","outputs":[{"internalType":"Id","name":"id","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collateralToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"internalType":"struct MarketParams","name":"marketParams","type":"tuple"}],"name":"getMarketInfo","outputs":[{"components":[{"internalType":"Id","name":"id","type":"bytes32"},{"internalType":"uint128","name":"totalSupplyAssets","type":"uint128"},{"internalType":"uint128","name":"totalSupplyShares","type":"uint128"},{"internalType":"uint128","name":"totalBorrowAssets","type":"uint128"},{"internalType":"uint128","name":"totalBorrowShares","type":"uint128"},{"internalType":"uint256","name":"lastUpdate","type":"uint256"},{"internalType":"uint256","name":"fee","type":"uint256"},{"internalType":"uint256","name":"borrowRate","type":"uint256"},{"internalType":"uint256","name":"oracle","type":"uint256"}],"internalType":"struct MorphoBlueView.MarketInfo","name":"","type":"tuple"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"name":"getMarketInfoNotTuple","outputs":[{"components":[{"internalType":"Id","name":"id","type":"bytes32"},{"internalType":"uint128","name":"totalSupplyAssets","type":"uint128"},{"internalType":"uint128","name":"totalSupplyShares","type":"uint128"},{"internalType":"uint128","name":"totalBorrowAssets","type":"uint128"},{"internalType":"uint128","name":"totalBorrowShares","type":"uint128"},{"internalType":"uint256","name":"lastUpdate","type":"uint256"},{"internalType":"uint256","name":"fee","type":"uint256"},{"internalType":"uint256","name":"borrowRate","type":"uint256"},{"internalType":"uint256","name":"oracle","type":"uint256"}],"internalType":"struct MorphoBlueView.MarketInfo","name":"","type":"tuple"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"Id","name":"marketId","type":"bytes32"},{"components":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collateralToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"internalType":"struct MarketParams","name":"marketParams","type":"tuple"},{"internalType":"address","name":"owner","type":"address"}],"name":"getRatio","outputs":[{"internalType":"uint256","name":"ratio","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"Id","name":"marketId","type":"bytes32"},{"internalType":"address","name":"owner","type":"address"}],"name":"getRatioUsingId","outputs":[{"internalType":"uint256","name":"ratio","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collateralToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"internalType":"struct MarketParams","name":"marketParams","type":"tuple"},{"internalType":"address","name":"owner","type":"address"}],"name":"getRatioUsingParams","outputs":[{"internalType":"uint256","name":"ratio","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"loanToken","type":"address"},{"internalType":"address","name":"collateralToken","type":"address"},{"internalType":"address","name":"oracle","type":"address"},{"internalType":"address","name":"irm","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"}],"internalType":"struct MarketParams","name":"marketParams","type":"tuple"},{"internalType":"address","name":"owner","type":"address"}],"name":"getUserInfo","outputs":[{"components":[{"internalType":"uint256","name":"supplyShares","type":"uint256"},{"internalType":"uint256","name":"suppliedInAssets","type":"uint256"},{"internalType":"uint256","name":"borrowShares","type":"uint256"},{"internalType":"uint256","name":"borrowedInAssets","type":"uint256"},{"internalType":"uint256","name":"collateral","type":"uint256"}],"internalType":"struct MorphoBlueView.PositionInfo","name":"","type":"tuple"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"morphoBlue","outputs":[{"internalType":"contract IMorphoBlue","name":"","type":"address"}],"stateMutability":"view","type":"function"}],
822
822
  "networks": {
823
823
  "1": {
824
- "address": "0xE29175a86B60138403a9534A391acaDb19f1E9a6",
824
+ "address": "0x10B621823D4f3E85fBDF759e252598e4e097C1fd",
825
825
  },
826
826
  "8453": {
827
- "address": "0xDBCACF59C5063da8B15481F88E7D70E13c92f2a1",
827
+ "address": "0x53c0E962bd0AC53928ca04703238b2ec2894195B",
828
828
  }
829
829
  }
830
830
  },
@@ -1,8 +1,15 @@
1
1
  import Dec from 'decimal.js';
2
+ import { assetAmountInWei } from '@defisaver/tokens';
3
+ import Web3 from 'web3';
2
4
  import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
3
5
  import { calculateNetApy } from '../../staking';
4
- import { MMUsedAssets } from '../../types/common';
5
- import { MorphoBlueAggregatedPositionData, MorphoBlueAssetsData, MorphoBlueMarketInfo } from '../../types';
6
+ import { MMUsedAssets, NetworkNumber } from '../../types/common';
7
+ import {
8
+ MorphoBlueAggregatedPositionData, MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo,
9
+ } from '../../types';
10
+ import { SECONDS_PER_YEAR, WAD } from '../../constants';
11
+ import { MorphoBlueViewContract } from '../../contracts';
12
+ import { MarketParamsStruct } from '../../types/contracts/generated/MorphoBlueView';
6
13
 
7
14
  export const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, marketInfo }: { usedAssets: MMUsedAssets, assetsData: MorphoBlueAssetsData, marketInfo: MorphoBlueMarketInfo }): MorphoBlueAggregatedPositionData => {
8
15
  const payload = {} as MorphoBlueAggregatedPositionData;
@@ -54,4 +61,58 @@ export const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, ma
54
61
  }
55
62
 
56
63
  return payload;
64
+ };
65
+
66
+ const compound = (ratePerSeconds: string) => {
67
+ const compounding = new Dec(ratePerSeconds).mul(SECONDS_PER_YEAR).toString();
68
+ const apyNumber = Math.expm1(new Dec(compounding).div(WAD).toNumber());
69
+ return new Dec(apyNumber).mul(WAD).floor().toString();
70
+ };
71
+
72
+ export const getSupplyRate = (totalSupplyAssets: string, totalBorrowAssets: string, borrowRate: string, fee: string) => {
73
+ if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
74
+ return '0';
75
+ }
76
+ const utillization = new Dec(totalBorrowAssets).mul(WAD).div(totalSupplyAssets).ceil()
77
+ .toString();
78
+ const supplyRate = new Dec(utillization).mul(borrowRate).div(WAD).ceil()
79
+ .toString();
80
+ const ratePerSecond = new Dec(supplyRate).mul(new Dec(WAD).minus(fee)).div(WAD).ceil()
81
+ .toString();
82
+ return new Dec(compound(ratePerSecond)).div(1e18).mul(100).toString();
83
+ };
84
+
85
+ export const getBorrowRate = (borrowRate: string, totalBorrowShares: string) => {
86
+ if (totalBorrowShares === '0') {
87
+ return '0';
88
+ }
89
+ return new Dec(compound(borrowRate)).div(1e18).mul(100).toString();
90
+ };
91
+
92
+ const borrowOperations = ['borrow', 'payback'];
93
+
94
+ export const getApyAfterValuesEstimation = async (selectedMarket: MorphoBlueMarketData, action: string, amount: string, asset: string, web3: Web3, network: NetworkNumber) => {
95
+ const morphoBlueViewContract = MorphoBlueViewContract(web3, network);
96
+ const lltvInWei = assetAmountInWei(selectedMarket.lltv, 'ETH');
97
+ const marketData: MarketParamsStruct = [selectedMarket.loanToken, selectedMarket.collateralToken, selectedMarket.oracle, selectedMarket.irm, lltvInWei];
98
+ const isBorrowOperation = borrowOperations.includes(action);
99
+ const amountInWei = assetAmountInWei(amount, asset);
100
+ let liquidityAdded;
101
+ let liquidityRemoved;
102
+ if (isBorrowOperation) {
103
+ liquidityAdded = action === 'payback' ? amountInWei : '0';
104
+ liquidityRemoved = action === 'borrow' ? amountInWei : '0';
105
+ } else {
106
+ liquidityAdded = action === 'collateral' ? amountInWei : '0';
107
+ liquidityRemoved = action === 'withdraw' ? amountInWei : '0';
108
+ }
109
+ const data = await morphoBlueViewContract.methods.getApyAfterValuesEstimation([
110
+ marketData,
111
+ isBorrowOperation,
112
+ liquidityAdded,
113
+ liquidityRemoved,
114
+ ]).call();
115
+ const borrowRate = getBorrowRate(data.borrowRate, data.market.totalBorrowShares);
116
+ const supplyRate = getSupplyRate(data.market.totalSupplyAssets, data.market.totalBorrowAssets, data.borrowRate, data.market.fee);
117
+ return { borrowRate, supplyRate };
57
118
  };
@@ -11,38 +11,10 @@ import {
11
11
  import {
12
12
  MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo, MorphoBluePositionData,
13
13
  } from '../types';
14
- import { WAD, SECONDS_PER_YEAR, USD_QUOTE } from '../constants';
14
+ import { WAD, USD_QUOTE } from '../constants';
15
15
  import { getStakingApy, STAKING_ASSETS } from '../staking';
16
16
  import { wethToEth } from '../services/utils';
17
- import { getMorphoBlueAggregatedPositionData } from '../helpers/morphoBlueHelpers';
18
-
19
-
20
- const compound = (ratePerSeconds: string) => {
21
- const compounding = new Dec(ratePerSeconds).mul(SECONDS_PER_YEAR).toString();
22
- const apyNumber = Math.expm1(new Dec(compounding).div(WAD).toNumber());
23
- return new Dec(apyNumber).mul(WAD).floor().toString();
24
- };
25
-
26
- const getSupplyRate = (totalSupplyAssets: string, totalBorrowAssets: string, borrowRate: string, fee: string) => {
27
- if (totalBorrowAssets === '0' || totalSupplyAssets === '0') {
28
- return 0;
29
- }
30
- const utillization = new Dec(totalBorrowAssets).mul(WAD).div(totalSupplyAssets).ceil()
31
- .toString();
32
- const supplyRate = new Dec(utillization).mul(borrowRate).div(WAD).ceil()
33
- .toString();
34
- const ratePerSecond = new Dec(supplyRate).mul(new Dec(WAD).minus(fee)).div(WAD).ceil()
35
- .toString();
36
- return compound(ratePerSecond);
37
- };
38
-
39
- const getBorrowRate = (borrowRate: string, totalBorrowShares: string) => {
40
- if (totalBorrowShares === '0') {
41
- return 0;
42
- }
43
- return compound(borrowRate);
44
- };
45
-
17
+ import { getBorrowRate, getMorphoBlueAggregatedPositionData, getSupplyRate } from '../helpers/morphoBlueHelpers';
46
18
 
47
19
  export async function getMorphoBlueMarketData(web3: Web3, network: NetworkNumber, selectedMarket: MorphoBlueMarketData, mainnetWeb3: Web3): Promise<MorphoBlueMarketInfo> {
48
20
  const {
@@ -80,8 +52,8 @@ export async function getMorphoBlueMarketData(web3: Web3, network: NetworkNumber
80
52
  symbol: wethToEth(loanTokenInfo.symbol),
81
53
  address: loanToken,
82
54
  price: new Dec(loanTokenPrice).div(1e8).toString(),
83
- supplyRate: new Dec(supplyRate).div(WAD).mul(100).toString(),
84
- borrowRate: new Dec(compoundedBorrowRate).div(WAD).mul(100).toString(),
55
+ supplyRate,
56
+ borrowRate: compoundedBorrowRate,
85
57
  totalSupply: new Dec(marketInfo.totalSupplyAssets).div(scale).toString(),
86
58
  totalBorrow: new Dec(marketInfo.totalBorrowAssets).div(scale).toString(),
87
59
  canBeSupplied: true,
@@ -81,7 +81,7 @@ const getApyFromDfsApi = async (asset: string) => {
81
81
  const res = await fetch(`https://app.defisaver.com/api/staking/apy?asset=${asset}`);
82
82
  const data = await res.json();
83
83
  return data.apy;
84
- }
84
+ };
85
85
 
86
86
  export const STAKING_ASSETS = ['cbETH', 'wstETH', 'cbETH', 'rETH', 'sDAI', 'weETH', 'sUSDe', 'osETH', 'ezETH', 'ETHx'];
87
87
 
@@ -93,7 +93,7 @@ export const getStakingApy = (asset: string, web3: Web3, blockNumber: 'latest' |
93
93
  if (asset === 'sDAI') return getDsrApy(web3);
94
94
  if (asset === 'sUSDe') return getApyFromDfsApi('sUSDe');
95
95
  if (asset === 'weETH') return getApyFromDfsApi('weETH');
96
- if (asset === 'ezETH') return getApyFromDfsApi('ezETH')
96
+ if (asset === 'ezETH') return getApyFromDfsApi('ezETH');
97
97
  if (asset === 'osETH') return getApyFromDfsApi('osETH');
98
98
  if (asset === 'ETHx') return getApyFromDfsApi('ETHx');
99
99
  } catch (e) {
@@ -48,7 +48,69 @@ export type MarketParamsStructOutputStruct = {
48
48
  export type MarketParamsStructOutput = MarketParamsStructOutputArray &
49
49
  MarketParamsStructOutputStruct;
50
50
 
51
+ export type MarketStruct =
52
+ | [
53
+ number | string | BN,
54
+ number | string | BN,
55
+ number | string | BN,
56
+ number | string | BN,
57
+ number | string | BN,
58
+ number | string | BN
59
+ ]
60
+ | {
61
+ totalSupplyAssets: number | string | BN;
62
+ totalSupplyShares: number | string | BN;
63
+ totalBorrowAssets: number | string | BN;
64
+ totalBorrowShares: number | string | BN;
65
+ lastUpdate: number | string | BN;
66
+ fee: number | string | BN;
67
+ };
68
+
69
+ export type MarketStructOutputArray = [
70
+ string,
71
+ string,
72
+ string,
73
+ string,
74
+ string,
75
+ string
76
+ ];
77
+ export type MarketStructOutputStruct = {
78
+ totalSupplyAssets: string;
79
+ totalSupplyShares: string;
80
+ totalBorrowAssets: string;
81
+ totalBorrowShares: string;
82
+ lastUpdate: string;
83
+ fee: string;
84
+ };
85
+ export type MarketStructOutput = MarketStructOutputArray &
86
+ MarketStructOutputStruct;
87
+
51
88
  export declare namespace MorphoBlueView {
89
+ export type LiquidityChangeParamsStruct =
90
+ | [MarketParamsStruct, boolean, number | string | BN, number | string | BN]
91
+ | {
92
+ marketParams: MarketParamsStruct;
93
+ isBorrowOperation: boolean;
94
+ liquidityAdded: number | string | BN;
95
+ liquidityRemoved: number | string | BN;
96
+ };
97
+
98
+ export type LiquidityChangeParamsStructOutputArray = [
99
+ MarketParamsStructOutput,
100
+ boolean,
101
+ string,
102
+ string
103
+ ];
104
+ export type LiquidityChangeParamsStructOutputStruct = {
105
+ marketParams: MarketParamsStructOutput;
106
+ isBorrowOperation: boolean;
107
+ liquidityAdded: string;
108
+ liquidityRemoved: string;
109
+ };
110
+ export type LiquidityChangeParamsStructOutput =
111
+ LiquidityChangeParamsStructOutputArray &
112
+ LiquidityChangeParamsStructOutputStruct;
113
+
52
114
  export type MarketInfoStruct =
53
115
  | [
54
116
  string | number[],
@@ -142,6 +204,15 @@ export interface MorphoBlueView extends BaseContract {
142
204
  methods: {
143
205
  MORPHO_BLUE_ADDRESS(): NonPayableTransactionObject<string>;
144
206
 
207
+ getApyAfterValuesEstimation(
208
+ params: MorphoBlueView.LiquidityChangeParamsStruct
209
+ ): NonPayableTransactionObject<
210
+ [string, MarketStructOutput] & {
211
+ borrowRate: string;
212
+ market: MarketStructOutput;
213
+ }
214
+ >;
215
+
145
216
  getMarketId(
146
217
  marketParams: MarketParamsStruct
147
218
  ): NonPayableTransactionObject<string>;
@@ -158,6 +229,22 @@ export interface MorphoBlueView extends BaseContract {
158
229
  lltv: number | string | BN
159
230
  ): NonPayableTransactionObject<MorphoBlueView.MarketInfoStructOutput>;
160
231
 
232
+ getRatio(
233
+ marketId: string | number[],
234
+ marketParams: MarketParamsStruct,
235
+ owner: string
236
+ ): NonPayableTransactionObject<string>;
237
+
238
+ getRatioUsingId(
239
+ marketId: string | number[],
240
+ owner: string
241
+ ): NonPayableTransactionObject<string>;
242
+
243
+ getRatioUsingParams(
244
+ marketParams: MarketParamsStruct,
245
+ owner: string
246
+ ): NonPayableTransactionObject<string>;
247
+
161
248
  getUserInfo(
162
249
  marketParams: MarketParamsStruct,
163
250
  owner: string