Skip to content

FlowStakingCollection: withdraw…AsVault variants (return the vault instead of depositing it) - #622

Open
Kay-Zee wants to merge 1 commit into
masterfrom
feat/staking-collection-withdraw-returns-vault
Open

Kay-Zee wants to merge 1 commit into
masterfrom
feat/staking-collection-withdraw-returns-vault

Conversation

@Kay-Zee

@Kay-Zee Kay-Zee commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Why

withdrawUnstakedTokens / withdrawRewardedTokens return Void — they depositTokens the withdrawn FLOW into the collection's own Cadence vault:

access(CollectionOwner) fun withdrawUnstakedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
    let tokens <- node.withdrawUnstakedTokens(amount: amount)
    self.depositTokens(from: <-tokens)      // returns nothing
}

The underlying FlowIDTableStaking.NodeStaker.withdrawUnstakedTokens already returns a vault. So a caller that must route the withdrawn funds somewhere other than the collection's Cadence vault — e.g. bridging them to an EVM account — has no way to receive them. This came up building EVM-side staking control, where a registered EVM EOA takes custody of withdrawn funds and the wrapper must bridge the vault to that address.

What

Two additive functions that return the vault to the caller instead — both halves of the pair, plus both transaction templates:

access(CollectionOwner) fun withdrawUnstakedTokensAsVault(nodeID: String, delegatorID: UInt32?, amount: UFix64): @{FungibleToken.Vault}
access(CollectionOwner) fun withdrawRewardedTokensAsVault(nodeID: String, delegatorID: UInt32?, amount: UFix64): @{FungibleToken.Vault}

They mirror the existing functions' bookkeeping — unlockedTokensUsed, including the same cannot return more FLOW than is in use for staking guard that depositTokens has (without it, withdrawing unstaked tokens that originated as rewards underflows unlockedTokensUsed).

Restricted to collections without a locked account. For a locked-token collection the withdrawn funds belong back in the locked account, so returning a vault to an arbitrary caller would bypass the lock (depositTokens is what splits locked vs unlocked). Locked collections keep using the existing functions. Enforced by a precondition with a clear message.

Compatibility

Additive only — no new fields, so no storage migration, and existing collections in user storage are unaffected. Ships as a flow-core-contracts release plus a service-account contract update.

Tests

  • New subtest in TestStakingCollectionStakeTokens: both AsVault variants revert on josh's locked-backed collection (asserts the guard). Passes.
  • The full TestStakingCollection* suite passes, and the regenerated Go assets are verified to embed both functions and the guards.
  • The success path (an AsVault call on an unlocked collection with unstaked tokens, asserting the balance delta) still needs an unlocked-collection-with-node fixture — the existing fixtures are locked-token based (registerStakingCollectionNodesAndDelegators). Happy to add it, or to change the naming/semantics if you'd prefer a different shape.

Also adds both transaction templates (SCO.18/SCO.19, appended after SCO.17 so no existing id moves) and regenerates the assets/manifests.

The collection's withdrawUnstakedTokens / withdrawRewardedTokens return Void —
they deposit the withdrawn tokens into the collection's own Cadence vault. A
caller that must route the funds elsewhere (e.g. bridging them to an EVM
account, as Flow's EVM-side staking control needs) has no way to receive them.

This adds withdrawUnstakedTokensAsVault / withdrawRewardedTokensAsVault, which
return the vault to the caller instead. They mirror the existing functions'
bookkeeping — unlockedTokensUsed, including the same 'cannot return more than
is in use for staking' guard that depositTokens has — and are restricted to
collections WITHOUT a locked account: for a locked-token collection the
withdrawn funds belong back in the locked account, so returning a vault to an
arbitrary caller would bypass the lock. Locked collections keep using the
existing functions.

Additive only: no new fields, so no storage migration — existing collections in
user storage are unaffected. Ships as a flow-core-contracts release plus a
service-account contract update.

Adds both transaction templates (SCO.18/SCO.19, appended after SCO.17),
regenerates the Go assets/manifests, and adds a test asserting both variants
revert on a collection with a locked account.
@Kay-Zee
Kay-Zee force-pushed the feat/staking-collection-withdraw-returns-vault branch from 02917df to ad1e6b9 Compare November 24, 2026 22:28
})
})

t.Run("Should not be able to withdraw AsVault from a collection with a locked account", func(t *testing.T) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth adding a happy-path test for the new …AsVault functions?

let delegator = self.borrowDelegator(nodeID: nodeID, delegatorID: delegatorID)
?? panic("FlowStakingCollection.StakingCollection.withdrawUnstakedTokensAsVault: delegator does not exist in the collection")
let tokens <- delegator.withdrawUnstakedTokens(amount: amount)
assert(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the bookkeeping here needed? With lockedVault == nil, lockedTokensUsed is always 0 and the rewards add/sub nets out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants