Skip to content

Commit

Permalink
feat: proxy client impl (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
xilosada authored Nov 4, 2024
1 parent 410df0a commit 2a3aa2a
Show file tree
Hide file tree
Showing 13 changed files with 402 additions and 140 deletions.
10 changes: 6 additions & 4 deletions contracts/proxy-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub struct ProxyContract {
pub context_id: ContextId,
pub context_config_account_id: AccountId,
pub num_approvals: u32,
pub proposal_nonce: ProposalId,
pub proposals: IterableMap<ProposalId, Proposal>,
pub approvals: IterableMap<ProposalId, HashSet<SignerId>>,
pub num_proposals_pk: IterableMap<SignerId, u32>,
Expand All @@ -51,7 +50,6 @@ impl ProxyContract {
Self {
context_id: context_id.rt().expect("Invalid context id"),
context_config_account_id,
proposal_nonce: 0,
proposals: IterableMap::new(b"r".to_vec()),
approvals: IterableMap::new(b"c".to_vec()),
num_proposals_pk: IterableMap::new(b"k".to_vec()),
Expand All @@ -61,17 +59,21 @@ impl ProxyContract {
}
}

pub fn proposals(&self, offset: usize, length: usize) -> Vec<(&u32, &Proposal)> {
pub fn proposals(&self, offset: usize, length: usize) -> Vec<&Proposal> {
let effective_len = (self.proposals.len() as usize)
.saturating_sub(offset)
.min(length);
let mut proposals = Vec::with_capacity(effective_len);
for proposal in self.proposals.iter().skip(offset).take(length) {
proposals.push(proposal);
proposals.push(proposal.1);
}
proposals
}

pub fn proposal(&self, proposal_id: &ProposalId) -> Option<Proposal> {
self.proposals.get(proposal_id).cloned()
}

pub fn get_confirmations_count(
&self,
proposal_id: ProposalId,
Expand Down
23 changes: 12 additions & 11 deletions contracts/proxy-lib/src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use calimero_context_config::{
ProposalAction, ProposalId, ProposalWithApprovals, ProxyMutateRequest,
};
use near_sdk::{
env, near, AccountId, Gas, NearToken, Promise, PromiseError, PromiseOrValue, PromiseResult,
env, near, require, AccountId, Gas, NearToken, Promise, PromiseError, PromiseOrValue,
PromiseResult,
};

use super::{Proposal, ProxyContract, ProxyContractExt, Signed};
Expand Down Expand Up @@ -60,18 +61,13 @@ impl ProxyContract {
self.num_proposals_pk
.insert(*proposal.author_id, num_proposals);

let proposal_id = self.proposal_nonce;

self.proposals.insert(proposal_id, proposal.clone());
self.approvals.insert(proposal_id, HashSet::new());
self.proposals.insert(proposal.id, proposal.clone());
self.approvals.insert(proposal.id, HashSet::new());
self.internal_confirm(
proposal_id,
proposal.id,
proposal.author_id.rt().expect("Invalid signer"),
);

self.proposal_nonce += 1;

self.build_proposal_response(proposal_id)
self.build_proposal_response(proposal.id)
}

#[private]
Expand Down Expand Up @@ -120,6 +116,7 @@ impl ProxyContract {

if promise_actions.is_empty() {
self.finalize_execution(Proposal {
id: proposal.id,
author_id: proposal.author_id,
actions: non_promise_actions,
});
Expand Down Expand Up @@ -166,6 +163,7 @@ impl ProxyContract {
match chained_promise {
Some(promise) => PromiseOrValue::Promise(promise.then(
Self::ext(env::current_account_id()).finalize_execution(Proposal {
id: proposal.id,
author_id: proposal.author_id,
actions: non_promise_actions,
}),
Expand Down Expand Up @@ -203,8 +201,11 @@ impl ProxyContract {

impl ProxyContract {
fn propose(&self, proposal: Proposal) -> Promise {
require!(
!self.proposals.contains_key(&proposal.id),
"Proposal already exists"
);
let author_id = proposal.author_id;

let num_proposals = self.num_proposals_pk.get(&author_id).unwrap_or(&0) + 1;
assert!(
num_proposals <= self.active_proposals_limit,
Expand Down
17 changes: 16 additions & 1 deletion contracts/proxy-lib/tests/common/proxy_lib_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ impl ProxyContractHelper {

pub fn create_proposal_request(
&self,
id: &ProposalId,
author: &SigningKey,
actions: &Vec<ProposalAction>,
) -> eyre::Result<Signed<ProxyMutateRequest>> {
let request = ProxyMutateRequest::Propose {
proposal: Proposal {
id: id.clone(),
author_id: author.verifying_key().rt().expect("Invalid signer"),
actions: actions.clone(),
},
Expand Down Expand Up @@ -148,12 +150,25 @@ impl ProxyContractHelper {
caller: &Account,
offset: usize,
length: usize,
) -> eyre::Result<Vec<(u32, Proposal)>> {
) -> eyre::Result<Vec<Proposal>> {
let res = caller
.view(self.proxy_contract.id(), "proposals")
.args_json(json!({ "offset": offset, "length": length }))
.await?
.json()?;
Ok(res)
}

pub async fn view_proposal(
&self,
caller: &Account,
id: &ProposalId,
) -> eyre::Result<Option<Proposal>> {
let res = caller
.view(self.proxy_contract.id(), "proposal")
.args_json(json!({ "proposal_id": id }))
.await?
.json()?;
Ok(res)
}
}
Loading

0 comments on commit 2a3aa2a

Please sign in to comment.