Skip to content

Commit

Permalink
fix: leo's review
Browse files Browse the repository at this point in the history
  • Loading branch information
X committed Aug 12, 2024
1 parent 3843a57 commit 4e77bb3
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 839 deletions.
945 changes: 131 additions & 814 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.73"
channel = "1.80"
components = ["rustfmt", "clippy"]
targets = ["wasm32-unknown-unknown"]
12 changes: 6 additions & 6 deletions src/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ crate-type = ["cdylib"]

[dependencies]
base64 = "0.21.5"
candid = { version = "0.9.11", features = ["parser"] }
candid = { version = "0.10.10" }
hex = "0.4.3"
ic-cdk = "0.11.3"
ic-cdk-macros = "0.8.1"
ic-cdk-timers = "0.5.1"
ic-cdk = "0.15.0"
ic-cdk-macros = "0.15.0"
ic-cdk-timers = "0.9.0"
ic-certified-map = "0.4.0"
serde = { version = "1.0.192", features = ["derive"] }
serde_bytes = "0.11.12"
serde_bytes = "0.11.15"
serde_cbor = "0.11.2"
serde_json = "1.0.108"
serde_json = "1.0.124"
sha2 = "0.10.8"

[features]
Expand Down
2 changes: 1 addition & 1 deletion src/backend/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn load() {

let mut domains = vec![
"beacondex.link",
"srn4v-3aaaa-aaaar-qaftq-cai.icp.io",
"srn4v-3aaaa-aaaar-qaftq-cai.icp0.io",
"cetrr-jaaaa-aaaak-afgxq-cai.icp0.io",
];
let can_domain = format!("{}.icp0.io", ic_cdk::id());
Expand Down
4 changes: 2 additions & 2 deletions src/backend/beacon.did
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type HttpRequest = record { url : text };
type HttpResponse = record {
body : vec nat8;
body : blob;
headers : vec record { text; text };
status_code : nat16;
};
Expand All @@ -27,7 +27,7 @@ service : () -> {
orders : (principal, OrderType) -> (vec Order) query;
set_payment_token : (principal) -> ();
set_revenue_account : (principal) -> ();
stable_mem_read : (nat64) -> (vec record { nat64; vec nat8 }) query;
stable_mem_read : (nat64) -> (vec record { nat64; blob }) query;
trade : (principal, nat, nat, OrderType) -> (OrderExecution);
withdraw : (principal) -> (Result_1);
}
23 changes: 12 additions & 11 deletions src/backend/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ic_cdk::api::stable::{stable64_grow, stable64_read, stable64_size, stable64_write};
use ic_cdk::api::stable::{stable_grow, stable_read, stable_size, stable_write};
use icrc1::Account;
use serde::Serialize;
use std::cell::RefCell;
Expand All @@ -21,7 +21,8 @@ mod updates;

const BACKUP_PAGE_SIZE: u32 = 1024 * 1024;
pub const LISTING_PRICE_USD: u128 = 100;
pub const MINUTE: u64 = 60000000000_u64;
pub const SECOND: u64 = 1_000_000_000_u64;
pub const MINUTE: u64 = 60 * SECOND;
pub const HOUR: u64 = 60 * MINUTE;
pub const DAY: u64 = 24 * HOUR;

Expand Down Expand Up @@ -52,7 +53,7 @@ where
}

// Mutates the state and checks one invariant: that any token liquidity was changed only
// in line with the expected delta (or not changed at all, if delta is None.
// in line with the expected delta (or not changed at all, if delta is None).
fn mutate_with_invarant_check<F, R>(f: F, liquidity_delta: Option<(TokenId, i128)>) -> R
where
F: FnOnce(&mut State) -> R,
Expand Down Expand Up @@ -115,14 +116,14 @@ pub fn heap_to_stable(state: &mut State) {
let offset = 16; // start of the heap
let bytes = serde_cbor::to_vec(&state).expect("couldn't serialize the state");
let len = bytes.len() as u64;
let stable_mem_size_bytes = stable64_size() << 16;
let stable_mem_size_bytes = stable_size() << 16;
let new_pages = (offset + len).saturating_sub(stable_mem_size_bytes) >> 16;
if new_pages > 0 {
stable64_grow(new_pages + 1).expect("couldn't grow memory");
stable_grow(new_pages + 1).expect("couldn't grow memory");
}
stable64_write(offset, &bytes);
stable64_write(0, &offset.to_be_bytes());
stable64_write(8, &len.to_be_bytes());
stable_write(offset, &bytes);
stable_write(0, &offset.to_be_bytes());
stable_write(8, &len.to_be_bytes());
}

fn stable_to_heap() -> State {
Expand All @@ -133,16 +134,16 @@ fn stable_to_heap() -> State {
unsafe {
bytes.set_len(len as usize);
}
stable64_read(offset, &mut bytes);
stable_read(offset, &mut bytes);
serde_cbor::from_slice(&bytes).expect("couldn't deserialize")
}

fn heap_address() -> (u64, u64) {
let mut offset_bytes: [u8; 8] = Default::default();
stable64_read(0, &mut offset_bytes);
stable_read(0, &mut offset_bytes);
let offset = u64::from_be_bytes(offset_bytes);
let mut len_bytes: [u8; 8] = Default::default();
stable64_read(8, &mut len_bytes);
stable_read(8, &mut len_bytes);
let len = u64::from_be_bytes(len_bytes);
(offset, len)
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ fn stable_mem_read(page: u64) -> Vec<(u64, Vec<u8>)> {
unsafe {
buf.set_len(chunk_size);
}
ic_cdk::api::stable::stable64_read(offset, &mut buf);
ic_cdk::api::stable::stable_read(offset, &mut buf);
vec![(page, buf)]
}
6 changes: 3 additions & 3 deletions src/backend/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::*;

#[init]
fn init() {
stable64_grow(1).expect("stable memory intialization failed");
stable_grow(1).expect("stable memory intialization failed");
kickstart();
}

Expand Down Expand Up @@ -183,13 +183,13 @@ async fn withdraw(token: Principal) -> Result<u128, String> {
async fn list_token(token: TokenId) -> Result<(), String> {
let user = caller();

// we subtract the fee twice, because the user moved the funds to BEACON internal account
// first and now we need to move it to the payment pool again
let Metadata { fee, decimals, .. } = read(|state| {
state
.token(state.payment_token_id())
.expect("no payment token")
});
// we subtract the fee twice, because the user moved the funds to BEACON internal account
// first and now we need to move it to the payment pool again
let effective_amount = LISTING_PRICE_USD * 10_u128.pow(decimals) - fee - fee;

if read(|state| state.payment_token_pool().get(&user) < Some(&effective_amount)) {
Expand Down

0 comments on commit 4e77bb3

Please sign in to comment.