33mod errors;
44mod events;
55
6- use soroban_sdk:: { contract, contractimpl, Env } ;
6+ use soroban_sdk:: { contract, contractimpl, Address , Env } ;
77
88pub use errors:: Error ;
99pub use events:: NativeTransferExecuted ;
@@ -13,5 +13,58 @@ pub struct NativeTransferContract;
1313
1414#[ contractimpl]
1515impl NativeTransferContract {
16- // Implementation coming in a future issue
16+ /// Initialize with the native asset contract address
17+ ///
18+ /// # Arguments
19+ /// * `native_token_address` - The Stellar native XLM token contract address
20+ ///
21+ /// # Errors
22+ /// * `Error::AlreadyInitialized` - called more than once
23+ pub fn initialize ( env : Env , native_token_address : Address ) -> Result < ( ) , Error > {
24+ if env. storage ( ) . instance ( ) . has ( & "native_token" ) {
25+ return Err ( Error :: AlreadyInitialized ) ;
26+ }
27+
28+ env. storage ( )
29+ . instance ( )
30+ . set ( & "native_token" , & native_token_address) ;
31+
32+ Ok ( ( ) )
33+ }
34+
35+ /// Execute a native XLM transfer
36+ ///
37+ /// # Arguments
38+ /// * `from` - Address sending XLM
39+ /// * `to` - Address receiving XLM
40+ /// * `amount` - Amount in stroops (must be positive)
41+ ///
42+ /// # Errors
43+ /// * `Error::NotInitialized` - contract not initialized
44+ /// * `Error::InvalidAmount` - amount is zero or negative
45+ pub fn transfer ( env : Env , from : Address , to : Address , amount : i128 ) -> Result < ( ) , Error > {
46+ // Check initialized
47+ let native_token_address: Address = env
48+ . storage ( )
49+ . instance ( )
50+ . get ( & "native_token" )
51+ . ok_or ( Error :: NotInitialized ) ?;
52+
53+ // Validate amount
54+ if amount <= 0 {
55+ return Err ( Error :: InvalidAmount ) ;
56+ }
57+
58+ // Require authorization from sender
59+ from. require_auth ( ) ;
60+
61+ // Execute transfer via native token client
62+ let native_token = soroban_sdk:: token:: Client :: new ( & env, & native_token_address) ;
63+ native_token. transfer ( & from, & to, & amount) ;
64+
65+ // Emit event
66+ events:: emit_native_transfer_executed ( & env, from, to, amount) ;
67+
68+ Ok ( ( ) )
69+ }
1770}
0 commit comments