Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canister api #23

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
445 changes: 437 additions & 8 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

hex = "0.4.3"
hmac = "0.12.1"
sha2 = "0.10.8"

axum = { version = "0.6.20", features = ["macros"] }
bb8 = "0.8.2"
bb8-redis = "0.15.0"
candid = "0.10.6"
chrono = { version = "0.4.34", features = ["serde"] }
futures = "0.3.30"
ic-agent = "0.33.0"
rust-s3 = { version = "0.33.0", features = [
"tokio-rustls-tls",
], default-features = false }
# redis = { version = "0.23.0", features = ["tls-native-tls"] }
redis = { version = "0.25.2", features = [
"tokio-rustls-comp",
"aio",
"connection-manager",
] }
serde = "1.0.197"
serde_bytes = "0.11.14"
serde_json = "1.0.114"
stringreader = "0.1.1"
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread", "time"] }
Expand Down Expand Up @@ -42,8 +56,15 @@ config = { version = "0.14.0", features = ["toml"] }
serde_with = "3.7.0"
thiserror = "1.0.58"
env_logger = "0.11.3"
anyhow = "1.0.86"
# yral-metadata-types = { git = "https://github.com/go-bazzinga/yral-metadata", rev = "c2c5d254ac273681a66966d62b6f01785679a1be", default-features = false }


[build-dependencies]
serde = { version = "1.0", features = ["derive"] }
candid_parser = "0.1.1"
serde_json = "1.0.110"
convert_case = "0.6.0"
tonic-build = "0.11"

[[bin]]
Expand Down
85 changes: 83 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,91 @@
// .unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e));
// }

use std::env;
use std::path::PathBuf;
use std::{collections::HashMap, env, ffi::OsStr, fs, io, path::PathBuf};

use candid_parser::Principal;
use convert_case::{Case, Casing};
use serde::Deserialize;

#[derive(Deserialize)]
struct CanId {
ic: Principal,
}

fn read_candid_ids() -> io::Result<HashMap<String, CanId>> {
let can_ids_file = fs::File::open("did/canister_ids.json")?;
let reader = io::BufReader::new(can_ids_file);
Ok(serde_json::from_reader(reader).expect("invalid candid ids"))
}



fn build_candid() -> Result<(), Box<dyn std::error::Error>>{

println!("cargo:rerun-if-changed=did/*");

let can_ids = read_candid_ids()?;

let mut candid_config = candid_parser::bindings::rust::Config::new();
candid_config.set_target(candid_parser::bindings::rust::Target::Agent);
let mut did_mod_contents = String::new();

// create $OUT_DIR/did
let out_dir = env::var("OUT_DIR").unwrap();
let did_dir = PathBuf::from(&out_dir).join("did");
fs::create_dir_all(&did_dir)?;

// Auto generate bindings for did files
for didinfo in fs::read_dir("did")? {
let didpath = didinfo?.path();
if didpath.extension() != Some(OsStr::new("did")) {
continue;
}
let file_name = didpath.file_stem().unwrap().to_str().unwrap();

// compile bindings from did
candid_config.set_canister_id(
can_ids
.get(file_name)
.unwrap_or_else(|| panic!("no canister id for {file_name}"))
.ic,
);
let service_name: String = file_name.to_case(Case::Pascal);
candid_config.set_service_name(service_name);
let (type_env, actor) = candid_parser::pretty_check_file(&didpath).unwrap_or_else(|e| {
panic!(
"invalid did file: {}, err: {e}",
didpath.as_os_str().to_string_lossy()
)
});
let bindings = candid_parser::bindings::rust::compile(&candid_config, &type_env, &actor);

// write bindings to $OUT_DIR/did/<did file>.rs
let mut binding_file = did_dir.clone();
binding_file.push(file_name);
binding_file.set_extension("rs");
fs::write(&binding_file, bindings)?;

// #[path = "$OUT_DIR/did/<did file>.rs"] pub mod <did file>;
did_mod_contents.push_str(&format!(
"#[path = \"{}\"] pub mod {};\n",
binding_file.to_string_lossy(),
file_name
));
}

// create mod file for did bindings
// ideally i'd like to manually write this file in src/canister/mod.rs
// but can't, due to https://github.com/rust-lang/rfcs/issues/752
let binding_mod_file = PathBuf::from(&out_dir).join("did").join("mod.rs");
fs::write(binding_mod_file, did_mod_contents)?;

Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
build_candid()?;

let proto_file = "contracts/projects/warehouse_events/warehouse_events.proto";
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

Expand Down
20 changes: 20 additions & 0 deletions did/canister_ids.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"configuration": {
"ic": "efsfj-sqaaa-aaaap-qatwa-cai"
},
"data_backup": {
"ic": "jwktp-qyaaa-aaaag-abcfa-cai"
},
"individual_user_template": {
"ic": "dc47w-kaaaa-aaaak-qav3q-cai"
},
"platform_orchestrator": {
"ic": "74zq4-iqaaa-aaaam-ab53a-cai"
},
"post_cache": {
"ic": "zyajx-3yaaa-aaaag-acoga-cai"
},
"user_index": {
"ic": "rimrc-piaaa-aaaao-aaljq-cai"
}
}
Loading