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
    • Workflow
    • FAQ
    • Bounty Program
    • Social Medias
    • Thank You
  • Workshop
    • KRNL Workshop
Powered by GitBook
On this page
  • Prerequisites
  • Oasis Sapphire
  • Example
  • Remix
  • Remix Configuration
  • Compile
  • Deploy
  • Verify
  • Hardhat
  • Environment
  • Install Dependencies
  • Hardhat Configuration
  • Compile
  • Deploy
  • Verify
  • Foundry
  • Environment
  • Install Dependencies
  • Foundry Configuration
  • Remappings
  • Deploy
  • Verify
  • Verification on Sourcify

Was this helpful?

  1. Smart Contract
  2. Token Authority

How to Deploy a Token Authority on Oasis?

PreviousGeneric Token AuthorityNextDecoding Kernel Responses in Solidity

Last updated 1 month ago

Was this helpful?

Prerequisites

  • Read more about Oasis network .


In this tutorial, we will be using Oasis Sapphire testnet as an example for the deployment. Oasis is just the first option we support, but in the future will be one of many.

Smart contract deployment on mainnet will need different network details.


Oasis Sapphire

On the date of making this document, the version of Solidity has to be 0.8.24 for smart contracts on Oasis Sapphire network.

Firstly, make sure you have added an Oasis testnet into your wallet. The network details are in the below picture and code block.

Network name:
    Oasis Sapphire Testnet
RPC:
    https://testnet.sapphire.oasis.io
Chain ID:
    23295
Currency symbol:
    TEST

Once you have received the tokens from Oasis Sapphire faucet, it will be shown on your wallet application as in the image below.

Keep in mind that, smart contracts that will be deployed on Oasis Sapphire would have to be adjusted in the way that Oasis recommends. The below example is the simplified version of Token Authority smart contract for this case.

Example

It is important to inherit Oasis Sapphire into the smart contract that will be deployed on Oasis network. The code block below is the example of Oasis inheritance.

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";

import "@oasisprotocol/sapphire-contracts/contracts/EthereumUtils.sol";

contract TokenAuthority {
  // ...
  // ...
  // ...
}

Remix

Using Remix for deploying smart contract on Oasis Sapphire does not require many more steps if the provider is set to Injected Provider. Remix can fetch the network details from wallet application like MetaMask. After that the deployment process would be similar to Sepolia testnet deployment.

Remix Configuration

One thing to keep in mind is that Use configuration file on Solidity compiler from the sidebar has to be ticked when compiling Token Authority.

The given compiler_config.json which you need to put into Remix project is below this line. You may create your a new file if compiler_config.json does not exist.


{
	"language": "Solidity",
	"settings": {
		"optimizer": {
			"enabled": true,
			"runs": 200
		},
		"viaIR": true,
		"outputSelection": {
			"*": {
			"": ["ast"],
			"*": ["abi", "metadata", "devdoc", "userdoc", "storageLayout", "evm.legacyAssembly", "evm.bytecode", "evm.deployedBytecode", "evm.methodIdentifiers", "evm.gasEstimates", "evm.assembly"]
			}
		}
	}
}

Compile

You can now compile the smart contract. A useful tip here is, Remix has a short-cut of CTRL + S for compiling smart contract.

Deploy

After compiling, you can now deploy Token Authority on Oasis Sapphire. There are two ways of deploying this contract.

Deploy Using Remix Interface

The first one is to use the web-interface. For this case, ensure that you have selected the correct network in MetaMask. You will also need to fill the constructors of this Token Authority.

Deploy Using a Script

The second method is to use a script. We are going to use a script from default template of Remix IDE which is <deploy_with_ethers.ts>. The below code block is the modified version of that default script. Constructors are also mandatory to be added in the code.

import { deploy } from './ethers-lib'

(async () => {
  try {
    const result = await deploy('SimpleTokenAuthority',
                                [<OWNER_ADDRESS>])
    console.log(`address: ${result.address}`)
  } catch (e) {
    console.log(e.message)
  }
})()

Verify

After deploying, verification of deployed smart contract on Oasis Sapphire can be done within Remix, you might have to activate the plugin from Sourcify in the Plugin manager from the sidebar menu.

This plugin requires the network name, deployed contract address, and the name of the contract which should be available in your interface.


Hardhat

Environment

First of all, make sure you have set up private key of the wallet that will be the one which deploys smart contract in .env file as in the below example.

PRIVATE_KEY=YOUR_WALLET_PRIVATE_KEY

Secondly, install the relevant dependencies in the Hardhat project.

Install Dependencies

npm install @oasisprotocol/sapphire-contracts

npm install @openzeppelin/contracts

Hardhat Configuration

In the Hardhat config file, we have to add the Oasis Sapphire network as in the code block below. It is worth mentioning that in order to deploy Token Authority, Hardhat config file will need to set the viaIR to true. Also, make sure you import the private key from .env file properly.

// ...
const config: HardhatUserConfig = {
  solidity: {
    version: "0.8.24",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
      },
      viaIR: true // set viaIR to true
    }
  },
  networks: {
    'sapphire-testnet': {
      url: `https://testnet.sapphire.oasis.io`,
      chainId: 23295,
      accounts: [`0x${process.env.PRIVATE_KEY}`],
    },
  },
  sourcify: {
    enabled: true
  }
}
// ...

Compile

After that, the smart contract can be compile using Hardhat compile.

npx hardhat compile

Deploy

Then, we can use a script in the project for deploying the smart contract as in the below code block. The script can be saved in the directory of scripts/<SCRIPT>.

In this case, we name it as deploy.ts.

import { ethers } from "hardhat";

async function main() {
  const [deployer] = await ethers.getSigners();
  
  console.log("Deploying contracts with the account:", deployer.address);

  const Contract = await ethers.getContractFactory("SimpleTokenAuthority");
  const contract = await Contract.deploy("<OWNER_ADDRESS>");

  console.log("Contract deployed to:", contract.target);
  console.log("Contract: ", contract)
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

The command-line for running script to deploy smart contract on Oasis Sapphire using Hardhat would be in the below attached.

npx hardhat run scripts/deploy.ts --network sapphire-testnet

After running this command-line, you might want to verify your smart contract afterwards.

Verify

Verify Using Sourcify Interface

If you wish to use the verification interface on Sourcify. The relevant file that might be required later is <metadata.json> which can be found on project/artifacts/build-info/<your_solidity_file_name>.json.

Verify Using CLI

Another choice is to use Hardhat CLI, which can be done using the following code block.

npx hardhat verify --network sapphire-testnet \
  <DEPLOYED_CONTRACT_ADDRESS> \
  <OWNER_ADDRESS>

At the moment, verifying deployed smart contract on Oasis Sapphire network using Hardhat CLI might show red error message as in the following figure. Even though, the terminal shows errors, the smart contract might already be verified. It is recommended to re-check with the deployed address on ABI Playground


Foundry

Environment

Your .env file might have to be refined for supporting Oasis Sapphire network as in the block below.

PRIVATE_KEY=<YOUR_PRIVATE_KEY>

OASIS_TESTNET=https://testnet.sapphire.oasis.io

Install Dependencies

forge install OpenZeppelin/openzeppelin-contracts-upgradeable
forge install OpenZeppelin/openzeppelin-contracts-upgradeable --no-commit

Also, it is good to know that we have to inherit Oasis Sapphire before deploying smart contracts into the Oasis network.

Using Foundry forge in the below block of code to install Oasis Sapphire SDK into the project. You might need to use the no commit CLI to install this SDK.

forge install oasisprotocol/sapphire-paratime
forge install oasisprotocol/sapphire-paratime --no-commit

Foundry Configuration

You might notice that <.gitmodules> file in your project will have this part of Sapphire after installing the dependency.

# ...

[submodule "lib/sapphire-paratime"]
	path = lib/sapphire-paratime
	
# ...

Moreover, foundry.toml needs to enable via_ir for deploying Token Authority, as in the below code block.

[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# ENABLE VIA IR



evm_version = "paris"

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

Remappings

Next, the Foundry project needs <remappings.txt> for specify the Oasis Sapphire as in the given example below.

If this file already exist in your Foundry project, you may paste the given lines into your <remapping.txt>.

If you do not have remappings.txt, you can manually create one and paste the given lines into that file.

forge-std/=lib/forge-std/src/
@openzeppelin/contracts/=lib/openzeppelin-contracts-upgradeable/lib/openzeppelin-contracts/contracts/
openzeppelin-foundry-upgrades/=lib/openzeppelin-foundry-upgrades/src/
@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/
@oasisprotocol/sapphire-contracts/contracts=lib/sapphire-paratime/contracts/contracts/

Then, use the command-line below to remap Oasis into the proper approach.

forge remappings

Deploy

The last but not least step here is to deploy. The CLI for deploying smart contract on Oasis Sapphire is below.

forge create --rpc-url $OASIS_TESTNET \
  --private-key $PRIVATE_KEY \
  src/SimpleTokenAuthority.sol:SimpleTokenAuthority \
  --constructor-args <OWNER_ADDRESS>

Verify

Verify Using Sourcify Interface

After running this command-line, you might want to verify your smart contract afterwards. The relevant file that might be required later is <metadata.json> which can be found on project/out/Contract.sol/ContractName.json. You might have to upload the Solidity contract file as well when you verify through Sourcify.

Verify Using CLI

If you wish to use Foundry command-line to verify the contract, it natively supports verification on Sourcify.

forge verify-contract <DEPLOYED_CONTRACT_ADDRESS> \
  src/Contract.sol:ContractName \
  --chain-id 23295 \
  --verifier sourcify
  --verifier-url https://localhost:5555 # optional, defaults to https://sourcify.dev

Verification on Sourcify

Keep in mind that if you wish to continue using the web-interface you will to upload the <metadata.json> file, which is already compiled from the tool that you have used for deploying contract.

After verifying, we can now then see on Oasis Explorer, the deployed contract has been verified.

After successfully verified your contract, you can enter ABI playground for testing the smart contract through your browser.

It is worth mentioning that the Verification part on Oasis Explorer might not be updated accordingly to the verification from Sourcify. See the instruction for more details.

In case that the smart contract does not show as verified, there is a method to access ABI playground by substituting deployed contract address in the URL below.

https://abi-playground.oasis.io/?network=23295&contractAddress=<CONTRACT_ADDRESS>

Secondly, get the test tokens through faucet provided by Oasis on this .

In Foundry project, it is important to install the dependencies so that it can perform proficiently. One of the example we can focus on is Openzeppelin Ownable which is from the above section. It can be installed using the below code line.

After deploying smart contract to Oasis Sapphire network, you can access your smart contract on . On Oasis Explorer, the suggested method of verifying deployed smart contract is to use .

website
Oasis Explorer here
Sourcify
Example
here
How to Deploy a Smart Contract?