Skip to content

Commit

Permalink
use paginated icrc1Canister list
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantinos Gaitanis committed Nov 18, 2024
1 parent 787a13e commit 8f9247a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 146 deletions.
18 changes: 11 additions & 7 deletions Sources/DAB/Token/DidFiles/ICRC1Oracle.did
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ type ICRC1Request = record {
symbol : text;
decimals : nat8;
fee : nat;
};service : (opt Conf) -> {
get_all_icrc1_canisters : () -> (vec ICRC1) query;
replace_icrc1_canisters : (vec ICRC1) -> ();
store_new_icrc1_canisters : (vec ICRC1) -> ();
store_icrc1_canister : (ICRC1Request) -> ();
sync_controllers: () -> (vec text);
}
};

service : {
count_icrc1_canisters : () -> (nat64) query;
get_all_icrc1_canisters : () -> (vec ICRC1) query;
get_icrc1_paginated : (nat64, nat64) -> (vec ICRC1) query;
replace_icrc1_canisters : (vec ICRC1) -> ();
set_operator : (principal) -> ();
store_icrc1_canister : (ICRC1Request) -> ();
store_new_icrc1_canisters : (vec ICRC1) -> ();
}
43 changes: 29 additions & 14 deletions Sources/DAB/Token/Generated/ICRC1Oracle.did.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,46 @@ enum ICRC1Oracle {
}


/// service : (opt Conf) -> {
/// get_all_icrc1_canisters : () -> (vec ICRC1) query;
/// replace_icrc1_canisters : (vec ICRC1) -> ();
/// store_new_icrc1_canisters : (vec ICRC1) -> ();
/// store_icrc1_canister : (ICRC1Request) -> ();
/// sync_controllers: () -> (vec text);
/// service : {
/// count_icrc1_canisters : () -> (nat64) query;
/// get_all_icrc1_canisters : () -> (vec ICRC1) query;
/// get_icrc1_paginated : (nat64, nat64) -> (vec ICRC1) query;
/// replace_icrc1_canisters : (vec ICRC1) -> ();
/// set_operator : (principal) -> ();
/// store_icrc1_canister : (ICRC1Request) -> ();
/// store_new_icrc1_canisters : (vec ICRC1) -> ();
/// }
class Service: ICPService {
/// count_icrc1_canisters : () -> (nat64) query;
func count_icrc1_canisters(sender: ICPSigningPrincipal? = nil) async throws -> UInt64 {
let caller = ICPQueryNoArgs<UInt64>(canister, "count_icrc1_canisters")
let response = try await caller.callMethod(client, sender: sender)
return response
}

/// get_all_icrc1_canisters : () -> (vec ICRC1) query;
func get_all_icrc1_canisters(sender: ICPSigningPrincipal? = nil) async throws -> [ICRC1] {
let caller = ICPQueryNoArgs<[ICRC1]>(canister, "get_all_icrc1_canisters")
let response = try await caller.callMethod(client, sender: sender)
return response
}

/// get_icrc1_paginated : (nat64, nat64) -> (vec ICRC1) query;
func get_icrc1_paginated(_ arg0: UInt64, _ arg1: UInt64, sender: ICPSigningPrincipal? = nil) async throws -> [ICRC1] {
let caller = ICPQuery<ICPFunctionArgs2<UInt64, UInt64>, [ICRC1]>(canister, "get_icrc1_paginated")
let response = try await caller.callMethod(.init(arg0, arg1), client, sender: sender)
return response
}

/// replace_icrc1_canisters : (vec ICRC1) -> ();
func replace_icrc1_canisters(_ arg0: [ICRC1], sender: ICPSigningPrincipal? = nil) async throws {
let caller = ICPCallNoResult<[ICRC1]>(canister, "replace_icrc1_canisters")
let _ = try await caller.callMethod(arg0, client, sender: sender)
}

/// store_new_icrc1_canisters : (vec ICRC1) -> ();
func store_new_icrc1_canisters(_ arg0: [ICRC1], sender: ICPSigningPrincipal? = nil) async throws {
let caller = ICPCallNoResult<[ICRC1]>(canister, "store_new_icrc1_canisters")
/// set_operator : (principal) -> ();
func set_operator(_ arg0: ICPPrincipal, sender: ICPSigningPrincipal? = nil) async throws {
let caller = ICPCallNoResult<ICPPrincipal>(canister, "set_operator")
let _ = try await caller.callMethod(arg0, client, sender: sender)
}

Expand All @@ -120,11 +136,10 @@ enum ICRC1Oracle {
let _ = try await caller.callMethod(arg0, client, sender: sender)
}

/// sync_controllers: () -> (vec text);
func sync_controllers(sender: ICPSigningPrincipal? = nil) async throws -> [String] {
let caller = ICPCallNoArgs<[String]>(canister, "sync_controllers")
let response = try await caller.callMethod(client, sender: sender)
return response
/// store_new_icrc1_canisters : (vec ICRC1) -> ();
func store_new_icrc1_canisters(_ arg0: [ICRC1], sender: ICPSigningPrincipal? = nil) async throws {
let caller = ICPCallNoResult<[ICRC1]>(canister, "store_new_icrc1_canisters")
let _ = try await caller.callMethod(arg0, client, sender: sender)
}

}
Expand Down
25 changes: 24 additions & 1 deletion Sources/DAB/TokenOracle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class TokenOracle {
if let cachedCanisters = cachedCanisters {
return cachedCanisters.compactMap { try? ICPToken($0) }
}
cachedCanisters = try await service.get_all_icrc1_canisters()
cachedCanisters = try await fetchAllIcrc1Canisters()
return cachedCanisters!.compactMap { try? ICPToken($0) }
}

Expand Down Expand Up @@ -100,6 +100,29 @@ public class TokenOracle {
}
}

private extension TokenOracle {
private static let pageSize: UInt64 = 50
func fetchAllIcrc1Canisters() async throws -> [ICRC1Oracle.ICRC1] {
let canisterCount = try await service.count_icrc1_canisters()
let nPages = canisterCount / Self.pageSize + 1
let canisters = await withTaskGroup(of: [ICRC1Oracle.ICRC1].self) { [weak self] group in
for i in 0..<nPages {
group.addTask {
let startAt = i * Self.pageSize
let canisters = try? await self?.service.get_icrc1_paginated(startAt, Self.pageSize)
return canisters ?? []
}
}
var canisters: [ICRC1Oracle.ICRC1] = []
while let finished = await group.next() {
canisters.append(contentsOf: finished)
}
return canisters
}
return canisters
}
}

private extension ICPToken {
init(_ icrc1: ICRC1Oracle.ICRC1) throws {
name = icrc1.name
Expand Down
86 changes: 0 additions & 86 deletions Sources/IcpKit/Utils/Paginator.swift

This file was deleted.

37 changes: 0 additions & 37 deletions Sources/IcpKit/Utils/Stack.swift

This file was deleted.

3 changes: 2 additions & 1 deletion Tests/DABTests/DABTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ final class DABTests: XCTestCase {
}

func testIcrc1Oracle() async throws {
let _ = try await oracle.allTokens()
let tokens = try await oracle.allTokens()
print(tokens.count)
}

func testAllTokens() async throws {
Expand Down

0 comments on commit 8f9247a

Please sign in to comment.