Add foundry base (#362)

* Add foundry base

* Address Reisen comments
This commit is contained in:
Ali Behjati 2022-10-25 17:49:24 +02:00 committed by GitHub
parent eea42578b0
commit 1c17499d03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 157 additions and 34 deletions

4
ethereum/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
lib/*
!lib/README.md
out
cache

View File

@ -1,44 +1,36 @@
# Wormhole bridge - ETH # Pyth Ethereum Contract
These smart contracts allow to use Ethereum as foreign chain in the Wormhole protocol. This directory contains The Pyth contract on Ethereum and utilities to deploy it in EVM chains.
The `Wormhole` contract is the bridge contract and allows tokens to be transferred out of ETH and VAAs to be submitted ## Installation
to transfer tokens in or change configuration settings.
The `WrappedAsset` is a ERC-20 token contract that holds metadata about a wormhole asset on ETH. Wormhole assets are all Run the following command to install required dependencies for the contract:
wrapped non-ETH assets that are currently held on ETH.
### Deploying ```
# xc-governance-sdk-js is a local dependency that should be built
# it is used in deployment (truffle migrations) to generate/sanity check
# the governance VAAs
pushd third_party/pyth/xc-governance-sdk-js && npm ci && popd
npm ci
```
To deploy the bridge on Ethereum you first need to compile all smart contracts: ## Deployment
`npx truffle compile` Please refer to [Deploying.md](./Deploying.md) for more information.
To deploy you can either use the bytecode from the `build/contracts` folder or the oz cli `oz deploy <Contract>` ## Foundry
([Documentation](https://docs.openzeppelin.com/learn/deploying-and-interacting)).
You first need to deploy one `Wrapped Asset` and initialize it using dummy data. Foundry can be installed by the official installer, or by running our helper script which will automatically pull the correct installation script individually for Foundry and the Solidity compiler for your current OS. This may work better if you are running into networking/firewall issues using Foundry's Solidity installer. To use helper script, run the command below from this directory:
Then deploy the `Wormhole` using the initial guardian key (`key_x,y_parity,0`) and the address of the previously deployed ``` sh
`WrappedAsset`. The wrapped asset contract will be used as proxy library to all the creation of cheap proxy wrapped pyth-crosschain/ethereum $ bash ../scripts/install-foundry.sh
assets. ```
### Testing You need to install npm dependencies as described in [Installation](#installation). Also, you need to run the following
command to install forge dependencies:
For each test run: ```
npm run install-forge-deps
```
Run `npx ganache-cli --deterministic --time "1970-01-01T00:00:00+00:00"` to start a chain. After installing the dependencies. Run `forge build` to build the contracts and `forge test` to
test the contracts using tests in `forge-test` directory.
Run the tests using `npm run test`
### User methods
`submitVAA(bytes vaa)` can be used to execute a VAA.
`lockAssets(address asset, uint256 amount, bytes32 recipient, uint8 target_chain)` can be used
to transfer any ERC20 compliant asset out of ETH to any recipient on another chain that is connected to the Wormhole
protocol. `asset` is the asset to be transferred, `amount` is the amount to transfer (this must be <= the allowance that
you have previously given to the bridge smart contract if the token is not a wormhole token), `recipient` is the foreign
chain address of the recipient, `target_chain` is the id of the chain to transfer to.
`lockETH(bytes32 recipient, uint8 target_chain)` is a convenience function to wrap the Ether sent with the function call
and transfer it as described in `lockAssets`.

View File

@ -0,0 +1,20 @@
// SPDX-License-Identifier: Apache 2
pragma solidity ^0.8.0;
import "../contracts/pyth/PythUpgradable.sol";
import "forge-std/Test.sol";
contract TestPythUpgradable is Test {
PythUpgradable public pyth;
function setUp() public {
pyth = new PythUpgradable();
// The values below are just dummy values and this test does nothing.
pyth.initialize(
address(0x0000000000000000000000000000000000000000000000000000000000000000),
0,
0x0000000000000000000000000000000000000000000000000000000000000000
);
}
}

13
ethereum/foundry.toml Normal file
View File

@ -0,0 +1,13 @@
[profile.default]
solc_version = "0.8.4"
optimizer = true
optimizer_runs = 200
src="contracts"
# We put the tests into the forge-test directory (instead of test) so that
# truffle doesn't try to build them
test="forge-test"
libs = [
'lib',
'node_modules',
]

1
ethereum/lib/README.md Normal file
View File

@ -0,0 +1 @@
Forge installs the dependencies in this folder. They are .gitignored

View File

@ -22,7 +22,8 @@
"test": "truffle test", "test": "truffle test",
"migrate": "mkdir -p build/contracts && cp node_modules/@openzeppelin/contracts/build/contracts/* build/contracts/ && truffle migrate", "migrate": "mkdir -p build/contracts && cp node_modules/@openzeppelin/contracts/build/contracts/* build/contracts/ && truffle migrate",
"receiver-submit-guardian-sets": "truffle exec scripts/receiverSubmitGuardianSetUpgrades.js", "receiver-submit-guardian-sets": "truffle exec scripts/receiverSubmitGuardianSetUpgrades.js",
"verify": "patch -u -f node_modules/truffle-plugin-verify/constants.js -i truffle-verify-constants.patch; truffle run verify $npm_config_module@$npm_config_contract_address --network $npm_config_network" "verify": "patch -u -f node_modules/truffle-plugin-verify/constants.js -i truffle-verify-constants.patch; truffle run verify $npm_config_module@$npm_config_contract_address --network $npm_config_network",
"install-forge-deps": "forge install foundry-rs/forge-std@2c7cbfc6fbede6d7c9e6b17afe997e3fdfe22fef --no-git --no-commit"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",

92
scripts/install-foundry.sh Executable file
View File

@ -0,0 +1,92 @@
#!/bin/bash
# This script install foundry and the solidity compiler required to build the
# ethereum contracts. Foundry itself provides a mechanism to install solc, but
# it doesn't work with certain firewall configurations.
set -euo pipefail
# check if foundry.toml exists
if [ ! -f foundry.toml ]; then
echo "foundry.toml not found. Please call from the ethereum directory." >& 2
exit 1
fi
# Read compiler version from foundry.toml
SOLC_VERSION=$(grep solc_version foundry.toml | cut -d'=' -f2 | tr -d '" ') || true
if [ -z "$SOLC_VERSION" ]; then
echo "solc_version not found in foundry.toml." >& 2
exit 1
fi
main() {
OS=$(uname -s)
case "$OS" in
Darwin)
install_mac
;;
Linux)
install_linux
;;
*)
echo "Unsupported OS: $OS"
exit 1
;;
esac
}
function install_mac() {
if ! command -v brew > /dev/null; then
echo "brew is unavailable. Please install: https://brew.sh"
fi
if ! brew list libusb > /dev/null 2>&1; then
echo "Installing libusb"
brew install libusb
fi
if ! command -v foundryup > /dev/null; then
curl -L https://foundry.paradigm.xyz --silent | bash
"$HOME/.foundry/bin/foundryup"
fi
INSTALL_DIR="$HOME/.svm/$SOLC_VERSION"
mkdir -p "$INSTALL_DIR"
SOLC_PATH="$INSTALL_DIR/solc-$SOLC_VERSION"
if [ ! -f "$SOLC_PATH" ]; then
echo "Installing solc-$SOLC_VERSION"
curl -L --silent "https://github.com/ethereum/solidity/releases/download/v$SOLC_VERSION/solc-macos" > "$SOLC_PATH"
chmod +x "$SOLC_PATH"
echo "Installed $SOLC_PATH"
else
echo "Solidity compiler found: $SOLC_PATH"
fi
}
function install_linux() {
if ! command -v foundryup > /dev/null; then
curl -L https://foundry.paradigm.xyz --silent | bash
"$HOME/.foundry/bin/foundryup"
fi
INSTALL_DIR="$HOME/.svm/$SOLC_VERSION"
mkdir -p "$INSTALL_DIR"
SOLC_PATH="$INSTALL_DIR/solc-$SOLC_VERSION"
if [ ! -f "$SOLC_PATH" ]; then
echo "Installing solc-$SOLC_VERSION"
curl -L --silent "https://github.com/ethereum/solidity/releases/download/v$SOLC_VERSION/solc-static-linux" > "$SOLC_PATH"
chmod +x "$SOLC_PATH"
echo "Installed $SOLC_PATH"
else
echo "Solidity compiler found: $SOLC_PATH"
fi
}
main "$@"; exit