Skip to content

Commit

Permalink
Merge pull request #37 from dfinity/FI-876-import-icrc-1-test-suite
Browse files Browse the repository at this point in the history
feat(FI-876) import icrc 1 test suite
  • Loading branch information
NikolasHaimerl authored Aug 28, 2023
2 parents 0768eb7 + d7b1ae8 commit e8084b9
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 17 deletions.
168 changes: 164 additions & 4 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion cycles-ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ thiserror = "1.0"
depositor = { path = "../depositor" }
escargot = { version = "0.5.7", features = ["print"] }
ic-test-state-machine-client = "2.2.1"
proptest = "1.2.0"
icrc1-test-env-state-machine = { git = "https://github.com/dfinity/ICRC-1", version = "0.1.0" }
icrc1-test-suite = { git = "https://github.com/dfinity/ICRC-1", version = "0.1.1" }
futures = "0.3.28"
proptest = "1.2.0"
18 changes: 6 additions & 12 deletions cycles-ledger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ic_cdk::api::call::{msg_cycles_accept128, msg_cycles_available128};
use ic_cdk::api::management_canister;
use ic_cdk::api::management_canister::provisional::CanisterIdRecord;
use ic_cdk_macros::{query, update};
use icrc_ledger_types::icrc::generic_value::Value;
use icrc_ledger_types::icrc::generic_metadata_value::MetadataValue;
use icrc_ledger_types::icrc1::account::Account;
use icrc_ledger_types::icrc1::transfer::{Memo, TransferArg, TransferError};
use icrc_ledger_types::icrc2::allowance::{Allowance, AllowanceArgs};
Expand Down Expand Up @@ -77,18 +77,12 @@ fn icrc1_total_supply() -> Nat {

#[query]
#[candid_method(query)]
fn icrc1_metadata() -> Vec<(String, Value)> {
fn icrc1_metadata() -> Vec<(String, MetadataValue)> {
vec![
(
"icrc1:decimals".to_string(),
Value::Nat(config::DECIMALS.into()),
),
("icrc1:fee".to_string(), Value::Nat(config::FEE.into())),
("icrc1:name".to_string(), Value::text(config::TOKEN_NAME)),
(
"icrc1:symbol".to_string(),
Value::text(config::TOKEN_SYMBOL),
),
MetadataValue::entry("icrc1:decimals", config::DECIMALS as u64),
MetadataValue::entry("icrc1:name", config::TOKEN_NAME),
MetadataValue::entry("icrc1:symbol", config::TOKEN_SYMBOL),
MetadataValue::entry("icrc1:fee", config::FEE),
]
}

Expand Down
31 changes: 31 additions & 0 deletions cycles-ledger/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
path::PathBuf,
sync::Arc,
time::{Duration, SystemTime},
};

Expand All @@ -11,6 +12,7 @@ use cycles_ledger::{
};
use depositor::endpoints::InitArg as DepositorInitArg;
use escargot::CargoBuild;
use futures::FutureExt;
use ic_cdk::api::call::RejectionCode;
use ic_test_state_machine_client::{ErrorCode, StateMachine};
use icrc_ledger_types::{
Expand Down Expand Up @@ -1380,3 +1382,32 @@ fn test_total_supply_after_upgrade() {
.unwrap();
assert_eq!(total_supply(env, ledger_id), expected_total_supply);
}

#[test]
fn test_icrc1_test_suite() {
let env = new_state_machine();
let ledger_id = install_ledger(&env);
let depositor_id = install_depositor(&env, ledger_id);
let user = Account {
owner: Principal::from_slice(&[10]),
subaccount: Some([0; 32]),
};

// make the first deposit to the user and check the result
let deposit_res = deposit(&env, depositor_id, user, 1_000_000_000_000_000);
assert_eq!(deposit_res.txid, Nat::from(0));
assert_eq!(deposit_res.balance, 1_000_000_000_000_000_u128);
assert_eq!(1_000_000_000_000_000, balance_of(&env, ledger_id, user));

let ledger_env =
icrc1_test_env_state_machine::SMLedger::new(Arc::new(env), ledger_id, user.owner);
let tests = icrc1_test_suite::test_suite(ledger_env)
.now_or_never()
.unwrap();
if !icrc1_test_suite::execute_tests(tests)
.now_or_never()
.unwrap()
{
panic!("The ICRC-1 test suite failed");
}
}

0 comments on commit e8084b9

Please sign in to comment.