Skip to content

Add transferStakeFrom and approve functions to staking precompile#2478

Open
jeremyp-tao wants to merge 6 commits intoopentensor:devnet-readyfrom
jeremyp-tao:jeremy-staking-precompile-approvals
Open

Add transferStakeFrom and approve functions to staking precompile#2478
jeremyp-tao wants to merge 6 commits intoopentensor:devnet-readyfrom
jeremyp-tao:jeremy-staking-precompile-approvals

Conversation

@jeremyp-tao
Copy link

Description

Adds new functions to the staking precompile similar to ERC20 transferFrom and approve, which will allow smart contracts to atomically receive alpha tokens and react to the transfer, which is not possible today (if an user transfers alpha token to a smart contract, the latter is not aware of this transfer). Allowance is managed per netuid for finer control.

Related Issue(s)

None

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Other (please describe):

Breaking Change

None

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

Screenshots (if applicable)

Not applicable

Additional Notes

None

@open-junius open-junius self-requested a review March 3, 2026 00:39
}

#[precompile::public("increaseAllowance(bytes32,uint256,uint256)")]
fn increase_allowance(
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use approve interface to do increase/decrease allowance, and they are not in the standard ERC20 definition.

Copy link
Author

Choose a reason for hiding this comment

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

Contracts can easily get the current allowance and increase it atomically (they must check for re-entrancy attacks), however users cannot do that. That's why those functions are very common in tokens implementing ERC20.

@open-junius
Copy link
Contributor

It is better to implement both function in an Alpha wrapper contract, instead of in precompile. @ppolewicz @camfairchild any update for ERC20-alpha-wrapper

Comment on lines +63 to +66
<R as frame_system::Config>::AccountId,
// For each pair of (spender, netuid)
Blake2_128Concat,
(<R as frame_system::Config>::AccountId, u16),
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens on subnet de-reg?

Copy link
Author

@jeremyp-tao jeremyp-tao Mar 25, 2026

Choose a reason for hiding this comment

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

I'm not sure a de-reg would cause any issue. There approve would still be here but the user can update the allowance at anytime, and is suppose to trust/verify that the contract will do appropriate things with the approved funds. So I think it is not worth it to complicate the design.

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