Usage

Prerequisites

  • Guide for registering your dApp here.

  • It is suggested to try completing 10-minutes Tutorial before reading this page, since the code example will refer to kernel 337 (example kernel of 10-minutes Tutorial).

It is 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.


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 snipped 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 10-minutes Tutorial
        "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": {
        "505": { // 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);
    }
}

Last updated