@berachain/berajs 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/exports.d.cts +3 -3
- package/dist/actions/exports.d.ts +3 -3
- package/dist/{getValidatorQueuedOperatorAddress-Dstyaomr.d.cts → getValidatorQueuedOperatorAddress-D_LEt3vv.d.cts} +1 -1
- package/dist/{getValidatorQueuedOperatorAddress-RW4OMcD1.d.ts → getValidatorQueuedOperatorAddress-NrjpnEn8.d.ts} +1 -1
- package/dist/{global.d-CN6l2b9E.d.cts → global.d-DLRtrPG5.d.cts} +1 -0
- package/dist/{global.d-wYu8G8IQ.d.ts → global.d-X3cqpCEB.d.ts} +1 -0
- package/dist/hooks/exports.cjs +3 -3
- package/dist/hooks/exports.cjs.map +1 -1
- package/dist/hooks/exports.d.cts +26 -4
- package/dist/hooks/exports.d.ts +26 -4
- package/dist/hooks/exports.mjs +3 -3
- package/dist/hooks/exports.mjs.map +1 -1
- package/dist/types/exports.d.cts +1 -1
- package/dist/types/exports.d.ts +1 -1
- package/package.json +3 -3
- package/src/hooks/pol/useStakedData.ts +169 -92
- package/src/types/pol.d.ts +1 -0
package/dist/types/exports.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as AggregatedTokenTotals, g as BribeBoostReward, b as BribeBoostRewardItem, B as BribeBoostRewardProof, h as BribeBoostValidatorApr, t as ClaimedIncentives, u as ClaimedIncentivesLocalState, C as CustomProposal, n as CustomProposalActionErrors, a as CustomProposalErrors, D as DefaultHookOptions, c as DefaultHookReturnType, G as GovernanceTopic, I as IContractWrite, i as IContractWriteFn, r as IncentiveReward, s as IncentiveRewardChunksWithValidatorData, m as PROPOSAL_GENRE, j as Proposal, p as ProposalAction, l as ProposalVotes, P as ProtocolMetadata, o as SafeProposalAction, d as StakedBeraAsset, S as StakingConfig, U as UserVaultInfo, q as VerifiedAbi, V as Vote, k as Voter } from '../global.d-
|
|
1
|
+
export { A as AggregatedTokenTotals, g as BribeBoostReward, b as BribeBoostRewardItem, B as BribeBoostRewardProof, h as BribeBoostValidatorApr, t as ClaimedIncentives, u as ClaimedIncentivesLocalState, C as CustomProposal, n as CustomProposalActionErrors, a as CustomProposalErrors, D as DefaultHookOptions, c as DefaultHookReturnType, G as GovernanceTopic, I as IContractWrite, i as IContractWriteFn, r as IncentiveReward, s as IncentiveRewardChunksWithValidatorData, m as PROPOSAL_GENRE, j as Proposal, p as ProposalAction, l as ProposalVotes, P as ProtocolMetadata, o as SafeProposalAction, d as StakedBeraAsset, S as StakingConfig, U as UserVaultInfo, q as VerifiedAbi, V as Vote, k as Voter } from '../global.d-DLRtrPG5.cjs';
|
|
2
2
|
export { A as AllowanceQueryItem, j as AllowanceToken, B as BalanceToken, z as BexStatus, w as Calldata, H as HoneySwapActions, I as IAggregatorArgs, D as IAggregatorQuote, g as IRawAggregatorQuote, v as IUserPosition, M as MinimalERC20, h as PythPriceFeedMap, S as SwapRequest, b as Token, i as TokenCurrentPriceMap, x as TokenPriceInfo, t as TokenWithAmount, a as TokenWithMetadata, y as TokenWithPrice } from '../HoneyConfigProvider-Ca3ouozs.cjs';
|
|
3
3
|
import { BundleData, EnsoClient } from '@ensofinance/sdk';
|
|
4
4
|
import { TransactionRequest, Address, Hex, PublicClient } from 'viem';
|
package/dist/types/exports.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as AggregatedTokenTotals, g as BribeBoostReward, b as BribeBoostRewardItem, B as BribeBoostRewardProof, h as BribeBoostValidatorApr, t as ClaimedIncentives, u as ClaimedIncentivesLocalState, C as CustomProposal, n as CustomProposalActionErrors, a as CustomProposalErrors, D as DefaultHookOptions, c as DefaultHookReturnType, G as GovernanceTopic, I as IContractWrite, i as IContractWriteFn, r as IncentiveReward, s as IncentiveRewardChunksWithValidatorData, m as PROPOSAL_GENRE, j as Proposal, p as ProposalAction, l as ProposalVotes, P as ProtocolMetadata, o as SafeProposalAction, d as StakedBeraAsset, S as StakingConfig, U as UserVaultInfo, q as VerifiedAbi, V as Vote, k as Voter } from '../global.d-
|
|
1
|
+
export { A as AggregatedTokenTotals, g as BribeBoostReward, b as BribeBoostRewardItem, B as BribeBoostRewardProof, h as BribeBoostValidatorApr, t as ClaimedIncentives, u as ClaimedIncentivesLocalState, C as CustomProposal, n as CustomProposalActionErrors, a as CustomProposalErrors, D as DefaultHookOptions, c as DefaultHookReturnType, G as GovernanceTopic, I as IContractWrite, i as IContractWriteFn, r as IncentiveReward, s as IncentiveRewardChunksWithValidatorData, m as PROPOSAL_GENRE, j as Proposal, p as ProposalAction, l as ProposalVotes, P as ProtocolMetadata, o as SafeProposalAction, d as StakedBeraAsset, S as StakingConfig, U as UserVaultInfo, q as VerifiedAbi, V as Vote, k as Voter } from '../global.d-X3cqpCEB.js';
|
|
2
2
|
export { A as AllowanceQueryItem, j as AllowanceToken, B as BalanceToken, z as BexStatus, w as Calldata, H as HoneySwapActions, I as IAggregatorArgs, D as IAggregatorQuote, g as IRawAggregatorQuote, v as IUserPosition, M as MinimalERC20, h as PythPriceFeedMap, S as SwapRequest, b as Token, i as TokenCurrentPriceMap, x as TokenPriceInfo, t as TokenWithAmount, a as TokenWithMetadata, y as TokenWithPrice } from '../HoneyConfigProvider-CK6mOKFH.js';
|
|
3
3
|
import { BundleData, EnsoClient } from '@ensofinance/sdk';
|
|
4
4
|
import { TransactionRequest, Address, Hex, PublicClient } from 'viem';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@berachain/berajs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"typescript": "5.5.4",
|
|
32
32
|
"viem": "2.45.0",
|
|
33
33
|
"vitest": "3.2.4",
|
|
34
|
-
"@berachain/
|
|
35
|
-
"@berachain/
|
|
34
|
+
"@berachain/utils": "0.1.0",
|
|
35
|
+
"@berachain/wagmi": "0.3.0"
|
|
36
36
|
},
|
|
37
37
|
"exports": {
|
|
38
38
|
"./*": {
|
|
@@ -1,117 +1,194 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
1
2
|
import useSWR from "swr";
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
-
useBeraWallet,
|
|
5
|
-
useConfig,
|
|
6
|
-
usePublicClient,
|
|
7
|
-
} from "@berachain/wagmi/hooks";
|
|
4
|
+
import { useBeraWallet, usePublicClient } from "@berachain/wagmi/hooks";
|
|
8
5
|
import BigNumber from "@berachain/utils/pkg/bignumber.js";
|
|
9
6
|
|
|
10
7
|
import { getEarnedStakedBeraVault } from "~/actions/pol/getEarnedStakedBeraVault";
|
|
11
8
|
import { getRewardTokenToBeraRate } from "~/actions/pol/getRewardTokenToBeraRate";
|
|
12
9
|
import { getStakedBeraAPR } from "~/actions/pol/getStakedBeraAPR";
|
|
13
|
-
import { getTokenCurrentPrices } from "~/actions/prices/getTokenCurrentPrices";
|
|
14
|
-
import { getWalletBalances } from "~/actions/tokens/getWalletBalances";
|
|
15
10
|
import { useBeraFlag } from "~/contexts/BeraFlags/BeraFlags";
|
|
16
11
|
import { POLLING } from "~/enum/polling";
|
|
17
|
-
import {
|
|
12
|
+
import { assertPublicClient } from "~/errors/assert";
|
|
13
|
+
import { usePollWalletBalances } from "~/hooks/tokens/usePollWalletBalances";
|
|
14
|
+
import { useTokenPrices } from "~/hooks/tokens/useTokenPrices";
|
|
18
15
|
import type { StakedBeraAsset } from "~/types/pol";
|
|
19
16
|
import type { StakingConfig } from "~/types/staking";
|
|
20
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Fetches staked asset data in two phases for progressive loading:
|
|
20
|
+
*
|
|
21
|
+
* 1. APR — independent fetch, resolves quickly, shared by both phases.
|
|
22
|
+
* 2. Balances (fast path) — wallet balances (via usePollWalletBalances),
|
|
23
|
+
* token prices (via useTokenPrices), and exchange rates.
|
|
24
|
+
* Provides staked amounts and USD values immediately (earnings are 0).
|
|
25
|
+
* 3. Positions (slow path) — adds earnings data from the vault.
|
|
26
|
+
* Provides the full picture with earned amounts and USD values.
|
|
27
|
+
*
|
|
28
|
+
* Consumers can show `balances` right away and swap in `data` once positions resolve.
|
|
29
|
+
*/
|
|
21
30
|
export const useStakedData = (stakingConfigs: StakingConfig[]) => {
|
|
22
31
|
const window = useBeraFlag("swberaAprWindow");
|
|
23
32
|
|
|
24
|
-
const wagmiConfig = useConfig();
|
|
25
33
|
const publicClient = usePublicClient();
|
|
26
34
|
const { address: account } = useBeraWallet();
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
35
|
+
|
|
36
|
+
const receiptTokens = useMemo(
|
|
37
|
+
() => stakingConfigs.map((config) => config.receiptToken),
|
|
38
|
+
[stakingConfigs],
|
|
39
|
+
);
|
|
40
|
+
const inputTokens = useMemo(
|
|
41
|
+
() => stakingConfigs.map((config) => config.inputTokens[0]),
|
|
42
|
+
[stakingConfigs],
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// APR — only updates every ~12 hours on BE
|
|
46
|
+
const aprKey = window ? (["useStakedData:apr", window] as const) : null;
|
|
47
|
+
const { data: apr } = useSWR(
|
|
48
|
+
aprKey,
|
|
49
|
+
async ([, window]) => getStakedBeraAPR({ window }),
|
|
50
|
+
{ refreshInterval: POLLING.NORMAL },
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const { data: walletBalances, isLoading: isLoadingBalances } =
|
|
54
|
+
usePollWalletBalances({ tokens: receiptTokens });
|
|
55
|
+
|
|
56
|
+
// NOT BERA PRICE, INPUT TOKEN PRICE
|
|
57
|
+
const { getTokenPrice } = useTokenPrices({ tokens: inputTokens });
|
|
58
|
+
|
|
59
|
+
// exchange rates from receipt token → input token (on-chain reads)
|
|
60
|
+
const exchangeRatesKey =
|
|
61
|
+
publicClient && receiptTokens.length > 0
|
|
62
|
+
? (["useStakedData:exchangeRates", receiptTokens] as const)
|
|
63
|
+
: null;
|
|
64
|
+
|
|
65
|
+
const { data: exchangeRates } = useSWR(
|
|
66
|
+
exchangeRatesKey,
|
|
67
|
+
async ([, receiptTokens]) => {
|
|
68
|
+
assertPublicClient(publicClient);
|
|
69
|
+
return Promise.all(
|
|
70
|
+
receiptTokens.map((token) =>
|
|
71
|
+
getRewardTokenToBeraRate({
|
|
72
|
+
address: token.address,
|
|
73
|
+
publicClient,
|
|
74
|
+
}),
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
},
|
|
78
|
+
{ refreshInterval: POLLING.NORMAL },
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
// balances (fast path) — derived from hooks above, no extra requests
|
|
82
|
+
const balances = useMemo<StakedBeraAsset[] | undefined>(() => {
|
|
83
|
+
if (!walletBalances || !exchangeRates) return undefined;
|
|
84
|
+
|
|
85
|
+
return stakingConfigs.map((config, i): StakedBeraAsset => {
|
|
86
|
+
const inputToken = config.inputTokens[0];
|
|
87
|
+
const stakedBalance =
|
|
88
|
+
walletBalances.find((b) => b.address === config.receiptToken.address)
|
|
89
|
+
?.balance.raw ?? "0";
|
|
90
|
+
const exchangeRate = exchangeRates[i] ?? 0;
|
|
91
|
+
const inputTokenPrice = getTokenPrice(inputToken) ?? 0;
|
|
92
|
+
|
|
93
|
+
const total = new BigNumber(stakedBalance).multipliedBy(exchangeRate);
|
|
94
|
+
const stakedAmount = total.dividedBy(10 ** inputToken.decimals);
|
|
95
|
+
const stakedAmountUsd = stakedAmount.multipliedBy(inputTokenPrice);
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
token: inputToken,
|
|
99
|
+
apr: apr ?? 0,
|
|
100
|
+
stakedAmount: stakedAmount.toNumber(),
|
|
101
|
+
stakedAmountUsd: stakedAmountUsd.toNumber(),
|
|
102
|
+
earnedAmount: 0,
|
|
103
|
+
earnedAmountUsd: 0,
|
|
104
|
+
isLoadingEarnings: true,
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
}, [walletBalances, exchangeRates, stakingConfigs, apr, getTokenPrice]);
|
|
108
|
+
|
|
109
|
+
// positions (slow path) — fetch earnings and exchange rates from the vault
|
|
110
|
+
const positionsKey =
|
|
111
|
+
account && publicClient && stakingConfigs.length > 0
|
|
112
|
+
? (["useStakedData:positions", account, stakingConfigs] as const)
|
|
30
113
|
: null;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
114
|
+
|
|
115
|
+
const {
|
|
116
|
+
data: positionsRaw,
|
|
117
|
+
isLoading: isLoadingPositions,
|
|
118
|
+
isValidating: isValidatingPositions,
|
|
119
|
+
} = useSWR(
|
|
120
|
+
positionsKey,
|
|
121
|
+
async ([, account, stakingConfigs]) => {
|
|
35
122
|
assertPublicClient(publicClient);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const receiptTokens = stakingConfigs.map((config) => config.receiptToken);
|
|
40
|
-
// get the staked balances for the receipt tokens
|
|
41
|
-
const stakedBalancesData = await getWalletBalances({
|
|
42
|
-
account,
|
|
43
|
-
tokenList: receiptTokens,
|
|
44
|
-
wagmiConfig,
|
|
45
|
-
});
|
|
46
|
-
const data: StakedBeraAsset[] = await Promise.all(
|
|
47
|
-
stakingConfigs.map(async (stakingConfig): Promise<StakedBeraAsset> => {
|
|
48
|
-
// get the staking token & input/receipt token
|
|
123
|
+
|
|
124
|
+
return Promise.all(
|
|
125
|
+
stakingConfigs.map(async (stakingConfig) => {
|
|
49
126
|
const stakingToken = stakingConfig.receiptToken;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
address: stakingToken.address,
|
|
64
|
-
publicClient,
|
|
65
|
-
}),
|
|
66
|
-
]);
|
|
67
|
-
|
|
68
|
-
// input token price
|
|
69
|
-
const inputTokenPrice =
|
|
70
|
-
inputTokenPriceData[inputToken.address]?.price ?? 0;
|
|
71
|
-
// staked asset balance
|
|
72
|
-
const stakedBalance =
|
|
73
|
-
stakedBalancesData?.find((b) => b.address === stakingToken.address)
|
|
74
|
-
?.balance.raw ?? "0";
|
|
75
|
-
|
|
76
|
-
const exchangeRateNow = exchangeRateNowRaw ?? 0;
|
|
77
|
-
|
|
78
|
-
// unrealised gains is the users earnings
|
|
79
|
-
const unrealisedGains = new BigNumber(earnings.earningsTotal).minus(
|
|
80
|
-
earnings.earningsRealized,
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
const total = new BigNumber(stakedBalance).multipliedBy(
|
|
84
|
-
exchangeRateNow,
|
|
85
|
-
);
|
|
86
|
-
// desposited is the users deposited amount
|
|
87
|
-
const desposited = total.minus(unrealisedGains);
|
|
88
|
-
|
|
89
|
-
// in terms of bera
|
|
90
|
-
const stakedAmount = desposited.dividedBy(10 ** inputToken.decimals);
|
|
91
|
-
const earnedAmount = unrealisedGains.dividedBy(
|
|
92
|
-
10 ** inputToken.decimals,
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
// in terms of usd
|
|
96
|
-
const stakedAmountUsd = stakedAmount.multipliedBy(inputTokenPrice);
|
|
97
|
-
const earnedAmountUsd = earnedAmount.multipliedBy(inputTokenPrice);
|
|
98
|
-
|
|
99
|
-
const data: StakedBeraAsset = {
|
|
100
|
-
token: inputToken,
|
|
101
|
-
apr: apr ?? 0,
|
|
102
|
-
stakedAmount: stakedAmount.toNumber(),
|
|
103
|
-
earnedAmount: earnedAmount.toNumber(),
|
|
104
|
-
stakedAmountUsd: stakedAmountUsd.toNumber(),
|
|
105
|
-
earnedAmountUsd: earnedAmountUsd.toNumber(),
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
return data;
|
|
127
|
+
|
|
128
|
+
const [earnings, exchangeRate] = await Promise.all([
|
|
129
|
+
getEarnedStakedBeraVault({
|
|
130
|
+
address: stakingToken.address,
|
|
131
|
+
account,
|
|
132
|
+
}),
|
|
133
|
+
getRewardTokenToBeraRate({
|
|
134
|
+
address: stakingToken.address,
|
|
135
|
+
publicClient,
|
|
136
|
+
}),
|
|
137
|
+
]);
|
|
138
|
+
|
|
139
|
+
return { earnings, exchangeRate };
|
|
109
140
|
}),
|
|
110
141
|
);
|
|
111
|
-
return data;
|
|
112
|
-
},
|
|
113
|
-
{
|
|
114
|
-
refreshInterval: POLLING.FAST,
|
|
115
142
|
},
|
|
143
|
+
{ refreshInterval: POLLING.FAST },
|
|
116
144
|
);
|
|
145
|
+
|
|
146
|
+
// derive full StakedBeraAsset[] from raw positions + shared hooks
|
|
147
|
+
const data = useMemo<StakedBeraAsset[] | undefined>(() => {
|
|
148
|
+
if (!positionsRaw || !walletBalances) return undefined;
|
|
149
|
+
|
|
150
|
+
return stakingConfigs.map((config, i): StakedBeraAsset => {
|
|
151
|
+
const inputToken = config.inputTokens[0];
|
|
152
|
+
const stakingToken = config.receiptToken;
|
|
153
|
+
const { earnings, exchangeRate: exchangeRateRaw } = positionsRaw[i];
|
|
154
|
+
|
|
155
|
+
const inputTokenPrice = getTokenPrice(inputToken) ?? 0;
|
|
156
|
+
const stakedBalance =
|
|
157
|
+
walletBalances.find((b) => b.address === stakingToken.address)?.balance
|
|
158
|
+
.raw ?? "0";
|
|
159
|
+
const exchangeRate = exchangeRateRaw ?? 0;
|
|
160
|
+
|
|
161
|
+
const unrealisedGains = new BigNumber(earnings.earningsTotal).minus(
|
|
162
|
+
earnings.earningsRealized,
|
|
163
|
+
);
|
|
164
|
+
const total = new BigNumber(stakedBalance).multipliedBy(exchangeRate);
|
|
165
|
+
const deposited = total.minus(unrealisedGains);
|
|
166
|
+
|
|
167
|
+
const stakedAmount = deposited.dividedBy(10 ** inputToken.decimals);
|
|
168
|
+
const earnedAmount = unrealisedGains.dividedBy(10 ** inputToken.decimals);
|
|
169
|
+
const stakedAmountUsd = stakedAmount.multipliedBy(inputTokenPrice);
|
|
170
|
+
const earnedAmountUsd = earnedAmount.multipliedBy(inputTokenPrice);
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
token: inputToken,
|
|
174
|
+
apr: apr ?? 0,
|
|
175
|
+
stakedAmount: stakedAmount.toNumber(),
|
|
176
|
+
earnedAmount: earnedAmount.toNumber(),
|
|
177
|
+
stakedAmountUsd: stakedAmountUsd.toNumber(),
|
|
178
|
+
earnedAmountUsd: earnedAmountUsd.toNumber(),
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
}, [positionsRaw, walletBalances, stakingConfigs, apr, getTokenPrice]);
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
/** Fast path: staked amounts + USD values (no earnings) */
|
|
185
|
+
balances,
|
|
186
|
+
/** Slow path: full enriched positions with earnings */
|
|
187
|
+
data,
|
|
188
|
+
/** True only when both balances and positions are still loading */
|
|
189
|
+
isLoading: isLoadingBalances && isLoadingPositions,
|
|
190
|
+
isLoadingBalances,
|
|
191
|
+
isLoadingPositions,
|
|
192
|
+
isValidatingPositions,
|
|
193
|
+
};
|
|
117
194
|
};
|