@chorus-one/polygon 1.0.0
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/.mocharc.json +6 -0
- package/LICENSE +13 -0
- package/README.md +233 -0
- package/dist/cjs/constants.d.ts +187 -0
- package/dist/cjs/constants.js +141 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/referrer.d.ts +2 -0
- package/dist/cjs/referrer.js +15 -0
- package/dist/cjs/staker.d.ts +335 -0
- package/dist/cjs/staker.js +716 -0
- package/dist/cjs/types.d.ts +40 -0
- package/dist/cjs/types.js +2 -0
- package/dist/mjs/constants.d.ts +187 -0
- package/dist/mjs/constants.js +138 -0
- package/dist/mjs/index.d.ts +4 -0
- package/dist/mjs/index.js +2 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/referrer.d.ts +2 -0
- package/dist/mjs/referrer.js +11 -0
- package/dist/mjs/staker.d.ts +335 -0
- package/dist/mjs/staker.js +712 -0
- package/dist/mjs/types.d.ts +40 -0
- package/dist/mjs/types.js +1 -0
- package/hardhat.config.ts +27 -0
- package/package.json +50 -0
- package/src/constants.ts +151 -0
- package/src/index.ts +14 -0
- package/src/referrer.ts +15 -0
- package/src/staker.ts +878 -0
- package/src/types.ts +45 -0
- package/test/fixtures/expected-data.ts +17 -0
- package/test/integration/localSigner.spec.ts +128 -0
- package/test/integration/setup.ts +41 -0
- package/test/integration/staker.spec.ts +587 -0
- package/test/integration/testStaker.ts +130 -0
- package/test/integration/utils.ts +263 -0
- package/test/lib/networks.json +14 -0
- package/test/staker.spec.ts +154 -0
- package/tsconfig.cjs.json +9 -0
- package/tsconfig.json +13 -0
- package/tsconfig.mjs.json +9 -0
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { type Address, type Hex } from 'viem';
|
|
2
|
+
import type { Signer } from '@chorus-one/signer';
|
|
3
|
+
import type { Transaction, PolygonNetworkConfig, PolygonTxStatus, StakeInfo, UnbondInfo } from './types';
|
|
4
|
+
export declare class PolygonStaker {
|
|
5
|
+
private readonly rpcUrl?;
|
|
6
|
+
private readonly contracts;
|
|
7
|
+
private readonly publicClient;
|
|
8
|
+
private readonly chain;
|
|
9
|
+
private withdrawalDelayCache;
|
|
10
|
+
private readonly validatorPrecisionCache;
|
|
11
|
+
/**
|
|
12
|
+
* This **static** method is used to derive an address from a public key.
|
|
13
|
+
*
|
|
14
|
+
* It can be used for signer initialization, e.g. `FireblocksSigner` or `LocalSigner`.
|
|
15
|
+
*
|
|
16
|
+
* @returns Returns an array containing the derived address.
|
|
17
|
+
*/
|
|
18
|
+
static getAddressDerivationFn: () => (publicKey: Uint8Array) => Promise<Array<string>>;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a PolygonStaker instance
|
|
21
|
+
*
|
|
22
|
+
* @param params - Initialization configuration
|
|
23
|
+
* @param params.network - Network to use: 'mainnet' (Ethereum L1) or 'testnet' (Sepolia L1)
|
|
24
|
+
* @param params.rpcUrl - Optional RPC endpoint URL override. If not provided, uses viem's default for the network.
|
|
25
|
+
*
|
|
26
|
+
* @returns An instance of PolygonStaker
|
|
27
|
+
*/
|
|
28
|
+
constructor(params: PolygonNetworkConfig);
|
|
29
|
+
/** @deprecated No longer required. Kept for backward compatibility. */
|
|
30
|
+
init(): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Builds a token approval transaction
|
|
33
|
+
*
|
|
34
|
+
* Approves the StakeManager contract to spend POL tokens on behalf of the delegator.
|
|
35
|
+
* This must be called before staking if the current allowance is insufficient.
|
|
36
|
+
*
|
|
37
|
+
* @param params - Parameters for building the transaction
|
|
38
|
+
* @param params.amount - The amount to approve in POL (will be converted to wei internally). Pass "max" for unlimited approval.
|
|
39
|
+
*
|
|
40
|
+
* @returns Returns a promise that resolves to an approval transaction
|
|
41
|
+
*/
|
|
42
|
+
buildApproveTx(params: {
|
|
43
|
+
amount: string;
|
|
44
|
+
}): Promise<{
|
|
45
|
+
tx: Transaction;
|
|
46
|
+
}>;
|
|
47
|
+
/**
|
|
48
|
+
* Builds a staking (delegation) transaction
|
|
49
|
+
*
|
|
50
|
+
* Delegates POL tokens to a validator via their ValidatorShare contract.
|
|
51
|
+
* Requires prior token approval to the StakeManager contract.
|
|
52
|
+
*
|
|
53
|
+
* @param params - Parameters for building the transaction
|
|
54
|
+
* @param params.delegatorAddress - The delegator's Ethereum address
|
|
55
|
+
* @param params.validatorShareAddress - The validator's ValidatorShare contract address
|
|
56
|
+
* @param params.amount - The amount to stake in POL
|
|
57
|
+
* @param params.slippageBps - (Optional) Slippage tolerance in basis points (e.g., 50 = 0.5%). Used to calculate minSharesToMint.
|
|
58
|
+
* @param params.minSharesToMint - (Optional) Minimum validator shares to receive. Use this OR slippageBps, not both.
|
|
59
|
+
* @param params.referrer - (Optional) Custom referrer string for tracking. If not provided, uses 'sdk-chorusone-staking'.
|
|
60
|
+
*
|
|
61
|
+
* @returns Returns a promise that resolves to a Polygon staking transaction
|
|
62
|
+
*/
|
|
63
|
+
buildStakeTx(params: {
|
|
64
|
+
delegatorAddress: Address;
|
|
65
|
+
validatorShareAddress: Address;
|
|
66
|
+
amount: string;
|
|
67
|
+
slippageBps?: number;
|
|
68
|
+
minSharesToMint?: bigint;
|
|
69
|
+
referrer?: string;
|
|
70
|
+
}): Promise<{
|
|
71
|
+
tx: Transaction;
|
|
72
|
+
}>;
|
|
73
|
+
/**
|
|
74
|
+
* Builds an unstaking transaction
|
|
75
|
+
*
|
|
76
|
+
* Creates an unbond request to unstake POL tokens from a validator.
|
|
77
|
+
* After the unbonding period (~80 checkpoints, approximately 3-4 days), call buildWithdrawTx() to claim funds.
|
|
78
|
+
*
|
|
79
|
+
* @param params - Parameters for building the transaction
|
|
80
|
+
* @param params.delegatorAddress - The delegator's address
|
|
81
|
+
* @param params.validatorShareAddress - The validator's ValidatorShare contract address
|
|
82
|
+
* @param params.amount - The amount to unstake in POL (will be converted to wei internally)
|
|
83
|
+
* @param params.slippageBps - (Optional) Slippage tolerance in basis points (e.g., 50 = 0.5%). Used to calculate maximumSharesToBurn.
|
|
84
|
+
* @param params.maximumSharesToBurn - (Optional) Maximum validator shares willing to burn. Use this OR slippageBps, not both.
|
|
85
|
+
* @param params.referrer - (Optional) Custom referrer string for tracking. If not provided, uses 'sdk-chorusone-staking'.
|
|
86
|
+
*
|
|
87
|
+
* @returns Returns a promise that resolves to a Polygon unstaking transaction
|
|
88
|
+
*/
|
|
89
|
+
buildUnstakeTx(params: {
|
|
90
|
+
delegatorAddress: Address;
|
|
91
|
+
validatorShareAddress: Address;
|
|
92
|
+
amount: string;
|
|
93
|
+
slippageBps?: number;
|
|
94
|
+
maximumSharesToBurn?: bigint;
|
|
95
|
+
referrer?: string;
|
|
96
|
+
}): Promise<{
|
|
97
|
+
tx: Transaction;
|
|
98
|
+
}>;
|
|
99
|
+
/**
|
|
100
|
+
* Builds a withdraw transaction
|
|
101
|
+
*
|
|
102
|
+
* Claims unstaked POL tokens after the unbonding period has elapsed.
|
|
103
|
+
* Use getUnbond() to check if the unbonding period is complete.
|
|
104
|
+
*
|
|
105
|
+
* Note: Each unstake creates a separate unbond with its own nonce (1, 2, 3, etc.).
|
|
106
|
+
* Withdrawals must be done per-nonce. To withdraw all pending unbonds, iterate
|
|
107
|
+
* through nonces from 1 to getUnbondNonce() and withdraw each eligible one.
|
|
108
|
+
*
|
|
109
|
+
* @param params - Parameters for building the transaction
|
|
110
|
+
* @param params.delegatorAddress - The delegator's address that will receive the funds
|
|
111
|
+
* @param params.validatorShareAddress - The validator's ValidatorShare contract address
|
|
112
|
+
* @param params.unbondNonce - The specific unbond nonce to withdraw
|
|
113
|
+
*
|
|
114
|
+
* @returns Returns a promise that resolves to a Polygon withdrawal transaction
|
|
115
|
+
*/
|
|
116
|
+
buildWithdrawTx(params: {
|
|
117
|
+
delegatorAddress: Address;
|
|
118
|
+
validatorShareAddress: Address;
|
|
119
|
+
unbondNonce: bigint;
|
|
120
|
+
}): Promise<{
|
|
121
|
+
tx: Transaction;
|
|
122
|
+
}>;
|
|
123
|
+
/**
|
|
124
|
+
* Builds a claim rewards transaction
|
|
125
|
+
*
|
|
126
|
+
* Claims accumulated delegation rewards and sends them to the delegator's wallet.
|
|
127
|
+
*
|
|
128
|
+
* @param params - Parameters for building the transaction
|
|
129
|
+
* @param params.delegatorAddress - The delegator's address that will receive the rewards
|
|
130
|
+
* @param params.validatorShareAddress - The validator's ValidatorShare contract address
|
|
131
|
+
* @param params.referrer - (Optional) Custom referrer string for tracking. If not provided, uses 'sdk-chorusone-staking'.
|
|
132
|
+
*
|
|
133
|
+
* @returns Returns a promise that resolves to a Polygon claim rewards transaction
|
|
134
|
+
*/
|
|
135
|
+
buildClaimRewardsTx(params: {
|
|
136
|
+
delegatorAddress: Address;
|
|
137
|
+
validatorShareAddress: Address;
|
|
138
|
+
referrer?: string;
|
|
139
|
+
}): Promise<{
|
|
140
|
+
tx: Transaction;
|
|
141
|
+
}>;
|
|
142
|
+
/**
|
|
143
|
+
* Builds a compound (restake) rewards transaction
|
|
144
|
+
*
|
|
145
|
+
* Restakes accumulated rewards back into the validator, increasing delegation without new tokens.
|
|
146
|
+
*
|
|
147
|
+
* @param params - Parameters for building the transaction
|
|
148
|
+
* @param params.delegatorAddress - The delegator's address
|
|
149
|
+
* @param params.validatorShareAddress - The validator's ValidatorShare contract address
|
|
150
|
+
* @param params.referrer - (Optional) Custom referrer string for tracking. If not provided, uses 'sdk-chorusone-staking'.
|
|
151
|
+
*
|
|
152
|
+
* @returns Returns a promise that resolves to a Polygon compound transaction
|
|
153
|
+
*/
|
|
154
|
+
buildCompoundTx(params: {
|
|
155
|
+
delegatorAddress: Address;
|
|
156
|
+
validatorShareAddress: Address;
|
|
157
|
+
referrer?: string;
|
|
158
|
+
}): Promise<{
|
|
159
|
+
tx: Transaction;
|
|
160
|
+
}>;
|
|
161
|
+
/**
|
|
162
|
+
* Retrieves the delegator's staking information for a specific validator
|
|
163
|
+
*
|
|
164
|
+
* @param params - Parameters for the query
|
|
165
|
+
* @param params.delegatorAddress - Ethereum address of the delegator
|
|
166
|
+
* @param params.validatorShareAddress - The validator's ValidatorShare contract address
|
|
167
|
+
*
|
|
168
|
+
* @returns Promise resolving to stake information:
|
|
169
|
+
* - balance: Total staked amount formatted in POL
|
|
170
|
+
* - shares: Total shares held by the delegator
|
|
171
|
+
* - exchangeRate: Current exchange rate between shares and POL
|
|
172
|
+
*/
|
|
173
|
+
getStake(params: {
|
|
174
|
+
delegatorAddress: Address;
|
|
175
|
+
validatorShareAddress: Address;
|
|
176
|
+
}): Promise<StakeInfo>;
|
|
177
|
+
/**
|
|
178
|
+
* Retrieves the latest unbond nonce for a delegator
|
|
179
|
+
*
|
|
180
|
+
* Each unstake operation creates a new unbond request with an incrementing nonce.
|
|
181
|
+
* Nonces start at 1 and increment with each unstake.
|
|
182
|
+
* Note: a nonce having existed does not mean it is still pending —
|
|
183
|
+
* claimed unbonds are deleted, but the counter is never decremented.
|
|
184
|
+
*
|
|
185
|
+
* @param params - Parameters for the query
|
|
186
|
+
* @param params.delegatorAddress - Ethereum address of the delegator
|
|
187
|
+
* @param params.validatorShareAddress - The validator's ValidatorShare contract address
|
|
188
|
+
*
|
|
189
|
+
* @returns Promise resolving to the latest unbond nonce (0n if no unstakes performed)
|
|
190
|
+
*/
|
|
191
|
+
getUnbondNonce(params: {
|
|
192
|
+
delegatorAddress: Address;
|
|
193
|
+
validatorShareAddress: Address;
|
|
194
|
+
}): Promise<bigint>;
|
|
195
|
+
/**
|
|
196
|
+
* Retrieves unbond request information for a specific nonce
|
|
197
|
+
*
|
|
198
|
+
* Use this to check the status of individual unbond requests.
|
|
199
|
+
* For fetching multiple unbonds efficiently, use getUnbonds() instead.
|
|
200
|
+
*
|
|
201
|
+
* @param params - Parameters for the query
|
|
202
|
+
* @param params.delegatorAddress - Ethereum address of the delegator
|
|
203
|
+
* @param params.validatorShareAddress - The validator's ValidatorShare contract address
|
|
204
|
+
* @param params.unbondNonce - The specific unbond nonce to query (1, 2, 3, etc.)
|
|
205
|
+
*
|
|
206
|
+
* @returns Promise resolving to unbond information:
|
|
207
|
+
* - amount: Amount pending unbonding in POL
|
|
208
|
+
* - isWithdrawable: Whether the unbond can be withdrawn now
|
|
209
|
+
* - shares: Shares amount pending unbonding (0n if already withdrawn or doesn't exist)
|
|
210
|
+
* - withdrawEpoch: Epoch number when the unbond started
|
|
211
|
+
*/
|
|
212
|
+
getUnbond(params: {
|
|
213
|
+
delegatorAddress: Address;
|
|
214
|
+
validatorShareAddress: Address;
|
|
215
|
+
unbondNonce: bigint;
|
|
216
|
+
}): Promise<UnbondInfo>;
|
|
217
|
+
/**
|
|
218
|
+
* Retrieves unbond request information for multiple nonces efficiently
|
|
219
|
+
*
|
|
220
|
+
* This method batches all contract reads into a single RPC call, making it
|
|
221
|
+
* much more efficient than calling getUnbond() multiple times.
|
|
222
|
+
*
|
|
223
|
+
* @param params - Parameters for the query
|
|
224
|
+
* @param params.delegatorAddress - Ethereum address of the delegator
|
|
225
|
+
* @param params.validatorShareAddress - The validator's ValidatorShare contract address
|
|
226
|
+
* @param params.unbondNonces - Array of unbond nonces to query (1, 2, 3, etc.)
|
|
227
|
+
*
|
|
228
|
+
* @returns Promise resolving to array of unbond information (same order as input nonces)
|
|
229
|
+
*/
|
|
230
|
+
getUnbonds(params: {
|
|
231
|
+
delegatorAddress: Address;
|
|
232
|
+
validatorShareAddress: Address;
|
|
233
|
+
unbondNonces: bigint[];
|
|
234
|
+
}): Promise<UnbondInfo[]>;
|
|
235
|
+
/**
|
|
236
|
+
* Retrieves pending liquid rewards for a delegator
|
|
237
|
+
*
|
|
238
|
+
* @param params - Parameters for the query
|
|
239
|
+
* @param params.delegatorAddress - Ethereum address of the delegator
|
|
240
|
+
* @param params.validatorShareAddress - The validator's ValidatorShare contract address
|
|
241
|
+
*
|
|
242
|
+
* @returns Promise resolving to the pending rewards in POL
|
|
243
|
+
*/
|
|
244
|
+
getLiquidRewards(params: {
|
|
245
|
+
delegatorAddress: Address;
|
|
246
|
+
validatorShareAddress: Address;
|
|
247
|
+
}): Promise<string>;
|
|
248
|
+
/**
|
|
249
|
+
* Retrieves the current POL allowance for the StakeManager contract
|
|
250
|
+
*
|
|
251
|
+
* @param ownerAddress - The token owner's address
|
|
252
|
+
*
|
|
253
|
+
* @returns Promise resolving to the current allowance in POL
|
|
254
|
+
*/
|
|
255
|
+
getAllowance(ownerAddress: Address): Promise<string>;
|
|
256
|
+
/**
|
|
257
|
+
* Retrieves the current checkpoint epoch from the StakeManager
|
|
258
|
+
*
|
|
259
|
+
* @returns Promise resolving to the current epoch number
|
|
260
|
+
*/
|
|
261
|
+
getEpoch(): Promise<bigint>;
|
|
262
|
+
/**
|
|
263
|
+
* Retrieves the withdrawal delay from the StakeManager
|
|
264
|
+
*
|
|
265
|
+
* The withdrawal delay is the number of epochs that must pass after an unbond
|
|
266
|
+
* request before the funds can be withdrawn (~80 checkpoints, approximately 3-4 days).
|
|
267
|
+
*
|
|
268
|
+
* @returns Promise resolving to the withdrawal delay in epochs
|
|
269
|
+
*/
|
|
270
|
+
getWithdrawalDelay(): Promise<bigint>;
|
|
271
|
+
/**
|
|
272
|
+
* Retrieves the exchange rate precision for a validator
|
|
273
|
+
*
|
|
274
|
+
* Foundation validators (ID < 8) use precision of 100, others use 10^29.
|
|
275
|
+
*
|
|
276
|
+
* @param validatorShareAddress - The validator's ValidatorShare contract address
|
|
277
|
+
*
|
|
278
|
+
* @returns Promise resolving to the precision constant
|
|
279
|
+
*/
|
|
280
|
+
getExchangeRatePrecision(validatorShareAddress: Address): Promise<bigint>;
|
|
281
|
+
/**
|
|
282
|
+
* Retrieves the current exchange rate for a validator
|
|
283
|
+
*
|
|
284
|
+
* @param validatorShareAddress - The validator's ValidatorShare contract address
|
|
285
|
+
*
|
|
286
|
+
* @returns Promise resolving to the exchange rate
|
|
287
|
+
*/
|
|
288
|
+
private getExchangeRate;
|
|
289
|
+
/**
|
|
290
|
+
* Signs a transaction using the provided signer.
|
|
291
|
+
*
|
|
292
|
+
* @param params - Parameters for the signing process
|
|
293
|
+
* @param params.signer - A signer instance
|
|
294
|
+
* @param params.signerAddress - The address of the signer
|
|
295
|
+
* @param params.tx - The transaction to sign
|
|
296
|
+
* @param params.baseFeeMultiplier - (Optional) The multiplier for fees, which is used to manage fee fluctuations, is applied to the base fee per gas from the latest block to determine the final `maxFeePerGas`. The default value is 1.2
|
|
297
|
+
* @param params.defaultPriorityFee - (Optional) This overrides the `maxPriorityFeePerGas` estimated by the RPC
|
|
298
|
+
*
|
|
299
|
+
* @returns A promise that resolves to an object containing the signed transaction
|
|
300
|
+
*/
|
|
301
|
+
sign(params: {
|
|
302
|
+
signer: Signer;
|
|
303
|
+
signerAddress: Address;
|
|
304
|
+
tx: Transaction;
|
|
305
|
+
baseFeeMultiplier?: number;
|
|
306
|
+
defaultPriorityFee?: string;
|
|
307
|
+
}): Promise<{
|
|
308
|
+
signedTx: Hex;
|
|
309
|
+
}>;
|
|
310
|
+
/**
|
|
311
|
+
* Broadcasts a signed transaction to the network.
|
|
312
|
+
*
|
|
313
|
+
* @param params - Parameters for the broadcast process
|
|
314
|
+
* @param params.signedTx - The signed transaction to broadcast
|
|
315
|
+
*
|
|
316
|
+
* @returns A promise that resolves to the transaction hash
|
|
317
|
+
*/
|
|
318
|
+
broadcast(params: {
|
|
319
|
+
signedTx: Hex;
|
|
320
|
+
}): Promise<{
|
|
321
|
+
txHash: Hex;
|
|
322
|
+
}>;
|
|
323
|
+
/**
|
|
324
|
+
* Retrieves the status of a transaction using the transaction hash.
|
|
325
|
+
*
|
|
326
|
+
* @param params - Parameters for the transaction status request
|
|
327
|
+
* @param params.txHash - The transaction hash to query
|
|
328
|
+
*
|
|
329
|
+
* @returns A promise that resolves to an object containing the transaction status
|
|
330
|
+
*/
|
|
331
|
+
getTxStatus(params: {
|
|
332
|
+
txHash: Hex;
|
|
333
|
+
}): Promise<PolygonTxStatus>;
|
|
334
|
+
private parseAmount;
|
|
335
|
+
}
|