Skip to content

Commit 1c17499

Browse files
authored
Add foundry base (#362)
* Add foundry base * Address Reisen comments
1 parent eea4257 commit 1c17499

File tree

7 files changed

+157
-34
lines changed

7 files changed

+157
-34
lines changed

ethereum/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lib/*
2+
!lib/README.md
3+
out
4+
cache

ethereum/README.md

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,36 @@
1-
# Wormhole bridge - ETH
1+
# Pyth Ethereum Contract
22

3-
These smart contracts allow to use Ethereum as foreign chain in the Wormhole protocol.
3+
This directory contains The Pyth contract on Ethereum and utilities to deploy it in EVM chains.
44

5-
The `Wormhole` contract is the bridge contract and allows tokens to be transferred out of ETH and VAAs to be submitted
6-
to transfer tokens in or change configuration settings.
5+
## Installation
76

8-
The `WrappedAsset` is a ERC-20 token contract that holds metadata about a wormhole asset on ETH. Wormhole assets are all
9-
wrapped non-ETH assets that are currently held on ETH.
7+
Run the following command to install required dependencies for the contract:
108

11-
### Deploying
9+
```
10+
# xc-governance-sdk-js is a local dependency that should be built
11+
# it is used in deployment (truffle migrations) to generate/sanity check
12+
# the governance VAAs
13+
pushd third_party/pyth/xc-governance-sdk-js && npm ci && popd
14+
npm ci
15+
```
1216

13-
To deploy the bridge on Ethereum you first need to compile all smart contracts:
14-
`npx truffle compile`
17+
## Deployment
18+
Please refer to [Deploying.md](./Deploying.md) for more information.
1519

16-
To deploy you can either use the bytecode from the `build/contracts` folder or the oz cli `oz deploy <Contract>`
17-
([Documentation](https://docs.openzeppelin.com/learn/deploying-and-interacting)).
20+
## Foundry
1821

19-
You first need to deploy one `Wrapped Asset` and initialize it using dummy data.
22+
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:
2023

21-
Then deploy the `Wormhole` using the initial guardian key (`key_x,y_parity,0`) and the address of the previously deployed
22-
`WrappedAsset`. The wrapped asset contract will be used as proxy library to all the creation of cheap proxy wrapped
23-
assets.
24+
``` sh
25+
pyth-crosschain/ethereum $ bash ../scripts/install-foundry.sh
26+
```
2427

25-
### Testing
28+
You need to install npm dependencies as described in [Installation](#installation). Also, you need to run the following
29+
command to install forge dependencies:
2630

27-
For each test run:
31+
```
32+
npm run install-forge-deps
33+
```
2834

29-
Run `npx ganache-cli --deterministic --time "1970-01-01T00:00:00+00:00"` to start a chain.
30-
31-
Run the tests using `npm run test`
32-
33-
### User methods
34-
35-
`submitVAA(bytes vaa)` can be used to execute a VAA.
36-
37-
`lockAssets(address asset, uint256 amount, bytes32 recipient, uint8 target_chain)` can be used
38-
to transfer any ERC20 compliant asset out of ETH to any recipient on another chain that is connected to the Wormhole
39-
protocol. `asset` is the asset to be transferred, `amount` is the amount to transfer (this must be <= the allowance that
40-
you have previously given to the bridge smart contract if the token is not a wormhole token), `recipient` is the foreign
41-
chain address of the recipient, `target_chain` is the id of the chain to transfer to.
42-
43-
`lockETH(bytes32 recipient, uint8 target_chain)` is a convenience function to wrap the Ether sent with the function call
44-
and transfer it as described in `lockAssets`.
35+
After installing the dependencies. Run `forge build` to build the contracts and `forge test` to
36+
test the contracts using tests in `forge-test` directory.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: Apache 2
2+
3+
pragma solidity ^0.8.0;
4+
5+
import "../contracts/pyth/PythUpgradable.sol";
6+
import "forge-std/Test.sol";
7+
8+
contract TestPythUpgradable is Test {
9+
PythUpgradable public pyth;
10+
11+
function setUp() public {
12+
pyth = new PythUpgradable();
13+
// The values below are just dummy values and this test does nothing.
14+
pyth.initialize(
15+
address(0x0000000000000000000000000000000000000000000000000000000000000000),
16+
0,
17+
0x0000000000000000000000000000000000000000000000000000000000000000
18+
);
19+
}
20+
}

ethereum/foundry.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[profile.default]
2+
solc_version = "0.8.4"
3+
optimizer = true
4+
optimizer_runs = 200
5+
src="contracts"
6+
# We put the tests into the forge-test directory (instead of test) so that
7+
# truffle doesn't try to build them
8+
test="forge-test"
9+
10+
libs = [
11+
'lib',
12+
'node_modules',
13+
]

ethereum/lib/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Forge installs the dependencies in this folder. They are .gitignored

ethereum/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"test": "truffle test",
2323
"migrate": "mkdir -p build/contracts && cp node_modules/@openzeppelin/contracts/build/contracts/* build/contracts/ && truffle migrate",
2424
"receiver-submit-guardian-sets": "truffle exec scripts/receiverSubmitGuardianSetUpgrades.js",
25-
"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"
25+
"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",
26+
"install-forge-deps": "forge install foundry-rs/forge-std@2c7cbfc6fbede6d7c9e6b17afe997e3fdfe22fef --no-git --no-commit"
2627
},
2728
"author": "",
2829
"license": "ISC",

scripts/install-foundry.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
3+
# This script install foundry and the solidity compiler required to build the
4+
# ethereum contracts. Foundry itself provides a mechanism to install solc, but
5+
# it doesn't work with certain firewall configurations.
6+
7+
set -euo pipefail
8+
9+
# check if foundry.toml exists
10+
if [ ! -f foundry.toml ]; then
11+
echo "foundry.toml not found. Please call from the ethereum directory." >& 2
12+
exit 1
13+
fi
14+
15+
# Read compiler version from foundry.toml
16+
SOLC_VERSION=$(grep solc_version foundry.toml | cut -d'=' -f2 | tr -d '" ') || true
17+
18+
if [ -z "$SOLC_VERSION" ]; then
19+
echo "solc_version not found in foundry.toml." >& 2
20+
exit 1
21+
fi
22+
23+
main() {
24+
OS=$(uname -s)
25+
case "$OS" in
26+
Darwin)
27+
install_mac
28+
;;
29+
Linux)
30+
install_linux
31+
;;
32+
*)
33+
echo "Unsupported OS: $OS"
34+
exit 1
35+
;;
36+
esac
37+
}
38+
39+
function install_mac() {
40+
if ! command -v brew > /dev/null; then
41+
echo "brew is unavailable. Please install: https://brew.sh"
42+
fi
43+
44+
if ! brew list libusb > /dev/null 2>&1; then
45+
echo "Installing libusb"
46+
brew install libusb
47+
fi
48+
49+
if ! command -v foundryup > /dev/null; then
50+
curl -L https://foundry.paradigm.xyz --silent | bash
51+
"$HOME/.foundry/bin/foundryup"
52+
fi
53+
54+
INSTALL_DIR="$HOME/.svm/$SOLC_VERSION"
55+
56+
mkdir -p "$INSTALL_DIR"
57+
58+
SOLC_PATH="$INSTALL_DIR/solc-$SOLC_VERSION"
59+
60+
if [ ! -f "$SOLC_PATH" ]; then
61+
echo "Installing solc-$SOLC_VERSION"
62+
curl -L --silent "https://github.com/ethereum/solidity/releases/download/v$SOLC_VERSION/solc-macos" > "$SOLC_PATH"
63+
chmod +x "$SOLC_PATH"
64+
echo "Installed $SOLC_PATH"
65+
else
66+
echo "Solidity compiler found: $SOLC_PATH"
67+
fi
68+
}
69+
70+
function install_linux() {
71+
if ! command -v foundryup > /dev/null; then
72+
curl -L https://foundry.paradigm.xyz --silent | bash
73+
"$HOME/.foundry/bin/foundryup"
74+
fi
75+
76+
INSTALL_DIR="$HOME/.svm/$SOLC_VERSION"
77+
78+
mkdir -p "$INSTALL_DIR"
79+
80+
SOLC_PATH="$INSTALL_DIR/solc-$SOLC_VERSION"
81+
82+
if [ ! -f "$SOLC_PATH" ]; then
83+
echo "Installing solc-$SOLC_VERSION"
84+
curl -L --silent "https://github.com/ethereum/solidity/releases/download/v$SOLC_VERSION/solc-static-linux" > "$SOLC_PATH"
85+
chmod +x "$SOLC_PATH"
86+
echo "Installed $SOLC_PATH"
87+
else
88+
echo "Solidity compiler found: $SOLC_PATH"
89+
fi
90+
}
91+
92+
main "$@"; exit

0 commit comments

Comments
 (0)