Coder Social home page Coder Social logo

foundry-vyper's Introduction

Foundry x Vyper

A Foundry template to compile and test Vyper contracts.


                       ,,,,,,,,,,,,,                        ,,,,,,,,,,,,
                        *********///.         ////        ,//********** 
                         ,****,*//////       //////      //////******.  
                           ****////////    /////////    ////////****    
                            *//////////// /////////// ////////////*     
                              //////////##///////////#(//////////       
                               ////////####(///////(####////////        
                                ,////(#######/////#######(////          
                                  //##########//(##########//           
                                   (###########&###########*            
                                     #########&&&#########              
                                      ######&&&&&&&#####(               
                                        ###&&&&&&&&&###                 
                                         %&&&&&&&&&&&%                  
                                          %&&&&&&&&&.                   
                                            &&&&&&&                     
                                             %&&&*                      
                                               &


Installation / Setup

To set up Foundry x Vyper, first make sure you have Vyper installed.

Then set up a new Foundry project with the following command (replacing vyper_project_name with your new project's name).

forge init --template https://github.com/0xKitsune/Foundry-Vyper vyper_project_name

Now you are all set up and ready to go! Below is a quick example of how to set up, deploy and test Vyper contracts.



Compiling/Testing Vyper Contracts

The VyperDeployer is a pre-built contract that takes a filename and deploys the corresponding Vyper contract, returning the address that the bytecode was deployed to. If you want, you can check out how the VyperDeployer works under the hood. Below is a quick example of how to setup and deploy a SimpleStore contract written in Vyper.

SimpleStore.Vyper

Here is a simple Vyper contract called SimpleStore.Vyper, which is stored within the Vyper_contracts directory. Make sure to put all of your .Vyper files in the Vyper_contracts directory so that the Vyper compiler knows where to look when compiling.

val: uint256

@external
def store(_val: uint256):
    self.val = _val

@external
def get() -> uint256:
    return self.val

SimpleStore Interface

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

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

SimpleStore Test

First, the file imports ISimpleStore.sol as well as the VyperDeployer.sol contract.

To deploy the contract, simply create a new instance of VyperDeployer and call VyperDeployer.deployContract(fileName) method, passing in the file name of the contract you want to deploy. Additionally, if the contract requires constructor arguments you can pass them in by supplying an abi encoded representation of the constructor arugments, which looks like this VyperDeployer.deployContract(fileName, abi.encode(arg0, arg1, arg2...)).

In this example, SimpleStore is passed in to deploy the SimpleStore.vy contract. The deployContract function compiles the Vyper contract and deploys the newly compiled bytecode, returning the address that the contract was deployed to. Since the SimpleStore.vy takes one constructor argument, the argument is wrapped in abi.encode() and passed to the deployContract function as a second argument.

The deployed address is then used to initialize the ISimpleStore interface. Once the interface has been initialized, your Vyper contract can be used within Foundry like any other Solidity contract.

To test any Vyper contract deployed with VyperDeployer, simply run forge test. Since ffi is set to true in the foundry.toml file, you can run forge test without needing to pass in the --ffi flag. You can also use additional flags as you would with any other Foundry project. For example: forge test -f <url> -vvvv.

import "../../lib/ds-test/test.sol";
import "../../lib/utils/VyperDeployer.sol";

import "../ISimpleStore.sol";

contract SimpleStoreTest is DSTest {
    ///@notice create a new instance of VyperDeployer
    VyperDeployer vyperDeployer = new VyperDeployer();

    ISimpleStore simpleStore;

    function setUp() public {
        ///@notice deploy a new instance of ISimplestore by passing in the address of the deployed Vyper contract
        simpleStore = ISimpleStore(
            vyperDeployer.deployContract("SimpleStore", abi.encode(1234))
        );
    }

    function testGet() public {
        uint256 val = simpleStore.get();

        require(val == 1234);
    }

    function testStore(uint256 _val) public {
        simpleStore.store(_val);
        uint256 val = simpleStore.get();

        require(_val == val);
    }
}

Other Foundry Integrations

foundry-vyper's People

Contributors

0xkitsune avatar corddry avatar

Watchers

 avatar

Forkers

foxier25

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.