Skip to content

Commit 5fde52b

Browse files
authored
Merge pull request #58 from PotLock/feat/donation-totals
Feat/donation totals
2 parents 2db43b1 + c462685 commit 5fde52b

File tree

4 files changed

+138
-37
lines changed

4 files changed

+138
-37
lines changed

contracts/donation/README.md

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ pub struct Contract {
2626
donation_ids_by_recipient_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
2727
donation_ids_by_donor_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
2828
donation_ids_by_ft_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
29+
total_donations_amount: Balance, // Added total_donations_amount to track total donations amount without iterating through all donations
30+
net_donations_amount: Balance, // Added net_donations_amount to track net donations amount (after fees) without iterating through all donations
31+
total_protocol_fees: Balance, // Added total_protocol_fees to track total protocol fees without iterating through all donations
32+
total_referrer_fees: Balance, // Added total_referrer_fees to track total referral fees without iterating through all donations
2933
}
3034

3135
/// NOT stored in contract storage; only used for get_config response
@@ -34,6 +38,11 @@ pub struct Config {
3438
pub protocol_fee_basis_points: u32,
3539
pub referral_fee_basis_points: u32,
3640
pub protocol_fee_recipient_account: AccountId,
41+
pub total_donations_amount: U128,
42+
pub net_donations_amount: U128,
43+
pub total_donations_count: U64,
44+
pub total_protocol_fees: U128,
45+
pub total_referrer_fees: U128,
3746
}
3847
```
3948

@@ -45,17 +54,17 @@ _NB: Projects are automatically approved by default._
4554
pub struct Donation {
4655
/// Unique identifier for the donation
4756
pub id: DonationId,
48-
/// ID of the donor
57+
/// ID of the donor
4958
pub donor_id: AccountId,
50-
/// Amount donated
59+
/// Amount donated
5160
pub total_amount: U128,
5261
/// FT id (e.g. "near")
5362
pub ft_id: AccountId,
54-
/// Optional message from the donor
63+
/// Optional message from the donor
5564
pub message: Option<String>,
5665
/// Timestamp when the donation was made
5766
pub donated_at_ms: TimestampMs,
58-
/// ID of the account receiving the donation
67+
/// ID of the account receiving the donation
5968
pub recipient_id: AccountId,
6069
/// Protocol fee
6170
pub protocol_fee: U128,
@@ -181,25 +190,25 @@ Indicates that a `Donation` object has been created.
181190

182191
```json
183192
{
184-
"standard": "potlock",
185-
"version": "1.0.0",
186-
"event": "donation",
187-
"data": [
188-
{
189-
"donation": {
190-
"donated_at_ms": 1698948121940,
191-
"donor_id":"lachlan.near",
192-
"ft_id":"near",
193-
"id":9,
194-
"message": "Go go go!",
195-
"protocol_fee": "7000000000000000000000",
196-
"recipient_id": "magicbuild.near",
197-
"referrer_fee": "2000000000000000000000",
198-
"referrer_id": "plugrel.near",
199-
"total_amount": "100000000000000000000000"
200-
},
201-
}
202-
]
193+
"standard": "potlock",
194+
"version": "1.0.0",
195+
"event": "donation",
196+
"data": [
197+
{
198+
"donation": {
199+
"donated_at_ms": 1698948121940,
200+
"donor_id": "lachlan.near",
201+
"ft_id": "near",
202+
"id": 9,
203+
"message": "Go go go!",
204+
"protocol_fee": "7000000000000000000000",
205+
"recipient_id": "magicbuild.near",
206+
"referrer_fee": "2000000000000000000000",
207+
"referrer_id": "plugrel.near",
208+
"total_amount": "100000000000000000000000"
209+
}
210+
}
211+
]
203212
}
204213
```
205214

@@ -211,17 +220,17 @@ Indicates that `ContractSourceMetadata` object has been set/updated.
211220

212221
```json
213222
{
214-
"standard": "potlock",
215-
"version": "1.0.0",
216-
"event": "set_source_metadata",
217-
"data": [
218-
{
219-
"source_metadata": {
220-
"commit_hash":"ec02294253b22c2d4c50a75331df23ada9eb04db",
221-
"link":"https://github.com/PotLock/core",
222-
"version":"0.1.0",
223-
}
224-
}
225-
]
223+
"standard": "potlock",
224+
"version": "1.0.0",
225+
"event": "set_source_metadata",
226+
"data": [
227+
{
228+
"source_metadata": {
229+
"commit_hash": "ec02294253b22c2d4c50a75331df23ada9eb04db",
230+
"link": "https://github.com/PotLock/core",
231+
"version": "0.1.0"
232+
}
233+
}
234+
]
226235
}
227-
```
236+
```

contracts/donation/out/main.wasm

2.48 KB
Binary file not shown.

contracts/donation/src/donations.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ impl Contract {
198198
donation_ids_by_ft_set.insert(&donation.id);
199199
self.donation_ids_by_ft_id
200200
.insert(&donation.ft_id, &donation_ids_by_ft_set);
201+
// add to total donations amount
202+
self.total_donations_amount += donation.total_amount.0;
203+
// add to net donations amount
204+
let mut net_donation_amount = donation.total_amount.0 - donation.protocol_fee.0;
205+
if let Some(referrer_fee) = donation.referrer_fee {
206+
net_donation_amount -= referrer_fee.0;
207+
self.total_referrer_fees += referrer_fee.0;
208+
}
209+
self.net_donations_amount += net_donation_amount;
210+
// add to total protocol fees
211+
self.total_protocol_fees += donation.protocol_fee.0;
201212
}
202213

203214
// GETTERS

contracts/donation/src/lib.rs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,23 @@ pub use crate::utils::*;
2525
type DonationId = u64;
2626
type TimestampMs = u64;
2727

28-
/// Registry Contract
28+
/// DEPRECATED (V1) Registry Contract
29+
#[near_bindgen]
30+
#[derive(BorshDeserialize, BorshSerialize)]
31+
pub struct ContractV1 {
32+
/// Contract "source" metadata, as specified in NEP 0330 (https://github.com/near/NEPs/blob/master/neps/nep-0330.md), with addition of `commit_hash`
33+
contract_source_metadata: LazyOption<VersionedContractSourceMetadata>,
34+
owner: AccountId,
35+
protocol_fee_basis_points: u32,
36+
referral_fee_basis_points: u32,
37+
protocol_fee_recipient_account: AccountId,
38+
donations_by_id: UnorderedMap<DonationId, VersionedDonation>,
39+
donation_ids_by_recipient_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
40+
donation_ids_by_donor_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
41+
donation_ids_by_ft_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
42+
}
43+
44+
/// CURRENT Registry Contract
2945
#[near_bindgen]
3046
#[derive(BorshDeserialize, BorshSerialize)]
3147
pub struct Contract {
@@ -39,8 +55,13 @@ pub struct Contract {
3955
donation_ids_by_recipient_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
4056
donation_ids_by_donor_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
4157
donation_ids_by_ft_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
58+
total_donations_amount: Balance, // Add total_donations_amount to track total donations amount without iterating through all donations
59+
net_donations_amount: Balance, // Add net_donations_amount to track net donations amount (after fees) without iterating through all donations
60+
total_protocol_fees: Balance, // Add total_protocol_fees to track total protocol fees without iterating through all donations
61+
total_referrer_fees: Balance, // Add total_referrer_fees to track total referral fees without iterating through all donations
4262
}
4363

64+
4465
#[derive(BorshSerialize, BorshDeserialize)]
4566
pub enum VersionedContract {
4667
Current(Contract),
@@ -63,6 +84,11 @@ pub struct Config {
6384
pub protocol_fee_basis_points: u32,
6485
pub referral_fee_basis_points: u32,
6586
pub protocol_fee_recipient_account: AccountId,
87+
pub total_donations_amount: U128,
88+
pub net_donations_amount: U128,
89+
pub total_donations_count: U64,
90+
pub total_protocol_fees: U128,
91+
pub total_referrer_fees: U128,
6692
}
6793

6894
#[derive(BorshSerialize, BorshStorageKey)]
@@ -97,6 +123,10 @@ impl Contract {
97123
donation_ids_by_recipient_id: LookupMap::new(StorageKey::DonationIdsByRecipientId),
98124
donation_ids_by_donor_id: LookupMap::new(StorageKey::DonationIdsByDonorId),
99125
donation_ids_by_ft_id: LookupMap::new(StorageKey::DonationIdsByFtId),
126+
total_donations_amount: 0,
127+
net_donations_amount: 0,
128+
total_protocol_fees: 0,
129+
total_referrer_fees: 0,
100130
contract_source_metadata: LazyOption::new(
101131
StorageKey::SourceMetadata,
102132
Some(&VersionedContractSourceMetadata::Current(source_metadata)),
@@ -110,8 +140,55 @@ impl Contract {
110140
protocol_fee_basis_points: self.protocol_fee_basis_points,
111141
referral_fee_basis_points: self.referral_fee_basis_points,
112142
protocol_fee_recipient_account: self.protocol_fee_recipient_account.clone(),
143+
total_donations_amount: self.total_donations_amount.into(),
144+
net_donations_amount: self.net_donations_amount.into(),
145+
total_donations_count: self.donations_by_id.len().into(),
146+
total_protocol_fees: self.total_protocol_fees.into(),
147+
total_referrer_fees: self.total_referrer_fees.into(),
113148
}
114149
}
150+
151+
// LEAVING FOR REFERENCE - this is function used to migrate data in upgrade from v1.0.0 to v2.0.0
152+
// #[private]
153+
// pub fn migrate_chunk_temp(&mut self, donation_ids: Vec<DonationId>) {
154+
// for donation_id in donation_ids {
155+
// log!("Migrating donation {}", donation_id);
156+
// let donation = Donation::from(self
157+
// .donations_by_id
158+
// .get(&donation_id)
159+
// .expect(format!("Donation {} not found", donation_id).as_str()));
160+
// self.total_donations_amount += donation.total_amount.0;
161+
// let mut net_amount = donation.total_amount.0 - donation.protocol_fee.0;
162+
// self.total_protocol_fees += donation.protocol_fee.0;
163+
// if let Some(referral_fee) = donation.referrer_fee {
164+
// net_amount -= referral_fee.0;
165+
// self.total_referrer_fees += referral_fee.0;
166+
// }
167+
// self.net_donations_amount += net_amount;
168+
// }
169+
// }
170+
171+
// LEAVING FOR REFERENCE - this is the initFunction used in upgrade from v1.0.0 to v2.0.0
172+
// #[private]
173+
// #[init(ignore_state)]
174+
// pub fn migrate() -> Self {
175+
// let old_state: ContractV1 = env::state_read().expect("state read failed");
176+
// Self {
177+
// owner: old_state.owner,
178+
// protocol_fee_basis_points: old_state.protocol_fee_basis_points,
179+
// referral_fee_basis_points: old_state.referral_fee_basis_points,
180+
// protocol_fee_recipient_account: old_state.protocol_fee_recipient_account,
181+
// donations_by_id: old_state.donations_by_id,
182+
// donation_ids_by_recipient_id: old_state.donation_ids_by_recipient_id,
183+
// donation_ids_by_donor_id: old_state.donation_ids_by_donor_id,
184+
// donation_ids_by_ft_id: old_state.donation_ids_by_ft_id,
185+
// total_donations_amount: 0,
186+
// net_donations_amount: 0,
187+
// total_protocol_fees: 0,
188+
// total_referrer_fees: 0,
189+
// contract_source_metadata: old_state.contract_source_metadata,
190+
// }
191+
// }
115192
}
116193

117194
impl Default for Contract {
@@ -125,6 +202,10 @@ impl Default for Contract {
125202
donation_ids_by_recipient_id: LookupMap::new(StorageKey::DonationIdsByRecipientId),
126203
donation_ids_by_donor_id: LookupMap::new(StorageKey::DonationIdsByDonorId),
127204
donation_ids_by_ft_id: LookupMap::new(StorageKey::DonationIdsByFtId),
205+
total_donations_amount: 0,
206+
net_donations_amount: 0,
207+
total_protocol_fees: 0,
208+
total_referrer_fees: 0,
128209
contract_source_metadata: LazyOption::new(
129210
StorageKey::SourceMetadata,
130211
Some(&VersionedContractSourceMetadata::Current(

0 commit comments

Comments
 (0)