Coder Social home page Coder Social logo

starknet-debug's Introduction

Starknet debugging

The goal of this tutorial is to see what tool could be useful to debug a smart contract using 3 different testing frameworks:

We'll use a functionality of the Cairo language called hints, which allows you to inject python arbitrarily in your code. Hint usage is heavily restricted on StarkNet and is unapplicable in Smart Contracts. But it is extremely useful to debug your contract.

Smart contract

The first smart contract array_contract.cairo is a dumb smart contract which has a view function that will

  • Compute the product of each array element
  • Add the current contract’s address to it.

The second smart contract mock_contract.cairo is also a dumb smart contract which will save an array given at the initialization into a mapping and has a view function that will

  • Compute the product of each mapping element

Testing frameworks

Hardhat

Run the following command in the hardhat directory

npm i

You'll also need cairo-lang and the starknet-devnet. To setup a full cairo env you can take a look at this article

pip install "starknet-devnet>=0.2.1" cairo-lang

You can run any hint on the starknet-devnet if you manage to compile the contract. By default, StarkNet contracts can not use any hints in their code. But An option has been added in hardhat to compile a contract without validating the hints in the contract --disable-hint-validation. To execute the tests you can run the test script in package.json:

To debug a smart contract using hardhat you may want to print variables, there is an example in the smart contract.

The contracts to fix are here.

You can find the test file here.

The following command will run the hardhat tests and execute the hints in this terminal

npm run test

Python

To run the python unit test files you'll need pytest and asynctest

pip install pytest asynctest cairo-lang

The Python testing framework doesn't need to interact with the starknet-devnet as it can natively use the testing functions from the cairo-lang package so you can also use any python hint you want. You can even add a breakpoint in a contract, how powerful is that ? You can run your python unit tests with pytest and inspect whatever you want in your contract.

The contracts to fix are here.

You can find the test script here

Run the tests separately using:

pytest tests/test_array_contract.py -s -W ignore::DeprecationWarning
pytest tests/test_mock_contract.py -s -W ignore::DeprecationWarning

Or all at once with:

pytest -s -W ignore::DeprecationWarning

Ape

To run the ape unit test files you'll need ape configured. This is how you can do it:

pip install eth-ape "starknet.py==0.2.2a0"
ape plugins install cairo starknet

If you start from scratch you'd have to init your project and update the ape-config.yaml file but here it's already done.

If you want to learn a little bit more about ape here is a video that you can watch which explains the basis of ape for starknet

Since ape hides everything printed by the devnet we can use print to debug the contract. I chose to save the logs in a file but we could also setup a server that would receive data from the smart-contract execution or whatever other technique you can think of.

To run the tests run the following command:

ape test

starknet-debug's People

Contributors

amanusk avatar drspacemn avatar l-henri avatar lucaslvy avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.