-
Notifications
You must be signed in to change notification settings - Fork 3
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
fix: normalize transferred tokens by decimals #159
Merged
Merged
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
310698b
fix: normalize transferred tokens by decimals
karim-en 2e8df29
Fix naming
karim-en 8060258
Fix typo
karim-en 1c95ba1
Fix clippy
karim-en e97ba96
Store normalization dust for zero fee transfers
karim-en a0422d9
Fix tests
karim-en 8e753e5
Merge remote-tracking branch 'origin/main' into normalise-decimals
karim-en 1a91fa8
Fix test
karim-en fefd2eb
Fix tests
karim-en e8f4184
Fix dust increment
karim-en f84e6e2
Fix normalized amount in claim fee
karim-en 646e321
Remove dust from storage
karim-en 572a986
Panic if decimals not exist
karim-en cc62b6b
fix normalize and denormalize
karim-en 74e876b
Fix tests
karim-en 5f5735c
Rename `de_normalize` to `denormalize`
karim-en 0b8a117
Fix typo
karim-en 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
This file contains 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 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 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 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 |
---|---|---|
|
@@ -27,7 +27,7 @@ use omni_types::{ | |
BasicMetadata, ChainKind, Fee, InitTransferMsg, MetadataPayload, Nonce, OmniAddress, | ||
PayloadType, SignRequest, TransferId, TransferMessage, TransferMessagePayload, UpdateFee, | ||
}; | ||
use storage::{TransferMessageStorage, TransferMessageStorageValue, NEP141_DEPOSIT}; | ||
use storage::{Decimals, TransferMessageStorage, TransferMessageStorageValue, NEP141_DEPOSIT}; | ||
|
||
mod errors; | ||
mod storage; | ||
|
@@ -70,6 +70,7 @@ enum StorageKey { | |
TokenDeployerAccounts, | ||
DeployedTokens, | ||
DestinationNonces, | ||
TokenDecimals, | ||
} | ||
|
||
#[derive(AccessControlRole, Deserialize, Serialize, Copy, Clone)] | ||
|
@@ -164,6 +165,7 @@ pub struct Contract { | |
pub finalised_transfers: LookupSet<TransferId>, | ||
pub token_id_to_address: LookupMap<(ChainKind, AccountId), OmniAddress>, | ||
pub token_address_to_id: LookupMap<OmniAddress, AccountId>, | ||
pub token_decimals: LookupMap<OmniAddress, Decimals>, | ||
pub deployed_tokens: LookupSet<AccountId>, | ||
pub token_deployer_accounts: LookupMap<ChainKind, AccountId>, | ||
pub mpc_signer: AccountId, | ||
|
@@ -248,6 +250,7 @@ impl Contract { | |
finalised_transfers: LookupSet::new(StorageKey::FinalisedTransfers), | ||
token_id_to_address: LookupMap::new(StorageKey::TokenIdToAddress), | ||
token_address_to_id: LookupMap::new(StorageKey::TokenAddressToId), | ||
token_decimals: LookupMap::new(StorageKey::TokenDecimals), | ||
deployed_tokens: LookupSet::new(StorageKey::DeployedTokens), | ||
token_deployer_accounts: LookupMap::new(StorageKey::TokenDeployerAccounts), | ||
mpc_signer, | ||
|
@@ -398,12 +401,18 @@ impl Contract { | |
) | ||
.unwrap_or_else(|| env::panic_str("ERR_FAILED_TO_GET_TOKEN_ADDRESS")); | ||
|
||
let decimals = self.token_decimals.get(&token_address); | ||
let amount_to_transfer = Self::normalize_amount( | ||
transfer_message.amount.0 - transfer_message.fee.fee.0, | ||
decimals, | ||
); | ||
|
||
let transfer_payload = TransferMessagePayload { | ||
prefix: PayloadType::TransferMessage, | ||
destination_nonce: transfer_message.destination_nonce, | ||
transfer_id, | ||
token_address, | ||
amount: U128(transfer_message.amount.0 - transfer_message.fee.fee.0), | ||
amount: U128(amount_to_transfer), | ||
recipient: transfer_message.recipient, | ||
fee_recipient, | ||
}; | ||
|
@@ -495,14 +504,19 @@ impl Contract { | |
"Unknown factory" | ||
); | ||
|
||
let decimals = self.token_decimals.get(&init_transfer.token); | ||
karim-en marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let destination_nonce = | ||
self.get_next_destination_nonce(init_transfer.recipient.get_chain()); | ||
let transfer_message = TransferMessage { | ||
origin_nonce: init_transfer.origin_nonce, | ||
token: init_transfer.token, | ||
amount: init_transfer.amount, | ||
amount: Self::de_normalize_amount(init_transfer.amount.0, decimals).into(), | ||
recipient: init_transfer.recipient, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. denormalize would be one word |
||
fee: init_transfer.fee, | ||
fee: Fee { | ||
fee: Self::de_normalize_amount(init_transfer.fee.fee.0, decimals).into(), | ||
native_fee: init_transfer.fee.native_fee, | ||
}, | ||
sender: init_transfer.sender, | ||
msg: init_transfer.msg, | ||
destination_nonce, | ||
|
@@ -564,7 +578,11 @@ impl Contract { | |
); | ||
|
||
let message = self.remove_transfer_message(fin_transfer.transfer_id); | ||
let fee = message.amount.0 - fin_transfer.amount.0; | ||
let de_normalized_amount = Self::de_normalize_amount( | ||
fin_transfer.amount.0, | ||
self.token_decimals.get(&message.token), | ||
olga24912 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
let fee = message.amount.0 - de_normalized_amount; | ||
|
||
if message.fee.native_fee.0 != 0 { | ||
if message.get_origin_chain() == ChainKind::Near { | ||
|
@@ -774,6 +792,15 @@ impl Contract { | |
); | ||
self.token_address_to_id | ||
.insert(&deploy_token.token_address, &deploy_token.token); | ||
|
||
self.token_decimals.insert( | ||
&deploy_token.token_address, | ||
&Decimals { | ||
decimals: deploy_token.decimals, | ||
origin_decimals: deploy_token.origin_decimals, | ||
}, | ||
); | ||
|
||
let required_deposit = env::storage_byte_cost() | ||
.saturating_mul((env::storage_usage().saturating_sub(storage_usage)).into()); | ||
|
||
|
@@ -1192,4 +1219,22 @@ impl Contract { | |
Promise::new(account_id).transfer(amount); | ||
} | ||
} | ||
|
||
fn de_normalize_amount(amount: u128, decimals: Option<Decimals>) -> u128 { | ||
if let Some(decimals) = decimals { | ||
let diff_decimals: u128 = (decimals.origin_decimals - decimals.decimals).into(); | ||
amount * 10 ^ diff_decimals | ||
} else { | ||
amount | ||
} | ||
} | ||
|
||
fn normalize_amount(amount: u128, decimals: Option<Decimals>) -> u128 { | ||
if let Some(decimals) = decimals { | ||
let diff_decimals: u128 = (decimals.origin_decimals - decimals.decimals).into(); | ||
amount / 10 ^ diff_decimals | ||
} else { | ||
amount | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use crate::constants::{AUTHORITY_SEED, WRAPPED_MINT_SEED}; | ||
use crate::constants::{AUTHORITY_SEED, MAX_ALLOWED_DECIMALS, WRAPPED_MINT_SEED}; | ||
use crate::instructions::wormhole_cpi::*; | ||
use crate::state::message::SignedPayload; | ||
use crate::state::message::{ | ||
|
@@ -25,7 +25,7 @@ pub struct DeployToken<'info> { | |
payer = wormhole.payer, | ||
seeds = [WRAPPED_MINT_SEED, data.payload.token.as_bytes().as_ref()], | ||
bump, | ||
mint::decimals = data.payload.decimals, | ||
mint::decimals = std::cmp::min(MAX_ALLOWED_DECIMALS, data.payload.decimals), | ||
mint::authority = authority, | ||
)] | ||
pub mint: Box<Account<'info, Mint>>, | ||
|
@@ -49,9 +49,11 @@ pub struct DeployToken<'info> { | |
} | ||
|
||
impl<'info> DeployToken<'info> { | ||
pub fn initialize_token_metadata(&self, metadata: DeployTokenPayload) -> Result<()> { | ||
pub fn initialize_token_metadata(&self, mut metadata: DeployTokenPayload) -> Result<()> { | ||
let bump = &[self.wormhole.config.bumps.authority]; | ||
let signer_seeds = &[&[AUTHORITY_SEED, bump][..]]; | ||
let origin_decimals = metadata.decimals; | ||
metadata.decimals = std::cmp::min(MAX_ALLOWED_DECIMALS, metadata.decimals); | ||
|
||
let cpi_accounts = CreateMetadataAccountsV3 { | ||
payer: self.wormhole.payer.to_account_info(), | ||
|
@@ -86,6 +88,8 @@ impl<'info> DeployToken<'info> { | |
let payload = DeployTokenResponse { | ||
token: metadata.token, | ||
solana_mint: self.mint.key(), | ||
decimals: metadata.decimals, | ||
origin_decimals, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
.serialize_for_near(())?; | ||
|
||
|
This file contains 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.
Do we need
originDecimals
? Since we havetoken
we can look it up on NearThere 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.
It is a little bit complicated to retrieve the origin decimals due to async specific of the cross calls on Near, so by adding this we can reduce the number of callbacks on Near side