@agether/sdk 1.6.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -656,7 +656,6 @@ var init_MorphoClient = __esm({
656
656
  */
657
657
  async repay(usdcAmount, tokenSymbol, marketParams) {
658
658
  const acctAddr = await this.getAccountAddress();
659
- const amount = import_ethers.ethers.parseUnits(usdcAmount, 6);
660
659
  const morphoAddr = this.config.contracts.morphoBlue;
661
660
  const usdcAddr = this.config.contracts.usdc;
662
661
  let params;
@@ -668,14 +667,60 @@ var init_MorphoClient = __esm({
668
667
  const { params: p } = await this._findActiveMarket();
669
668
  params = p;
670
669
  }
670
+ let repayAssets;
671
+ let repayShares;
672
+ let approveAmount;
673
+ if (usdcAmount === "all") {
674
+ const markets = await this.getMarkets();
675
+ const mkt = markets.find(
676
+ (m) => m.collateralAsset?.address.toLowerCase() === params.collateralToken.toLowerCase()
677
+ );
678
+ if (mkt) {
679
+ const pos = await this.morphoBlue.position(mkt.uniqueKey, acctAddr);
680
+ repayShares = BigInt(pos.borrowShares);
681
+ repayAssets = 0n;
682
+ const onChainMkt = await this.morphoBlue.market(mkt.uniqueKey);
683
+ const totalBorrowAssets = BigInt(onChainMkt.totalBorrowAssets);
684
+ const totalBorrowShares = BigInt(onChainMkt.totalBorrowShares);
685
+ const estimated = totalBorrowShares > 0n ? repayShares * totalBorrowAssets / totalBorrowShares + 10n : 0n;
686
+ approveAmount = estimated > 0n ? estimated : import_ethers.ethers.parseUnits("1", 6);
687
+ } else {
688
+ repayAssets = import_ethers.ethers.parseUnits("999999", 6);
689
+ repayShares = 0n;
690
+ approveAmount = repayAssets;
691
+ }
692
+ } else {
693
+ repayAssets = import_ethers.ethers.parseUnits(usdcAmount, 6);
694
+ repayShares = 0n;
695
+ approveAmount = repayAssets;
696
+ try {
697
+ const markets = await this.getMarkets();
698
+ const mkt = markets.find(
699
+ (m) => m.collateralAsset?.address.toLowerCase() === params.collateralToken.toLowerCase()
700
+ );
701
+ if (mkt) {
702
+ const pos = await this.morphoBlue.position(mkt.uniqueKey, acctAddr);
703
+ const onChainMkt = await this.morphoBlue.market(mkt.uniqueKey);
704
+ const totalBorrowAssets = BigInt(onChainMkt.totalBorrowAssets);
705
+ const totalBorrowShares = BigInt(onChainMkt.totalBorrowShares);
706
+ const currentDebt = totalBorrowShares > 0n ? BigInt(pos.borrowShares) * totalBorrowAssets / totalBorrowShares : 0n;
707
+ if (repayAssets >= currentDebt && BigInt(pos.borrowShares) > 0n) {
708
+ repayShares = BigInt(pos.borrowShares);
709
+ repayAssets = 0n;
710
+ approveAmount = currentDebt + 10n;
711
+ }
712
+ }
713
+ } catch {
714
+ }
715
+ }
671
716
  const targets = [usdcAddr, morphoAddr];
672
717
  const values = [0n, 0n];
673
718
  const datas = [
674
- erc20Iface.encodeFunctionData("approve", [morphoAddr, amount]),
719
+ erc20Iface.encodeFunctionData("approve", [morphoAddr, approveAmount]),
675
720
  morphoIface.encodeFunctionData("repay", [
676
721
  this._toTuple(params),
677
- amount,
678
- 0n,
722
+ repayAssets,
723
+ repayShares,
679
724
  acctAddr,
680
725
  "0x"
681
726
  ])
package/dist/index.js CHANGED
@@ -891,7 +891,6 @@ var MorphoClient = class {
891
891
  */
892
892
  async repay(usdcAmount, tokenSymbol, marketParams) {
893
893
  const acctAddr = await this.getAccountAddress();
894
- const amount = import_ethers2.ethers.parseUnits(usdcAmount, 6);
895
894
  const morphoAddr = this.config.contracts.morphoBlue;
896
895
  const usdcAddr = this.config.contracts.usdc;
897
896
  let params;
@@ -903,14 +902,60 @@ var MorphoClient = class {
903
902
  const { params: p } = await this._findActiveMarket();
904
903
  params = p;
905
904
  }
905
+ let repayAssets;
906
+ let repayShares;
907
+ let approveAmount;
908
+ if (usdcAmount === "all") {
909
+ const markets = await this.getMarkets();
910
+ const mkt = markets.find(
911
+ (m) => m.collateralAsset?.address.toLowerCase() === params.collateralToken.toLowerCase()
912
+ );
913
+ if (mkt) {
914
+ const pos = await this.morphoBlue.position(mkt.uniqueKey, acctAddr);
915
+ repayShares = BigInt(pos.borrowShares);
916
+ repayAssets = 0n;
917
+ const onChainMkt = await this.morphoBlue.market(mkt.uniqueKey);
918
+ const totalBorrowAssets = BigInt(onChainMkt.totalBorrowAssets);
919
+ const totalBorrowShares = BigInt(onChainMkt.totalBorrowShares);
920
+ const estimated = totalBorrowShares > 0n ? repayShares * totalBorrowAssets / totalBorrowShares + 10n : 0n;
921
+ approveAmount = estimated > 0n ? estimated : import_ethers2.ethers.parseUnits("1", 6);
922
+ } else {
923
+ repayAssets = import_ethers2.ethers.parseUnits("999999", 6);
924
+ repayShares = 0n;
925
+ approveAmount = repayAssets;
926
+ }
927
+ } else {
928
+ repayAssets = import_ethers2.ethers.parseUnits(usdcAmount, 6);
929
+ repayShares = 0n;
930
+ approveAmount = repayAssets;
931
+ try {
932
+ const markets = await this.getMarkets();
933
+ const mkt = markets.find(
934
+ (m) => m.collateralAsset?.address.toLowerCase() === params.collateralToken.toLowerCase()
935
+ );
936
+ if (mkt) {
937
+ const pos = await this.morphoBlue.position(mkt.uniqueKey, acctAddr);
938
+ const onChainMkt = await this.morphoBlue.market(mkt.uniqueKey);
939
+ const totalBorrowAssets = BigInt(onChainMkt.totalBorrowAssets);
940
+ const totalBorrowShares = BigInt(onChainMkt.totalBorrowShares);
941
+ const currentDebt = totalBorrowShares > 0n ? BigInt(pos.borrowShares) * totalBorrowAssets / totalBorrowShares : 0n;
942
+ if (repayAssets >= currentDebt && BigInt(pos.borrowShares) > 0n) {
943
+ repayShares = BigInt(pos.borrowShares);
944
+ repayAssets = 0n;
945
+ approveAmount = currentDebt + 10n;
946
+ }
947
+ }
948
+ } catch {
949
+ }
950
+ }
906
951
  const targets = [usdcAddr, morphoAddr];
907
952
  const values = [0n, 0n];
908
953
  const datas = [
909
- erc20Iface.encodeFunctionData("approve", [morphoAddr, amount]),
954
+ erc20Iface.encodeFunctionData("approve", [morphoAddr, approveAmount]),
910
955
  morphoIface.encodeFunctionData("repay", [
911
956
  this._toTuple(params),
912
- amount,
913
- 0n,
957
+ repayAssets,
958
+ repayShares,
914
959
  acctAddr,
915
960
  "0x"
916
961
  ])
package/dist/index.mjs CHANGED
@@ -827,7 +827,6 @@ var MorphoClient = class {
827
827
  */
828
828
  async repay(usdcAmount, tokenSymbol, marketParams) {
829
829
  const acctAddr = await this.getAccountAddress();
830
- const amount = ethers2.parseUnits(usdcAmount, 6);
831
830
  const morphoAddr = this.config.contracts.morphoBlue;
832
831
  const usdcAddr = this.config.contracts.usdc;
833
832
  let params;
@@ -839,14 +838,60 @@ var MorphoClient = class {
839
838
  const { params: p } = await this._findActiveMarket();
840
839
  params = p;
841
840
  }
841
+ let repayAssets;
842
+ let repayShares;
843
+ let approveAmount;
844
+ if (usdcAmount === "all") {
845
+ const markets = await this.getMarkets();
846
+ const mkt = markets.find(
847
+ (m) => m.collateralAsset?.address.toLowerCase() === params.collateralToken.toLowerCase()
848
+ );
849
+ if (mkt) {
850
+ const pos = await this.morphoBlue.position(mkt.uniqueKey, acctAddr);
851
+ repayShares = BigInt(pos.borrowShares);
852
+ repayAssets = 0n;
853
+ const onChainMkt = await this.morphoBlue.market(mkt.uniqueKey);
854
+ const totalBorrowAssets = BigInt(onChainMkt.totalBorrowAssets);
855
+ const totalBorrowShares = BigInt(onChainMkt.totalBorrowShares);
856
+ const estimated = totalBorrowShares > 0n ? repayShares * totalBorrowAssets / totalBorrowShares + 10n : 0n;
857
+ approveAmount = estimated > 0n ? estimated : ethers2.parseUnits("1", 6);
858
+ } else {
859
+ repayAssets = ethers2.parseUnits("999999", 6);
860
+ repayShares = 0n;
861
+ approveAmount = repayAssets;
862
+ }
863
+ } else {
864
+ repayAssets = ethers2.parseUnits(usdcAmount, 6);
865
+ repayShares = 0n;
866
+ approveAmount = repayAssets;
867
+ try {
868
+ const markets = await this.getMarkets();
869
+ const mkt = markets.find(
870
+ (m) => m.collateralAsset?.address.toLowerCase() === params.collateralToken.toLowerCase()
871
+ );
872
+ if (mkt) {
873
+ const pos = await this.morphoBlue.position(mkt.uniqueKey, acctAddr);
874
+ const onChainMkt = await this.morphoBlue.market(mkt.uniqueKey);
875
+ const totalBorrowAssets = BigInt(onChainMkt.totalBorrowAssets);
876
+ const totalBorrowShares = BigInt(onChainMkt.totalBorrowShares);
877
+ const currentDebt = totalBorrowShares > 0n ? BigInt(pos.borrowShares) * totalBorrowAssets / totalBorrowShares : 0n;
878
+ if (repayAssets >= currentDebt && BigInt(pos.borrowShares) > 0n) {
879
+ repayShares = BigInt(pos.borrowShares);
880
+ repayAssets = 0n;
881
+ approveAmount = currentDebt + 10n;
882
+ }
883
+ }
884
+ } catch {
885
+ }
886
+ }
842
887
  const targets = [usdcAddr, morphoAddr];
843
888
  const values = [0n, 0n];
844
889
  const datas = [
845
- erc20Iface.encodeFunctionData("approve", [morphoAddr, amount]),
890
+ erc20Iface.encodeFunctionData("approve", [morphoAddr, approveAmount]),
846
891
  morphoIface.encodeFunctionData("repay", [
847
892
  this._toTuple(params),
848
- amount,
849
- 0n,
893
+ repayAssets,
894
+ repayShares,
850
895
  acctAddr,
851
896
  "0x"
852
897
  ])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agether/sdk",
3
- "version": "1.6.0",
3
+ "version": "1.6.1",
4
4
  "description": "TypeScript SDK for Agether - autonomous credit for AI agents on Base",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",