Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into swap
Browse files Browse the repository at this point in the history
# Conflicts:
#	frontend/app/src/components/home/profile/Accounts.svelte
  • Loading branch information
megrogan committed Dec 4, 2023
2 parents fe92552 + e49decc commit 65af2b7
Show file tree
Hide file tree
Showing 178 changed files with 2,228 additions and 879 deletions.
44 changes: 44 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ members = [
"backend/canisters/community/impl",
"backend/canisters/cycles_dispenser/api",
"backend/canisters/cycles_dispenser/impl",
"backend/canisters/escrow/api",
"backend/canisters/escrow/impl",
"backend/canisters/group/api",
"backend/canisters/group/c2c_client",
"backend/canisters/group/client",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ async fn process_action(action: Action) {
token: Cryptocurrency::CKBTC,
amount: amount as u128,
fee: 10,
from: icrc1::CryptoAccount::Account(from),
to: icrc1::CryptoAccount::Account(Account::from(Principal::from(user_id))),
from: from.into(),
to: Account::from(Principal::from(user_id)).into(),
memo: None,
created: now_nanos,
block_index: block_index.0.try_into().unwrap(),
Expand Down
1 change: 1 addition & 0 deletions backend/canister_installer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ candid = { workspace = true }
canister_agent_utils = { path = "../libraries/canister_agent_utils" }
clap = { workspace = true, features = ["derive"] }
cycles_dispenser_canister = { path = "../canisters/cycles_dispenser/api" }
escrow_canister = { path = "../canisters/escrow/api" }
futures = { workspace = true }
group_index_canister = { path = "../canisters/group_index/api" }
group_index_canister_client = { path = "../canisters/group_index/client" }
Expand Down
17 changes: 17 additions & 0 deletions backend/canister_installer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ async fn install_service_canisters_impl(
set_controllers(management_canister, &canister_ids.cycles_dispenser, controllers.clone()),
set_controllers(management_canister, &canister_ids.registry, controllers.clone()),
set_controllers(management_canister, &canister_ids.market_maker, controllers.clone()),
set_controllers(management_canister, &canister_ids.neuron_controller, controllers.clone()),
set_controllers(management_canister, &canister_ids.escrow, controllers.clone()),
set_controllers(
management_canister,
&canister_ids.local_user_index,
Expand Down Expand Up @@ -185,6 +187,13 @@ async fn install_service_canisters_impl(
test_mode,
};

let escrow_canister_wasm = get_canister_wasm(CanisterName::Escrow, version);
let escrow_init_args = escrow_canister::init::Args {
cycles_dispenser_canister_id: canister_ids.cycles_dispenser,
wasm_version: version,
test_mode,
};

futures::future::join5(
install_wasm(
management_canister,
Expand Down Expand Up @@ -253,6 +262,14 @@ async fn install_service_canisters_impl(
)
.await;

install_wasm(
management_canister,
&canister_ids.escrow,
&escrow_canister_wasm.module,
escrow_init_args,
)
.await;

let user_canister_wasm = get_canister_wasm(CanisterName::User, version);
let group_canister_wasm = get_canister_wasm(CanisterName::Group, version);
let community_canister_wasm = get_canister_wasm(CanisterName::Community, version);
Expand Down
4 changes: 4 additions & 0 deletions backend/canister_installer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async fn main() {
registry: opts.registry,
market_maker: opts.market_maker,
neuron_controller: opts.neuron_controller,
escrow: opts.escrow,
nns_root: opts.nns_root,
nns_governance: opts.nns_governance,
nns_internet_identity: opts.nns_internet_identity,
Expand Down Expand Up @@ -85,6 +86,9 @@ struct Opts {
#[arg(long)]
neuron_controller: CanisterId,

#[arg(long)]
escrow: CanisterId,

#[arg(long)]
nns_root: CanisterId,

Expand Down
1 change: 1 addition & 0 deletions backend/canister_upgrader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ candid = { workspace = true }
canister_agent_utils = { path = "../libraries/canister_agent_utils" }
clap = { workspace = true, features = ["derive"] }
cycles_dispenser_canister = { path = "../canisters/cycles_dispenser/api" }
escrow_canister = { path = "../canisters/escrow/api" }
group_index_canister = { path = "../canisters/group_index/api" }
group_index_canister_client = { path = "../canisters/group_index/client" }
ic-agent = { workspace = true }
Expand Down
19 changes: 19 additions & 0 deletions backend/canister_upgrader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,25 @@ pub async fn upgrade_neuron_controller_canister(
println!("Neuron controller canister upgraded");
}

pub async fn upgrade_escrow_canister(
identity: Box<dyn Identity>,
url: String,
escrow_canister_id: CanisterId,
version: BuildVersion,
) {
upgrade_top_level_canister(
identity,
url,
escrow_canister_id,
version,
escrow_canister::post_upgrade::Args { wasm_version: version },
CanisterName::Escrow,
)
.await;

println!("Escrow canister upgraded");
}

pub async fn upgrade_local_group_index_canister(
identity: Box<dyn Identity>,
url: String,
Expand Down
4 changes: 4 additions & 0 deletions backend/canister_upgrader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async fn main() {
CanisterName::CyclesDispenser => {
upgrade_cycles_dispenser_canister(identity, opts.url, opts.cycles_dispenser, opts.version).await
}
CanisterName::Escrow => upgrade_escrow_canister(identity, opts.url, opts.escrow, opts.version).await,
CanisterName::Group => upgrade_group_canister(identity, opts.url, opts.group_index, opts.version).await,
CanisterName::LocalGroupIndex => {
upgrade_local_group_index_canister(identity, opts.url, opts.group_index, opts.version).await
Expand Down Expand Up @@ -86,6 +87,9 @@ struct Opts {
#[arg(long)]
neuron_controller: CanisterId,

#[arg(long)]
escrow: CanisterId,

#[arg(long)]
canister_to_upgrade: CanisterName,

Expand Down
13 changes: 13 additions & 0 deletions backend/canisters/escrow/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Added

- Introduce the Escrow canister for supporting P2P trades ([#4903](https://github.com/open-chat-labs/open-chat/pull/4903))
- Implement `create_offer` and `notify_deposit` ([#4904](https://github.com/open-chat-labs/open-chat/pull/4904))
- Transfer out funds once trade is complete ([#4906](https://github.com/open-chat-labs/open-chat/pull/4906))
- Implement `cancel_offer` ([#4907](https://github.com/open-chat-labs/open-chat/pull/4907))
14 changes: 14 additions & 0 deletions backend/canisters/escrow/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "escrow_canister"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
candid = { workspace = true }
candid_gen = { path = "../../../libraries/candid_gen" }
icrc-ledger-types = { workspace = true }
serde = { workspace = true }
sha256 = { path = "../../../libraries/sha256" }
types = { path = "../../../libraries/types" }
2 changes: 2 additions & 0 deletions backend/canisters/escrow/api/can.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
service : {
}
19 changes: 19 additions & 0 deletions backend/canisters/escrow/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use candid::Principal;
use icrc_ledger_types::icrc1::account::Subaccount;
use sha256::sha256;
use types::UserId;

mod lifecycle;
mod queries;
mod updates;

pub use lifecycle::*;
pub use queries::*;
pub use updates::*;

pub fn deposit_subaccount(user_id: UserId, offer_id: u32) -> Subaccount {
let mut bytes = Vec::new();
bytes.extend_from_slice(Principal::from(user_id).as_slice());
bytes.extend_from_slice(&offer_id.to_be_bytes());
sha256(&bytes)
}
10 changes: 10 additions & 0 deletions backend/canisters/escrow/api/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{BuildVersion, CanisterId};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub cycles_dispenser_canister_id: CanisterId,
pub wasm_version: BuildVersion,
pub test_mode: bool,
}
2 changes: 2 additions & 0 deletions backend/canisters/escrow/api/src/lifecycle/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod init;
pub mod post_upgrade;
8 changes: 8 additions & 0 deletions backend/canisters/escrow/api/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::BuildVersion;

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub wasm_version: BuildVersion,
}
5 changes: 5 additions & 0 deletions backend/canisters/escrow/api/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[allow(deprecated)]
fn main() {
candid::export_service!();
std::print!("{}", __export_service());
}
1 change: 1 addition & 0 deletions backend/canisters/escrow/api/src/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

15 changes: 15 additions & 0 deletions backend/canisters/escrow/api/src/updates/cancel_offer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct Args {
pub offer_id: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum Response {
Success,
OfferAlreadyAccepted,
OfferExpired,
OfferNotFound,
NotAuthorized,
}
22 changes: 22 additions & 0 deletions backend/canisters/escrow/api/src/updates/create_offer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use serde::{Deserialize, Serialize};
use types::{TimestampMillis, TokenInfo};

#[derive(Serialize, Deserialize, Debug)]
pub struct Args {
pub input_token: TokenInfo,
pub input_amount: u128,
pub output_token: TokenInfo,
pub output_amount: u128,
pub expires_at: TimestampMillis,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum Response {
Success(SuccessResult),
InvalidOffer(String),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SuccessResult {
pub id: u32,
}
3 changes: 3 additions & 0 deletions backend/canisters/escrow/api/src/updates/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod cancel_offer;
pub mod create_offer;
pub mod notify_deposit;
25 changes: 25 additions & 0 deletions backend/canisters/escrow/api/src/updates/notify_deposit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use serde::{Deserialize, Serialize};
use types::UserId;

#[derive(Serialize, Deserialize, Debug)]
pub struct Args {
pub offer_id: u32,
pub user_id: Option<UserId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum Response {
Success,
BalanceTooLow(BalanceTooLowResult),
OfferAlreadyAccepted,
OfferCancelled,
OfferExpired,
OfferNotFound,
InternalError(String),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BalanceTooLowResult {
pub balance: u128,
pub balance_required: u128,
}
Loading

0 comments on commit 65af2b7

Please sign in to comment.