-
Notifications
You must be signed in to change notification settings - Fork 53
Erc3643 functions #1992
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
Open
HenriqueNogara
wants to merge
7
commits into
erc7943-functions
Choose a base branch
from
erc3643-functions
base: erc7943-functions
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Erc3643 functions #1992
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5e94e5d
Add set_symbol, set_name, unpause, pause
HenriqueNogara 9ba787a
Add set_symbol, set_name, unpause, pause
HenriqueNogara 68324f8
Use new common struct
HenriqueNogara 5aa7266
Add set_address_frozen
HenriqueNogara b032b24
Add ensure_holder_is_not_frozen for transfers
HenriqueNogara 5377784
Update precompile .bin
HenriqueNogara b0272de
Fix benchark build; Use keccak256
HenriqueNogara 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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,189 @@ | ||
| // This file is part of the Polymesh distribution (https://github.com/PolymeshAssociation/Polymesh). | ||
| // Copyright (c) 2020 Polymesh Association | ||
|
|
||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, version 3. | ||
|
|
||
| // This program is distributed in the hope that it will be useful, but | ||
| // WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| // General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| use alloc::vec::Vec; | ||
| use frame_support::traits::Get; | ||
|
|
||
| use pallet_revive::precompiles::alloy::primitives::keccak256; | ||
| use pallet_revive::precompiles::alloy::primitives::FixedBytes; | ||
| use pallet_revive::precompiles::alloy::sol_types::Revert; | ||
| use pallet_revive::precompiles::Error; | ||
| use pallet_revive::precompiles::Ext; | ||
|
|
||
| use pallet_asset::{AssetIdTicker, AssetNames}; | ||
| use polymesh_precompiles::{IFungibleAsset, IFungibleAssetEvents}; | ||
| use polymesh_primitives::asset::{AssetId, AssetName}; | ||
| use polymesh_primitives::ticker::TICKER_LEN; | ||
| use polymesh_primitives::Ticker; | ||
|
|
||
| use crate::common::Common; | ||
| use crate::interface::FungibleAssetInterface; | ||
| use crate::interface::{DECIMALS, ERR_INVALID_SYMBOL}; | ||
| use crate::Config; | ||
|
|
||
| impl<T: Config> FungibleAssetInterface<T> { | ||
| /// Freezes the asset, preventing token transfers. Only an agent of the token can call this function. | ||
| pub(crate) fn pause(asset_id: AssetId, env: &mut impl Ext<T = T>) -> Result<Vec<u8>, Error> { | ||
| let caller = Common::<T>::caller(env)?; | ||
|
|
||
| Common::<T>::call_runtime( | ||
| env, | ||
| caller.runtime_origin(), | ||
| pallet_asset::Call::<T>::freeze { asset_id }, | ||
| )?; | ||
|
|
||
| Common::<T>::deposit_event( | ||
| env, | ||
| IFungibleAssetEvents::Paused(IFungibleAsset::Paused { | ||
| userAddress: caller.address.0.into(), | ||
| }), | ||
| )?; | ||
| Ok(Vec::new()) | ||
| } | ||
|
|
||
| /// Unfreezes the token contract, allowing token transfers. Only an agent of the token can call this function. | ||
| pub(crate) fn unpause(asset_id: AssetId, env: &mut impl Ext<T = T>) -> Result<Vec<u8>, Error> { | ||
| let caller = Common::<T>::caller(env)?; | ||
|
|
||
| Common::<T>::call_runtime( | ||
| env, | ||
| caller.runtime_origin(), | ||
| pallet_asset::Call::<T>::unfreeze { asset_id }, | ||
| )?; | ||
|
|
||
| Common::<T>::deposit_event( | ||
| env, | ||
| IFungibleAssetEvents::Unpaused(IFungibleAsset::Unpaused { | ||
| userAddress: caller.address.0.into(), | ||
| }), | ||
| )?; | ||
| Ok(Vec::new()) | ||
| } | ||
|
|
||
| /// Sets the token name. Only the owner of the token contract can call this function. | ||
| pub(crate) fn set_name( | ||
| asset_id: AssetId, | ||
| call: &IFungibleAsset::setNameCall, | ||
| env: &mut impl Ext<T = T>, | ||
| ) -> Result<Vec<u8>, Error> { | ||
| // Charges the additional `AssetIdTicker` read | ||
| env.charge(T::DbWeight::get().reads(1))?; | ||
|
|
||
| let caller = Common::<T>::caller(env)?; | ||
| let new_asset_name = AssetName::from(&call.name.as_bytes().to_vec()); | ||
|
|
||
| Common::<T>::call_runtime( | ||
| env, | ||
| caller.runtime_origin(), | ||
| pallet_asset::Call::<T>::rename_asset { | ||
| asset_id, | ||
| asset_name: new_asset_name.clone(), | ||
| }, | ||
| )?; | ||
|
|
||
| let ticker = AssetIdTicker::<T>::get(&asset_id).unwrap_or_default(); | ||
| Common::<T>::deposit_event( | ||
| env, | ||
| IFungibleAssetEvents::UpdatedTokenInformation( | ||
| IFungibleAsset::UpdatedTokenInformation { | ||
| newName: FixedBytes::from(keccak256(new_asset_name.0.as_slice())), | ||
| newSymbol: FixedBytes::from(keccak256(ticker.as_ref())), | ||
| newDecimals: DECIMALS, | ||
| newVersion: Default::default(), | ||
| newOnchainID: Default::default(), | ||
| }, | ||
| ), | ||
| )?; | ||
| Ok(Vec::new()) | ||
| } | ||
|
|
||
| /// Sets the token symbol. Only the owner of the token contract can call this function. | ||
| pub(crate) fn set_symbol( | ||
| asset_id: AssetId, | ||
| call: &IFungibleAsset::setSymbolCall, | ||
| env: &mut impl Ext<T = T>, | ||
| ) -> Result<Vec<u8>, Error> { | ||
| // Charges the additional `AssetNames` read | ||
| env.charge(T::DbWeight::get().reads(1))?; | ||
|
|
||
| let new_symbol = &call.symbol.as_bytes(); | ||
|
|
||
| if new_symbol.len() > TICKER_LEN { | ||
| return Err(Error::Revert(Revert { | ||
| reason: ERR_INVALID_SYMBOL.into(), | ||
| })); | ||
| } | ||
|
|
||
| let ticker = Ticker::from_slice_truncated(new_symbol); | ||
| let caller = Common::<T>::caller(env)?; | ||
|
|
||
| Common::<T>::call_runtime( | ||
| env, | ||
| caller.runtime_origin(), | ||
| pallet_asset::Call::<T>::register_unique_ticker { ticker }, | ||
| )?; | ||
| Common::<T>::call_runtime( | ||
| env, | ||
| caller.runtime_origin(), | ||
| pallet_asset::Call::<T>::link_ticker_to_asset_id { ticker, asset_id }, | ||
| )?; | ||
|
|
||
| let asset_name = AssetNames::<T>::get(&asset_id).unwrap_or_default(); | ||
| Common::<T>::deposit_event( | ||
| env, | ||
| IFungibleAssetEvents::UpdatedTokenInformation( | ||
| IFungibleAsset::UpdatedTokenInformation { | ||
| newName: FixedBytes::from(keccak256(asset_name.0.as_slice())), | ||
| newSymbol: FixedBytes::from(keccak256(ticker.as_ref())), | ||
| newDecimals: DECIMALS, | ||
| newVersion: Default::default(), | ||
| newOnchainID: Default::default(), | ||
| }, | ||
| ), | ||
| )?; | ||
| Ok(Vec::new()) | ||
| } | ||
|
|
||
| /// Sets the frozen status of a specific address. Only an agent of the token can call this function. | ||
| pub(crate) fn set_address_frozen( | ||
| asset_id: AssetId, | ||
| call: &IFungibleAsset::setAddressFrozenCall, | ||
| env: &mut impl Ext<T = T>, | ||
| ) -> Result<Vec<u8>, Error> { | ||
| let caller = Common::<T>::caller(env)?; | ||
|
|
||
| let acc_to_freeze = Common::<T>::account_id32(call.account)?; | ||
|
|
||
| Common::<T>::call_runtime( | ||
| env, | ||
| caller.runtime_origin(), | ||
| pallet_asset::Call::<T>::set_address_frozen { | ||
| asset_id, | ||
| freeze: call.freeze, | ||
| account: acc_to_freeze, | ||
| }, | ||
| )?; | ||
|
|
||
| Common::<T>::deposit_event( | ||
| env, | ||
| IFungibleAssetEvents::AddressFrozen(IFungibleAsset::AddressFrozen { | ||
| account: call.account, | ||
| freeze: call.freeze, | ||
| owner: caller.address.0.into(), | ||
| }), | ||
| )?; | ||
| Ok(Vec::new()) | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should implement this freeze feature for both accounts and portfolios (so take an
AssetHoldertarget here). Also need to make sure that the freeze blocks same DID transfers (portfolio moves and transfer_funds).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The precompile can be limited to account for now. I am not sure if we want to commit to supporting Portfolios long-term (since accounts are more commonly used).