-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
- Loading branch information
1 parent
7a103a9
commit 7d7bd6f
Showing
5 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule create-util
added at
ed814d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |