Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev authored Nov 2, 2022
0 parents commit fd1a24c
Show file tree
Hide file tree
Showing 17 changed files with 419 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ETHERSCAN_API_KEY=ABC123ABC123ABC123ABC123ABC123ABC1
ROPSTEN_URL=https://eth-ropsten.alchemyapi.io/v2/<YOUR ALCHEMY KEY>
PRIVATE_KEY=0xabc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc1
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Any Node Modules
node_modules/

# Environment Variables
.env
.env.prod

# Cached Build
cache/
out/

# Vscode
.vscode/

# huffc artifacts
artifacts

# Ignore flattened files
flattened.txt

# Ignore Broadcasts
broadcast
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "lib/foundry-huff"]
path = lib/foundry-huff
url = https://github.com/huff-language/foundry-huff
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/foundry-dasy"]
path = lib/foundry-dasy
url = https://github.com/dasylang/foundry-dasy
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
A Foundry playground for Vyper, Dasy, and Huff contracts

## Getting Started

### Requirements

The following will need to be installed in order to use this template. Please follow the links and instructions.

- [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
- You'll know you've done it right if you can run `git --version`
- [Foundry / Foundryup](https://github.com/gakonst/foundry)
- This will install `forge`, `cast`, and `anvil`
- You can test you've installed them right by running `forge --version` and get an output like: `forge 0.2.0 (92f8951 2022-08-06T00:09:32.96582Z)`
- To get the latest of each, just run `foundryup`
- [Huff Compiler](https://docs.huff.sh/get-started/installing/)
- You'll know you've done it right if you can run `huffc --version` and get an output like: `huffc 0.3.0`
- [Dasy Compiler](https://github.com/dasylang/dasy)
- You'll know you've done it right if you can run `which dasy` and get an output

### Quickstart

1. Clone this repo

2. Install dependencies

Once you've cloned and entered into your repository, you need to install the necessary dependencies. In order to do so, simply run:

```shell
forge install
```

3. Build & Test

To build and test your contracts, you can run:

```shell
forge build
forge test
```

For more information on how to use Foundry, check out the [Foundry Github Repository](https://github.com/foundry-rs/foundry/tree/master/forge) and the [foundry-huff library repository](https://github.com/huff-language/foundry-huff).


## Blueprint

```ml
lib
├─ forge-std — https://github.com/foundry-rs/forge-std
├─ foundry-huff — https://github.com/huff-language/foundry-huff
scripts
├─ Deploy.s.sol — Deployment Script
src
├─ SimpleStore — A Simple Storage Contract in Huff/Vyper/Dasy
test
└─ SimpleStore.t — SimpleStoreTests
```


## License

[The Unlicense](https://github.com/huff-language/huff-project-template/blob/master/LICENSE)


## Acknowledgements

- [forge-template](https://github.com/foundry-rs/forge-template)
- [femplate](https://github.com/abigger87/femplate)


## Disclaimer

_These smart contracts are being provided as is. No guarantee, representation or warranty is being made, express or implied, as to the safety or correctness of the user interface or the smart contracts. They have not been audited and as such there can be no assurance they will work as intended, and users may experience delays, failures, errors, omissions, loss of transmitted information or loss of funds. The creators are not liable for any of the foregoing. Users should proceed with caution and use at their own risk._
Binary file added assets/blueprint.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

[profile.default]
solc_version = '0.8.15'
auto_detect_solc = false
libs = ['lib']
optimizer = true
optimizer_runs = 200 # Default amount
ffi = true
fuzz_runs = 1_000
remappings = [
"forge-std=lib/forge-std/src/",
"foundry-huff=lib/foundry-huff/src/",
"foundry-dasy=lib/foundry-dasy/src/",
]
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 2a2ce3
1 change: 1 addition & 0 deletions lib/foundry-dasy
Submodule foundry-dasy added at 2469c9
1 change: 1 addition & 0 deletions lib/foundry-huff
Submodule foundry-huff added at eb9ea3
80 changes: 80 additions & 0 deletions lib/utils/DasyDeployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.13;

///@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 DasyDeployer {
address constant HEVM_ADDRESS =
address(bytes20(uint160(uint256(keccak256("hevm cheat code")))));

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

///@notice Compiles a Dasy 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 Dasy 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 Dasy contracts
string[] memory cmds = new string[](2);
cmds[0] = "dasy";
cmds[1] = string.concat("dasy_contracts/", fileName, ".dasy");

///@notice compile the Dasy 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),
"DasyDeployer could not deploy contract"
);

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

///@notice Compiles a Dasy 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 Dasy 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 Dasy contracts
string[] memory cmds = new string[](2);
cmds[0] = "dasy";
cmds[1] = string.concat("dasy_contracts/", fileName, ".dasy");

///@notice compile the Dasy 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;
assembly {
deployedAddress := create(0, add(bytecode, 0x20), mload(bytecode))
}

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

///@notice return the address that the contract was deployed to
return deployedAddress;
}
}
80 changes: 80 additions & 0 deletions lib/utils/VyperDeployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.13;

///@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 {
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;
assembly {
deployedAddress := create(0, add(bytecode, 0x20), mload(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;
assembly {
deployedAddress := create(0, add(bytecode, 0x20), mload(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;
}
}
16 changes: 16 additions & 0 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.15;

import "foundry-huff/HuffDeployer.sol";
import "forge-std/Script.sol";

interface SimpleStore {
function setValue(uint256) external;
function getValue() external returns (uint256);
}

contract Deploy is Script {
function run() public returns (SimpleStore simpleStore) {
simpleStore = SimpleStore(HuffDeployer.deploy("SimpleStore"));
}
}
7 changes: 7 additions & 0 deletions src/SimpleStore.dasy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(defvar val :uint256)

(defn setValue [:uint256 val] :external
(set-self val))

(defn getValue [] :uint256 :external
(return self/val))
40 changes: 40 additions & 0 deletions src/SimpleStore.huff
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Interface */
#define function setValue(uint256) nonpayable returns ()
#define function getValue() view returns (uint256)

/* Storage Slots */
#define constant VALUE_LOCATION = FREE_STORAGE_POINTER()

/* Methods */
#define macro SET_VALUE() = takes (0) returns (0) {
0x04 calldataload // [value]
[VALUE_LOCATION] // [ptr, value]
sstore // []
}

#define macro GET_VALUE() = takes (0) returns (0) {
// Load value from storage.
[VALUE_LOCATION] // [ptr]
sload // [value]

// Store value in memory.
0x00 mstore

// Return value
0x20 0x00 return
}

#define macro MAIN() = takes (0) returns (0) {
// Identify which function is being called.
0x00 calldataload 0xE0 shr
dup1 __FUNC_SIG(setValue) eq set jumpi
dup1 __FUNC_SIG(getValue) eq get jumpi

0x00 0x00 revert

set:
SET_VALUE()
get:
GET_VALUE()

}
Loading

0 comments on commit fd1a24c

Please sign in to comment.