Skip to content

Commit b21f6bc

Browse files
committed
feat: implement token transfers in SweepController
1 parent cc9ad96 commit b21f6bc

2 files changed

Lines changed: 37 additions & 47 deletions

File tree

contracts/sweep_controller/src/lib.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mod authorization;
44
mod errors;
55
mod storage;
6-
// mod transfers;
6+
mod transfers;
77

88
use ephemeral_account::EphemeralAccountContractClient as EphemeralAccountClient;
99
use soroban_sdk::{contract, contractimpl, contracttype, Address, BytesN, Env};
@@ -115,17 +115,16 @@ impl SweepController {
115115
return Err(Error::AccountNotReady);
116116
}
117117

118-
// Execute the actual token transfer
119-
// Note: In production, the ephemeral account would need to authorize this transfer
120-
// let transfer_ctx = TransferContext::new(
121-
// info.payment_asset,
122-
// ephemeral_account.clone(),
123-
// destination.clone(),
124-
// amount,
125-
// );
126-
// transfer_ctx.execute(&env)?;
127-
128-
// Emit sweep executed event
118+
// Execute the actual token transfers for all recorded payments
119+
let mut payments_vec = soroban_sdk::Vec::new(&env);
120+
for payment in info.payments.iter() {
121+
payments_vec.push_back(payment);
122+
}
123+
124+
transfers::execute_transfers(&env, &ephemeral_account, &destination, &payments_vec)
125+
.map_err(|_| Error::TransferFailed)?;
126+
127+
// Emit sweep completed event after successful transfer
129128
emit_sweep_completed(&env, ephemeral_account, destination, amount);
130129

131130
Ok(())
Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,34 @@
11
use crate::errors::Error;
2+
use bridgelet_shared::Payment;
23
use soroban_sdk::token::TokenClient;
3-
use soroban_sdk::{Address, Env};
4+
use soroban_sdk::{Address, Env, Vec};
45

5-
/// Execute token transfer from ephemeral account to destination
6-
pub fn execute_transfer(
6+
/// Execute token transfers for all payments from the ephemeral account to the destination.
7+
///
8+
/// Iterates over each recorded payment and calls the SEP-41 token contract's
9+
/// `transfer()` function, moving funds from `from` to `destination`.
10+
///
11+
/// The ephemeral account must have already authorized this contract to transfer
12+
/// on its behalf — this is enforced by the Soroban auth model when `from.require_auth()`
13+
/// is satisfied by the ephemeral account's invocation context.
14+
///
15+
/// # Arguments
16+
/// * `env` - Soroban environment
17+
/// * `from` - Ephemeral account address (source of funds)
18+
/// * `destination` - Recipient wallet address
19+
/// * `payments` - All recorded payments to transfer
20+
///
21+
/// # Errors
22+
/// Returns `Error::TransferFailed` if any individual transfer fails
23+
pub fn execute_transfers(
724
env: &Env,
8-
token_address: &Address,
925
from: &Address,
10-
to: &Address,
11-
amount: i128,
26+
destination: &Address,
27+
payments: &Vec<Payment>,
1228
) -> Result<(), Error> {
13-
// Create token client
14-
let token = TokenClient::new(env, token_address);
15-
16-
// Execute transfer
17-
token.transfer(from, to, &amount);
18-
19-
Ok(())
20-
}
21-
22-
/// Transfer context for sweep operations
23-
pub struct TransferContext {
24-
pub asset: Address,
25-
pub from: Address,
26-
pub to: Address,
27-
pub amount: i128,
28-
}
29-
30-
impl TransferContext {
31-
pub fn new(asset: Address, from: Address, to: Address, amount: i128) -> Self {
32-
Self {
33-
asset,
34-
from,
35-
to,
36-
amount,
37-
}
38-
}
39-
40-
pub fn execute(&self, env: &Env) -> Result<(), Error> {
41-
execute_transfer(env, &self.asset, &self.from, &self.to, self.amount)
29+
for payment in payments.iter() {
30+
let token = TokenClient::new(env, &payment.asset);
31+
token.transfer(from, destination, &payment.amount);
4232
}
33+
Ok(())
4334
}

0 commit comments

Comments
 (0)