Skip to content

Commit

Permalink
fix(bridge-withdrawer, cli)!: use decimals from erc20 contract (#1762)
Browse files Browse the repository at this point in the history
## Summary
Updates CLI and bridge withdrawer to read the decimals off the contract
when it is an erc20, to get the accurate decimals to utilize during
withdrawal.

## Background
Previously it was assumed that all erc20 contracts would have 18
decimals this is not guaranteed. #1760 is a similar fix but relies on
configuring the withdrawer correctly. This version automatically reads
off of the contract instead if it is an erc20.

## Changes
- Read decimals off contract if  or fallback to native prevision.

## Testing
Ran against Flame to ensure it gets decimals correct on 6 decimal
contract + CI tests.

## Changelogs
Changelogs updated.

---------

Co-authored-by: Richard Janis Goldschmidt <[email protected]>
  • Loading branch information
joroshiba and SuperFluffy authored Oct 30, 2024
1 parent d9913bb commit ca72c64
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/astria-bridge-contracts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Read the provided contract's `decimals` function, falling back to a hardcoded
value of 18 if the call fails.
[#1762](https://github.com/astriaorg/astria/pull/1762)

### Added

- Initial release.
1 change: 1 addition & 0 deletions crates/astria-bridge-contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ serde = { workspace = true }
serde_json = { workspace = true }
tendermint = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true, optional = true }
37 changes: 36 additions & 1 deletion crates/astria-bridge-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ use ethers::{
};
pub use generated::*;

const NON_ERC20_CONTRACT_DECIMALS: u32 = 18u32;

macro_rules! warn {
($($tt:tt)*) => {
#[cfg(feature = "tracing")]
{
#![cfg_attr(
feature = "tracing",
expect(
clippy::used_underscore_binding,
reason = "underscore is needed to quiet `unused-variables` warning if `tracing` feature is not set",
))]
::tracing::warn!($($tt)*);
}
}
}

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct BuildError(BuildErrorKind);
Expand Down Expand Up @@ -295,7 +312,25 @@ where
.await
.map_err(BuildError::call_base_chain_asset_precision)?;

let exponent = 18u32
let contract_decimals = {
let erc_20_contract = astria_bridgeable_erc20::AstriaBridgeableERC20::new(
contract_address,
provider.clone(),
);
match erc_20_contract.decimals().call().await {
Ok(decimals) => decimals.into(),
Err(_error) => {
warn!(
error = &_error as &dyn std::error::Error,
"failed reading decimals from contract; assuming it is not an ERC20 \
contract and falling back to `{NON_ERC20_CONTRACT_DECIMALS}`"
);
NON_ERC20_CONTRACT_DECIMALS
}
}
};

let exponent = contract_decimals
.checked_sub(base_chain_asset_precision)
.ok_or_else(|| BuildError::bad_divisor(base_chain_asset_precision))?;

Expand Down
4 changes: 3 additions & 1 deletion crates/astria-bridge-withdrawer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ tokio = { workspace = true, features = ["macros", "rt-multi-thread", "signal"] }
tokio-util = { workspace = true }
tonic = { workspace = true }

astria-bridge-contracts = { path = "../astria-bridge-contracts" }
astria-bridge-contracts = { path = "../astria-bridge-contracts", features = [
"tracing",
] }
astria-build-info = { path = "../astria-build-info", features = ["runtime"] }
astria-core = { path = "../astria-core", features = [
"serde",
Expand Down
4 changes: 3 additions & 1 deletion crates/astria-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ serde_yaml = "0.9.25"
termion = "4.0.3"
tracing-subscriber = "0.3.18"

astria-bridge-contracts = { path = "../astria-bridge-contracts" }
astria-bridge-contracts = { path = "../astria-bridge-contracts", features = [
"tracing",
] }
astria-core = { path = "../astria-core", features = ["serde"] }

clap = { workspace = true, features = ["derive", "env"] }
Expand Down

0 comments on commit ca72c64

Please sign in to comment.