Skip to content
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

Added erc20 token amount type #1233

Closed

Conversation

batconjurer
Copy link
Member

@batconjurer batconjurer commented Mar 20, 2023

This PR closes #1085, #1089, and #1090.

A new type, Erc20Amount has been added for amounts of ERC20 tokens according to the EIP-20 specification. Whitelisted ERC20 tokens must now keep a denomination in storage. These denominations must also be kept in sync with the oracle, which needs them for processing ethereum events.

@batconjurer batconjurer marked this pull request as draft March 20, 2023 10:01
@batconjurer batconjurer requested review from sug0 and tzemanovic March 24, 2023 12:43
@batconjurer batconjurer marked this pull request as ready for review March 24, 2023 12:46
Copy link
Collaborator

@sug0 sug0 left a comment

Choose a reason for hiding this comment

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

might need another review pass, since token whitelists need some changes to allow transfers to ethereum, even if a whitelist update removes a token whose balance is still greater than 0 for some namada client

@@ -336,6 +434,67 @@ where
Ok(changed_key)
}

fn act_on_whitelist_updates<D, H>(
wl_storage: &mut WlStorage<D, H>,
whitelist: HashMap<EthAddress, Erc20Amount>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

iteration order doesn't seem to matter here, but for the correctness of any potential change to this code, I would make this a BTreeMap instead

Copy link
Collaborator

Choose a reason for hiding this comment

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

eventually, we also need to update the token whitelist nonce in storage, I believe

@@ -2274,7 +2277,7 @@ pub mod args {
/// The sender of the transfer
pub sender: WalletAddress,
/// The amount to be transferred
pub amount: Amount,
pub amount: Decimal,
Copy link
Collaborator

Choose a reason for hiding this comment

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

let result: Result<Decimal, _> = "1.000000000012345".parse()

I tested this locally, and parsing worked. what was the issue with parsing amounts with more than 0 decimal places?

Comment on lines +2731 to +2739
let erc20_amount = TOKEN_AMOUNT_OPT.parse(matches);
let amount = AMOUNT_OPT.parse(matches);
if let Some(amt) = erc20_amount {
Self::Erc20(amt)
} else if let Some(amt) = amount {
Self::Native(amt)
} else {
panic!("No amount provided")
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

if CLI receives two distinct type of amount arguments, only erc20 amounts will be considered. for better UX, perhaps it's better to fail parsing if both of them are Some?

Copy link
Collaborator

Choose a reason for hiding this comment

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

https://docs.rs/clap/latest/clap/error/struct.Error.html
maybe we could also use this API instead of panicking

@@ -124,31 +128,34 @@ pub mod eth_events {
block_height: Uint256,
data: &[u8],
min_confirmations: Uint256,
erc20_tokens: &HashMap<EthAddress, u8>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This file was already heavily refactored here: #1252
shit, what do we do about this?

@@ -157,7 +157,7 @@ where
if let Some(config) = genesis.ethereum_bridge_params {
tracing::debug!("Initializing Ethereum bridge storage.");
config.init_storage(&mut self.wl_storage);
self.update_eth_oracle();
self.update_eth_oracle(true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I'm not generally a big fan of these "naked" boolean values. what does it mean for update_eth_oracle to take a value of true, without having any context on what the argument's name is?

Comment on lines +91 to +97
impl From<Uint> for u64 {
fn from(uint: Uint) -> Self {
// N.B. this will panic if `uint` > `u64::MAX`.
ethUint::from(uint).as_u64()
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's a bit odd for a From implementation to panic. can we do without it? or implement TryFrom instead

Comment on lines +16 to +17
update: impl FnOnce(Option<T>) -> T,
) -> Result<T>
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this change because erc20s don't have a default value?

Comment on lines +59 to +67
EthereumEvent::UpdateBridgeWhitelist { whitelist, .. } => {
Ok(act_on_whitelist_updates(
wl_storage,
whitelist
.iter()
.map(|TokenWhitelist { token, cap }| (*token, *cap))
.collect(),
))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

add a TODO here, since we need to update the token whitelist nonce in storage (I think?)

self.wl_storage.ethbridge_queries().get_wnam_address()
{
if addr == *token_address {
return Some(6);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: maybe implement a const fn token::Amount::denomination() -> u8 { 6 }?

|e| {
eyre!(
"Could not process the amount of NAM to \
be escrowed into the bridge pool: {:?}",
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: assuming this implements Error, it's best to display the actual error string

Suggested change
be escrowed into the bridge pool: {:?}",
be escrowed into the bridge pool: {}",

@batconjurer
Copy link
Member Author

might need another review pass, since token whitelists need some changes to allow transfers to ethereum, even if a whitelist update removes a token whose balance is still greater than 0 for some namada client

Yes, but that will be another PR.

@sug0
Copy link
Collaborator

sug0 commented Apr 13, 2023

This PR contains an implementation that closes #1290

@cwgoes cwgoes mentioned this pull request May 18, 2023
10 tasks
@sug0
Copy link
Collaborator

sug0 commented Jul 15, 2023

Superseded by #1718 and #1282

@sug0 sug0 closed this Jul 15, 2023
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.

2 participants