Skip to content

Commit

Permalink
Fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
CostinCarabas committed Aug 19, 2024
1 parent d7c4857 commit 906e146
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 121 deletions.
16 changes: 0 additions & 16 deletions contracts/potlock/interact-rs/src/potlock_interactor_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ async fn main() {
"getFeeTokenIdentifier" => interact.fee_token_identifier().await,
"getFeeAmount" => interact.fee_amount().await,
"feePotPayments" => interact.fee_pot_proposer().await,
"feeAmountAcceptPots" => interact.fee_amount_accepted_pots().await,
"potDonations" => interact.pot_donations().await,
"projectDonations" => interact.project_donations().await,
"isAdmin" => interact.is_admin().await,
Expand Down Expand Up @@ -400,21 +399,6 @@ impl ContractInteract {
println!("Result: {result_value:?}");
}

async fn fee_amount_accepted_pots(&mut self) {
let result_value = self
.interactor
.query()
.to(self.state.current_address())
.typed(proxy::PotlockProxy)
.fee_amount_accepted_pots()
.returns(ReturnsResultUnmanaged)
.prepare_async()
.run()
.await;

println!("Result: {result_value:?}");
}

async fn pot_donations(&mut self) {
let project_id = 0u32;

Expand Down
9 changes: 0 additions & 9 deletions contracts/potlock/interact-rs/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,6 @@ where
.original_result()
}

pub fn fee_amount_accepted_pots(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, BigUint<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("feeAmountAcceptPots")
.original_result()
}

pub fn pot_donations<
Arg0: ProxyArg<usize>,
>(
Expand Down
2 changes: 2 additions & 0 deletions contracts/potlock/src/potlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ multiversx_sc::imports!();
multiversx_sc::derive_imports!();
pub mod potlock_admin_interactions;
pub mod potlock_interactions;
pub mod potlock_requirements;
pub mod potlock_storage;

#[multiversx_sc::contract]
pub trait Potlock:
potlock_admin_interactions::PotlockAdminInteractions
+ potlock_interactions::PotlockInteractions
+ potlock_requirements::PotlockRequirements
+ potlock_storage::PotlockStorage
+ multiversx_sc_modules::only_admin::OnlyAdminModule
{
Expand Down
57 changes: 39 additions & 18 deletions contracts/potlock/src/potlock_admin_interactions.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use crate::potlock_storage::{self, PotlockId, ProjectId, Status};
use crate::{
potlock_requirements::{self, MAX_PERCENTAGE},
potlock_storage::{self, PotlockId, ProjectId, Status},
};

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

pub type ProjectPercentage = MultiValue2<usize, u64>;
pub const MAX_PERCENTAGE: u64 = 10_000; // 100%

#[multiversx_sc::module]
pub trait PotlockAdminInteractions:
potlock_storage::PotlockStorage + multiversx_sc_modules::only_admin::OnlyAdminModule
potlock_requirements::PotlockRequirements
+ potlock_storage::PotlockStorage
+ multiversx_sc_modules::only_admin::OnlyAdminModule
{
#[only_admin]
#[endpoint(changeFeeForPots)]
Expand All @@ -17,22 +21,17 @@ pub trait PotlockAdminInteractions:
token_identifier.is_valid_esdt_identifier(),
"Invalid token provided"
);
require!(
token_identifier.is_valid_esdt_identifier() && fee.gt(&BigUint::zero()),
"Invalid token identifier or amount is 0"
);
require!(fee > 0, "Amount is 0");
self.fee_token_identifier().set(&token_identifier);
self.fee_amount().set(fee);
self.fee_amount().set_if_empty(fee);
}

#[only_admin]
#[endpoint(acceptPot)]
fn accept_pot(&self, potlock_id: PotlockId) {
self.require_potlock_exists(potlock_id);
let fee_amount = self.fee_amount().get();
self.require_potlock_is_inactive(potlock_id);

self.fee_amount_accepted_pots()
.update(|amount| *amount += fee_amount);
let mut accepted_potlock = self.potlocks().get(potlock_id);
accepted_potlock.status = Status::Active;
self.potlocks().set(potlock_id, &accepted_potlock);
Expand All @@ -42,6 +41,7 @@ pub trait PotlockAdminInteractions:
#[endpoint(removePot)]
fn remove_pot(&self, potlock_id: PotlockId) {
self.require_potlock_exists(potlock_id);
self.require_potlock_is_inactive(potlock_id);

let potlock_mapper = self.potlocks();
let pot_proposer = potlock_mapper.get(potlock_id).proposer;
Expand All @@ -60,22 +60,35 @@ pub trait PotlockAdminInteractions:
#[endpoint(acceptApplication)]
fn accept_application(&self, project_id: ProjectId) {
self.require_project_exists(project_id);
let mut accepted_projects = self.projects().get(project_id);
accepted_projects.status = Status::Active;
self.projects().set(project_id, &accepted_projects);
self.require_project_is_inactive(project_id);

let mut accepted_project = self.projects().get(project_id);
accepted_project.status = Status::Active;
self.projects().set(project_id, &accepted_project);
}

#[only_admin]
#[endpoint(removeApplication)]
fn remove_application(&self, project_id: ProjectId) {
self.require_project_exists(project_id);
self.require_project_is_active(project_id);

let mut rejected_project = self.projects().get(project_id);
rejected_project.status = Status::Inactive;
self.projects().set(project_id, &rejected_project);
}

#[only_admin]
#[endpoint(rejectDonation)]
fn reject_donation(&self, potlock_id: PotlockId, user: ManagedAddress) {
self.require_potlock_exists(potlock_id);
let opt_fee_pot_payments = self.pot_donations(potlock_id).get(&user);
let opt_fee_pot_payment = self.pot_donations(potlock_id).get(&user);

require!(opt_fee_pot_payments.is_some(), "No donation for this user");
let fee_pot_payments = unsafe { opt_fee_pot_payments.unwrap_unchecked() };
require!(opt_fee_pot_payment.is_some(), "No donation for this user");
let fee_pot_payment = unsafe { opt_fee_pot_payment.unwrap_unchecked() };

self.send()
.direct_non_zero_esdt_payment(&user, &fee_pot_payments);
.direct_non_zero_esdt_payment(&user, &fee_pot_payment);
self.pot_donations(potlock_id).remove(&user);
}

Expand All @@ -89,9 +102,17 @@ pub trait PotlockAdminInteractions:
self.require_potlock_exists(potlock_id);
self.require_correct_percentages(project_percentages.clone());
let pot_donations = self.pot_donations(potlock_id);
let all_projects = self.projects();

for pp in project_percentages {
let (project_id, percentage) = pp.into_tuple();
let project = all_projects.get(project_id);

// The project must previously apply to this Pot
if project.potlock_id != potlock_id {
continue;
}

let mut output_payments = ManagedVec::new();
for (_, donation) in pot_donations.iter() {
let project_share_amount = donation.amount * percentage / MAX_PERCENTAGE;
Expand Down
35 changes: 24 additions & 11 deletions contracts/potlock/src/potlock_interactions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::potlock_requirements;
use crate::potlock_storage::{self, Pot, Project};
use crate::potlock_storage::{PotlockId, ProjectId};

Expand All @@ -6,7 +7,9 @@ multiversx_sc::derive_imports!();

#[multiversx_sc::module]
pub trait PotlockInteractions:
potlock_storage::PotlockStorage + multiversx_sc_modules::only_admin::OnlyAdminModule
potlock_requirements::PotlockRequirements
+ potlock_storage::PotlockStorage
+ multiversx_sc_modules::only_admin::OnlyAdminModule
{
#[payable("*")]
#[endpoint(addPot)]
Expand All @@ -33,21 +36,32 @@ pub trait PotlockInteractions:
potlock_id: PotlockId,
project_name: ManagedBuffer,
description: ManagedBuffer,
) {
let project_id = self.projects().len() + 1;
) -> usize {
let owner = self.blockchain().get_caller();
let project = Project::new(project_id, potlock_id, project_name, description, owner);
self.projects().push(&project);
let project = Project::new(potlock_id, project_name, description, owner);
self.projects().push(&project)
}

#[payable("*")]
#[endpoint(donateToPot)]
fn donate_to_pot(&self, potlock_id: PotlockId) {
let payment = self.call_value().single_esdt();
let caller = self.blockchain().get_caller();
self.require_potlock_exists(potlock_id);
self.require_potlock_is_active(potlock_id);
self.pot_donations(potlock_id).insert(caller, payment);

let payment = self.call_value().single_esdt();
let caller = self.blockchain().get_caller();
let mut pot_donations = self.pot_donations(potlock_id);

match pot_donations.get(&caller) {
Some(mut previous_payment) => {
// let a = pot_donations.get(&caller).unwrap();
previous_payment.amount += payment.amount;
pot_donations.insert(caller, previous_payment);
}
None => {
self.pot_donations(potlock_id).insert(caller, payment);
}
}
}

#[payable("*")]
Expand All @@ -60,16 +74,15 @@ pub trait PotlockInteractions:

let mut donation_mapper = self.project_donations(project_id);
if donation_mapper.contains_key(&caller) {
let opt_payment = donation_mapper.remove(&caller);
let opt_payment = donation_mapper.get(&caller);
if opt_payment.is_some() {
let mut previous_payment = opt_payment.unwrap();
require!(
previous_payment.token_identifier == payment.token_identifier.clone(),
"Already made a payment with a different TokenID"
);
previous_payment.amount += payment.amount;
self.project_donations(project_id)
.insert(caller, previous_payment);
donation_mapper.insert(caller, previous_payment);
}
} else {
self.project_donations(project_id).insert(caller, payment);
Expand Down
69 changes: 69 additions & 0 deletions contracts/potlock/src/potlock_requirements.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::{
potlock_admin_interactions::ProjectPercentage,
potlock_storage::{self, PotlockId, ProjectId, Status},
};

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

pub const MAX_PERCENTAGE: u64 = 10_000; // 100%

#[multiversx_sc::module]
pub trait PotlockRequirements: potlock_storage::PotlockStorage {
fn is_valid_potlock_id(&self, potlock_id: PotlockId) -> bool {
potlock_id >= 1 && potlock_id <= self.potlocks().len()
}

fn require_potlock_exists(&self, potlock_id: PotlockId) {
require!(
self.is_valid_potlock_id(potlock_id) && !self.potlocks().item_is_empty(potlock_id),
"Potlock doesn't exist!",
)
}

fn require_potlock_is_active(&self, potlock_id: PotlockId) {
let potlock = self.potlocks().get(potlock_id);
require!(potlock.status == Status::Active, "Pot is not active!",)
}

fn require_potlock_is_inactive(&self, potlock_id: PotlockId) {
let potlock = self.potlocks().get(potlock_id);
require!(potlock.status != Status::Active, "Pot is active!",)
}

fn is_valid_project_id(&self, project_id: ProjectId) -> bool {
project_id >= 1 && project_id <= self.projects().len()
}

fn require_project_exists(&self, project_id: ProjectId) {
require!(
self.is_valid_project_id(project_id) && !self.projects().item_is_empty(project_id),
"Project doesn't exist!",
)
}

fn require_project_is_active(&self, project_id: ProjectId) {
let project = self.projects().get(project_id);
require!(project.status == Status::Active, "Project is not active!",)
}

fn require_project_is_inactive(&self, project_id: ProjectId) {
let project = self.projects().get(project_id);
require!(project.status != Status::Active, "Project is active!",)
}

fn require_correct_percentages(
&self,
project_percentages: MultiValueEncoded<ProjectPercentage>,
) {
let mut total_perc: u64 = 0;
for pp in project_percentages {
let (_, perc) = pp.into_tuple();
total_perc += perc;
}
require!(
total_perc <= MAX_PERCENTAGE,
"Total percentages more than 100%"
);
}
}
Loading

0 comments on commit 906e146

Please sign in to comment.