Skip to content

Commit

Permalink
Merge pull request #2 from taggrx/sec_audit
Browse files Browse the repository at this point in the history
feat: beta version on defi subnet
  • Loading branch information
taggrx authored Jun 3, 2024
2 parents f97d728 + 9f23fc2 commit 5c7ed3f
Show file tree
Hide file tree
Showing 21 changed files with 584 additions and 515 deletions.
16 changes: 0 additions & 16 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fi

fetch() {
FILE="$1"
$QU raw "cetrr-jaaaa-aaaak-afgxq-cai" "stable_mem_read" --args "($PAGE:nat64)" --query |\
$QU raw "srn4v-3aaaa-aaaar-qaftq-cai" "stable_mem_read" --args "($PAGE:nat64)" --query |\
$QU send --yes --raw - > $FILE
}

Expand Down
2 changes: 1 addition & 1 deletion canister_ids.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"beacon": {
"ic": "cetrr-jaaaa-aaaak-afgxq-cai"
"ic": "srn4v-3aaaa-aaaar-qaftq-cai"
}
}
1 change: 0 additions & 1 deletion src/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ ic-cdk = "0.11.3"
ic-cdk-macros = "0.8.1"
ic-cdk-timers = "0.5.1"
ic-certified-map = "0.4.0"
ic-ledger-types = "0.8.0"
serde = { version = "1.0.192", features = ["derive"] }
serde_bytes = "0.11.12"
serde_cbor = "0.11.2"
Expand Down
6 changes: 5 additions & 1 deletion src/backend/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ pub fn load() {
include_bytes!("../../dist/frontend/apple-touch-icon.png").to_vec(),
);

let mut domains = vec!["beacondex.link", "alpha.beacondex.link"];
let mut domains = vec![
"beacondex.link",
"srn4v-3aaaa-aaaar-qaftq-cai.icp.io",
"cetrr-jaaaa-aaaak-afgxq-cai.icp0.io",
];
let can_domain = format!("{}.icp0.io", ic_cdk::id());
domains.push(&can_domain);
add_asset(
Expand Down
5 changes: 4 additions & 1 deletion src/backend/beacon.did
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ type HttpResponse = record {
type Order = record {
decimals : nat32;
owner : principal;
payment_token_fee : nat;
order_type : OrderType;
timestamp : nat64;
executed : nat64;
price : nat;
amount : nat;
};
type OrderExecution = variant { FilledAndOrderCreated : nat; Filled : nat };
type OrderType = variant { Buy; Sell };
type Result = variant { Ok; Err : text };
type Result_1 = variant { Ok : nat; Err : text };
service : () -> {
close_all_orders : () -> ();
close_order : (principal, OrderType, nat, nat, nat64) -> ();
deposit_liquidity : (principal) -> (Result);
http_request : (HttpRequest) -> (HttpResponse) query;
list_token : (principal) -> (Result);
orders : (principal, OrderType) -> (vec Order) query;
set_revenue_account : (principal) -> ();
stable_mem_read : (nat64) -> (vec record { nat64; vec nat8 }) query;
trade : (principal, nat, nat, OrderType) -> (vec record { nat; bool });
trade : (principal, nat, nat, OrderType) -> (OrderExecution);
withdraw : (principal) -> (Result_1);
}
35 changes: 22 additions & 13 deletions src/backend/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use candid::Principal;
use ic_cdk::{api::call::reply_raw, caller, spawn};
use ic_cdk_macros::*;
use ic_cdk_timers::{set_timer, set_timer_interval};
use ic_ledger_types::{Tokens as ICP, DEFAULT_FEE};
use order_book::{Order, OrderType, State, Timestamp, TokenId, Tokens, PAYMENT_TOKEN_ID, TX_FEE};

mod assets;
Expand All @@ -19,9 +18,9 @@ mod icrc1;
mod order_book;
mod queries;
mod updates;
mod xdr_rate;

const BACKUP_PAGE_SIZE: u32 = 1024 * 1024;
pub const LISTING_PRICE_USD: u128 = 100;
pub const MINUTE: u64 = 60000000000_u64;
pub const HOUR: u64 = 60 * MINUTE;
pub const DAY: u64 = 24 * HOUR;
Expand Down Expand Up @@ -70,8 +69,8 @@ where
if delta == 0 && balance == &0 {
balances_after.retain(|(id, _)| id != &token_id.to_string());
} else {
// S3: check that balance fits into `i128`.
// and check that subtracted result is not negative.
assert!(*balance < i128::MAX as u128);
assert!(*balance >= delta.unsigned_abs());
*balance = (*balance as i128 - delta) as u128;
}
}
Expand All @@ -87,21 +86,20 @@ fn reply<T: serde::Serialize>(data: T) {
// Starts all repeating tasks.
fn kickstart() {
assets::load();
let fetch_rate = || {
spawn(async {
if let Ok(e8s) = xdr_rate::get_xdr_in_e8s().await {
mutate(|state| state.e8s_per_xdr = e8s);
}
})
};
set_timer(Duration::from_millis(1), fetch_rate);
set_timer_interval(Duration::from_secs(24 * 60 * 60), fetch_rate);
set_timer_interval(Duration::from_secs(24 * 60 * 60), || {
mutate(|state| state.clean_up(ic_cdk::api::time()));
});
set_timer_interval(Duration::from_secs(60 * 60), || {
mutate(heap_to_stable);
});
// weekly payment token metadata updates
set_timer(Duration::from_secs(24 * 60 * 60 * 7), || {
spawn(async {
register_token(PAYMENT_TOKEN_ID)
.await
.expect("couldn't update payment token metadata");
})
});
}

fn stable_to_heap_core() {
Expand Down Expand Up @@ -146,5 +144,16 @@ fn heap_address() -> (u64, u64) {
(offset, len)
}

pub async fn register_token(token: TokenId) -> Result<(), String> {
let metadata = icrc1::metadata(token)
.await
.map_err(|err| format!("couldn't fetch metadata: {}", err))?;
mutate_with_invarant_check(
|state| state.list_token(token, metadata, ic_cdk::api::time()),
Some((token, 0)),
)
}

use crate::assets::{HttpRequest, HttpResponse};
use crate::order_book::OrderExecution;
export_candid!();
Loading

0 comments on commit 5c7ed3f

Please sign in to comment.