Skip to content

Commit

Permalink
Merge pull request #58 from PotLock/feat/donation-totals
Browse files Browse the repository at this point in the history
Feat/donation totals
  • Loading branch information
lachlanglen authored Feb 20, 2024
2 parents 2db43b1 + c462685 commit 5fde52b
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 37 deletions.
81 changes: 45 additions & 36 deletions contracts/donation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub struct Contract {
donation_ids_by_recipient_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
donation_ids_by_donor_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
donation_ids_by_ft_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
total_donations_amount: Balance, // Added total_donations_amount to track total donations amount without iterating through all donations
net_donations_amount: Balance, // Added net_donations_amount to track net donations amount (after fees) without iterating through all donations
total_protocol_fees: Balance, // Added total_protocol_fees to track total protocol fees without iterating through all donations
total_referrer_fees: Balance, // Added total_referrer_fees to track total referral fees without iterating through all donations
}

/// NOT stored in contract storage; only used for get_config response
Expand All @@ -34,6 +38,11 @@ pub struct Config {
pub protocol_fee_basis_points: u32,
pub referral_fee_basis_points: u32,
pub protocol_fee_recipient_account: AccountId,
pub total_donations_amount: U128,
pub net_donations_amount: U128,
pub total_donations_count: U64,
pub total_protocol_fees: U128,
pub total_referrer_fees: U128,
}
```

Expand All @@ -45,17 +54,17 @@ _NB: Projects are automatically approved by default._
pub struct Donation {
/// Unique identifier for the donation
pub id: DonationId,
/// ID of the donor
/// ID of the donor
pub donor_id: AccountId,
/// Amount donated
/// Amount donated
pub total_amount: U128,
/// FT id (e.g. "near")
pub ft_id: AccountId,
/// Optional message from the donor
/// Optional message from the donor
pub message: Option<String>,
/// Timestamp when the donation was made
pub donated_at_ms: TimestampMs,
/// ID of the account receiving the donation
/// ID of the account receiving the donation
pub recipient_id: AccountId,
/// Protocol fee
pub protocol_fee: U128,
Expand Down Expand Up @@ -181,25 +190,25 @@ Indicates that a `Donation` object has been created.

```json
{
"standard": "potlock",
"version": "1.0.0",
"event": "donation",
"data": [
{
"donation": {
"donated_at_ms": 1698948121940,
"donor_id":"lachlan.near",
"ft_id":"near",
"id":9,
"message": "Go go go!",
"protocol_fee": "7000000000000000000000",
"recipient_id": "magicbuild.near",
"referrer_fee": "2000000000000000000000",
"referrer_id": "plugrel.near",
"total_amount": "100000000000000000000000"
},
}
]
"standard": "potlock",
"version": "1.0.0",
"event": "donation",
"data": [
{
"donation": {
"donated_at_ms": 1698948121940,
"donor_id": "lachlan.near",
"ft_id": "near",
"id": 9,
"message": "Go go go!",
"protocol_fee": "7000000000000000000000",
"recipient_id": "magicbuild.near",
"referrer_fee": "2000000000000000000000",
"referrer_id": "plugrel.near",
"total_amount": "100000000000000000000000"
}
}
]
}
```

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

```json
{
"standard": "potlock",
"version": "1.0.0",
"event": "set_source_metadata",
"data": [
{
"source_metadata": {
"commit_hash":"ec02294253b22c2d4c50a75331df23ada9eb04db",
"link":"https://github.com/PotLock/core",
"version":"0.1.0",
}
}
]
"standard": "potlock",
"version": "1.0.0",
"event": "set_source_metadata",
"data": [
{
"source_metadata": {
"commit_hash": "ec02294253b22c2d4c50a75331df23ada9eb04db",
"link": "https://github.com/PotLock/core",
"version": "0.1.0"
}
}
]
}
```
```
Binary file modified contracts/donation/out/main.wasm
Binary file not shown.
11 changes: 11 additions & 0 deletions contracts/donation/src/donations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ impl Contract {
donation_ids_by_ft_set.insert(&donation.id);
self.donation_ids_by_ft_id
.insert(&donation.ft_id, &donation_ids_by_ft_set);
// add to total donations amount
self.total_donations_amount += donation.total_amount.0;
// add to net donations amount
let mut net_donation_amount = donation.total_amount.0 - donation.protocol_fee.0;
if let Some(referrer_fee) = donation.referrer_fee {
net_donation_amount -= referrer_fee.0;
self.total_referrer_fees += referrer_fee.0;
}
self.net_donations_amount += net_donation_amount;
// add to total protocol fees
self.total_protocol_fees += donation.protocol_fee.0;
}

// GETTERS
Expand Down
83 changes: 82 additions & 1 deletion contracts/donation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,23 @@ pub use crate::utils::*;
type DonationId = u64;
type TimestampMs = u64;

/// Registry Contract
/// DEPRECATED (V1) Registry Contract
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct ContractV1 {
/// Contract "source" metadata, as specified in NEP 0330 (https://github.com/near/NEPs/blob/master/neps/nep-0330.md), with addition of `commit_hash`
contract_source_metadata: LazyOption<VersionedContractSourceMetadata>,
owner: AccountId,
protocol_fee_basis_points: u32,
referral_fee_basis_points: u32,
protocol_fee_recipient_account: AccountId,
donations_by_id: UnorderedMap<DonationId, VersionedDonation>,
donation_ids_by_recipient_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
donation_ids_by_donor_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
donation_ids_by_ft_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
}

/// CURRENT Registry Contract
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Contract {
Expand All @@ -39,8 +55,13 @@ pub struct Contract {
donation_ids_by_recipient_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
donation_ids_by_donor_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
donation_ids_by_ft_id: LookupMap<AccountId, UnorderedSet<DonationId>>,
total_donations_amount: Balance, // Add total_donations_amount to track total donations amount without iterating through all donations
net_donations_amount: Balance, // Add net_donations_amount to track net donations amount (after fees) without iterating through all donations
total_protocol_fees: Balance, // Add total_protocol_fees to track total protocol fees without iterating through all donations
total_referrer_fees: Balance, // Add total_referrer_fees to track total referral fees without iterating through all donations
}


#[derive(BorshSerialize, BorshDeserialize)]
pub enum VersionedContract {
Current(Contract),
Expand All @@ -63,6 +84,11 @@ pub struct Config {
pub protocol_fee_basis_points: u32,
pub referral_fee_basis_points: u32,
pub protocol_fee_recipient_account: AccountId,
pub total_donations_amount: U128,
pub net_donations_amount: U128,
pub total_donations_count: U64,
pub total_protocol_fees: U128,
pub total_referrer_fees: U128,
}

#[derive(BorshSerialize, BorshStorageKey)]
Expand Down Expand Up @@ -97,6 +123,10 @@ impl Contract {
donation_ids_by_recipient_id: LookupMap::new(StorageKey::DonationIdsByRecipientId),
donation_ids_by_donor_id: LookupMap::new(StorageKey::DonationIdsByDonorId),
donation_ids_by_ft_id: LookupMap::new(StorageKey::DonationIdsByFtId),
total_donations_amount: 0,
net_donations_amount: 0,
total_protocol_fees: 0,
total_referrer_fees: 0,
contract_source_metadata: LazyOption::new(
StorageKey::SourceMetadata,
Some(&VersionedContractSourceMetadata::Current(source_metadata)),
Expand All @@ -110,8 +140,55 @@ impl Contract {
protocol_fee_basis_points: self.protocol_fee_basis_points,
referral_fee_basis_points: self.referral_fee_basis_points,
protocol_fee_recipient_account: self.protocol_fee_recipient_account.clone(),
total_donations_amount: self.total_donations_amount.into(),
net_donations_amount: self.net_donations_amount.into(),
total_donations_count: self.donations_by_id.len().into(),
total_protocol_fees: self.total_protocol_fees.into(),
total_referrer_fees: self.total_referrer_fees.into(),
}
}

// LEAVING FOR REFERENCE - this is function used to migrate data in upgrade from v1.0.0 to v2.0.0
// #[private]
// pub fn migrate_chunk_temp(&mut self, donation_ids: Vec<DonationId>) {
// for donation_id in donation_ids {
// log!("Migrating donation {}", donation_id);
// let donation = Donation::from(self
// .donations_by_id
// .get(&donation_id)
// .expect(format!("Donation {} not found", donation_id).as_str()));
// self.total_donations_amount += donation.total_amount.0;
// let mut net_amount = donation.total_amount.0 - donation.protocol_fee.0;
// self.total_protocol_fees += donation.protocol_fee.0;
// if let Some(referral_fee) = donation.referrer_fee {
// net_amount -= referral_fee.0;
// self.total_referrer_fees += referral_fee.0;
// }
// self.net_donations_amount += net_amount;
// }
// }

// LEAVING FOR REFERENCE - this is the initFunction used in upgrade from v1.0.0 to v2.0.0
// #[private]
// #[init(ignore_state)]
// pub fn migrate() -> Self {
// let old_state: ContractV1 = env::state_read().expect("state read failed");
// Self {
// owner: old_state.owner,
// protocol_fee_basis_points: old_state.protocol_fee_basis_points,
// referral_fee_basis_points: old_state.referral_fee_basis_points,
// protocol_fee_recipient_account: old_state.protocol_fee_recipient_account,
// donations_by_id: old_state.donations_by_id,
// donation_ids_by_recipient_id: old_state.donation_ids_by_recipient_id,
// donation_ids_by_donor_id: old_state.donation_ids_by_donor_id,
// donation_ids_by_ft_id: old_state.donation_ids_by_ft_id,
// total_donations_amount: 0,
// net_donations_amount: 0,
// total_protocol_fees: 0,
// total_referrer_fees: 0,
// contract_source_metadata: old_state.contract_source_metadata,
// }
// }
}

impl Default for Contract {
Expand All @@ -125,6 +202,10 @@ impl Default for Contract {
donation_ids_by_recipient_id: LookupMap::new(StorageKey::DonationIdsByRecipientId),
donation_ids_by_donor_id: LookupMap::new(StorageKey::DonationIdsByDonorId),
donation_ids_by_ft_id: LookupMap::new(StorageKey::DonationIdsByFtId),
total_donations_amount: 0,
net_donations_amount: 0,
total_protocol_fees: 0,
total_referrer_fees: 0,
contract_source_metadata: LazyOption::new(
StorageKey::SourceMetadata,
Some(&VersionedContractSourceMetadata::Current(
Expand Down

0 comments on commit 5fde52b

Please sign in to comment.