Skip to content

Commit

Permalink
feat: icrc1 metadata http endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Feb 19, 2024
1 parent c0b15ca commit 1a8125d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
27 changes: 26 additions & 1 deletion integration-tests/tests/http/ekoke.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use did::ekoke::{LiquidityPoolAccounts, LiquidityPoolBalance};
use integration_tests::client::HttpClient;
use integration_tests::TestEnv;
use serde_json::Value;

#[test]
#[serial_test::serial]
fn test_should_get_liquidity_pool_accounts_and_balance() {
fn test_http_should_get_liquidity_pool_accounts_and_balance() {
let env = TestEnv::init();

let http_client = HttpClient::new(env.ekoke_ledger_id, &env);
Expand All @@ -20,3 +21,27 @@ fn test_should_get_liquidity_pool_accounts_and_balance() {
assert_eq!(liquidity_pool_balance.ckbtc, 0u64);
assert_eq!(liquidity_pool_balance.icp, 0u64);
}

#[test]
#[serial_test::serial]
fn test_http_should_get_icrc1_metadata() {
let env = TestEnv::init();

let http_client = HttpClient::new(env.ekoke_ledger_id, &env);

let icrc1_name: Value = http_client.http_request("icrc1Name", serde_json::json!({}));
assert_eq!(icrc1_name["name"], "ekoke");

let icrc1_symbol: Value = http_client.http_request("icrc1Symbol", serde_json::json!({}));
assert_eq!(icrc1_symbol["symbol"], "EKOKE");

let icrc1_decimals: Value = http_client.http_request("icrc1Decimals", serde_json::json!({}));
assert_eq!(icrc1_decimals["decimals"], 12);

let icrc1_total_supply: Value =
http_client.http_request("icrc1TotalSupply", serde_json::json!({}));
assert_eq!(icrc1_total_supply["totalSupply"], 8880101010000000000u64);

let icrc1_fee: Value = http_client.http_request("icrc1Fee", serde_json::json!({}));
assert_eq!(icrc1_fee["fee"], 10_000u64);
}
47 changes: 47 additions & 0 deletions src/ekoke_ledger/src/http.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use did::{HttpRequest, HttpResponse};
use icrc::icrc1::Icrc1 as _;
use num_traits::ToPrimitive;

use crate::app::EkokeCanister;

Expand All @@ -25,6 +27,11 @@ impl HttpApi {
match method.as_str() {
"liquidityPoolBalance" => Self::liquidity_pool_balance().await,
"liquidityPoolAccounts" => Self::liquidity_pool_accounts(),
"icrc1Name" => Self::icrc1_name(),
"icrc1Symbol" => Self::icrc1_symbol(),
"icrc1Decimals" => Self::icrc1_decimals(),
"icrc1TotalSupply" => Self::icrc1_total_supply(),
"icrc1Fee" => Self::icrc1_fee(),
_ => HttpResponse::bad_request("unknown method".to_string()),
}
}
Expand All @@ -47,4 +54,44 @@ impl HttpApi {

HttpResponse::ok(params)
}

fn icrc1_name() -> HttpResponse {
let response = EkokeCanister::icrc1_name();

HttpResponse::ok(serde_json::json!({
"name": response
}))
}

fn icrc1_symbol() -> HttpResponse {
let response = EkokeCanister::icrc1_symbol();

HttpResponse::ok(serde_json::json!({
"symbol": response
}))
}

fn icrc1_decimals() -> HttpResponse {
let response = EkokeCanister::icrc1_decimals();

HttpResponse::ok(serde_json::json!({
"decimals": response
}))
}

fn icrc1_total_supply() -> HttpResponse {
let response = EkokeCanister::icrc1_total_supply();

HttpResponse::ok(serde_json::json!({
"totalSupply": response.0.to_u64().unwrap_or_default()
}))
}

fn icrc1_fee() -> HttpResponse {
let response = EkokeCanister::icrc1_fee();

HttpResponse::ok(serde_json::json!({
"fee": response.0.to_u64().unwrap_or_default()
}))
}
}

0 comments on commit 1a8125d

Please sign in to comment.