Skip to content

Commit

Permalink
feat(240): impl written and test passing. Previous pass due to checki…
Browse files Browse the repository at this point in the history
…ng user balance instead of the project pot
  • Loading branch information
JuaniRios committed Aug 10, 2023
1 parent 8c8f939 commit 6658e89
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
38 changes: 34 additions & 4 deletions pallets/funding/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,11 +1517,41 @@ impl<T: Config> Pallet<T> {
}

pub fn do_payout_bid_funds_for(
_caller: AccountIdOf<T>,
_project_id: T::ProjectIdentifier,
_bidder: AccountIdOf<T>,
_bid_id: StorageItemIdOf<T>,
caller: AccountIdOf<T>,
project_id: T::ProjectIdentifier,
bidder: AccountIdOf<T>,
bid_id: StorageItemIdOf<T>,
) -> Result<(), DispatchError> {
// * Get variables *
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectInfoNotFound)?;
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectInfoNotFound)?;
let mut bid = Bids::<T>::get((project_id, bidder.clone(), bid_id)).ok_or(Error::<T>::BidNotFound)?;


// * Validity checks *
ensure!(
project_details.status == ProjectStatus::FundingSuccessful &&
matches!(bid.status, BidStatus::Accepted | BidStatus::PartiallyAccepted(..)),
Error::<T>::NotAllowed
);

// * Calculate variables *
let issuer = project_details.issuer;
let project_pot = Self::fund_account_id(project_id);
let payout_amount = bid.funding_asset_amount_locked;
let payout_asset = bid.funding_asset;

// * Update storage *
T::FundingCurrency::transfer(
payout_asset.to_statemint_id(),
&project_pot,
&issuer,
payout_amount,
Preservation::Expendable,
)?;
bid.funds_released = true;
Bids::<T>::insert((project_id, bidder.clone(), bid_id), bid);

Ok(())
}
}
Expand Down
8 changes: 1 addition & 7 deletions pallets/funding/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,14 +615,8 @@ fn issuer_funding_payout_one_bid<T: Config>(project_id: T::ProjectIdentifier) ->
error: e,
}),
};
(Weight::zero(), remaining_bids.count() as u64)

bid.funds_released = true;

Bids::<T>::insert((project_id, bid.bidder.clone(), bid.id), bid);

// (Weight::zero(), remaining_bids.count() as u64)
// TODO: Remove this when function is implemented
(Weight::zero(), 0u64)
} else {
(Weight::zero(), 0u64)
}
Expand Down
19 changes: 17 additions & 2 deletions pallets/funding/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2973,12 +2973,13 @@ mod auction_round_success {
community_contributions,
remainder_contributions,
);

let project_id = finished_project.get_project_id();
let final_bid_payouts = test_env.in_ext(|| {
Bids::<TestRuntime>::iter_prefix_values((finished_project.project_id,))
.map(|bid| (bid.bidder, bid.funding_asset_amount_locked, bid.funding_asset.to_statemint_id()))
.collect::<UserToStatemintAsset>()
});
let total_expected_bid_payout = final_bid_payouts.iter().map(|bid| bid.1.clone()).sum::<BalanceOf<TestRuntime>>();

let prev_issuer_funding_balance = test_env.get_free_statemint_asset_balances_for(
final_bid_payouts[0].2,
Expand All @@ -2992,6 +2993,10 @@ mod auction_round_success {
.iter()
.map(|(_, balance, _)| balance)
.sum::<BalanceOf<TestRuntime>>();
let prev_project_pot_funding_balance = test_env.get_free_statemint_asset_balances_for(
final_bid_payouts[0].2,
vec![Pallet::<TestRuntime>::fund_account_id(project_id)],
)[0].1;

test_env.advance_time(<TestRuntime as Config>::SuccessToSettlementTime::get() + 1).unwrap();
assert_eq!(
Expand All @@ -3011,15 +3016,25 @@ mod auction_round_success {
.iter()
.map(|(_, balance, _)| balance)
.sum::<BalanceOf<TestRuntime>>();
let post_project_pot_funding_balance = test_env.get_free_statemint_asset_balances_for(
final_bid_payouts[0].2,
vec![Pallet::<TestRuntime>::fund_account_id(project_id)],
)[0].1;

let issuer_funding_delta = post_issuer_funding_balance - prev_issuer_funding_balance;
let bidders_funding_delta = prev_total_bidder_balance - post_total_bidder_balance;
let project_pot_funding_delta = prev_project_pot_funding_balance - post_project_pot_funding_balance;

assert_eq!(issuer_funding_delta, bidders_funding_delta);
assert_eq!(issuer_funding_delta, total_expected_bid_payout);
assert_eq!(issuer_funding_delta, project_pot_funding_delta);

for (_bidder, balance, _asset) in post_bidders_funding_balances {
assert_eq!(balance, 0);
}
// 1_052_631_5_789_473_682
// 894_736_8_421_052_630

assert_eq!(post_project_pot_funding_balance, 0u128);
}
}

Expand Down

0 comments on commit 6658e89

Please sign in to comment.