-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add support for Celo's stateful transfer precompile #11209
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
karlb
wants to merge
3
commits into
foundry-rs:master
Choose a base branch
from
celo-org:karlb/celo-transfer-precompile
base: master
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
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,99 @@ | ||
//! Celo precompile implementation for token transfers. | ||
//! | ||
//! This module implements the Celo transfer precompile that enables native token transfers from an | ||
//! EVM contract. The precompile is part of Celo's token duality system, allowing transfer of | ||
//! native tokens via ERC20. | ||
//! | ||
//! For more details, see: <https://specs.celo.org/token_duality.html#the-transfer-precompile> | ||
//! | ||
//! The transfer precompile is deployed at address 0xfd and accepts 96 bytes of input: | ||
//! - from address (32 bytes, left-padded) | ||
//! - to address (32 bytes, left-padded) | ||
//! - value (32 bytes, big-endian U256) | ||
|
||
use alloy_evm::precompiles::{DynPrecompile, PrecompileInput}; | ||
use alloy_primitives::{Address, U256, address}; | ||
use revm::precompile::{PrecompileError, PrecompileOutput, PrecompileResult}; | ||
|
||
pub const CELO_TRANSFER_ADDRESS: Address = address!("0x00000000000000000000000000000000000000fd"); | ||
|
||
/// Gas cost for Celo transfer precompile | ||
const CELO_TRANSFER_GAS_COST: u64 = 9000; | ||
|
||
/// Celo transfer precompile implementation. | ||
/// | ||
/// Uses load_account to modify balances directly, making it compatible with PrecompilesMap. | ||
pub fn celo_transfer_precompile(input: PrecompileInput<'_>) -> PrecompileResult { | ||
// Check minimum gas requirement | ||
if input.gas < CELO_TRANSFER_GAS_COST { | ||
return Err(PrecompileError::OutOfGas); | ||
} | ||
|
||
// Validate input length (must be exactly 96 bytes: 32 + 32 + 32) | ||
if input.data.len() != 96 { | ||
return Err(PrecompileError::Other(format!( | ||
"Invalid input length for Celo transfer precompile: expected 96 bytes, got {}", | ||
input.data.len() | ||
))); | ||
} | ||
|
||
// Parse input: from (bytes 12-32), to (bytes 44-64), value (bytes 64-96) | ||
let from_bytes = &input.data[12..32]; | ||
let to_bytes = &input.data[44..64]; | ||
let value_bytes = &input.data[64..96]; | ||
|
||
let from_address = Address::from_slice(from_bytes); | ||
let to_address = Address::from_slice(to_bytes); | ||
let value = U256::from_be_slice(value_bytes); | ||
|
||
// Perform the transfer using load_account to modify balances directly | ||
let mut internals = input.internals; | ||
|
||
// Load and check the from account balance first | ||
{ | ||
let from_account = match internals.load_account(from_address) { | ||
Ok(account) => account, | ||
Err(e) => { | ||
return Err(PrecompileError::Other(format!("Failed to load from account: {e:?}"))); | ||
} | ||
}; | ||
|
||
// Check if from account has sufficient balance | ||
if from_account.data.info.balance < value { | ||
return Err(PrecompileError::Other("Insufficient balance".into())); | ||
} | ||
|
||
// Deduct balance from the from account | ||
from_account.data.info.balance -= value; | ||
} | ||
|
||
// Load and update the to account | ||
{ | ||
let to_account = match internals.load_account(to_address) { | ||
Ok(account) => account, | ||
Err(e) => { | ||
return Err(PrecompileError::Other(format!("Failed to load to account: {e:?}"))); | ||
} | ||
}; | ||
|
||
// Check for overflow in to account | ||
if to_account.data.info.balance.checked_add(value).is_none() { | ||
return Err(PrecompileError::Other("Balance overflow in to account".into())); | ||
} | ||
|
||
// Add balance to the to account | ||
to_account.data.info.balance += value; | ||
} | ||
|
||
// No output data for successful transfer | ||
Ok(PrecompileOutput::new(CELO_TRANSFER_GAS_COST, alloy_primitives::Bytes::new())) | ||
} | ||
|
||
/// Can be used as PrecompilesMap lookup function | ||
pub fn celo_precompile_lookup(address: &Address) -> Option<DynPrecompile> { | ||
if *address == CELO_TRANSFER_ADDRESS { | ||
Some(DynPrecompile::new_stateful(celo_transfer_precompile)) | ||
} else { | ||
None | ||
} | ||
} |
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.
hmm, not convinced we want to add a new arg for every network that we can add custom precompiles for.
should we perhaps derive this from the chainid, although an additional celo flag doesnt really hurt
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.
If we only rely on the chainid, new networks won't work with anvil until they have been explicitly supported. And getting PRs with config for short-lived and local networks sounds annoying to me. Adding a flag seems the lesser evil to me.
We can still automatically detect the flags for the most important networks based on the chainid. But if we need a flag anyway, that falls into the "nice to have" category.
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.
We could also enable the
transfer
precompile by setting--hardfork cel2
, but I'm not sure if adding chain-specific hardforks is better than adding a flag for the chain itself.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.
wouldn't be better to have a
--precompiles
arg and enable the precompiles based on the passed value? like--precompiles celo
or--precompiles arbitrum
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 of it like the
--optimism
flag. Users want to run an Optimism-like chain because they plan to deploy on Optimism without explicitly thinking about every specific feature. Other features that might also be enabled by the--celo
flag in the future could be a higher MaxCodeSize or Fee Abstraction. We didn't have requests to toggle such features separately for our blockchain client, so I would think the situation is similar for anvil. But I don't hold any strong opinions on this.