Giter VIP home page Giter VIP logo

foundry-yulp-template's Introduction

Foundry-Yulp-Template

A Foundry Template to compile and test your Yul+ Contracts.

Installation / Setup

Installation is only two easy steps. First, clone this repo.

https://github.com/ControlCplusControlV/Foundry-Yulp-Template.git

Next, make sure you have Yul-Log installed. Yul-Log is a Yul/Yul+ toolchain that enables Yul+ contracts to be compiled into bytecode.

npm i -g yul-log  

Now you are all set up and ready to go!


Compiling/Testing Yul+ Contracts

In order to compile and test Yul+ contracts with Foundry, there are two simple steps. First make sure to put your .yulp files in the directory named Yul+ Contracts. This way, yul-log will know where to look when compiling your contracts.

Next, you will need to create an interface for your contract. This will allow Foundry to interact with your Yul+ contract, enabling the full testing capabilities that Foundry has to offer.

Once you have an interface set up for your contract, you are ready to use the YulpDeployer!

The YulpDeployer is a pre-built contract that takes a filename, deploys the corresponding Yul+ contract and returns the address that the bytecode was deployed to. If you want, you can check out the YulpDeployer contract here.

From here, you can simply initalize a new contract through the interface you made for the Yul+ contract and pass in the address of the deployed bytecode. Now your Yul+ contract is fully functional within Foundry!


Example

Here is a quick example of how to setup and deploy a SimpleStore contract written in Yul+.

Here is the SimpleStore.yulp file, which should be within the Yul+ Contracts directory.

SimpleStore.yulp

object "SimpleStore" {
  code {
    datacopy(0, dataoffset("Runtime"), datasize("Runtime"))
    return(0, datasize("Runtime"))
  }
  object "Runtime" {
    code {
      calldatacopy(0, 0, 36) // write calldata to memory

      mstruct StoreCalldata( // Custom addressable calldata structure
        sig: 4,
        val: 32
      )

      switch StoreCalldata.sig(0) // select signature from memory (at position 0)

      case sig"function store(uint256 val)" { // new signature method
        sstore(0, StoreCalldata.val(0)) // sstore calldata value
        log2(0, 0, topic"event Store(uint256 value)", StoreCalldata.val(0))
      }

      case sig"function get() returns (uint256)" {
        mstore(100, sload(0))
        return (100, 32)
      }
    }
  }
}

Next, here is an example interface for the SimpleStore contract.

SimpleStore Interface

interface SimpleStore {
    function store(uint256 val) external;
    function get() external returns (uint256);
}

Lastly, here is the test file that deploys the Yul+ contract and tests the get() function. You can see that this file imports the SimpleStore.sol interface as well as the YulpDeployer.sol contract. To deploy the contract, simply create a new instance of YulpDeployer and call yulpDeployer.deployContract(fileName) method, passing in the file name of the contract you want to deploy. In this example, we pass in SimpleStore to deploy the SimpleStore.yulp contract. This function returns the address that the contract was deployed to, which we can use to initialize the SimpleStore interface. With that, your Yul+ contract can now be used within Foundry like any other Solidity contract. To test any Yul+ contract deployed with YulpDeployer, simply run forge test --ffi. You can use this command with any additional flags. For example: forge test --ffi -f <url> -vvvv.

SimpleStore Test

import "../../lib/ds-test/test.sol";
import "../SimpleStore.sol";
import "../../lib/YulpDeployer.sol";


contract SimpleStoreTest is DSTest {
    YulpDeployer yulpDeployer = new YulpDeployer();

    SimpleStore simpleStore;

    function setUp() public {
        simpleStore = SimpleStore(yulpDeployer.deployContract("SimpleStore"));
    }

    function testGet() public {
        simpleStore.get();
    }
}

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.