Integrate Skynet package into your Project

Introduction to Skynet

What is the Skynet Package?

This package handles CRUD (Create, Read, Updated, and Delete) operations for applications deployed to Skynet.

Here are some things you need to know about a SkyID:

  • It can have single or multiple applications. For example, a database, backend, and frontend website deployed as three applications under a SkyID with an ID 1535.

  • You need sUsd tokens to run your SkyID, and you have to deposit them into the SkyID.

  • The package is modular; this means you can extend and modify it according to your specific use case.

The Skynet package handles:

  • Contract interactions.

  • Application encryption and decryption.

  • Manages uploading and downloading of application payloads to and from decentralized storage platforms.

  • Perform CRUD operations on your application–Application manager.

  • Estimates and manages the cost of running an application; this happens through a drip manager.


Prerequisite

To get the best out of this package and the contract calls to perform for specific use cases. You need to:

To carry out these contract initialization and calls appropriately. You should include the required files for your specific use case in your project’s root directory.


Installation

Run this in your project's root directory:

npm i @decloudlabs/skynet

Initialise Contract Service

It contains the required definitions for contract application deployment to initialize contract service. Implement this code:

import { ethers } from 'ethers';
import SkyBrowserSigner from '@decloudlabs/skynet/lib/services/SkyEtherContractService';
import SkyEtherContractService from '@decloudlabs/skynet/lib/services/SkyEtherContractService';
import SkyMainBrowser from '@decloudlabs/skynet/lib/services/SkyMainBrowser';


const provider = new ethers.providers.Web3Provider(window.ethereum);

const signer = provider.getSigner();

 const envConfig: SkyEnvConfigBrowser = {
        STORAGE_API: {
          IPFS: {
            PROJECT_ID: "infurs ipfs project id",
            PROJECT_SECRET: "infurs ipfs project secret",
          }
        },
        CACHE: {
          TYPE: "CACHE",
        },
      };

const contractInstance = new SkyEtherContractService(provider, signer, address, 11); // 11 is the chain Id of Skynet

const skyMainBrowser = new SkyMainBrowser(
        contractInstance,
        address, // connected wallet address
        new SkyBrowserSigner(address, signer),
        envConfig
      );

await skyMainBrowser.init(true);

Explaining the parameters:

  • provider:Initializes a Web3 provider using the window.ethereum object. The provider here allows the application to read from and send transactions to the Ethereum blockchain.

  • signer: This allows users to sign transactions and applications to initiate state-changing operations on the blockchain.

  • contractInstance : it's an SDK library that is used to create an instance of contracts that contains the main contracts deployed in the Skynet chain.

  • address: The Ethereum address associated with your wallet would be used for performing the transactions. Ensure the signer is authorized to act on behalf of this address.

  • skyMainBrowser: This initializes an instance of skyMainBrowser while passing contractInstance, address, and a new instance of SkyBrowserSigner.


Mint and Fetch SkyId

To mint and fetch the ID of a SkyID, implement this code:

const fetchNFTID = async () => {
    const mintResp = await contractService.callContractWrite(
	contractService.AppNFTMinter.mint(address, {
        	value: 10 ** 11,
        	from: address,
    	})
	);
    
    if(!mintResp.success) throw new Error("failed to mint");
    
    const nftCountResp = await appCrypto.contractService.callContractRead(
   		 AppNFT.balanceOf(address),
   		 (res) => res.toNumber()
   	 );
    if(!nftCountResp.success) throw new Error("failed to fetch NFT");
    
    const nftCount = nftCountResp.data;
    
    const nftReq = appCrypto.contractService.callContractRead(
   	 AppNFT.tokenOfOwnerByIndex(address, nftCount-1),
   	 (res) => res.toNumber()
    );
    if(!nftReq.success) throw new Error("failed to fetch the nftID");
    
    const nftID = nftReq.data;
}

The code does the following:

  • The fetchNFTID function helps you mint a new NFT and fetch the ID of the NFT owned by a specific address.

Example Code
import { fetchNFTID } from './filepath';

fetchNFTID()

Fetch Application List

Use this code to implement:

const appList = await appManager.contractCall.getAppList(nftID);

Implementing this code does the following:

  • It retrieves the applications that are linked to specific SkyIDs.

  • After retrieving, services related to the applications can be accessed or used.

Example Code
import { STKAppManager, ContractApp, SubscriptionParam, APICallReturn } from '@decloudlabs/stk-v2';

// Initialize the necessary services and callbacks
// Replace these with your actual implementations
const appBrowserCache = {};
const multiStorage = {};
const EtherContracts = {};
const appEncryptor = {};
const appDecryptor = {};

const saveAppToLocal = (app: ContractApp): Promise<APICallReturn<string>> => {
    // Implementation here
};

const saveSubParamToLocal = (subParam: SubscriptionParam): Promise<APICallReturn<string>> => {
    // Implementation here
};

const removeAppFromLocal = (nftID: string, appID: string): Promise<APICallReturn<string>> => {
    // Implementation here
};

// Create a new instance of STKAppManager
const appManager = new STKAppManager(
    appBrowserCache,
    multiStorage,
    EtherContracts,
    appEncryptor,
    appDecryptor,
    saveAppToLocal,
    saveSubParamToLocal,
    removeAppFromLocal
);

// Use the getAppList method
// Replace 'nftID' with your actual NFT ID
const nftID = 'nftID';
appManager.contractCall.getAppList(nftID)
    .then(appList => {
        console.log(appList);
    })
    .catch(error => {
        console.error('Failed to get app list:', error);
    });

Last updated