@ape.swap/bonds-sdk 2.0.0-test.4 → 2.0.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/main.js CHANGED
@@ -18442,7 +18442,6 @@ var types = {};
18442
18442
  // Network chain ids
18443
18443
  // These are the lists we will display to the user
18444
18444
  const MAINNET_CHAINS = [
18445
- types.ChainId.GRAPHLINQ,
18446
18445
  types.ChainId.MAINNET,
18447
18446
  types.ChainId.BSC,
18448
18447
  types.ChainId.MATIC,
@@ -18451,6 +18450,7 @@ const MAINNET_CHAINS = [
18451
18450
  types.ChainId.LINEA,
18452
18451
  types.ChainId.ARBITRUM,
18453
18452
  types.ChainId.LIGHTLINK,
18453
+ types.ChainId.GRAPHLINQ,
18454
18454
  types.ChainId.AVAX,
18455
18455
  ];
18456
18456
  // Network Icons
@@ -71573,25 +71573,103 @@ const calculateARR = (bond) => {
71573
71573
  };
71574
71574
 
71575
71575
  function useBondsData() {
71576
+ const { data: tokenPrices } = useTokenPrices();
71577
+ const { data: bondList } = useBondsList();
71576
71578
  const realTimeApiURL = useURLByEnvironment('realTimeApi');
71577
71579
  return useQuery({
71578
71580
  queryKey: [QUERY_KEYS.BONDS_DATA],
71579
- queryFn: () => getBondsData(realTimeApiURL),
71580
- refetchInterval: 30000, // i.e. 30 sec
71581
+ queryFn: () => getBondsData(realTimeApiURL, tokenPrices, bondList),
71582
+ refetchInterval: 45000, // i.e. 45 sec
71581
71583
  refetchOnWindowFocus: false,
71582
71584
  retry: 1,
71583
71585
  initialData: [],
71584
71586
  });
71585
71587
  }
71586
- const getBondsData = (realTimeApiURL) => __awaiter$9(void 0, void 0, void 0, function* () {
71588
+ const getBondsData = (realTimeApiURL, tokenPrices, bondList) => __awaiter$9(void 0, void 0, void 0, function* () {
71587
71589
  try {
71588
71590
  const response = yield axios.get(realTimeApiURL);
71589
71591
  return response.data.bonds;
71590
71592
  }
71591
71593
  catch (_a) {
71592
- return [];
71594
+ const billData = [];
71595
+ if (tokenPrices && bondList) {
71596
+ yield Promise.all(MAINNET_CHAINS.map((chainId) => __awaiter$9(void 0, void 0, void 0, function* () {
71597
+ var _a;
71598
+ const bills = (_a = bondList === null || bondList === void 0 ? void 0 : bondList.filter((bill) => {
71599
+ var _a, _b;
71600
+ return ((_b = (_a = bill === null || bill === void 0 ? void 0 : bill.contractAddress) === null || _a === void 0 ? void 0 : _a[chainId]) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== ACF_TO_ABOND.toLowerCase() &&
71601
+ !bill.soldOut &&
71602
+ bill.chainId === chainId;
71603
+ })) !== null && _a !== void 0 ? _a : [];
71604
+ const returnedBills = yield fetchBills(chainId, tokenPrices, bills);
71605
+ billData.push(returnedBills);
71606
+ })));
71607
+ }
71608
+ return billData === null || billData === void 0 ? void 0 : billData.flat();
71593
71609
  }
71594
71610
  });
71611
+ const fetchBills = (chainId, tokenPrices, bills) => __awaiter$9(void 0, void 0, void 0, function* () {
71612
+ const billIds = [];
71613
+ const billCalls = bills.flatMap((bill) => {
71614
+ var _a, _b, _c, _d;
71615
+ billIds.push(bill.index);
71616
+ return [
71617
+ // Get bill price with LP fees
71618
+ {
71619
+ address: (_a = bill.contractAddress[bill.chainId]) !== null && _a !== void 0 ? _a : '',
71620
+ name: 'trueBillPrice',
71621
+ },
71622
+ // Get bill debt ratio
71623
+ {
71624
+ address: (_b = bill.contractAddress[bill.chainId]) !== null && _b !== void 0 ? _b : '',
71625
+ name: 'totalPayoutGiven',
71626
+ },
71627
+ // Terms
71628
+ // (1) controlVariable (2) vestingTerm (3) minimumPrice (4) maxPayout (5) maxDebt
71629
+ {
71630
+ address: (_c = bill.contractAddress[bill.chainId]) !== null && _c !== void 0 ? _c : '',
71631
+ name: 'terms',
71632
+ },
71633
+ {
71634
+ address: (_d = bill.contractAddress[bill.chainId]) !== null && _d !== void 0 ? _d : '',
71635
+ name: bill.billVersion !== types.BillVersion.V1 ? 'getMaxTotalPayout' : 'maxTotalPayout',
71636
+ },
71637
+ ];
71638
+ });
71639
+ const vals = yield multicall(chainId, BOND_ABI, billCalls);
71640
+ const chunkSize = (vals === null || vals === void 0 ? void 0 : vals.length) / (bills === null || bills === void 0 ? void 0 : bills.length);
71641
+ const chunkedBills = lodashExports.chunk(vals, chunkSize);
71642
+ return cleanBillsData(billIds, chunkedBills, tokenPrices, chainId, bills);
71643
+ });
71644
+ const cleanBillsData = (billIds, chunkedBills, tokenPrices, chainId, bills) => {
71645
+ const data = chunkedBills === null || chunkedBills === void 0 ? void 0 : chunkedBills.map((chunk, index) => {
71646
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
71647
+ const bill = bills === null || bills === void 0 ? void 0 : bills.find((bill) => (bill === null || bill === void 0 ? void 0 : bill.index) === billIds[index]);
71648
+ const lpPrice = (_a = tokenPrices === null || tokenPrices === void 0 ? void 0 : tokenPrices.find((tokenPrice) => {
71649
+ var _a, _b, _c, _d;
71650
+ return ((_a = tokenPrice === null || tokenPrice === void 0 ? void 0 : tokenPrice.address) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === ((_d = (_c = (_b = bill === null || bill === void 0 ? void 0 : bill.lpToken) === null || _b === void 0 ? void 0 : _b.address) === null || _c === void 0 ? void 0 : _c[bill === null || bill === void 0 ? void 0 : bill.chainId]) === null || _d === void 0 ? void 0 : _d.toLowerCase()) &&
71651
+ (tokenPrice === null || tokenPrice === void 0 ? void 0 : tokenPrice.chainId) === chainId;
71652
+ })) === null || _a === void 0 ? void 0 : _a.price;
71653
+ const earnTokenPrice = (_b = tokenPrices === null || tokenPrices === void 0 ? void 0 : tokenPrices.find((tokenPrice) => {
71654
+ var _a, _b, _c, _d;
71655
+ return ((_a = tokenPrice === null || tokenPrice === void 0 ? void 0 : tokenPrice.address) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === ((_d = (_c = (_b = bill === null || bill === void 0 ? void 0 : bill.earnToken) === null || _b === void 0 ? void 0 : _b.address) === null || _c === void 0 ? void 0 : _c[bill === null || bill === void 0 ? void 0 : bill.chainId]) === null || _d === void 0 ? void 0 : _d.toLowerCase()) &&
71656
+ (tokenPrice === null || tokenPrice === void 0 ? void 0 : tokenPrice.chainId) === chainId;
71657
+ })) === null || _b === void 0 ? void 0 : _b.price;
71658
+ const [trueBillPrice, totalPayoutGiven, terms, maxTotalPayout] = chunk;
71659
+ const [, vestingTerm, , maxPayout] = terms;
71660
+ const priceUsd = lpPrice && trueBillPrice && getBalanceNumber(trueBillPrice === null || trueBillPrice === void 0 ? void 0 : trueBillPrice.toString()) * lpPrice;
71661
+ const discount = earnTokenPrice && priceUsd && ((earnTokenPrice - priceUsd) / earnTokenPrice) * 100;
71662
+ const tokensRemaining = new BigNumber$1(maxTotalPayout !== null && maxTotalPayout !== void 0 ? maxTotalPayout : '0')
71663
+ .minus(new BigNumber$1(totalPayoutGiven))
71664
+ .div(new BigNumber$1(10).pow((_c = bill === null || bill === void 0 ? void 0 : bill.earnToken.decimals[bill === null || bill === void 0 ? void 0 : bill.chainId]) !== null && _c !== void 0 ? _c : 18))
71665
+ .toString();
71666
+ //I've added a ts ignore here due to some props marked as optional in the list repo which are actually not optional
71667
+ //@ts-ignore
71668
+ const cleanedData = Object.assign(Object.assign({}, bill), { type: (_d = bill === null || bill === void 0 ? void 0 : bill.billType) !== null && _d !== void 0 ? _d : '', billAddress: (_e = bill === null || bill === void 0 ? void 0 : bill.contractAddress[bill === null || bill === void 0 ? void 0 : bill.chainId]) !== null && _e !== void 0 ? _e : '', billNftAddress: (_f = bill === null || bill === void 0 ? void 0 : bill.billNnftAddress[bill === null || bill === void 0 ? void 0 : bill.chainId]) !== null && _f !== void 0 ? _f : '', showcaseTokenName: (_j = (_h = (_g = bill === null || bill === void 0 ? void 0 : bill.showcaseToken) === null || _g === void 0 ? void 0 : _g.symbol) !== null && _h !== void 0 ? _h : bill === null || bill === void 0 ? void 0 : bill.earnToken.symbol) !== null && _j !== void 0 ? _j : '', discount: discount, principalTokenPrice: (_k = lpPrice === null || lpPrice === void 0 ? void 0 : lpPrice.toString()) !== null && _k !== void 0 ? _k : '0', payoutTokenPrice: (_l = earnTokenPrice === null || earnTokenPrice === void 0 ? void 0 : earnTokenPrice.toString()) !== null && _l !== void 0 ? _l : '0', tokensRemaining, maxTotalPayout: maxTotalPayout === null || maxTotalPayout === void 0 ? void 0 : maxTotalPayout.toString(), trueBillPrice: trueBillPrice === null || trueBillPrice === void 0 ? void 0 : trueBillPrice.toString(), maxPayout: maxPayout === null || maxPayout === void 0 ? void 0 : maxPayout.toString(), totalPayoutGiven: totalPayoutGiven === null || totalPayoutGiven === void 0 ? void 0 : totalPayoutGiven.toString(), vestingTerm: vestingTerm.toNumber() });
71669
+ return cleanedData;
71670
+ });
71671
+ return data;
71672
+ };
71595
71673
 
71596
71674
  function adjustDecimals(numStr) {
71597
71675
  const parts = numStr.split('.');
@@ -73738,9 +73816,17 @@ const Bonds = () => {
73738
73816
  // Fetch data
73739
73817
  useHotBonds();
73740
73818
  const { favTokens } = useFavoriteTokens();
73741
- const { data: bondData } = useBondsData();
73819
+ const { data: bondData, refetch: refetchBondsData } = useBondsData();
73742
73820
  const { data: allBonds } = useBondsList();
73743
73821
  const { data: chainFilterOption, setChainFilterOption } = useChainFilterOption();
73822
+ const { data: tokenPrices } = useTokenPrices();
73823
+ useEffect(() => {
73824
+ //@ts-ignore
73825
+ if ((bondData === null || bondData === void 0 ? void 0 : bondData.length) === 0 && (tokenPrices === null || tokenPrices === void 0 ? void 0 : tokenPrices.length) > 0 && (allBonds === null || allBonds === void 0 ? void 0 : allBonds.length) > 0) {
73826
+ console.log('Real time api call failed, triggering fallback strategy');
73827
+ refetchBondsData();
73828
+ }
73829
+ }, [bondData === null || bondData === void 0 ? void 0 : bondData.length, tokenPrices === null || tokenPrices === void 0 ? void 0 : tokenPrices.length]);
73744
73830
  // State
73745
73831
  const [sortConfig, setSortConfig] = useState({ key: 'discount', direction: 'desc' });
73746
73832
  const [searchQuery, setSearchQuery] = useState('');
@@ -73752,14 +73838,15 @@ const Bonds = () => {
73752
73838
  let billsToReturn = filterOption === 'SOLD OUT' ? (allBonds !== null && allBonds !== void 0 ? allBonds : []) : (sortedBonds !== null && sortedBonds !== void 0 ? sortedBonds : []);
73753
73839
  if (searchQuery) {
73754
73840
  billsToReturn = billsToReturn === null || billsToReturn === void 0 ? void 0 : billsToReturn.filter((bill) => {
73755
- return [`${bill.earnToken.symbol.toUpperCase()}`, `${bill.lpToken.symbol.toUpperCase()}`].includes(searchQuery.toUpperCase());
73841
+ var _a, _b;
73842
+ return [`${(_a = bill === null || bill === void 0 ? void 0 : bill.earnToken) === null || _a === void 0 ? void 0 : _a.symbol.toUpperCase()}`, `${(_b = bill === null || bill === void 0 ? void 0 : bill.lpToken) === null || _b === void 0 ? void 0 : _b.symbol.toUpperCase()}`].includes(searchQuery.toUpperCase());
73756
73843
  });
73757
73844
  }
73758
73845
  if (topTags.includes(filterOption)) {
73759
73846
  billsToReturn = billsToReturn === null || billsToReturn === void 0 ? void 0 : billsToReturn.filter((bill) => { var _a; return (_a = bill === null || bill === void 0 ? void 0 : bill.tags) === null || _a === void 0 ? void 0 : _a.includes(filterOption); });
73760
73847
  }
73761
73848
  if (!(chainFilterOption === null || chainFilterOption === void 0 ? void 0 : chainFilterOption.includes('All Chains'))) {
73762
- billsToReturn = billsToReturn === null || billsToReturn === void 0 ? void 0 : billsToReturn.filter((bill) => chainFilterOption === null || chainFilterOption === void 0 ? void 0 : chainFilterOption.includes(bill.chainId.toString()));
73849
+ billsToReturn = billsToReturn === null || billsToReturn === void 0 ? void 0 : billsToReturn.filter((bill) => { var _a; return chainFilterOption === null || chainFilterOption === void 0 ? void 0 : chainFilterOption.includes((_a = bill === null || bill === void 0 ? void 0 : bill.chainId) === null || _a === void 0 ? void 0 : _a.toString()); });
73763
73850
  }
73764
73851
  if (filterOption !== 'SOLD OUT') {
73765
73852
  billsToReturn = billsToReturn === null || billsToReturn === void 0 ? void 0 : billsToReturn.filter((bond) => !isBondSoldOut(bond));
@@ -1,4 +1,6 @@
1
1
  import { UseQueryResult } from '@tanstack/react-query';
2
2
  import { BondsData } from '../../types/bonds';
3
+ import { TokenPrices } from '../tokenPrices/useTokenPrices';
4
+ import { BillsConfig } from '@ape.swap/apeswap-lists';
3
5
  export default function useBondsData(): UseQueryResult<BondsData[]>;
4
- export declare const getBondsData: (realTimeApiURL: string) => Promise<BondsData[]>;
6
+ export declare const getBondsData: (realTimeApiURL: string, tokenPrices?: TokenPrices[], bondList?: BillsConfig[]) => Promise<BondsData[]>;
@@ -1,14 +1,14 @@
1
1
  import { ChainId, Token } from '@ape.swap/apeswap-lists';
2
2
  export interface BondsData {
3
- index: number;
3
+ index?: number;
4
4
  cmcId?: number;
5
5
  chainId: ChainId;
6
6
  type: string;
7
7
  discount: number;
8
8
  billAddress?: string;
9
9
  billNftAddress: string;
10
- soldOut: boolean;
11
- inactive: boolean;
10
+ soldOut?: boolean;
11
+ inactive?: boolean;
12
12
  trueBillPrice: string;
13
13
  maxTotalPayout: string;
14
14
  totalPayoutGiven: string;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Ape Bond SDK",
4
4
  "author": "Ape Bond",
5
5
  "license": "MIT",
6
- "version": "2.0.0-test.4",
6
+ "version": "2.0.1",
7
7
  "module": "dist/main.js",
8
8
  "type": "module",
9
9
  "types": "dist/main.d.ts",
@@ -22,7 +22,7 @@
22
22
  "peerDependencies": {
23
23
  "react": "^18.2.0",
24
24
  "react-dom": "^17.0.0 || ^18.0.0",
25
- "axios": "^1.7.2",
25
+ "axios": "1.7.2",
26
26
  "wagmi": "2.12.17",
27
27
  "@tanstack/react-query": "5.51.5",
28
28
  "@rainbow-me/rainbowkit": "^2.1.6"
@@ -38,7 +38,7 @@
38
38
  "@types/react": "^18.3.3",
39
39
  "@types/react-dom": "^18.3.0",
40
40
  "@vitejs/plugin-react": "^4.2.1",
41
- "axios": "^1.7.2",
41
+ "axios": "1.7.2",
42
42
  "eslint": "^9.10.0",
43
43
  "eslint-plugin-react": "^7.35.2",
44
44
  "ethers": "5.7.2",