diff --git a/scripts/did.sh b/scripts/did.sh index 7197fa0..458f77f 100755 --- a/scripts/did.sh +++ b/scripts/did.sh @@ -1,37 +1,37 @@ #!/usr/bin/env bash did_file_location_from_cargo() { - # Warning: Does no _- conversion. - cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path | sub("Cargo.toml";"\($v).did")' + # Warning: Does no _- conversion. + cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path | sub("Cargo.toml";"\($v).did")' } cargo_manifest_path() { - # Warning: Does no _- conversion. - cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path")' + # Warning: Does no _- conversion. + cargo metadata --format-version 1 | jq -r --arg v "$1" '.packages[] | select(.name==$v) | .manifest_path")' } did_file_location_from_dfx_json() { - # Warning: Does no _- conversion. - jq -r --arg v "$1" '.canisters[$v].candid' dfx.json + # Warning: Does no _- conversion. + jq -r --arg v "$1" '.canisters[$v].candid' dfx.json } function generate_did() { - local canister=$1 - echo "Deriving candid file from Rust for $canister" - #local manifest_path="$(cargo_manifest_path "$canister")" - #local candid_file="${manifest_path%Cargo.toml}$canister.did" - local candid_file="$(did_file_location_from_dfx_json "$canister")" + local canister=$1 + echo "Deriving candid file from Rust for $canister" + #local manifest_path="$(cargo_manifest_path "$canister")" + #local candid_file="${manifest_path%Cargo.toml}$canister.did" + local candid_file="$(did_file_location_from_dfx_json "$canister")" - test -e "target/wasm32-unknown-unknown/release/$canister.wasm" || - cargo build -p "$canister" \ - --target wasm32-unknown-unknown \ - --release --package "$canister" + test -e "target/wasm32-unknown-unknown/release/$canister.wasm" || + cargo build -p "$canister" \ + --target wasm32-unknown-unknown \ + --release --package "$canister" - # cargo install candid-extractor - candid-extractor "target/wasm32-unknown-unknown/release/$canister.wasm" >"$candid_file" - echo "Written: $candid_file" + # cargo install candid-extractor + candid-extractor "target/wasm32-unknown-unknown/release/$canister.wasm" >"$candid_file" + echo "Written: $candid_file" } CANISTERS=(example_app_backend example_paid_service) for canister in ${CANISTERS[@]}; do - generate_did "$canister" + generate_did "$canister" done diff --git a/scripts/pic-install b/scripts/pic-install index b796a0c..0edf992 100755 --- a/scripts/pic-install +++ b/scripts/pic-install @@ -13,7 +13,7 @@ else exit 1 fi -if "$POCKET_IC_SERVER_PATH" --version &>/dev/null | grep -wq "$POCKET_IC_SERVER_VERSION" ; then +if "$POCKET_IC_SERVER_PATH" --version &>/dev/null | grep -wq "$POCKET_IC_SERVER_VERSION"; then echo "PocketIC server already exists at: $POCKET_IC_SERVER_PATH" echo "Skipping download." else diff --git a/scripts/test-rs b/scripts/test-rs new file mode 100755 index 0000000..2bd8931 --- /dev/null +++ b/scripts/test-rs @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +print_help() { + echo "Runs rust unit tests." +} + +[[ "${1:-}" != "--help" ]] || print_help + +cargo test --lib --bins "${@}" diff --git a/src/api/src/lib.rs b/src/api/src/lib.rs index a5bff2f..aacde2e 100644 --- a/src/api/src/lib.rs +++ b/src/api/src/lib.rs @@ -9,6 +9,7 @@ pub use caller::PaymentType; pub use error::PaymentError; pub use vendor::Icrc2Payer; +#[must_use] pub fn principal2account(principal: &Principal) -> ByteBuf { // TODO: This is NOT the right way. let mut ans = principal.as_slice().to_vec(); diff --git a/src/example/paid_service/src/lib.rs b/src/example/paid_service/src/lib.rs index 9dda2a5..e21ad2b 100644 --- a/src/example/paid_service/src/lib.rs +++ b/src/example/paid_service/src/lib.rs @@ -18,7 +18,7 @@ fn init(init_args: Option) { } #[update()] -async fn free() -> String { +fn free() -> String { "Yes, I am free!".to_string() } diff --git a/src/example/paid_service/src/state.rs b/src/example/paid_service/src/state.rs index aca6cad..e027b6d 100644 --- a/src/example/paid_service/src/state.rs +++ b/src/example/paid_service/src/state.rs @@ -14,7 +14,7 @@ where INIT_ARGS.with(|init_args| f(init_args.borrow().as_ref().expect("No init args provided"))) } -/// Provides the canister_id of the ledger used for payments. +/// Provides the canister id of the ledger used for payments. pub fn payment_ledger() -> Principal { init_element(|init_args| init_args.ledger.expect("Init args specify no ledger")) } diff --git a/src/guard/src/guards/icrc2_cycles.rs b/src/guard/src/guards/icrc2_cycles.rs index 243e097..c34b23e 100644 --- a/src/guard/src/guards/icrc2_cycles.rs +++ b/src/guard/src/guards/icrc2_cycles.rs @@ -16,17 +16,28 @@ pub struct Icrc2CyclesPaymentGuard { pub own_canister_id: Principal, } impl Icrc2CyclesPaymentGuard { + #[must_use] pub fn default_account() -> Account { Account { owner: ic_cdk::caller(), subaccount: None, } } + /// The normal cycles ledger canister ID. + /// + /// - If the cycles ledger is listed in `dfx.json`, a normal `dfx build` will set the + /// environment variable `CANISTER_ID_CYCLES_LEDGER` and we use this to obtain the canister ID. + /// - Otherwise, we use the mainnet cycled ledger canister ID, which is `um5iw-rqaaa-aaaaq-qaaba-cai`. + /// + /// # Panics + /// - If the `CANISTER_ID_CYCLES_LEDGER` environment variable is not a valid canister ID at + /// build time. + #[must_use] pub fn default_cycles_ledger() -> Principal { Principal::from_text( - option_env!("DFX_CYCLES_LEDGER_CANISTER_ID").unwrap_or("um5iw-rqaaa-aaaaq-qaaba-cai"), + option_env!("CANISTER_ID_CYCLES_LEDGER").unwrap_or("um5iw-rqaaa-aaaaq-qaaba-cai"), ) - .expect("Failed to parse cycles ledger canister ID") + .expect("Compile error: Failed to parse build env var 'CANISTER_ID_CYCLES_LEDGER' as a canister ID.") } }