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

feat(frontend): use AgentManager from @dfinity/utils #4508

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
78c38de
feat(frontend): use AgentManager from @dfinity/utils
AntonioVentilii Jan 30, 2025
26551da
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 30, 2025
9054e02
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 30, 2025
abac9a8
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 30, 2025
44fb40e
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 30, 2025
096138b
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 30, 2025
a2519b3
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 30, 2025
73326cc
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 31, 2025
7d7abb1
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 31, 2025
acf6348
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 31, 2025
cbfa796
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 31, 2025
78efcb9
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 31, 2025
cb868a7
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 31, 2025
8167699
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 31, 2025
7270a12
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Jan 31, 2025
92b0e8e
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
1df2cfa
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
00857f9
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
ef4e38e
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
6f4ae60
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
56ec1d7
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
a608640
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
14de6b8
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
02fb64e
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
0f86b7c
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
0e3fdaf
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
d13e57a
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
6a91ba3
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
9e8c999
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
81ce797
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
c4ceaf7
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
c232e17
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
d88539b
clean
AntonioVentilii Feb 3, 2025
705c658
adjust mock
AntonioVentilii Feb 3, 2025
3b86937
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
ccb8626
Merge branch 'main' into feat(frontend)/use-AgentManager-from-@dfinit…
AntonioVentilii Feb 3, 2025
babbaf7
fix mock
AntonioVentilii Feb 3, 2025
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
4 changes: 2 additions & 2 deletions src/frontend/src/icp-eth/api/cketh-minter.api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import type { CanisterIdText } from '$lib/types/canister';
import type { OptionIdentity } from '$lib/types/identity';
import type { Identity } from '@dfinity/agent';
Expand Down Expand Up @@ -28,7 +28,7 @@ export const ckEthMinterCanister = async ({
identity: Identity;
minterCanisterId: CanisterIdText;
}): Promise<CkETHMinterCanister> => {
const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

return CkETHMinterCanister.create({
agent,
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/icp/api/bitcoin.api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import type { CanisterIdText } from '$lib/types/canister';
import type { OptionIdentity } from '$lib/types/identity';
import type { Identity } from '@dfinity/agent';
Expand Down Expand Up @@ -56,7 +56,7 @@ const bitcoinCanister = async ({
identity: Identity;
bitcoinCanisterId: CanisterIdText;
}): Promise<BitcoinCanister> => {
const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

return BitcoinCanister.create({
agent,
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/icp/api/ckbtc-minter.api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MinterInfoParams } from '$icp/types/ck';
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import type { CanisterIdText } from '$lib/types/canister';
import type { OptionIdentity } from '$lib/types/identity';
import type { Identity } from '@dfinity/agent';
Expand Down Expand Up @@ -124,7 +124,7 @@ const minterCanister = async ({
identity: Identity;
minterCanisterId: CanisterIdText;
}): Promise<CkBTCMinterCanister> => {
const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

return CkBTCMinterCanister.create({
agent,
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/icp/api/icp-index.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ICP_INDEX_CANISTER_ID } from '$env/networks/networks.icp.env';
import { getAccountIdentifier } from '$icp/utils/icp-account.utils';
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import { WALLET_PAGINATION } from '$lib/constants/app.constants';
import type { OptionIdentity } from '$lib/types/identity';
import { IndexCanister, type GetAccountIdentifierTransactionsResponse } from '@dfinity/ledger-icp';
Expand All @@ -21,7 +21,7 @@ export const getTransactions = async ({
} & QueryParams): Promise<GetAccountIdentifierTransactionsResponse> => {
assertNonNullish(identity);

const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

const { getTransactions } = IndexCanister.create({
agent,
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/icp/api/icp-ledger.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ICP_LEDGER_CANISTER_ID } from '$env/networks/networks.icp.env';
import { nowInBigIntNanoSeconds } from '$icp/utils/date.utils';
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import type { OptionIdentity } from '$lib/types/identity';
import type { Identity } from '@dfinity/agent';
import { AccountIdentifier, LedgerCanister, type BlockHeight } from '@dfinity/ledger-icp';
Expand Down Expand Up @@ -53,7 +53,7 @@ export const icrc1Transfer = async ({
};

const ledgerCanister = async (identity: Identity): Promise<LedgerCanister> => {
const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

return LedgerCanister.create({
agent,
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/icp/api/icrc-index-ng.api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getIcrcAccount } from '$icp/utils/icrc-account.utils';
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import { WALLET_PAGINATION } from '$lib/constants/app.constants';
import type { CanisterIdText } from '$lib/types/canister';
import type { OptionIdentity } from '$lib/types/identity';
Expand Down Expand Up @@ -56,7 +56,7 @@ const indexNgCanister = async ({
identity: Identity;
indexCanisterId: CanisterIdText;
}): Promise<IcrcIndexNgCanister> => {
const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

return IcrcIndexNgCanister.create({
agent,
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/icp/api/icrc-ledger.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { nowInBigIntNanoSeconds } from '$icp/utils/date.utils';
import { getIcrcAccount } from '$icp/utils/icrc-account.utils';
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import type { CanisterIdText } from '$lib/types/canister';
import type { OptionIdentity } from '$lib/types/identity';
import { type Identity } from '@dfinity/agent';
Expand Down Expand Up @@ -163,7 +163,7 @@ const ledgerCanister = async ({
identity: Identity;
ledgerCanisterId: CanisterIdText;
}): Promise<IcrcLedgerCanister> => {
const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

return IcrcLedgerCanister.create({
agent,
Expand Down
39 changes: 2 additions & 37 deletions src/frontend/src/lib/actors/agents.ic.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,4 @@
import { LOCAL, REPLICA_HOST } from '$lib/constants/app.constants';
import type { Option } from '$lib/types/utils';
import type { HttpAgent, Identity } from '@dfinity/agent';
import { createAgent as createAgentUtils, isNullish } from '@dfinity/utils';
import { AgentManager } from '@dfinity/utils';

let agents: Option<Record<string, HttpAgent>> = undefined;

export const getAgent = async ({ identity }: { identity: Identity }): Promise<HttpAgent> => {
const key = identity.getPrincipal().toText();

if (isNullish(agents) || isNullish(agents[key])) {
const agent = await createAgent({ identity });

agents = {
...(agents ?? {}),
[key]: agent
};

return agent;
}

return agents[key];
};

export const createAgent = ({
identity,
verifyQuerySignatures = true
}: {
identity: Identity;
verifyQuerySignatures?: boolean;
}): Promise<HttpAgent> =>
createAgentUtils({
identity,
fetchRootKey: LOCAL,
host: REPLICA_HOST,
verifyQuerySignatures
});

export const clearAgents = () => (agents = null);
export const agents = AgentManager.create({ fetchRootKey: LOCAL, host: REPLICA_HOST });
4 changes: 2 additions & 2 deletions src/frontend/src/lib/canisters/backend.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from '$declarations/backend/backend.did';
import { idlFactory as idlCertifiedFactoryBackend } from '$declarations/backend/backend.factory.certified.did';
import { idlFactory as idlFactoryBackend } from '$declarations/backend/backend.factory.did';
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import {
mapAllowSigningError,
mapBtcPendingTransactionError,
Expand All @@ -31,7 +31,7 @@ export class BackendCanister extends Canister<BackendService> {
identity,
...options
}: CreateCanisterOptions<BackendService>): Promise<BackendCanister> {
const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

const { service, certifiedService, canisterId } = createServices<BackendService>({
options: {
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/lib/canisters/kong_backend.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
} from '$declarations/kong_backend/kong_backend.did';
import { idlFactory as idlCertifiedFactoryKongBackend } from '$declarations/kong_backend/kong_backend.factory.certified.did';
import { idlFactory as idlFactoryKongBackend } from '$declarations/kong_backend/kong_backend.factory.did';
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import { mapKongBackendCanisterError } from '$lib/canisters/kong_backend.errors';
import type { KongSwapAmountsParams, KongSwapParams } from '$lib/types/api';
import type { CreateCanisterOptions } from '$lib/types/canister';
Expand All @@ -16,7 +16,7 @@ export class KongBackendCanister extends Canister<KongBackendService> {
identity,
...options
}: CreateCanisterOptions<KongBackendService>): Promise<KongBackendCanister> {
const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

const { service, certifiedService, canisterId } = createServices<KongBackendService>({
options: {
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/lib/canisters/reward.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from '$declarations/rewards/rewards.did';
import { idlFactory as idlCertifiedFactoryReward } from '$declarations/rewards/rewards.factory.certified.did';
import { idlFactory as idlFactoryReward } from '$declarations/rewards/rewards.factory.did';
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import type { CreateCanisterOptions } from '$lib/types/canister';
import { Canister, createServices, type QueryParams } from '@dfinity/utils';

Expand All @@ -16,7 +16,7 @@ export class RewardCanister extends Canister<RewardService> {
identity,
...options
}: CreateCanisterOptions<RewardService>): Promise<RewardCanister> {
const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

const { service, certifiedService, canisterId } = createServices<RewardService>({
options: {
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/lib/canisters/signer.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
} from '$declarations/signer/signer.did';
import { idlFactory as idlCertifiedFactorySigner } from '$declarations/signer/signer.factory.certified.did';
import { idlFactory as idlFactorySigner } from '$declarations/signer/signer.factory.did';
import { getAgent } from '$lib/actors/agents.ic';
import { agents } from '$lib/actors/agents.ic';
import { P2WPKH, SIGNER_PAYMENT_TYPE } from '$lib/canisters/signer.constants';
import type { BtcAddress, EthAddress } from '$lib/types/address';
import type {
Expand All @@ -32,7 +32,7 @@ export class SignerCanister extends Canister<SignerService> {
identity,
...options
}: CreateCanisterOptions<SignerService>): Promise<SignerCanister> {
const agent = await getAgent({ identity });
const agent = await agents.getAgent({ identity });

const { service, certifiedService, canisterId } = createServices<SignerService>({
options: {
Expand Down
8 changes: 6 additions & 2 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
failTestsThatLogToConsole
} from '$tests/utils/console.test-utils';
import { HttpAgent } from '@dfinity/agent';
import { AgentManager } from '@dfinity/utils';
import '@testing-library/jest-dom';
import { configure } from '@testing-library/svelte';
import 'fake-indexeddb/auto';
Expand All @@ -18,8 +19,11 @@ vi.mock(import('$lib/actors/agents.ic'), async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
// eslint-disable-next-line require-await
getAgent: async () => mock<HttpAgent>()
agents: {
...actual.agents,
// eslint-disable-next-line require-await
getAgent: async () => mock<HttpAgent>()
} as unknown as AgentManager
};
});

Expand Down