@anywalletkit/protocol-staking-native-near
@anywalletkit/protocol-staking-native-near
Section titled “@anywalletkit/protocol-staking-native-near”Native NEAR staking for any NEAR account: deposit_and_stake into the staking pools your app
allows, unstake with the 4-epoch unbonding (about 21–28 h at ~6.9 h per epoch), withdraw_all
once unlocked. No extra contract, no token: the position is the account’s balance in each pool.
npm install @anywalletkit/protocol-staking-native-near @anywalletkit/wallet-near @anywalletkit/kitimport { Wallet } from '@anywalletkit/kit'import { WalletManagerNear } from '@anywalletkit/wallet-near'import { NativeStakingProtocolNear } from '@anywalletkit/protocol-staking-native-near'
const wallet = new Wallet(seedPhrase)wallet.registerWallet('near', WalletManagerNear, { provider: 'https://free.rpc.fastnear.com' })const account = await wallet.getAccount('near')
const staking = new NativeStakingProtocolNear(account, { pools: ['astro-stakers.poolv1.near'] }) // or 'any'
const quote = await staking.quoteStake({ amount: 10n ** 24n }) // 1 NEAR// quote.transactions is exactly what stake() sends; quote.pool carries the live checks, quote.apr the APRawait staking.stake({ amount: 10n ** 24n, expected: quote.transactions }) // re-checks, refuses if the plan changed
const unstake = await staking.quoteRequestWithdrawal({ amount: 'all' }) // unstake_all ([withdraw_all, unstake_all] if unlocked NEAR waits)await staking.requestWithdrawal({ amount: 'all', expected: unstake.transactions })// … 4 epochs later (unstake.unlock.earliestAt … latestAt; unlock is null when the epoch could not be read) …await staking.claimWithdrawals() // withdraw_all per poolAmounts are yoctoNEAR (bigint). Reads use provider, else the wallet-near account’s own RPC.
External signers (wallet-selector and the like)
Section titled “External signers (wallet-selector and the like)”The module touches only account.chain, account.getAddress() and, for writes,
account.sendTransaction(tx) with a NearTransaction of function-call actions ({ receiverId, actions }, no signer, nonce or block hash). Balances, fees and contract state come from the module’s
own RPC, so an adapter over a browser wallet is small: map each functionCall to the wallet’s action
type, sign and send, and return { hash, fee }. An account that also has
sendTransactions(txs) (one approval for several transactions) gets multi-pool claims in one prompt.
Pass provider: such accounts have no RPC of their own.
A watch-only object with chain and getAddress() is enough for every read (positions, requests,
APR). Writes on it throw ReadOnlyAccountError.
Config
Section titled “Config”| Option | Default | |
|---|---|---|
pools |
— (required) | Pool ids ('x.poolv1.near' or { id, name }), or 'any' to let users pick from the validator set. |
defaultPool |
the only listed pool | Used when a call names no pool. |
maxFee |
10 |
Percent. Pools above it are refused for new stake. Read fresh at every stake; compared in integers. |
allowInactive |
false |
Stake into pools outside the current or next validator set (that stake earns nothing). |
minUptime |
0 (off) |
Refuse pools whose production this epoch is below it, percent. |
codeHashes |
— | Extra staking-pool code hashes to accept on top of the pinned builds. A *.pool.near pool on such a build still gets the scheduled-fee check (Safety, 5). |
provider / timeoutMs |
the account’s RPC / 15 s | URL, transport, or a failover list. |
network |
the account’s | 'mainnet' / 'testnet'. The account must be on near:<network>. |
reserve |
0.02 NEAR | Left on the account by stake; never less than the upfront gas of the largest exit, [withdraw_all, unstake] in one transaction (~0.11 NEAR today, see Fees). |
gas |
50 Tgas per call | Prepaid gas. It is all bought upfront at min_gas_purchase_price; the unburnt part is refunded. |
discover |
'fastnear' |
How positions outside pools are found: fastnear’s indexer (/v1/account/{id}/staking), your own (accountId) => poolIds, or false. A failed lookup makes listPositions throw (never “no stake”); balances, requests and claims go on with the listed, passed and touched pools and say partial: true. |
history |
— | A HistorySource (e.g. wallet-near’s createNearHistory): dates the last unstake for an exact unlock epoch. |
enrich |
— | (ids) => ({ [id]: { apr, name, logo, … } }). Decoration only; a failing enricher is ignored. |
concurrency |
4 |
Reads in flight. |
Methods
Section titled “Methods”Shared StakingProtocol |
On NEAR |
|---|---|
stake({ amount, pool?, expected? }) |
deposit_and_stake() with exactly amount attached. There is no “stake everything”. |
requestWithdrawal({ amount | 'all', pool?, allowTimerReset?, expected? }) |
unstake({ amount }) / unstake_all(); [withdraw_all, unstake…] in one transaction when an unlocked balance waits |
getWithdrawalRequests({ pools? }) |
one request per pool holding an unstaked balance; id = poolRequestId(pool) |
claimWithdrawals({ ids?, pools?, expected? }) |
withdraw_all() per pool, one approval with sendTransactions |
getStakedBalance() |
balance = staked, total = staked + unstaked, plus unstaked, withdrawable, positions |
getApr({ pool? }) |
the pool’s (else the default pool’s) delegator APR; without a pool, the network average after a 10 % fee |
NEAR extras: getEpoch(), getNetworkStats(), listPools() (one validators call: factory
pools by stake, light info), getPool(id) (live checks, never cached), getPools(ids) (live checks,
cached 5 min for display), listPositions({ pools? }), getMaxStakeable(). Every write has a
quote* twin that signs nothing and returns the exact transactions, fee, upfront (see Fees)
and warnings.
Every write takes expected: the transactions the user confirmed. The write plans again with fresh
reads and refuses with quote-changed (and the new details.transactions) when anything differs, for
example when an unstaked balance unlocked between the quote and the click.
Refusals are NearStakingError with a code: wrong-chain, no-provider, invalid-amount,
below-minimum (a stake under 0.001 NEAR, or a partial unstake that would leave less than that
staked or unstaked),
pool-required, pool-not-allowed (+ refusals), exceeds-staked, timer-reset-refused,
claim-first (claim the unlocked balance, then unstake: see Fees), max-fee-exceeded, nothing-staked, not-withdrawable (+ details.unlock),
nothing-to-claim (+ details.soonest), unknown-request, quote-changed, invalid-config. A
write that does not fit the balance throws core’s InsufficientFundsError.
NEAR buys a transaction’s whole prepaid gas when it accepts it, at
max(gas price, min_gas_purchase_price): 10^9 yoctoNEAR per gas on mainnet since protocol 88, ten
times the 10^8 gas price. The unburnt part comes back a few blocks later. So a 50 Tgas unstake needs
about 0.055 NEAR on the account even though it finally costs about 0.0005 NEAR. Quotes carry both:
upfront: what the node holds back besides the deposit (Σ gas × the purchase price). Every balance check uses it:stakeneedsamount + upfront + reserve ≤the spendable balance (the liquid balance minus the storage cost itslockedbalance does not cover), unstakes and claims needupfront.fee: the upper bound of what is finally paid (Σ gas × gas price + 10 %, the margin wallet-near’s estimate uses), somaxFee: quote.feepasses on a wallet-near account. The module checksmaxFeeitself too (max-fee-exceeded), because wallet-selector adapters ignore it.
min_gas_purchase_price is read from EXPERIMENTAL_protocol_config (cached 10 min); a node that
cannot answer gets the mainnet value. stake leaves at least the upfront gas of the largest exit on
the account (quote.reserve): an unstake that withdraws an unlocked balance first, two 50 Tgas
calls in one transaction (~0.11 NEAR today), so a stake can always be left without a top-up. When
the balance no longer covers both calls (it was spent, or gas got dearer) but covers the
withdraw_all alone, such an unstake is refused with claim-first (details.id is the request to
claim): claimWithdrawals takes the unlocked NEAR out (and back to the balance), then the unstake
needs one call’s gas only. The module
never falls back to the unstake alone, which would lock the unlocked balance for 4 more epochs.
Timing
Section titled “Timing”- An epoch is 43 200 block heights, about 6.9 h on mainnet (measured from the chain at every
getEpoch; 12 h is wrong). - An unstake unlocks at epoch current + 4: about 21–28 h, depending on where in the epoch it
lands. Quotes carry
unlockwithearliestEpoch(current + 4),latestEpoch(current + 5, in case the transaction lands after the epoch ends) and their dates. - Every new unstake restarts the 4-epoch timer for the account’s whole unstaked balance in that
pool. Such an unstake is refused (
timer-reset-refused) unless the call passesallowTimerReset: true; the quote then says so (resetsTimer, warningresets-unbonding-timer). An amount below 0.001 NEAR next to a balance still unbonding is refused even then, and dust stake never makes a pool “the only pool with stake”. When the unstaked balance is already unlocked, the unstake withdraws it first in the same transaction (withdrawsFirst, warningwithdraws-unlocked-first), so it is never locked again. - The pool exposes only
can_withdraw, not the unlock epoch. Positions in unbonding carry an estimate (current + 1 … current + 4), or, withhistory, the exact epoch: the last unstake’s block, its epoch, + 4 (source: 'history'). An unstake in the last 3 heights of an epoch may have run in the next one (the pool’s receipt runs a block or more after the transaction), so it is dated as a two-epoch range. If the chain says it is still locked after that epoch, the module trusts the chain and falls back to the estimate. - Pools leave 1–10 yoctoNEAR of share-rounding dust in
unstaked(most delegators have some). Balances below 0.001 NEAR (UNSTAKED_DUST) are not requests, not claims and never trigger the withdraw-first batch. So a partial unstake that would leave less than that unstaked in the pool, or less than that staked, is refused (below-minimum; unstake'all'instead). Unstaking a whole stake below 0.001 NEAR (made outside the module) still goes out, with warningleaves-dust: what it leaves unstaked is below the claim minimum, and it is the only way to leave that stake.
From the protocol’s own data: validator_reward_paid_prev_epoch ÷ the pool’s stake, annualized by
the measured epoch length (ratio in bigint fixed point), times (1 − the pool’s fee). A pool with no
reward entry yet (new in the set) gets the network average after its fee, with warning
apr-network-fallback. The network average is Σ rewards ÷ Σ stake over the current validators,
treasury.near excluded (it takes the protocol’s ~10 % share). On 2026-11-08: network 5.03 % gross,
astro-stakers (1 % fee) 5.01 %, figment (10 %) 4.53 %. enrich overrides it.
Safety
Section titled “Safety”- Pool allow-list, read fresh for every stake:
poolslists it (or is'any');- its id has a staking-pool factory suffix (
*.poolv1.near,*.pool.near; testnet*.pool.f863973.m0), with one name segment before it. This is local: no RPC answer can change it, and other ids get no RPC call at all.lst-pool.near(in the validator set, its own code, nois_staking_paused) andmeta-pool.nearare refused here; - the account exists and runs one of the pinned staking-pool builds (
KNOWN_POOL_CODE_HASHES: the four builds every factory pool in the current and next sets ran on 2026-11-08) or one incodeHashes. Unknown code is never called bygetPool; is_staking_pausedanswersfalse(a missing method, an error or a non-boolean ispause-unknown, never “not paused”);- the fee is well-formed and at most
maxFee:get_reward_fee_fractionfor core-contracts pools (*.poolv1.near); for the staking-farm builds of*.pool.near(every*.pool.nearpool, builds added throughcodeHashesincluded),get_pool_summary, and the higher of the current fee and the one already scheduled for the next epoch (nextFee); - it is in the current and next validator sets (unless
allowInactive), withminUptimeif set.
- Exits are always possible from a known pool. Unstake and claim check only 2 and 3, so users can
leave a pool that was paused, raised its fee (an owner can raise it to 100 % at any time; positions
warn with
pool-fee-above-max) or left the set. A*.pool.nearpool that upgraded itself to code thepool.nearfactory allows (is_contract_allowed) can still be left. The validator set and the epoch only decorate exits: when those reads fail, unstakes and claims still go out (the unstake’sunlockis null, positions carrypoolInfo: nulland no unlock estimate).withdrawpays the caller only. - The module builds function calls on the checked pool only:
deposit_and_stake,unstake,unstake_all,withdraw_all. Never transfers, keys,Stakeactions or token calls. stakesends exactlyamountand requiresamount + upfront + reserve ≤the spendable balance (amount − max(0, storage − locked)ofview_account:lockedis not part ofamountand pays for storage first).- Policies: the transactions go to the pool account; an
allowlistmust include it.
Activity
Section titled “Activity”describeActivity recognizes the wallet’s own calls on factory pools: deposit_and_stake /
deposit / stake* (stake), unstake* (unstake; “Withdrew and requested unstake” with
withdraw_all in the same transaction), withdraw* (claim, with the NEAR the pool sent back from
the receipts, its log, or the indexer’s transfer). ping, owner methods, other contracts and other
signers return null.
Testing
Section titled “Testing”pnpm test: an in-memory node seeded with answers recorded on mainnet (fixtures/rpc/mainnet.json); every allow-list refusal, including a lying RPC (a foreign validator with a 1 % fee, a fake code hash, a missing or failing pause method, malformed fees, a scheduled fee); stake amount math against the reserve and the upfront gas (the audit’s fresh-account delivery); unstake batches and timer resets; claims with and withoutsendTransactions;expectedmismatches; epochs with skipped and pruned heights; APR in bigint; conformance with recorded mainnet transactions and negatives.pnpm verify:live [rpcUrl]: view calls only. Epoch, validator set and APRs, verdicts on real pools and foreign contracts, then positions and every write planned as real delegators found withget_accounts(one in unbonding, one with an unlocked balance next to stake).NEAR_HISTORY=1also dates an unlock through nearblocks. Nothing is signed.node scripts/capture-fixtures.mjs: recaptures the activity fixtures and the recorded RPC answers.
Classes
Section titled “Classes”Interfaces
Section titled “Interfaces”- GasCost
- GasPrices
- NativeStakingNearConfig
- NearClaimOptions
- NearClaimQuote
- NearClaimResult
- NearEpoch
- NearNetworkStats
- NearPoolInfo
- NearPoolPosition
- NearStakedBalance
- NearStakeOptions
- NearStakeQuote
- NearStakeResult
- NearStakingWarning
- NearTransactionsSender
- NearUnstakeOptions
- NearUnstakeQuote
- NearUnstakeResult
- NearWithdrawalRequest
- NearWriteOptions
- PoolConfig
- PoolExtra
- ReadEpochOptions
- UnlockEstimate
- ValidatorEntry
- ValidatorSet
Type Aliases
Section titled “Type Aliases”- NearNetwork
- NearStakingErrorCode
- NearStakingWarningCode
- NearWithdrawalRequestsView
- NormalizedPools
- PoolDiscovery
- PoolEnricher
- PoolFactory
- PoolRefusal
- PoolsOption
- PositionWarning
Variables
Section titled “Variables”- DEFAULT_CONCURRENCY
- DEFAULT_GAS
- DEFAULT_MAX_FEE
- DEFAULT_RESERVE
- DISPLAY_TTL_MS
- EPOCH_LENGTH
- FASTNEAR_API
- GAS_PRICE_MARGIN_DEN
- GAS_PRICE_MARGIN_NUM
- KNOWN_POOL_CODE_HASHES
- MIN_GAS_PURCHASE_PRICE_FALLBACK
- MIN_STAKE
- MODULE_NAME
- NEAR_CHAINS
- NEAR_MAINNET
- NEAR_TESTNET
- NUM_EPOCHS_TO_UNLOCK
- POOL_FACTORY_SUFFIXES
- STAKING_FARM_CODE_HASHES
- TGAS
- TREASURY_ACCOUNT
- TYPICAL_EPOCH_MS
- UNSTAKED_DUST
- UPGRADING_FACTORIES
- YOCTO_PER_NEAR
Functions
Section titled “Functions”- annualize
- canSendBatch
- costOf
- describeNativeNearStakingActivity
- epochOfHeight
- epochsPerYear
- epochStartsAt
- factoryOf
- gasOf
- networkGrossApr
- normalizePools
- parseValidators
- poolGrossApr
- poolRequestId
- readEpoch
- resolveRpc
- sameTransactions
References
Section titled “References”default
Section titled “default”Renames and re-exports NativeStakingProtocolNear