Skip to content

Commit

Permalink
Add PuffDeployer for Foundry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Oct 29, 2024
1 parent fc401fe commit 5df15ea
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
4 changes: 2 additions & 2 deletions examples/add_two.huff
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "./dummy.huff"

#define macro MAIN() = takes(0) returns(0) {
0x00 calldataload // [number1] // load first 32 bytes onto the stack - number 1
0x20 calldataload // [number2] // load second 32 bytes onto the stack - number 2
0x04 calldataload // [number1] // load first 32 bytes onto the stack - number 1
0x24 calldataload // [number2] // load second 32 bytes onto the stack - number 2
add // [number1+number2] // add number 1 and 2 and put the result onto the stack

0x00 mstore // place [number1 + number2] in memory
Expand Down
89 changes: 89 additions & 0 deletions src/Deployers.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.8.7 <0.9.0;

///@notice This cheat codes interface is named _CheatCodes so you can use the CheatCodes interface in other testing files without errors
interface _CheatCodes {
function ffi(string[] calldata) external returns (bytes memory);
}

contract PuffDeployer {

address constant HEVM_ADDRESS = address(bytes20(uint160(uint256(keccak256("hevm cheat code")))));

/// @notice Initializes cheat codes in order to use ffi to compile Vyper contracts
_CheatCodes cheatCodes = _CheatCodes(HEVM_ADDRESS);

///@notice Compiles a Huff contract and returns the address that the contract was deployeod to
///@notice If deployment fails, an error will be thrown
///@param fileName - The file name of the Huff contract.
///@return deployedAddress - The address that the contract was deployed to

function deployContract(string memory fileName) public returns (address) {
///@notice create a list of strings with the commands necessary to compile Vyper contracts
string[] memory cmds = new string[](3);
cmds[0] = "puffc";
cmds[1] = "-b";
cmds[2] = fileName;

///@notice compile the Huff contract and return the bytecode
bytes memory bytecode = cheatCodes.ffi(cmds);

///@notice deploy the bytecode with the create instruction
address deployedAddress;
assembly {
deployedAddress := create(0, add(bytecode, 0x20), mload(bytecode))
}

///@notice check that the deployment was successful
require(
deployedAddress != address(0),
"PuffDeployer could not deploy contract"
);

///@notice return the address that the contract was deployed to
return deployedAddress;
}

}


contract HuffDeployer {

address constant HEVM_ADDRESS = address(bytes20(uint160(uint256(keccak256("hevm cheat code")))));

/// @notice Initializes cheat codes in order to use ffi to compile Vyper contracts
_CheatCodes cheatCodes = _CheatCodes(HEVM_ADDRESS);

///@notice Compiles a Huff contract and returns the address that the contract was deployeod to
///@notice If deployment fails, an error will be thrown
///@param fileName - The file name of the Vyper contract. For example, the file name for "SimpleStore.vy" is "SimpleStore"
///@return deployedAddress - The address that the contract was deployed to

function deployContract(string memory fileName) public returns (address) {
///@notice create a list of strings with the commands necessary to compile Vyper contracts
string[] memory cmds = new string[](3);
cmds[0] = "huffc";
cmds[1] = "-b";
cmds[2] = fileName;

///@notice compile the Vyper contract and return the bytecode
bytes memory bytecode = cheatCodes.ffi(cmds);

///@notice deploy the bytecode with the create instruction
address deployedAddress;
assembly {
deployedAddress := create(0, add(bytecode, 0x20), mload(bytecode))
}

///@notice check that the deployment was successful
require(
deployedAddress != address(0),
"PuffDeployer could not deploy contract"
);

///@notice return the address that the contract was deployed to
return deployedAddress;
}

}
11 changes: 6 additions & 5 deletions test/Counter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ pragma solidity ^0.8.13;
import {Test, console} from "forge-std/Test.sol";
import {Counter} from "../src/Counter.sol";
import {CreateX} from "../src/CreateX.sol";
import {PuffDeployer, HuffDeployer} from "../src/Deployers.sol";

interface IAddsUints {
function addUints(uint256, uint256) external returns (uint256);
}

contract CounterTest is Test {
Counter public counter;
CreateX public createx;
PuffDeployer public puffDeployer;
HuffDeployer public huffDeployer;

function setUp() public {
counter = new Counter();
counter.setNumber(0);
createx = new CreateX();
puffDeployer = new PuffDeployer();
huffDeployer = new HuffDeployer();
}

function test_addNumbers() public {
bytes memory bytecode = hex"600c8060093d393df3600435602435015952595ff3";
address newContract = createx.deployCreate(bytecode);
address newContract = puffDeployer.deployContract("examples/add_two.huff");
IAddsUints adder = IAddsUints(newContract);
uint256 sum = adder.addUints(1, 2);
assertEq(sum, 3);
Expand Down

0 comments on commit 5df15ea

Please sign in to comment.