Skip to content

Feat/recycle burn chain exts#2560

Open
LandynDev wants to merge 6 commits intoopentensor:devnet-readyfrom
LandynDev:feat/recycle-burn-chain-exts
Open

Feat/recycle burn chain exts#2560
LandynDev wants to merge 6 commits intoopentensor:devnet-readyfrom
LandynDev:feat/recycle-burn-chain-exts

Conversation

@LandynDev
Copy link
Copy Markdown

@LandynDev LandynDev commented Apr 2, 2026

Description

Add 4 new chain extension functions (IDs 16-19) enabling WASM smart contracts to recycle and burn alpha stake.

  • RecycleAlphaV1 (16): Recycle alpha stake
  • BurnAlphaV1 (17): Burn alpha stake
  • AddStakeRecycleV1 (18): Atomically add TAO stake then recycle
  • AddStakeBurnV1 (19): Atomically add TAO stake then burn

All functions return the actual alpha amount via the output buffer.

Related Issue(s)

Closes #2564

Type of Change

  • New feature (non-breaking change which adds functionality)

Breaking Change

No breaking changes. Existing chain extension functions (IDs 0-15) are unaffected. New function IDs 16-19 are additive.

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have run ./scripts/fix_rust.sh to ensure my code is formatted and linted correctly
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Additional Notes

7 unit tests cover: success paths for all 4 functions, root subnet rejection, nonexistent subnet, and insufficient balance. E2E validated on localnet with an ink! contract calling add_stake_recycle via chain extension.

I also ran some local E2E testing with a local running subtensor chain (with the new extensions), deployed contract, etc.

Screenshot 1 shows:

  • Accumulated fees of 0.0034 TAO on the contract (to be recycled)
  • Total recycled fees of 0.0000 TAO on the contract
  • Contract Balance of 5.0000 TAO
image

Screenshot 2 shows:

  • Recycle of 0.0034 TAO succeeded (CLI -> Smart contract func -> New add stake + recycle chain ext)
  • Accumulated fees of 0.0000 TAO on the contract (it got recycled)
  • Total recycled fees of 0.0034 TAO on the contract
  • Contract Balance of 4.9966 TAO
image

Unit tests successfully passing:
image

@open-junius
Copy link
Copy Markdown
Contributor

Thanks for the contribution. Can you add the ticket for the requirement? I am wondering the extension like AddStakeRecycleV1, who really want to do it. just confirm all is from the real requirements.

@LandynDev
Copy link
Copy Markdown
Author

Thanks for the contribution. Can you add the ticket for the requirement? I am wondering the extension like AddStakeRecycleV1, who really want to do it. just confirm all is from the real requirements.

Just got it ticketed up here - #2564

Particular snippet from the issue to elaborate on the AddStakeBurn + AddStakeRecycle funcs and why those 2 are included:

The current AddStakeV1 chain extension only returns a success/error code, not the resulting alpha amount. While a contract could work around this by calling AddStakeV1 followed by RecycleAlphaV1(MAX), this recycles all alpha the contract holds on that subnet — not just the alpha from that specific stake. This is fine for simple contracts, but may break down for contracts that hold alpha for multiple purposes on the same subnet.

The AddStakeRecycleV1 and AddStakeBurnV1 functions handle this atomically in a single call, recycling/burning only the alpha produced by that specific stake. This provides the robustness needed for a general-purpose chain extension interface.

@LandynDev LandynDev marked this pull request as ready for review April 3, 2026 16:11
@open-junius
Copy link
Copy Markdown
Contributor

Looks good to me, please fix the 'Cargo fmt'

open-junius
open-junius previously approved these changes Apr 7, 2026
@LandynDev
Copy link
Copy Markdown
Author

Looks good to me, please fix the 'Cargo fmt'

👍
it dismissed your review bc of the repush, but should be good to go now

@open-junius open-junius added the skip-cargo-audit This PR fails cargo audit but needs to be merged anyway label Apr 7, 2026
Strip development log::info!/log::error! calls from dispatch entry
and AddStakeRecycleV1 handler. Normalize AddStakeRecycleV1 to use
the same concise ? pattern as all other handlers.
@LandynDev LandynDev force-pushed the feat/recycle-burn-chain-exts branch from e527c74 to 1c4f180 Compare April 7, 2026 02:01
@LandynDev
Copy link
Copy Markdown
Author

it also needed devnet-ready rebase, got outdated but thats not situated

open-junius
open-junius previously approved these changes Apr 7, 2026
Copy link
Copy Markdown
Contributor

@open-junius open-junius left a comment

Choose a reason for hiding this comment

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

Looks good for me.

@LandynDev
Copy link
Copy Markdown
Author

LandynDev commented Apr 7, 2026

Addressed the review feedback:

  • Removed redundant .min() clamping in RecycleAlphaV1/BurnAlphaV1. Pallet handles this internally
  • Wrapped AddStakeRecycleV1/AddStakeBurnV1 in with_transaction for atomicity. If recycle/burn fails, stake is fully rolled back
  • Added 2 rollback tests (add_stake_recycle_rollback_on_recycle_failure, add_stake_burn_rollback_on_burn_failure)
image

@evgeny-s / @open-junius if you could give this a re-look when you have a chance, thanks

Commit of changes: 65357d3


match call_result {
Ok(_) => {
env.write_output(&amount.encode())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I can see you remove the real_amount, and put the amount into result directly. but it could be wrong if the amount is more than you staked. So I suggest we can update the recycle_alpha function with the real recycled alpha as result, or doesn't return any value in RecycleAlphaV1. and the same for burn function.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good call, I got it updated w this commit - 60dffb3

Trying to keep the diff concise with what i add/change. Reran unit tests and passing

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

Labels

skip-cargo-audit This PR fails cargo audit but needs to be merged anyway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants