-
-
Save samlinux/2f5eefa06ad80096da021cbbb409ff04 to your computer and use it in GitHub Desktop.
Angular meets Internet Computer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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