KRNL Platform Guidebook
KRNL Platformkrnl.xyz
  • Introduction
    • Introduction
    • How Does kOS Work?
  • Setup
    • Getting Started with KRNL
    • Quick Start (CLI)
    • Quick Start (Online IDE)
      • Quick Start (Remix IDE)
      • Quick Start (Atlas IDE)
    • create-krnl-app
    • Platform Registration
  • Kernel
    • What are Kernels?
    • Kernel Registration
      • Kernel Registration (on-chain)
      • Kernel Registration (off-chain)
    • Supported OpenAPI (Off-chain Kernel)
    • Staking
  • Smart Contract
    • Steps for Smart Contract Developers
    • Choosing Kernel(s)
    • Token Authority
      • What is a Token Authority?
      • How to Build a Token Authority?
      • Generic Token Authority
      • How to Deploy a Token Authority on Oasis?
    • Decoding Kernel Responses in Solidity
    • How to Integrate Your Smart Contract with kOS?
    • Smart Contract Registration
  • DApp
    • dApp Registration
    • KRNL SDK Installation
    • Usage
    • KRNL Node RPC
  • Miscellaneous
    • Overview
    • What is the KRNL Platform?
    • What are You Trying to BUIDL?
    • Smart Contract Fundamentals
      • Why Do I Have to Register a Smart Contract?
      • How to Deploy a Smart Contract?
    • Recommended Kernels List
  • Helpful Resources
    • New to Web3?
    • Dictionary
    • Testnet Faucets
    • How to Get Etherscan API Key?
  • Litepaper
    • Litepaper
      • Overview
      • Problem Statement
      • Current State Does Not Solve the Problem
      • Introducing Kernels
      • Ecosystem of Kernels
      • The KRNL Operating System (kOS)
      • Decentralization and Security Considerations
      • Use Cases for KRNL
  • Appendices
    • FAQ
    • Bounty Program
    • Social Medias
    • Thank You
  • Workshop
    • KRNL Workshop
    • Speed's Workshop
Powered by GitBook
On this page
  • Prerequisites
  • RPC Endpoint
  • executeKernels(...)
  • Off-chain Kernel (GET method) Example
  • getKernelsCost(...)

Was this helpful?

  1. DApp

Usage

PreviousKRNL SDK InstallationNextKRNL Node RPC

Last updated 8 days ago

Was this helpful?

Prerequisites

  • Guide for registering your dApp .

  • It is suggested to try completing before reading this page, since the code example will refer to kernel 337 (example kernel of Quick Start guides).

It is also recommended to explore the documentations from the kernel(s) that you are going to select.

Every kernel is different when it comes to response data types, authentication methods/keys, etc.


RPC Endpoint

When it comes to building a dApp using KRNL SDK, the RPC endpoint is required to be able to call KRNL node.

Visit the page in the portal below for more details.


executeKernels(...)

executeKernels(entryId, accessToken, kernelRequestData, functionParams)

There are 4 parameters which are required for using this function from the KRNL SDK.

  • entryId - obtain after dApp registration

  • accessToken - obtain after dApp registration

  • kernelRequestData - parameter(s) that will be sent to predefined kernel(s)

  • functionParams - parameter that will be sent to registered smart contract

The below code snipped is the example of how to use executeKernels(...) function, as well as how to utilize the response from with the transaction.

This snippet is made from Next.js (JavaScript). Feel free to try it with other environment such as Next.js (TypeScript), React, or else.

"use client";
import { ethers } from "krnl-sdk";
import { useState } from 'react';
import { contractAbi } from "./abi";
import dotenv from 'dotenv';
dotenv.config();

// ==========================================================

// The value of private key here is hardcoded.
// If you are using WalletConnect or similar framework, make sure to update this line
const privateKey = process.env.NEXT_PUBLIC_PRIVATE_KEY; // READ ABOVE
const wallet = new ethers.Wallet(privateKey);

// ==========================================================

const contractAddress = YOUR_CONTRACT_ADDRESS; // YOUR CONTRACT ADDRESS
const provider = new ethers.JsonRpcProvider(process.env.NEXT_PUBLIC_RPC_KRNL);
const signer = new ethers.Wallet(privateKey, provider);

// ==========================================================

// ABI of your deployed smart contract
const contract = new ethers.Contract(contractAddress, contractAbi, signer);

// ==========================================================

const abiCoder = new ethers.AbiCoder();

const parameterForKernel337 = abiCoder.encode(["address"], [`${wallet.address}`])

const entryId = "<YOUR_ENTRY_ID>";
const accessToken = "<YOUR_ACCESS_TOKEN>";
const kernelRequestData = {
    senderAddress: wallet.address,
    kernelPayload: {
        // CODE HERE
        // Kernel 337 is used in Quick Start guides
        "337": { // <<<<< REPLACE WITH YOUR KERNEL ID
            functionParams: parameterForKernel337
        },
        // If registered smart contract required more than one kernel,
        // you have to add them here.
        // "123": { // <<<<< REPLACE WITH YOUR KERNEL ID
        //     functionParams: parameterForKernel123
        // }
    }
}

// EXAMPLE
const textInput = "Example"
const functionParams = abiCoder.encode(["string"], [textInput]);

async function executeKrnl() {
    const krnlPayload = await provider.executeKernels(entryId, accessToken, kernelRequestData, functionParams);
    return krnlPayload;
}

async function callContractProtectedFunction(executeResult) {
    const krnlPayload = {
        auth: executeResult.auth,
        kernelResponses: executeResult.kernel_responses,
        kernelParams: executeResult.kernel_params
    };
    const tx = await contract.protectedFunction(krnlPayload, textInput);
    return tx.hash;
}

export default function dAppExample() {
    const handleClick = async () => {
        try {
            const krnlPayload = await executeKrnl();
            if (krnlPayload) {
                console.log(krnlPayload);
                const tx = await callContractProtectedFunction(krnlPayload);
                console.log("Transaction Hash is:", tx);
            }
        } catch (error) {
            console.error(error);
        }
    }

    return (
        <div>
            <h1>Example dApp</h1>
            <button onClick={handleClick}>Make a transaction</button>
        </div>
    )
}

Off-chain Kernel (GET method) Example

// ...
const walletAddressToCheck = "0x1234567890";
const kernelRequestData = {
    "senderAddress": "<SENDER_ADDRESS>",
    "kernelPayload": {
        "123456": { // off-chain with openapi schema GET method
            "parameters": {
                "header": {},
                "body": {},
                "query": {},
                "path": {
                    "wallet_address": walletAddressToCheck
                }
            }
        }
    }
}
// ...

getKernelsCost(...)

getKernelsCost(entryID)

This function is not essential for using the KRNL protocol. Implementing getKernelsCost on your dApp can show the cost of utilized kernels to the user.

entryID is the parameter which you should already have at this point. Assuming that the entryID for this case is 12345 as in the example below.

import { ethers } from 'krnl';

async function sample() {
    try {
        const provider = new ethers.JsonRpcProvider("<RPC_URL>");
        const entryID = "12345";
        const cost = await provider.getKernelsCost(entryID);
        console.log("Cost of kernels would be: ", cost);
    } catch (error) {
        console.error("Error fetching kernels cost: ", error);
    }
}
here
Getting Start With KRNL
KRNL Node RPC