Skip to content

Commit

Permalink
merged proxy contract branch
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Dec 3, 2024
2 parents 1031918 + 7a08e59 commit f0d81c9
Show file tree
Hide file tree
Showing 14 changed files with 725 additions and 715 deletions.
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 0 additions & 35 deletions contracts/icp/context-config/src/error.rs

This file was deleted.

4 changes: 0 additions & 4 deletions contracts/icp/context-config/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ impl ICContextId {
pub fn new(bytes: [u8; 32]) -> Self {
Self(Identity(bytes))
}

pub fn as_bytes(&self) -> [u8; 32] {
self.0.as_bytes()
}
}

impl ReprBytes for ICContextId {
Expand Down
1 change: 1 addition & 0 deletions contracts/icp/context-config/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use calimero_context_config::repr::ReprBytes;
use candid::Principal;
use context_contract::types::{
ContextRequest, ContextRequestKind, ICApplication, ICApplicationId, ICBlobId, ICCapability,
Expand Down
1 change: 1 addition & 0 deletions contracts/icp/proxy-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ed25519-dalek.workspace = true
hex.workspace = true
ic-cdk = "0.16"
ic-cdk-macros = "0.16"
ic-ledger-types = "0.14.0"
serde = { version = "1.0", features = ["derive"] }
thiserror.workspace = true

Expand Down
53 changes: 48 additions & 5 deletions contracts/icp/proxy-contract/mock/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion contracts/icp/proxy-contract/mock/ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ crate-type = ["cdylib"]
candid = "0.10"
serde = { version = "1.0", features = ["derive"] }
ic-cdk = "0.16"
ic-cdk-macros = "0.16"
ic-cdk-macros = "0.16"
ic-ledger-types = "0.14.0"
53 changes: 37 additions & 16 deletions contracts/icp/proxy-contract/mock/ledger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,58 @@
use std::cell::RefCell;
use candid::{CandidType, Deserialize, Principal};
use ic_ledger_types::{Tokens, AccountIdentifier, Memo, TransferArgs, Timestamp, BlockIndex, TransferError};

thread_local! {
static BALANCE: RefCell<u64> = RefCell::new(1_000_000_000);
}

#[derive(CandidType, Deserialize)]
struct TransferArgs {
to: Principal,
amount: u128,
}
type TransferResult = Result<BlockIndex, TransferError>;

#[ic_cdk::update]
fn transfer(args: Vec<u8>) {
let transfer_args: TransferArgs = candid::decode_one(&args)
.expect("Failed to decode transfer args");

fn transfer(args: TransferArgs) -> TransferResult {
ic_cdk::println!("Mock ledger received transfer: to={:?}, amount={}",
transfer_args.to, transfer_args.amount);

args.to, args.amount);

// Verify fee
if args.fee.e8s() != 10_000 {
return Err(TransferError::BadFee {
expected_fee: Tokens::from_e8s(10_000)
});
}

let amount_e8s = args.amount.e8s();

BALANCE.with(|balance| {
let mut bal = balance.borrow_mut();
*bal = bal.saturating_sub(transfer_args.amount.try_into().unwrap());

// Check if we have enough balance
if amount_e8s > *bal {
return Err(TransferError::InsufficientFunds {
balance: Tokens::from_e8s(*bal)
});
}

// Subtract amount and fee
*bal = bal.saturating_sub(amount_e8s);
*bal = bal.saturating_sub(args.fee.e8s());

ic_cdk::println!("New balance: {}", *bal);
});

// Return mock block index
Ok(1)
})
}

#[ic_cdk::query]
fn balance() -> u128 {
fn account_balance(args: AccountBalanceArgs) -> Tokens {
BALANCE.with(|balance| {
let bal = *balance.borrow();
bal.into()
Tokens::from_e8s(*balance.borrow())
})
}

#[derive(CandidType, Deserialize)]
struct AccountBalanceArgs {
account: AccountIdentifier,
}

ic_cdk::export_candid!();
1 change: 0 additions & 1 deletion contracts/icp/proxy-contract/proxy_contract.did
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ service : (blob, principal) -> {
get_active_proposals_limit : () -> (nat32) query;
get_confirmations_count : (blob) -> (opt ICProposalWithApprovals) query;
get_context_value : (blob) -> (opt blob) query;
get_ledger_id : () -> (principal) query;
get_num_approvals : () -> (nat32) query;
get_proposal_approvals_with_signer : (blob) -> (
vec ICProposalApprovalWithSigner,
Expand Down
34 changes: 33 additions & 1 deletion contracts/icp/proxy-contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet, HashMap};

use candid::Principal;
use serde::{Deserialize, Serialize};
use types::{ICContextId, LedgerId};

use crate::types::{
ICPSigned, ICProposal, ICProposalApprovalWithSigner, ICProposalId, ICProposalWithApprovals,
ICProxyContract, ICRequest, ICSignerId,
ICRequest, ICSignerId,
};

pub mod mutate;
pub mod query;
pub mod types;

#[derive(Serialize, Deserialize, Default)]
pub struct ICProxyContract {
pub context_id: ICContextId,
pub context_config_id: String,
pub num_approvals: u32,
pub proposals: BTreeMap<ICProposalId, ICProposal>,
pub approvals: BTreeMap<ICProposalId, BTreeSet<ICSignerId>>,
pub num_proposals_pk: BTreeMap<ICSignerId, u32>,
pub active_proposals_limit: u32,
pub context_storage: HashMap<Vec<u8>, Vec<u8>>,
pub ledger_id: LedgerId,
}

impl ICProxyContract {
pub fn new(context_id: ICContextId, ledger_id: Principal) -> Self {
Self {
context_id,
context_config_id: ic_cdk::caller().to_string(),
num_approvals: 3,
proposals: BTreeMap::new(),
approvals: BTreeMap::new(),
num_proposals_pk: BTreeMap::new(),
active_proposals_limit: 10,
context_storage: HashMap::new(),
ledger_id: ledger_id.into(),
}
}
}

thread_local! {
static PROXY_CONTRACT: RefCell<ICProxyContract> = RefCell::new(ICProxyContract::default());
}
Expand Down
Loading

0 comments on commit f0d81c9

Please sign in to comment.