Skip to content

Commit

Permalink
Include backend declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoferlund committed Nov 7, 2024
1 parent 86db5a8 commit b22e662
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ dist-ssr
#IC
.env
.dfx
src/backend/declarations
src/frontend/declarations
src/evm_rpc
target
canister_ids.json
canister_ids.json
27 changes: 27 additions & 0 deletions src/backend/declarations/backend.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type Result = variant { Ok : text; Err : text };
type Result_2 = variant { Ok : bool; Err : text };
type Result_3 = variant { Ok : vec text; Err : text };
type Result_4 = variant { Ok : nat64; Err : text };

service : {
get_address : () -> (Result);
get_balance_usdc : (opt text) -> (Result);
get_balance : (opt text) -> (Result);
get_batch_balances : (vec text) -> (Result);
get_latest_block : () -> (Result);
send_eth : () -> (Result);
send_eth_with_fillers : () -> (Result);
sign_message : (text) -> (Result);
transfer_usdc : () -> (Result);
watch_contract_event : () -> (Result);
watch_blocks_start : () -> (Result);
watch_blocks_stop : () -> (Result);
watch_blocks_is_polling : () -> (Result_2) query;
watch_blocks_poll_count : () -> (Result_4) query;
watch_blocks_get : () -> (Result_3) query;
watch_usdc_transfer_start : () -> (Result);
watch_usdc_transfer_stop : () -> (Result);
watch_usdc_transfer_is_polling : () -> (Result_2) query;
watch_usdc_transfer_poll_count : () -> (Result_4) query;
watch_usdc_transfer_get : () -> (Result_3) query;
}
36 changes: 36 additions & 0 deletions src/backend/declarations/backend.did.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Principal } from '@dfinity/principal';
import type { ActorMethod } from '@dfinity/agent';
import type { IDL } from '@dfinity/candid';

export type Result = { 'Ok' : string } |
{ 'Err' : string };
export type Result_2 = { 'Ok' : boolean } |
{ 'Err' : string };
export type Result_3 = { 'Ok' : Array<string> } |
{ 'Err' : string };
export type Result_4 = { 'Ok' : bigint } |
{ 'Err' : string };
export interface _SERVICE {
'get_address' : ActorMethod<[], Result>,
'get_balance' : ActorMethod<[[] | [string]], Result>,
'get_balance_usdc' : ActorMethod<[[] | [string]], Result>,
'get_batch_balances' : ActorMethod<[Array<string>], Result>,
'get_latest_block' : ActorMethod<[], Result>,
'send_eth' : ActorMethod<[], Result>,
'send_eth_with_fillers' : ActorMethod<[], Result>,
'sign_message' : ActorMethod<[string], Result>,
'transfer_usdc' : ActorMethod<[], Result>,
'watch_blocks_get' : ActorMethod<[], Result_3>,
'watch_blocks_is_polling' : ActorMethod<[], Result_2>,
'watch_blocks_poll_count' : ActorMethod<[], Result_4>,
'watch_blocks_start' : ActorMethod<[], Result>,
'watch_blocks_stop' : ActorMethod<[], Result>,
'watch_contract_event' : ActorMethod<[], Result>,
'watch_usdc_transfer_get' : ActorMethod<[], Result_3>,
'watch_usdc_transfer_is_polling' : ActorMethod<[], Result_2>,
'watch_usdc_transfer_poll_count' : ActorMethod<[], Result_4>,
'watch_usdc_transfer_start' : ActorMethod<[], Result>,
'watch_usdc_transfer_stop' : ActorMethod<[], Result>,
}
export declare const idlFactory: IDL.InterfaceFactory;
export declare const init: (args: { IDL: typeof IDL }) => IDL.Type[];
29 changes: 29 additions & 0 deletions src/backend/declarations/backend.did.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const idlFactory = ({ IDL }) => {
const Result = IDL.Variant({ 'Ok' : IDL.Text, 'Err' : IDL.Text });
const Result_3 = IDL.Variant({ 'Ok' : IDL.Vec(IDL.Text), 'Err' : IDL.Text });
const Result_2 = IDL.Variant({ 'Ok' : IDL.Bool, 'Err' : IDL.Text });
const Result_4 = IDL.Variant({ 'Ok' : IDL.Nat64, 'Err' : IDL.Text });
return IDL.Service({
'get_address' : IDL.Func([], [Result], []),
'get_balance' : IDL.Func([IDL.Opt(IDL.Text)], [Result], []),
'get_balance_usdc' : IDL.Func([IDL.Opt(IDL.Text)], [Result], []),
'get_batch_balances' : IDL.Func([IDL.Vec(IDL.Text)], [Result], []),
'get_latest_block' : IDL.Func([], [Result], []),
'send_eth' : IDL.Func([], [Result], []),
'send_eth_with_fillers' : IDL.Func([], [Result], []),
'sign_message' : IDL.Func([IDL.Text], [Result], []),
'transfer_usdc' : IDL.Func([], [Result], []),
'watch_blocks_get' : IDL.Func([], [Result_3], ['query']),
'watch_blocks_is_polling' : IDL.Func([], [Result_2], ['query']),
'watch_blocks_poll_count' : IDL.Func([], [Result_4], ['query']),
'watch_blocks_start' : IDL.Func([], [Result], []),
'watch_blocks_stop' : IDL.Func([], [Result], []),
'watch_contract_event' : IDL.Func([], [Result], []),
'watch_usdc_transfer_get' : IDL.Func([], [Result_3], ['query']),
'watch_usdc_transfer_is_polling' : IDL.Func([], [Result_2], ['query']),
'watch_usdc_transfer_poll_count' : IDL.Func([], [Result_4], ['query']),
'watch_usdc_transfer_start' : IDL.Func([], [Result], []),
'watch_usdc_transfer_stop' : IDL.Func([], [Result], []),
});
};
export const init = ({ IDL }) => { return []; };
50 changes: 50 additions & 0 deletions src/backend/declarations/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type {
ActorSubclass,
HttpAgentOptions,
ActorConfig,
Agent,
} from "@dfinity/agent";
import type { Principal } from "@dfinity/principal";
import type { IDL } from "@dfinity/candid";

import { _SERVICE } from './backend.did';

export declare const idlFactory: IDL.InterfaceFactory;
export declare const canisterId: string;

export declare interface CreateActorOptions {
/**
* @see {@link Agent}
*/
agent?: Agent;
/**
* @see {@link HttpAgentOptions}
*/
agentOptions?: HttpAgentOptions;
/**
* @see {@link ActorConfig}
*/
actorOptions?: ActorConfig;
}

/**
* Intializes an {@link ActorSubclass}, configured with the provided SERVICE interface of a canister.
* @constructs {@link ActorSubClass}
* @param {string | Principal} canisterId - ID of the canister the {@link Actor} will talk to
* @param {CreateActorOptions} options - see {@link CreateActorOptions}
* @param {CreateActorOptions["agent"]} options.agent - a pre-configured agent you'd like to use. Supercedes agentOptions
* @param {CreateActorOptions["agentOptions"]} options.agentOptions - options to set up a new agent
* @see {@link HttpAgentOptions}
* @param {CreateActorOptions["actorOptions"]} options.actorOptions - options for the Actor
* @see {@link ActorConfig}
*/
export declare const createActor: (
canisterId: string | Principal,
options?: CreateActorOptions
) => ActorSubclass<_SERVICE>;

/**
* Intialized Actor using default settings, ready to talk to a canister using its candid interface
* @constructs {@link ActorSubClass}
*/
export declare const backend: ActorSubclass<_SERVICE>;
42 changes: 42 additions & 0 deletions src/backend/declarations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Actor, HttpAgent } from "@dfinity/agent";

// Imports and re-exports candid interface
import { idlFactory } from "./backend.did.js";
export { idlFactory } from "./backend.did.js";

/* CANISTER_ID is replaced by webpack based on node environment
* Note: canister environment variable will be standardized as
* process.env.CANISTER_ID_<CANISTER_NAME_UPPERCASE>
* beginning in dfx 0.15.0
*/
export const canisterId =
process.env.CANISTER_ID_BACKEND;

export const createActor = (canisterId, options = {}) => {
const agent = options.agent || new HttpAgent({ ...options.agentOptions });

if (options.agent && options.agentOptions) {
console.warn(
"Detected both agent and agentOptions passed to createActor. Ignoring agentOptions and proceeding with the provided agent."
);
}

// Fetch root key for certificate validation during development
if (process.env.DFX_NETWORK !== "ic") {
agent.fetchRootKey().catch((err) => {
console.warn(
"Unable to fetch root key. Check to ensure that your local replica is running"
);
console.error(err);
});
}

// Creates an actor with using the candid interface and the HttpAgent
return Actor.createActor(idlFactory, {
agent,
canisterId,
...options.actorOptions,
});
};

export const backend = canisterId ? createActor(canisterId) : undefined;

0 comments on commit b22e662

Please sign in to comment.