-
Notifications
You must be signed in to change notification settings - Fork 20
Add eHMND ERC20 precompile #746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
71 commits
Select commit
Hold shift + click to select a range
20f7188
Add basic implementation for total_supply and balance_of calls
dmitrylavrenov 95481c1
Integrate initial precompile-evm-balances-erc20 implementation into h…
dmitrylavrenov abd9a18
Add metadata erc20 methods
dmitrylavrenov a24707d
Add approvals related logic
dmitrylavrenov 5d3fa58
Add transfer related method
dmitrylavrenov d53e157
Add transferFrom related method
dmitrylavrenov 34325d7
Add event logs
dmitrylavrenov 440d8d0
Add docs to interface actions
dmitrylavrenov 448bd57
Fix some clippy
dmitrylavrenov 7c1e371
Add docs for approves logic
dmitrylavrenov b06c562
Fix typos
dmitrylavrenov 5faec0f
Remove unnecesary docs
dmitrylavrenov 64a9610
Introduce helper functions to simplify code
dmitrylavrenov 70d0b08
Some refactoring
dmitrylavrenov 5f81c84
Update features snapshot
dmitrylavrenov 0d5b4d6
Fix docs
dmitrylavrenov 41be202
Add ERC20 solidity interface
dmitrylavrenov 6785a6e
Rename erc20 metadata at frontier precompiles
dmitrylavrenov c055a1a
Add mocked environment
dmitrylavrenov 68de669
Add metadata related tests
dmitrylavrenov c7c1c34
Add balance_of_works and total_supply_works tests
dmitrylavrenov 115acc3
Add approve_works test
dmitrylavrenov 88b2356
Rename some tests
dmitrylavrenov 8147024
Add transfer_works test
dmitrylavrenov e6a33ea
Add transfer_from_works test
dmitrylavrenov 890f6a7
Rename symbol
dmitrylavrenov 2992d29
Edit name erc20 metadata
dmitrylavrenov 99d30dc
Introduce pallet-erc20 to store approvals data
dmitrylavrenov f0a31c8
Use approvals logic from pallet-erc20 at precompile
dmitrylavrenov 9736378
Use currency config instead of pallet-evm-balances config
dmitrylavrenov 350d670
Add erc20 related logic into pallet-erc20
dmitrylavrenov be70bdd
Add utility aliases
dmitrylavrenov aadf8b2
Integrate pallet-erc20 logic into precompile
dmitrylavrenov 60f43cc
Fix mock at pallet-erc20
dmitrylavrenov c7421e3
Fix mock at precompile
dmitrylavrenov 1fdbb26
Remove redundant dependency
dmitrylavrenov 4b9a3e1
Rename pallet-erc20 to pallet-token-wrapper
dmitrylavrenov 3aa99bd
Rename precompile-evm-balances-erc20 into precompile-token-wrapper
dmitrylavrenov 9e5460e
Integrate introduced changes into humanode-runtime
dmitrylavrenov 922f15f
Use wrapped-token instead for token-wrapper
dmitrylavrenov 14883e3
Merge branch 'master' into precompile-balances-erc20
dmitrylavrenov 9275197
Rename EvmBalancesErc20 into WrappedEvmBalances
dmitrylavrenov c52f822
Add total_supply_works test to pallet-wrapped-token
dmitrylavrenov 9af708f
Improve approvals related test at pallet-wrapped-token
dmitrylavrenov 03d9703
Add transfer_works test
dmitrylavrenov 2a547f0
Add transfer_from_works test
dmitrylavrenov 85a6c63
Add fails tests for transfer from logic
dmitrylavrenov 2c1e87a
Add with_storage_layer usage at pallet-wrapped-token
dmitrylavrenov 2a4ee15
Fix features
dmitrylavrenov 654dc65
Use approvals aliases
dmitrylavrenov 0f21930
Merge branch 'master' into precompile-balances-erc20
dmitrylavrenov cec5068
Rename pallet-wrapped-token into pallet-erc20-support
dmitrylavrenov 87d81ff
Merge branch 'master' into precompile-balances-erc20
dmitrylavrenov 38f68c2
Some renaming and docs improvements
dmitrylavrenov 93be731
Update features snapshot
dmitrylavrenov 0fdc379
Add comments for tests at pallet-erc20-support
dmitrylavrenov 6e9c7db
Improve dispatch error handling
dmitrylavrenov 7a6f1b5
Add more tests at precompile-erc20-support
dmitrylavrenov ad08491
Merge branch 'master' into precompile-balances-erc20
dmitrylavrenov 38966f0
Merge branch 'master' into precompile-balances-erc20
dmitrylavrenov d66cddf
Rename precompile-erc20-support into precompile-native-currency
dmitrylavrenov 78b3097
Rename Erc20EvmBalancesMetadata into EvmBalancesErc20Metadata
dmitrylavrenov 974d188
Rename Erc20EvmBalances into EvmBalancesErc20Support
dmitrylavrenov 66f9c50
Revert "Use approvals aliases"
dmitrylavrenov b293aad
Merge branch 'master' into precompile-balances-erc20
dmitrylavrenov 276d52f
Properly handle transferFrom logic
dmitrylavrenov e600842
Improve dispatch error handling
dmitrylavrenov 1354e9e
Add approve_overwrite_works test
dmitrylavrenov a7944d6
Fix transfer
dmitrylavrenov ebed3b0
Add tests to check approve logic with sending full approved balance
dmitrylavrenov 00e1d79
Add approve_approval_value_more_than_balance_works test
dmitrylavrenov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| name = "pallet-erc20-support" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| codec = { workspace = true, package = "parity-scale-codec", features = ["derive"] } | ||
| frame-support = { workspace = true } | ||
| frame-system = { workspace = true } | ||
| scale-info = { workspace = true, features = ["derive"] } | ||
|
|
||
| [dev-dependencies] | ||
| pallet-balances = { workspace = true } | ||
| sp-core = { workspace = true } | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = ["codec/std", "frame-support/std", "frame-system/std", "pallet-balances/std", "scale-info/std", "sp-core/std"] | ||
| try-runtime = ["frame-support/try-runtime", "frame-system/try-runtime", "pallet-balances/try-runtime"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| //! A substrate pallet that exposes currency instance using the ERC20 interface standard.. | ||
|
|
||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| use frame_support::{ | ||
| sp_runtime::{traits::CheckedSub, DispatchResult}, | ||
| storage::with_storage_layer, | ||
| traits::{Currency, StorageVersion}, | ||
| }; | ||
| pub use pallet::*; | ||
|
|
||
| #[cfg(test)] | ||
| mod mock; | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; | ||
|
|
||
| /// Metadata of an ERC20 token. | ||
| pub trait Metadata { | ||
| /// Returns the name of the token. | ||
| fn name() -> &'static str; | ||
|
|
||
| /// Returns the symbol of the token. | ||
| fn symbol() -> &'static str; | ||
|
|
||
| /// Returns the decimals places of the token. | ||
| fn decimals() -> u8; | ||
| } | ||
|
|
||
| /// The current storage version. | ||
| const STORAGE_VERSION: StorageVersion = StorageVersion::new(0); | ||
|
|
||
| /// Utility alias for easy access to the [`Config::AccountId`]. | ||
| type AccountIdOf<T, I> = <T as Config<I>>::AccountId; | ||
|
|
||
| /// Utility alias for easy access to the [`Currency::Balance`] of the [`Config::Currency`] type. | ||
| type BalanceOf<T, I> = <<T as Config<I>>::Currency as Currency<AccountIdOf<T, I>>>::Balance; | ||
|
|
||
| // We have to temporarily allow some clippy lints. Later on we'll send patches to substrate to | ||
| // fix them at their end. | ||
| #[allow(clippy::missing_docs_in_private_items)] | ||
| #[frame_support::pallet] | ||
| pub mod pallet { | ||
| use frame_support::{pallet_prelude::*, sp_runtime::traits::MaybeDisplay, sp_std::fmt::Debug}; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[pallet::pallet] | ||
| #[pallet::storage_version(STORAGE_VERSION)] | ||
| #[pallet::generate_store(pub(super) trait Store)] | ||
| pub struct Pallet<T, I = ()>(_); | ||
|
|
||
| /// Configuration trait of this pallet. | ||
| #[pallet::config] | ||
| pub trait Config<I: 'static = ()>: frame_system::Config { | ||
| /// The user account identifier type. | ||
| type AccountId: Parameter | ||
| + Member | ||
| + MaybeSerializeDeserialize | ||
| + Debug | ||
| + MaybeDisplay | ||
| + Ord | ||
| + MaxEncodedLen; | ||
|
|
||
| /// The currency to be exposed as ERC20 token. | ||
| type Currency: Currency<AccountIdOf<Self, I>>; | ||
|
|
||
| /// Interface into ERC20 metadata implementation. | ||
| type Metadata: Metadata; | ||
| } | ||
|
|
||
| /// ERC20-style approvals data. | ||
| /// (Owner => Allowed => Amount). | ||
| #[pallet::storage] | ||
| #[pallet::getter(fn approvals)] | ||
| pub type Approvals<T: Config<I>, I: 'static = ()> = StorageDoubleMap< | ||
| _, | ||
| Blake2_128Concat, | ||
| AccountIdOf<T, I>, | ||
| Blake2_128Concat, | ||
| AccountIdOf<T, I>, | ||
| BalanceOf<T, I>, | ||
| ValueQuery, | ||
| >; | ||
|
|
||
| /// Possible errors. | ||
| #[pallet::error] | ||
| pub enum Error<T, I = ()> { | ||
| /// Spender can't transfer tokens more than allowed. | ||
| SpendMoreThanAllowed, | ||
| } | ||
| } | ||
|
|
||
| impl<T: Config<I>, I: 'static> Pallet<T, I> { | ||
| /// Returns the amount of tokens in existence. | ||
| pub fn total_supply() -> BalanceOf<T, I> { | ||
| T::Currency::total_issuance() | ||
| } | ||
|
|
||
| /// Returns the amount of tokens owned by provided account. | ||
| pub fn balance_of(owner: &AccountIdOf<T, I>) -> BalanceOf<T, I> { | ||
| T::Currency::total_balance(owner) | ||
| } | ||
|
|
||
| /// Returns the remaining number of tokens that spender will be allowed to spend on behalf of | ||
| /// owner. This is zero by default. | ||
| pub fn allowance(owner: &AccountIdOf<T, I>, spender: &AccountIdOf<T, I>) -> BalanceOf<T, I> { | ||
| <Approvals<T, I>>::get(owner, spender) | ||
| } | ||
|
|
||
| /// Sets amount as the allowance of spender over the caller’s tokens. | ||
| pub fn approve(owner: AccountIdOf<T, I>, spender: AccountIdOf<T, I>, amount: BalanceOf<T, I>) { | ||
| <Approvals<T, I>>::insert(owner, spender, amount); | ||
MOZGIII marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// Moves amount tokens from the caller’s account to recipient. | ||
| pub fn transfer( | ||
| caller: AccountIdOf<T, I>, | ||
| recipient: AccountIdOf<T, I>, | ||
| amount: BalanceOf<T, I>, | ||
| ) -> DispatchResult { | ||
| with_storage_layer(move || { | ||
| T::Currency::transfer( | ||
| &caller, | ||
| &recipient, | ||
| amount, | ||
| frame_support::traits::ExistenceRequirement::AllowDeath, | ||
| )?; | ||
|
|
||
| Ok(()) | ||
| }) | ||
| } | ||
|
|
||
| /// Moves amount tokens from sender to recipient using the allowance mechanism, | ||
| /// amount is then deducted from the caller’s allowance. | ||
| pub fn transfer_from( | ||
| caller: AccountIdOf<T, I>, | ||
| sender: AccountIdOf<T, I>, | ||
| recipient: AccountIdOf<T, I>, | ||
| amount: BalanceOf<T, I>, | ||
| ) -> DispatchResult { | ||
| with_storage_layer(move || { | ||
| <Approvals<T, I>>::mutate(sender.clone(), caller, |entry| { | ||
| // Remove "value" from allowed, exit if underflow. | ||
| let allowed = entry | ||
| .checked_sub(&amount) | ||
| .ok_or(Error::<T, I>::SpendMoreThanAllowed)?; | ||
|
|
||
| // Update allowed value. | ||
| *entry = allowed; | ||
|
|
||
| Ok::<(), Error<T, I>>(()) | ||
| })?; | ||
|
|
||
| T::Currency::transfer( | ||
| &sender, | ||
| &recipient, | ||
| amount, | ||
| frame_support::traits::ExistenceRequirement::AllowDeath, | ||
| )?; | ||
|
|
||
| Ok(()) | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.