Skip to content

Commit

Permalink
🐍 vyper deployer
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
  • Loading branch information
pcaversaccio committed Jul 31, 2022
1 parent 7a103a9 commit 7d7bd6f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std.git
[submodule "lib/create-util"]
path = lib/create-util
url = https://github.com/pcaversaccio/create-util.git
5 changes: 4 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ src = 'src' # the source directory
test = 'test' # the test directory
out = 'out' # the output directory (for artifacts)
libs = ['lib'] # a list of library directories
remappings = ['ds-test/=lib/ds-test/src/'] # a list of remappings
remappings = [
'forge-std/=lib/forge-std/src/',
'create-util/=lib/create-util/contracts/'
] # a list of remappings
cache = true # whether to cache builds or not
cache_path = 'cache' # where the cache is stored if enabled
force = true # whether to ignore the cache (clean build)
Expand Down
1 change: 1 addition & 0 deletions lib/create-util
Submodule create-util added at ed814d
78 changes: 78 additions & 0 deletions lib/utils/VyperDeployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.15;

import {Create} from "../create-util/contracts/Create.sol";

///@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 VyperDeployer is Create {
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 Vyper 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[](2);
cmds[0] = "vyper";
cmds[1] = string.concat("vyper_contracts/", fileName, ".vy");

///@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;
deployedAddress = deploy(0, bytecode);

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

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

///@notice Compiles a Vyper contract with constructor arguments 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, bytes calldata args)
public
returns (address)
{
///@notice create a list of strings with the commands necessary to compile Vyper contracts
string[] memory cmds = new string[](2);
cmds[0] = "vyper";
cmds[1] = string.concat("vyper_contracts/", fileName, ".vy");

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

//add args to the deployment bytecode
bytes memory bytecode = abi.encodePacked(_bytecode, args);

///@notice deploy the bytecode with the create instruction
address deployedAddress;
deployedAddress = deploy(0, bytecode);

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

///@notice return the address that the contract was deployed to
return deployedAddress;
}
}
32 changes: 32 additions & 0 deletions test/utils/ECDSA.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;

import {Test} from "../../lib/forge-std/src/test.sol";
import {VyperDeployer} from "../../lib/utils/VyperDeployer.sol";

contract SimpleStoreTest is Test {
///@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);
}
}

0 comments on commit 7d7bd6f

Please sign in to comment.