Local Dev

Goal: To complete the integration of kOS into your smart contract and to be able to call transactions within a dApp.

Read time - 30 minutes

This guide contains the instructions for deploying relevant smart contracts and using the KRNL SDK . For this demo we will use a mock realestate dApp where you can list your property, analyze the property score using a bunch of external APIs, run this dataset through OpenAI GPT API and get a final score to use in the contract and the best part is we will do all of this in a atomic transaction


Preparations

  • Install Foundry

    • Follow the installation process and run forge --version to verify the installation

  • Install Docker

  • Etherscan (for verifying smart contracts on Sepolia network) - Find the API key here

  • Sepolia testnet - "Add a custom network" on your MetaMask wallet

  • Funds - make sure you have a SepoliaETH by claiming them from faucets here.

    • Sepolia token ≈ 0.1 SepoliaETH

  • Privy App ID and Secret

Deploy the Target Contract

1. Clone the repo

Go to your terminal and run this command:

git clone https://github.com/KRNL-Labs/poc-dapp-realestateinvestment-7702.git hello-krnl

2. Go to contract folder

Run this command:

cd hello-krnl/contracts

3. Create the .env file

3.1 Copy .env.example to .env

cp .env.example .env

3.2 Fill the .env file with these values:

PRIVATE_KEY=<your_wallet_private_key> //Add your private key with 0x like 0x.....

MOCK_USDC_ADDRESS=0xF2Ea67F83b58225edF11F3Af4A5733B3E0844509 //We have already deployed a mock USD for testing

DELEGATED_ACCOUNT_ADDRESS=0x9969827E2CB0582e08787B23F641b49Ca82bc774 //This is the SCA you will be authorizing to interact with KRNL

# Optional: Etherscan API key for contract verification
ETHERSCAN_API_KEY=<your_etherscan_api>

SEPOLIA_RPC_URL=https://ethereum-sepolia-rpc.publicnode.com //Use any Ethereum Sepolia RPC you have

3.3 Run this command in your terminal to set these values for the current session:

source .env

4. Deploy the contract

4.1 Run the following command to install the forge dependencies:

forge install OpenZeppelin/openzeppelin-contracts --no-commit
forge install eth-infinitism/[email protected] --no-commit
forge install foundry-rs/forge-std --no-commit

4.2 Run the following command to deploy the contract:

forge script script/Deploy.s.sol --rpc-url sepolia --broadcast

4.3 After the deployment you get screen like this:

You have to deploy your own target contract where your EOA will be used as the master key for signature verification, you will learn about this later in the docs

Setup the Attestor Image

Attestor is a component which will attest and also resolve the secrets in your DSL workflow (don't worry we will learn about all of this in the core concepts)

1. Download the create-attestor script

curl https://public.mypinata.cloud/ipfs/bafkreifvezdhwvmi6psqqk6vxalazp56ovx3fmgqkmfu5ih5xyxsdbfixi -o create-attestor-standalone.sh
chmod +x create-attestor-standalone.sh

2. Run the Attestor

./create-attestor-standalone.sh

3. Follow the onscreen instruction

3.1 Enter your project name:

3.2 Enter Docker registry, it should docker.io

3.3 Enter your docker username

3.4 Enter your private key

3.5 Create a encryption secret which is a password to the safe carrying your private key to make your private key is handled safely. Press enter to auto generate

3.6 Pass the secrets to resolve in the DSL workflow during execution

We will learn more about workflow DSL later in Workflows. For now make sure to add the exact key pair as a part of creating attestor image

3.7 Wait for few seconds and you will see a success screen with your attestor image

Setup the dApp

1. Go to frontend folder from the contract folder

cd ../frontend

2. Create the .env file

3.1 Copy .env.example to .env

cp .env.example .env

3.2 Fill the .env file with these values:

VITE_PRIVY_APP_ID=<your_privy_app_id>
VITE_PRIVY_APP_SECRET=<your_privy_secret>

VITE_CHAIN_ID=11155111 //Chain ID for Ethereum Sepolia

VITE_DELEGATED_ACCOUNT_ADDRESS=0xFFB5C2684532D8B24313Fc084d65DDaa0B946040 // Do not change this

VITE_DELEGATE_OWNER=<your_eoa_address> // This should be the same address you used to create your attestor
VITE_REAL_ESTATE_INVESTMENT_ADDRESS=0x038b5f24FF651d174411F15f8cc64C4156A9a6D0 //Replace this with the target contract address we just deployed
VITE_MOCK_USDC_ADDRESS=0xaC66E9916dCe765405E4A4297DdDF61729CbDFF9

VITE_ATTESTOR_IMAGE=image://docker.io/ash20pk20/attestor-realestate:latest //Replace this image with your attestor image link, make to keep the image:// prefix

VITE_RPC_URL=https://ethereum-sepolia-rpc.publicnode.com //Use any Ethereum Sepolia RPC you have

3. Install the dependencies

npm install

4. Run the application

npm run dev

Your app will be available at http://localhost:5173/

5. Run your KRNL workflow

We are running this workflow from your user's point of view

5.1 Connect your wallet

5.2 Fund the embedded wallet with Sepolia ETH token

5.3 Delegate execution to KRNL smart contract account

This embedded wallet is completely independent of your EOA wallet to make sure your user's personal wallets are safe when delegating execution to our SCA

5.4 Run the workflow DSL

The time taken for the execution depends on the latency of the external services being used in the workflow.

5.5 Verify the execution in your target contract

The solidity event signifies that the function was called with the KRNL response -> the response signature was verified by the modifier and the response was decoded for the computation.

These steps signifies the happy path to deploying using first KRNL integrated application which let's you use functions from our EVM contracts across multiple chains as well using external API letting you build dApps like apps without being limited by the existing smart contracts design

What's next?

Congrats on completing your first hands-on tutorial using KRNL now it's time to learn the Core Concepts to understand all the concepts and flows in detail.

Last updated