-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path3_auction.rs
275 lines (238 loc) · 10 KB
/
3_auction.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#[allow(clippy::wildcard_imports)]
use super::*;
impl<T: Config> Pallet<T> {
/// Place a bid on a project in the auction round
#[transactional]
pub fn do_bid(params: DoBidParams<T>) -> DispatchResultWithPostInfo {
// * Get variables *
let DoBidParams {
bidder,
project_id,
ct_amount,
mode,
funding_asset,
investor_type,
did,
whitelisted_policy,
receiving_account,
} = params;
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectMetadataNotFound)?;
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;
// Fetch current bucket details and other required info
let mut current_bucket = Buckets::<T>::get(project_id).ok_or(Error::<T>::BucketNotFound)?;
let now = <T as Config>::BlockNumberProvider::current_block_number();
let mut amount_to_bid = ct_amount;
let project_policy = project_metadata.policy_ipfs_cid.ok_or(Error::<T>::ImpossibleState)?;
// User will spend at least this amount of USD for his bid(s). More if the bid gets split into different buckets
let min_total_ticket_size =
current_bucket.current_price.checked_mul_int(ct_amount).ok_or(Error::<T>::BadMath)?;
let metadata_ticket_size_bounds = match investor_type {
InvestorType::Institutional => project_metadata.bidding_ticket_sizes.institutional,
InvestorType::Professional => project_metadata.bidding_ticket_sizes.professional,
InvestorType::Retail => project_metadata.bidding_ticket_sizes.retail,
};
let max_multiplier = match investor_type {
InvestorType::Professional => PROFESSIONAL_MAX_MULTIPLIER,
InvestorType::Institutional => INSTITUTIONAL_MAX_MULTIPLIER,
InvestorType::Retail => RETAIL_MAX_MULTIPLIER,
};
// * Validity checks *
ensure!(project_policy == whitelisted_policy, Error::<T>::PolicyMismatch);
ensure!(ct_amount > Zero::zero(), Error::<T>::TooLow);
ensure!(did != project_details.issuer_did, Error::<T>::ParticipationToOwnProject);
ensure!(matches!(project_details.status, ProjectStatus::AuctionRound), Error::<T>::IncorrectRound);
ensure!(
project_details.round_duration.started(now) && !project_details.round_duration.ended(now),
Error::<T>::IncorrectRound
);
ensure!(
project_metadata.participation_currencies.contains(&funding_asset),
Error::<T>::FundingAssetNotAccepted
);
ensure!(
metadata_ticket_size_bounds.usd_ticket_above_minimum_per_participation(min_total_ticket_size),
Error::<T>::TooLow
);
ensure!(mode.multiplier() <= max_multiplier && mode.multiplier() > 0u8, Error::<T>::ForbiddenMultiplier);
// Note: We limit the CT Amount to the auction allocation size, to avoid long-running loops.
ensure!(ct_amount <= project_metadata.total_allocation_size, Error::<T>::TooHigh);
ensure!(
project_metadata.participants_account_type.junction_is_supported(&receiving_account),
Error::<T>::UnsupportedReceiverAccountJunction
);
let mut perform_bid_calls = 0u8;
// While there's a remaining amount to bid for
while !amount_to_bid.is_zero() {
perform_bid_calls.saturating_accrue(1);
let ct_amount = if amount_to_bid <= current_bucket.amount_left {
// Simple case, the bucket has enough to cover the bid
amount_to_bid
} else {
// The bucket doesn't have enough to cover the bid, so we bid the remaining amount of the current bucket
current_bucket.amount_left
};
let bid_id = NextBidId::<T>::get();
let auction_oversubscribed = current_bucket.current_price > current_bucket.initial_price;
let perform_params = DoPerformBidParams {
bidder: bidder.clone(),
project_id,
ct_amount,
ct_usd_price: current_bucket.current_price,
mode,
funding_asset,
bid_id,
now,
did: did.clone(),
metadata_ticket_size_bounds,
receiving_account,
auction_oversubscribed,
};
BidsBucketBounds::<T>::mutate(project_id, current_bucket.current_price, |maybe_indexes| {
if let Some(bucket_bounds) = maybe_indexes {
bucket_bounds.last_bid_index = bid_id;
} else {
*maybe_indexes = Some(BidBucketBounds { first_bid_index: bid_id, last_bid_index: bid_id });
}
});
Self::do_perform_bid(perform_params)?;
perform_bid_calls = perform_bid_calls.saturating_add(1);
// Update the current bucket and reduce the amount to bid by the amount we just bid
current_bucket.update(ct_amount);
amount_to_bid.saturating_reduce(ct_amount);
}
// Note: If the bucket has been exhausted, the 'update' function has already made the 'current_bucket' point to the next one.
Buckets::<T>::insert(project_id, current_bucket);
Ok(PostDispatchInfo {
actual_weight: Some(<T as Config>::WeightInfo::bid(perform_bid_calls as u32)),
pays_fee: Pays::No,
})
}
/// Inner function to perform bids within a bucket. do_bid makes sure to split the bid into buckets and call this as
/// many times as necessary
#[transactional]
fn do_perform_bid(do_perform_bid_params: DoPerformBidParams<T>) -> Result<BidInfoOf<T>, DispatchError> {
let DoPerformBidParams {
bidder,
project_id,
ct_amount,
ct_usd_price,
mode,
funding_asset,
bid_id,
now,
did,
metadata_ticket_size_bounds,
receiving_account,
auction_oversubscribed,
} = do_perform_bid_params;
let usd_ticket_size = ct_usd_price.checked_mul_int(ct_amount).ok_or(Error::<T>::BadMath)?;
let total_usd_bid_by_did = AuctionBoughtUSD::<T>::get((project_id, did.clone()));
let multiplier: MultiplierOf<T> = mode.multiplier().try_into().map_err(|_| Error::<T>::BadMath)?;
ensure!(
metadata_ticket_size_bounds
.usd_ticket_below_maximum_per_did(total_usd_bid_by_did.saturating_add(usd_ticket_size)),
Error::<T>::TooHigh
);
// * Calculate new variables *
let plmc_bond = Self::calculate_plmc_bond(usd_ticket_size, multiplier).map_err(|_| Error::<T>::BadMath)?;
let funding_asset_amount_locked = Self::calculate_funding_asset_amount(usd_ticket_size, funding_asset)?;
let new_bid = BidInfoOf::<T> {
id: bid_id,
project_id,
bidder: bidder.clone(),
did: did.clone(),
status: BidStatus::YetUnknown,
original_ct_amount: ct_amount,
original_ct_usd_price: ct_usd_price,
funding_asset,
funding_asset_amount_locked,
mode,
plmc_bond,
when: now,
receiving_account,
};
Self::bond_plmc_with_mode(&bidder, project_id, plmc_bond, mode, funding_asset)?;
Self::try_funding_asset_hold(&bidder, project_id, funding_asset_amount_locked, funding_asset.id().into())?;
Bids::<T>::insert(project_id, bid_id, &new_bid);
NextBidId::<T>::set(bid_id.saturating_add(One::one()));
AuctionBoughtUSD::<T>::mutate((project_id, did), |amount| *amount = amount.saturating_add(usd_ticket_size));
if auction_oversubscribed {
CTAmountOversubscribed::<T>::mutate(project_id, |amount| *amount = amount.saturating_add(ct_amount));
}
Self::deposit_event(Event::Bid {
project_id,
bidder: bidder.clone(),
id: bid_id,
ct_amount,
ct_price: ct_usd_price,
funding_asset,
funding_amount: funding_asset_amount_locked,
plmc_bond,
mode,
});
Ok(new_bid)
}
/// Process a bid that was outbid by a new bid. This will set it to Rejected so the user can get their funds back with `settle_bid` and bid again.
pub fn do_process_next_oversubscribed_bid(project_id: ProjectId) -> DispatchResult {
// Load and validate initial state
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectMetadataNotFound)?;
let bucket = Buckets::<T>::get(project_id).ok_or(Error::<T>::BucketNotFound)?;
let mut ct_amount_oversubscribed = CTAmountOversubscribed::<T>::get(project_id);
ensure!(ct_amount_oversubscribed > Zero::zero(), Error::<T>::NoBidsOversubscribed);
// Determine the current cutoff
let current_cutoff = match OutbidBidsCutoffs::<T>::get(project_id) {
Some(cutoff @ OutbidBidsCutoff { bid_price, bid_index }) => {
let bid = Bids::<T>::get(project_id, bid_index).ok_or(Error::<T>::ImpossibleState)?;
if matches!(bid.status, BidStatus::PartiallyAccepted(_)) {
cutoff
} else {
let (new_price, new_index) =
Self::get_next_cutoff(project_id, bucket.delta_price, bid_price, bid_index)?;
OutbidBidsCutoff { bid_price: new_price, bid_index: new_index }
}
},
None => {
let first_price = project_metadata.minimum_price;
let first_bounds =
BidsBucketBounds::<T>::get(project_id, first_price).ok_or(Error::<T>::ImpossibleState)?;
OutbidBidsCutoff { bid_price: first_price, bid_index: first_bounds.last_bid_index }
},
};
// Process the bid at the cutoff
let mut bid = Bids::<T>::get(project_id, current_cutoff.bid_index).ok_or(Error::<T>::ImpossibleState)?;
let bid_amount = match bid.status {
BidStatus::PartiallyAccepted(amount) => amount,
_ => bid.original_ct_amount,
};
// Update bid status and oversubscribed amount
if bid_amount > ct_amount_oversubscribed {
bid.status = BidStatus::PartiallyAccepted(bid_amount.saturating_sub(ct_amount_oversubscribed));
ct_amount_oversubscribed = Zero::zero();
} else {
bid.status = BidStatus::Rejected;
ct_amount_oversubscribed = ct_amount_oversubscribed.saturating_sub(bid_amount);
}
// Save state changes
Bids::<T>::insert(project_id, bid.id, bid.clone());
OutbidBidsCutoffs::<T>::set(project_id, Some(current_cutoff));
CTAmountOversubscribed::<T>::insert(project_id, ct_amount_oversubscribed);
Self::deposit_event(Event::OversubscribedBidProcessed { project_id, bid_id: bid.id });
Ok(())
}
/// Get the next bid that should be processed by do_process_next_oversubscribed_bid
pub fn get_next_cutoff(
project_id: ProjectId,
delta_price: PriceOf<T>,
current_price: PriceOf<T>,
current_index: u32,
) -> Result<(PriceOf<T>, u32), DispatchError> {
let bounds = BidsBucketBounds::<T>::get(project_id, current_price).ok_or(Error::<T>::ImpossibleState)?;
if current_index == bounds.first_bid_index {
let new_price = current_price.saturating_add(delta_price);
let new_bounds = BidsBucketBounds::<T>::get(project_id, new_price).ok_or(Error::<T>::ImpossibleState)?;
Ok((new_price, new_bounds.last_bid_index))
} else {
Ok((current_price, current_index.saturating_sub(1)))
}
}
}