In this page, we are going to use a simple smart contract as an example.
Smart Contract (before)
The below base sample of smart contract code is provided as an example for illustration and learning purposes only. Make sure that your smart contract aligns with security standards before deploying.
Here is the example of base Solidity smart contract, which is the contract that we want to register on the KRNL Platform.
Below here is the block of Solidity which you need to add into your smart contract. It allows your smart contract to be able to use the KRNL Operating System, kOS.
Instead of having a long chuck of codes in the smart contract, this version requires the inheritance of KRNL.sol into your smart contract.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;
import {KRNL, KrnlPayload, KernelParameter, KernelResponse} from "./KRNL.sol";
contract Sample is KRNL {
// Token Authority public key as a constructor
constructor(address _tokenAuthorityPublicKey) KRNL(_tokenAuthorityPublicKey) {}
// Initial value of message when this contract is being created
string message = "hello";
// Results from kernel will be emitted through this event
event Broadcast(address sender, uint256 score, string message);
// protected function
function protectedFunction(
KrnlPayload memory krnlPayload,
string memory input
)
external
onlyAuthorized(krnlPayload, abi.encode(input))
{
// Decode response from kernel
KernelResponse[] memory kernelResponses = abi.decode(krnlPayload.kernelResponses, (KernelResponse[]));
uint256 score;
for (uint i; i < kernelResponses.length; i ++) {
// kernel id 337 is an example here
// the response from kernel is in uint256 format
if (kernelResponses[i].kernelId == 337) {
score = abi.decode(kernelResponses[i].result, (uint256));
}
}
// Write new message
message = input;
// Emitting an event
emit Broadcast(msg.sender, score, input);
}
// Read message from contract
function readMessage() external view returns (string memory) {
return message;
}
}
Deployment
At this point, you can now deploy your upgraded smart contract.