@@ -38,7 +38,7 @@ pub fn precompile() -> DynPrecompile {
3838/// Celo transfer precompile implementation.
3939///
4040/// Uses load_account to modify balances directly, making it compatible with PrecompilesMap.
41- pub fn celo_transfer_precompile ( input : PrecompileInput < ' _ > ) -> PrecompileResult {
41+ pub fn celo_transfer_precompile ( mut input : PrecompileInput < ' _ > ) -> PrecompileResult {
4242 // Check minimum gas requirement
4343 if input. gas < CELO_TRANSFER_GAS_COST {
4444 return Err ( PrecompileError :: OutOfGas ) ;
@@ -62,44 +62,39 @@ pub fn celo_transfer_precompile(input: PrecompileInput<'_>) -> PrecompileResult
6262 let value = U256 :: from_be_slice ( value_bytes) ;
6363
6464 // Perform the transfer using load_account to modify balances directly
65- let mut internals = input. internals ;
65+ let internals = input. internals_mut ( ) ;
6666
6767 // Load and check the from account balance first
68- {
69- let from_account = match internals. load_account ( from_address) {
70- Ok ( account) => account,
71- Err ( e) => {
72- return Err ( PrecompileError :: Other ( format ! ( "Failed to load from account: {e:?}" ) ) ) ;
73- }
74- } ;
75-
76- // Check if from account has sufficient balance
77- if from_account. data . info . balance < value {
78- return Err ( PrecompileError :: Other ( "Insufficient balance" . into ( ) ) ) ;
68+
69+ let from_account = match internals. load_account ( from_address) {
70+ Ok ( account) => account,
71+ Err ( e) => {
72+ return Err ( PrecompileError :: Other ( format ! ( "Failed to load from account: {e:?}" ) ) ) ;
7973 }
74+ } ;
8075
81- // Deduct balance from the from account
82- from_account. data . info . balance -= value;
76+ // Check if from account has sufficient balance
77+ if from_account. data . info . balance < value {
78+ return Err ( PrecompileError :: Other ( "Insufficient balance" . into ( ) ) ) ;
8379 }
8480
85- // Load and update the to account
86- {
87- let to_account = match internals. load_account ( to_address) {
88- Ok ( account) => account,
89- Err ( e) => {
90- return Err ( PrecompileError :: Other ( format ! ( "Failed to load to account: {e:?}" ) ) ) ;
91- }
92- } ;
93-
94- // Check for overflow in to account
95- if to_account. data . info . balance . checked_add ( value) . is_none ( ) {
96- return Err ( PrecompileError :: Other ( "Balance overflow in to account" . into ( ) ) ) ;
81+ let to_account = match internals. load_account ( to_address) {
82+ Ok ( account) => account,
83+ Err ( e) => {
84+ return Err ( PrecompileError :: Other ( format ! ( "Failed to load to account: {e:?}" ) ) ) ;
9785 }
86+ } ;
9887
99- // Add balance to the to account
100- to_account. data . info . balance += value;
88+ // Check for overflow in to account
89+ if to_account. data . info . balance . checked_add ( value) . is_none ( ) {
90+ return Err ( PrecompileError :: Other ( "Balance overflow in to account" . into ( ) ) ) ;
10191 }
10292
93+ // Transfer the value between accounts
94+ internals
95+ . transfer ( from_address, to_address, value)
96+ . map_err ( |e| PrecompileError :: Other ( format ! ( "Failed to perform transfer: {e:?}" ) ) ) ?;
97+
10398 // No output data for successful transfer
10499 Ok ( PrecompileOutput :: new ( CELO_TRANSFER_GAS_COST , alloy_primitives:: Bytes :: new ( ) ) )
105100}
0 commit comments