Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions contracts/boundless/src/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ pub enum BoundlessError {
InvalidTokenContract = 21,
/// No backers found for the project
NoBackerContributions = 22,
/// Transfer failed
TransferFailed = 23,
/// Balance check failed
BalanceCheckFailed = 24,
}

/// Enum representing the current status of a project
Expand Down
31 changes: 25 additions & 6 deletions contracts/boundless/src/logic/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ impl FundingOperations for BoundlessContract {
}

let token_client = TokenClient::new(&env, &token_contract);
token_client.transfer(&funder, &env.current_contract_address(), &amount);
token_client
.try_transfer(&funder, &env.current_contract_address(), &amount)
.map_err(|_| BoundlessError::TransferFailed)?
.map_err(|_| BoundlessError::TransferFailed)?;

let amount_u64 = amount as u64;
project.total_funded += amount_u64;
Expand All @@ -63,7 +66,9 @@ impl FundingOperations for BoundlessContract {
let mut found = false;
let mut updated_backers = Vec::new(&env);

for (backer_address, backer_amount, token) in project.backers.iter() {
let mut i = 0;
while i < project.backers.len() {
let (backer_address, backer_amount, token) = project.backers.get_unchecked(i);
if backer_address == funder && token == token_contract {
updated_backers.push_back((
backer_address.clone(),
Expand All @@ -74,6 +79,7 @@ impl FundingOperations for BoundlessContract {
} else {
updated_backers.push_back((backer_address.clone(), backer_amount, token.clone()));
}
i += 1;
}

if !found {
Expand Down Expand Up @@ -159,7 +165,10 @@ impl FundingOperations for BoundlessContract {

let contract_address = env.current_contract_address();
let token_client = TokenClient::new(&env, &token_contract);
let balance = token_client.balance(&contract_address);
let balance = token_client
.try_balance(&contract_address)
.map_err(|_| BoundlessError::BalanceCheckFailed)?
.map_err(|_| BoundlessError::BalanceCheckFailed)?;

// Check if the contract has enough balance to refund
let mut refund_amount = 0_u64;
Expand All @@ -168,18 +177,23 @@ impl FundingOperations for BoundlessContract {
.persistent()
.get(&DataKey::Backers(project_id.clone()))
.unwrap_or(Vec::new(&env));
for backer_contribution in backer_contributions.iter() {
let mut i = 0;
while i < backer_contributions.len() {
let backer_contribution = backer_contributions.get_unchecked(i);
if backer_contribution.token == token_contract {
refund_amount += backer_contribution.amount;
}
i += 1;
}

if balance < refund_amount as i128 {
return Err(BoundlessError::InsufficientFunds);
}

// Process refunds for each backer
for backer_contribution in backer_contributions.iter() {
let mut i = 0;
while i < backer_contributions.len() {
let backer_contribution = backer_contributions.get_unchecked(i);
let (backer, amount, token, _) = (
backer_contribution.backer.clone(),
backer_contribution.amount as i128,
Expand All @@ -188,6 +202,7 @@ impl FundingOperations for BoundlessContract {
);

if token != token_contract {
i += 1;
continue;
}

Expand All @@ -209,6 +224,7 @@ impl FundingOperations for BoundlessContract {
);
}
}
i += 1;
}

refunded_tokens.push_back(token_contract);
Expand Down Expand Up @@ -250,10 +266,13 @@ impl FundingOperations for BoundlessContract {
.get(&DataKey::Project(project_id.clone()))
.ok_or(BoundlessError::NotFound)?;

for (addr, amount, _) in project.backers.iter() {
let mut i = 0;
while i < project.backers.len() {
let (addr, amount, _) = project.backers.get_unchecked(i);
if addr == backer {
return Ok(amount);
}
i += 1;
}

Ok(0)
Expand Down
Loading