Code Monkey home page Code Monkey logo

node-ic0's Introduction

ic0  npm version GitHub license PRs Welcome

An easy-to-use JavaScript API for the Internet Computer.


The ic0 package is a simple, straightfoward way to interact with canisters running on the Internet Computer (IC).

Installation

npm i --save ic0

Quick Start

Try running the following code from Node.js or a web application:

import ic from 'ic0';

const ledger = ic('ryjl3-tyaaa-aaaaa-aaaba-cai'); // Ledger canister

console.log(await ledger.call('name')); // Call the `name()` method

Easily call any Internet Computer canister using the following syntax:

import ic from 'ic0';

ic(canisterId).call(method, ...args); // IC mainnet

ic.local(canisterId).call(method, ...args); // Local replica

Local Canisters

The dfx start command hosts a local execution environment for developing canister smart contracts. Here is an example of how to call a local canister:

const backend = ic.local('rrkah-fqaaa-aaaaa-aaaaq-cai'); // Access a local canister

backend.call('myFunction', 123); // Call `myFunction(123)`

Basic usage:

const ledger = ic('ryjl3-tyaaa-aaaaa-aaaba-cai'); // Principal for the IC ledger

console.log(await ledger.call('name')); // => { name: 'Internet Computer' }

Advanced usage:

Replica canisters use agent-js behind the scenes.

import { replica, HttpAgent } from 'ic0';

const ic = replica(new HttpAgent({ ... })); // Use a custom agent from `@dfinity/agent`

const ledger = ic('ryjl3-tyaaa-aaaaa-aaaba-cai');

console.log(await ledger.call('name')); // => { name: 'Internet Computer' }

Mock Canisters

A mock canister makes it easy to mock the behavior of a canister.

Basic usage:

import { mockCanister } from 'ic0';

const mock = mockCanister({
    // Mock canister method named `echo()`
    async echo(x: number) {
        return x;
    }
});

console.log(await mock.call('echo', 123)); // => 123

Advanced usage:

Provide a fallback canister and/or compose several mocks by passing a second argument to the mockCanister() function:

import { mockCanister, replicaCanister } from 'ic0';

const ledger = replicaCanister(principal, agent);

const mockLedger = mockCanister({
    async echo(x: number) {
        return x;
    }
}, ledger); // Fall back to the deployed `ledger` canister

const mock = mockCanister({
    async runMock() {
        return this.call('echo', 456); // Call the mocked `echo()` function
    }
}, mockLedger); // Fall back to another mock canister

console.log(await mock.call('runMock')); // => 456

Related Projects

Check out the following GitHub repositories for more IC-related npm packages:

  • agent-js: a collection of npm packages for building on the Internet Computer
  • node-motoko: run Motoko programs directly in the browser
  • mo-dev: a live-reload server for local Motoko dapp development
  • @infu/icblast: a community-built library for exploring the IC and writing integration tests

node-ic0's People

Contributors

dependabot[bot] avatar rvanasa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

randallcexln

node-ic0's Issues

The ic0.app domain is not on the official "effective top-level domain" list

Someone at Dfinity really should add ic0.app to Mozilla's official publicsuffix.org list

Hi,
This post is not about this JS library. Rather, this is about the ic0.app domain.

I have noticed that ic0.app is not yet on the official list of effective top-level domains that is being maintained by the Mozilla Foundation.

Ic0.app really should be on that list, because the subdomains of ic0.app are separate websites with separate owners. In that, ic0.app is similar to vercel.app, netlify.app and herokuapp.com which are already on that list.

Note that some big software platforms such as Google Chrome's extension system rely on that list being up-to-date.

Thanks in advance.

Best.

Custom secion candid:service not found

Greetings,

I'm using this great library to query the ICP ledger (mainnet) for a transaction. The call is:

dfx canister --network=ic call ockk2-xaaaa-aaaai-aaaua-cai block '(XXXXXX:nat64)'

where XXXXX is the 'height' returned from a transaction done through plug wallet. This returns fine from the command line and it works using node-IC but I see a bunch of error's in the console from POST 404's from the lib

POST https://icp-api.io/api/v2/canister/ockk2-xaaaa-aaaai-aaaua-cai/read_state 404

Server returned an error:
Code: 404 ()
Body: Custom section candid:service not found.
Retrying request.

Eventually the result comes back successfully .. just wondering what these POST requests are? I'm not using any candid / did files for the ledger schema, just calling the one method "block"

The canister being called is:

https://icscan.io/canister/ockk2-xaaaa-aaaai-aaaua-cai

method: Block

Errors:

Capture
Capture1

socket hang up in local dfx canister

I was trying to make a call to a local canister via NodeJs:
const nameDip721 = await localDip721.call('nameDip721');
And the command is node --es-module-specifier-resolution=node src/node/index2.js

`/mnt/sda3/internet_computer/dip721-nft-container/node_modules/node-fetch/lib/index.js:1491
			reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
			       ^
FetchError: request to http://localhost:4943/api/v2/canister/ryjl3-tyaaa-aaaaa-aaaba-cai/query failed, reason: socket hang up
    at ClientRequest.<anonymous> (/mnt/sda3/internet_computer/dip721-nft-container/node_modules/node-fetch/lib/index.js:1491:11)
    at ClientRequest.emit (node:events:513:28)
    at Socket.socketOnEnd (node:_http_client:514:9)
    at Socket.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  type: 'system',
  errno: 'ECONNRESET',
  code: 'ECONNRESET'
}

Node.js v19.4.0
`
Did I miss something? Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.