Kitchen Layer Docs
  • 📌Welcome
  • Getting Started
    • Quickstart
    • Partners
    • Chain info
  • 📔Cook Book
    • Native Functions
    • Yield Bearing Wallet
    • AI Block Explorer
    • dApps Interoperability
    • Kitchen Account
  • 🍳Native dApps
    • Kitchen swap
    • Kitchen fun
    • Kitchen explorer
    • Kitchen hub
  • 💻Developer Docs
    • Foundry Kitchen
    • Use your first Precompiles
Powered by GitBook
On this page
  • Implementation
  • Testing
  • Deployment
  1. Developer Docs

Use your first Precompiles

Discover how to use kitchen precompi

PreviousFoundry Kitchen

Last updated 3 months ago

We will assume that you already have you

You are going to learn how to use kitchen precompiles. Our goal will be to implement a counter that is set to a random value using the kitchen precompiles Random.

Implementation

We are going to modify the Counter contract located in src/Counter.sol it should look like this:

First we are going to add the Random Precompile interface:

We will use this interface in order to call our random precompiles.

The next step is to implement our contract constructor and declare a new variable.

 function setNumber() public

We know implement the function: ![[Pasted image 20250127190035.png]] Since the precompiles interface function return an array we must get the first element of the array, we only need 1 number in this case.

Since we want this contrat to be simple we will won't go further for the implementation but feel free to play with precompiles.

Testing

Since we modified the implementation, we need to modify the test. Open the file located in test/Counter.t.sol

Since we changed the setNumber signature and it doesn't take a parameter anymore we need to change it. Remove the testFuzz_SetNumber test and remove the argument 0 in the setUp function. You will need also to create a new variable in test_Increment to store the counter number before updating it.

Now it's time to test it

⚠️ Be sure that you have foundry-kitchen installed.

In your terminal do the following command at the root of your repo.

forge-kitchen test

The output should be:

Deployment

Now that we have wrote and deployed the contract we need to deploy it.

We don't need to change anything.

In order to deploy our contract we are going to do:

forge script script/Counter.s.sol --broadcast --rpc-url RPC_URL
--private-key PK

Replace RPC_URL and PK by their value. For Private key management I recommend you to use Keystore however this will not be cover in this tutorial: https://book.getfoundry.sh/reference/cast/cast-wallet-address

You should normally see this:

To ensure that everything was smooth just copy paste the Hash address into the explorer: In our case everything is working as expected.

Now we are ready to change the setNumber function. The new signature of the function will be:

💻