Skip to content

Instantly share code, notes, and snippets.

@samlinux
Created March 17, 2022 09:24
Show Gist options
  • Save samlinux/2f5eefa06ad80096da021cbbb409ff04 to your computer and use it in GitHub Desktop.
Save samlinux/2f5eefa06ad80096da021cbbb409ff04 to your computer and use it in GitHub Desktop.
Angular meets Internet Computer
// Angular imports
import { Injectable } from '@angular/core';
import { environment } from './../environments/environment';
// IC Imports
const Actor = require("@dfinity/agent").Actor;
const HttpAgent = require("@dfinity/agent").HttpAgent;
// candid declarations from the backend canister
const idlFactory = require('../../../backend/src/declarations/backend/backend.did.js').idlFactory;
@Injectable({
providedIn: 'root'
})
export class IcService {
Actor? : any;
constructor() {
// define backend canister ID
const canisterId = environment.canisterId;
// create an finity/agent
const agent = new HttpAgent( {host: environment.host} );
/*
if(!environment.production) {
agent.fetchRootKey();
}
*/
// you can comment this line for production
agent.fetchRootKey();
// finally create an actor as the main gateway to your public exposed functions
this.Actor = Actor.createActor(idlFactory, {agent, canisterId});
}
// this is our gateway to the backend canister
public async greet(name:string){
return await this.Actor.greet(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment