@gearbox-protocol/sdk 3.0.0-next.15 → 3.0.0-next.16

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.
@@ -34,6 +34,11 @@ interface CalcQuotaUpdateReturnType {
34
34
  quotaIncrease: Array<Asset>;
35
35
  quotaDecrease: Array<Asset>;
36
36
  }
37
+ export interface CalcQuotaBorrowRateProps {
38
+ quotas: Record<string, Asset>;
39
+ quotaRates: Record<string, Pick<QuotaInfo, "rate">>;
40
+ borrowAmount: bigint;
41
+ }
37
42
  export declare class CreditAccountData {
38
43
  readonly addr: string;
39
44
  readonly borrower: string;
@@ -83,5 +88,6 @@ export declare class CreditAccountData {
83
88
  static hash(creditManager: string, borrower: string): string;
84
89
  static calcHealthFactor({ assets, quotas, liquidationThresholds, underlyingToken, borrowed, prices, }: CalcHealthFactorProps): number;
85
90
  static calcQuotaUpdate({ quotas, initialQuotas, assetsAfterUpdate, allowedToSpend, allowedToObtain, }: CalcQuotaUpdateProps): CalcQuotaUpdateReturnType;
91
+ static calcQuotaBorrowRate({ quotas, quotaRates, borrowAmount, }: CalcQuotaBorrowRateProps): number;
86
92
  }
87
93
  export {};
@@ -249,5 +249,16 @@ class CreditAccountData {
249
249
  }, { desiredQuota: {}, quotaIncrease: [], quotaDecrease: [] });
250
250
  return r;
251
251
  }
252
+ static calcQuotaBorrowRate({ quotas, quotaRates, borrowAmount, }) {
253
+ if (borrowAmount <= 0)
254
+ return 0;
255
+ const totalRateBalance = Object.values(quotas).reduce((acc, { token, balance }) => {
256
+ const { rate = 0 } = quotaRates[token] || {};
257
+ const rateBalance = balance * BigInt(rate);
258
+ return acc + rateBalance;
259
+ }, 0n);
260
+ const quotaBorrowRate = Number(totalRateBalance / borrowAmount);
261
+ return quotaBorrowRate;
262
+ }
252
263
  }
253
264
  exports.CreditAccountData = CreditAccountData;
@@ -710,3 +710,98 @@ describe("CreditAccount calcQuotaUpdate test", () => {
710
710
  });
711
711
  });
712
712
  });
713
+ describe("CreditAccount calcQuotaBorrowRate test", () => {
714
+ it("should calculate quota rate (same amounts, different rates)", () => {
715
+ const result = creditAccount_1.CreditAccountData.calcQuotaBorrowRate({
716
+ quotas: {
717
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.DAI]: {
718
+ token: sdk_gov_1.tokenDataByNetwork.Mainnet.DAI,
719
+ balance: 10n,
720
+ },
721
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.WETH]: {
722
+ token: sdk_gov_1.tokenDataByNetwork.Mainnet.WETH,
723
+ balance: 10n,
724
+ },
725
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.STETH]: {
726
+ token: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
727
+ balance: 10n,
728
+ },
729
+ },
730
+ quotaRates: {
731
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.DAI]: {
732
+ rate: 5,
733
+ },
734
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.WETH]: {
735
+ rate: 10,
736
+ },
737
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.STETH]: {
738
+ rate: 15,
739
+ },
740
+ },
741
+ borrowAmount: 30n,
742
+ });
743
+ (0, chai_1.expect)(result).to.be.eq(10);
744
+ });
745
+ it("should calculate quota rate (same rates, different amounts)", () => {
746
+ const result = creditAccount_1.CreditAccountData.calcQuotaBorrowRate({
747
+ quotas: {
748
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.DAI]: {
749
+ token: sdk_gov_1.tokenDataByNetwork.Mainnet.DAI,
750
+ balance: 5n,
751
+ },
752
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.WETH]: {
753
+ token: sdk_gov_1.tokenDataByNetwork.Mainnet.WETH,
754
+ balance: 10n,
755
+ },
756
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.STETH]: {
757
+ token: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
758
+ balance: 15n,
759
+ },
760
+ },
761
+ quotaRates: {
762
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.DAI]: {
763
+ rate: 10,
764
+ },
765
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.WETH]: {
766
+ rate: 10,
767
+ },
768
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.STETH]: {
769
+ rate: 10,
770
+ },
771
+ },
772
+ borrowAmount: 30n,
773
+ });
774
+ (0, chai_1.expect)(result).to.be.eq(10);
775
+ });
776
+ it("should calculate quota rate (borrow amount)", () => {
777
+ const result = creditAccount_1.CreditAccountData.calcQuotaBorrowRate({
778
+ quotas: {
779
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.DAI]: {
780
+ token: sdk_gov_1.tokenDataByNetwork.Mainnet.DAI,
781
+ balance: 5n,
782
+ },
783
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.WETH]: {
784
+ token: sdk_gov_1.tokenDataByNetwork.Mainnet.WETH,
785
+ balance: 10n,
786
+ },
787
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.STETH]: {
788
+ token: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
789
+ balance: 15n,
790
+ },
791
+ },
792
+ quotaRates: {
793
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.DAI]: {
794
+ rate: 10,
795
+ },
796
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.WETH]: {
797
+ rate: 10,
798
+ },
799
+ [sdk_gov_1.tokenDataByNetwork.Mainnet.STETH]: {
800
+ rate: 10,
801
+ },
802
+ },
803
+ borrowAmount: 60n,
804
+ });
805
+ (0, chai_1.expect)(result).to.be.eq(5);
806
+ });
807
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "3.0.0-next.15",
3
+ "version": "3.0.0-next.16",
4
4
  "description": "Gearbox SDK",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",