diff --git a/.gitignore b/.gitignore index 2528d7c..3abc90c 100644 --- a/.gitignore +++ b/.gitignore @@ -31,8 +31,7 @@ dist-ssr #IC .env .dfx -src/backend/declarations src/frontend/declarations src/evm_rpc target -canister_ids.json \ No newline at end of file +canister_ids.json diff --git a/src/backend/declarations/backend.did b/src/backend/declarations/backend.did new file mode 100644 index 0000000..7b902c1 --- /dev/null +++ b/src/backend/declarations/backend.did @@ -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; +} diff --git a/src/backend/declarations/backend.did.d.ts b/src/backend/declarations/backend.did.d.ts new file mode 100644 index 0000000..476cb83 --- /dev/null +++ b/src/backend/declarations/backend.did.d.ts @@ -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 } | + { '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], 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[]; diff --git a/src/backend/declarations/backend.did.js b/src/backend/declarations/backend.did.js new file mode 100644 index 0000000..e8f2443 --- /dev/null +++ b/src/backend/declarations/backend.did.js @@ -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 []; }; diff --git a/src/backend/declarations/index.d.ts b/src/backend/declarations/index.d.ts new file mode 100644 index 0000000..55daa14 --- /dev/null +++ b/src/backend/declarations/index.d.ts @@ -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>; diff --git a/src/backend/declarations/index.js b/src/backend/declarations/index.js new file mode 100644 index 0000000..8def379 --- /dev/null +++ b/src/backend/declarations/index.js @@ -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_ + * 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;