-
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
Closed
Closed
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.
Uh oh!
There was an error while loading. Please reload this page.