Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard v0 contract for generic adapter #184

Merged
merged 42 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
0e30a88
Started to work on Terms of Service acceptange management
miohtama Dec 20, 2023
7032c59
Working on Guard smart contract
miohtama Jan 11, 2024
5fef3d7
Allow Enzyme submodule to be dirty
miohtama Jan 11, 2024
888cd26
chore: forge init
miohtama Jan 11, 2024
a119973
forge install: forge-std
miohtama Jan 11, 2024
b418636
Cleaning up guard contract
miohtama Jan 11, 2024
7211bc9
forge install: openzeppelin-contracts
miohtama Jan 11, 2024
f437fac
Added OpenZeppelin dependency
miohtama Jan 11, 2024
a3bf92e
It compiles
miohtama Jan 11, 2024
2e58f24
More guards
miohtama Jan 11, 2024
329d903
Updating README
miohtama Jan 11, 2024
31af8ce
Update README
miohtama Jan 11, 2024
1d63173
Add ABI file copy for guard
miohtama Jan 11, 2024
4341e54
Adding guard tests
miohtama Jan 12, 2024
0332dcf
Adding guard tests
miohtama Jan 12, 2024
f257c4b
Working on Guard init
miohtama Jan 13, 2024
70c4c18
Call site checks work
miohtama Jan 13, 2024
6492780
Swap works
miohtama Jan 13, 2024
1507be0
Check for TRANSFER_FROM_FAILED
miohtama Jan 13, 2024
9b86e6d
Add exit scam test
miohtama Jan 13, 2024
7fe8032
Add exit scam test
miohtama Jan 13, 2024
d901262
Adding test coverage
miohtama Jan 13, 2024
2620102
And more tests
miohtama Jan 13, 2024
5d34cb1
Adding guard and terms of service integrations
miohtama Jan 14, 2024
59202b2
Finally guard integration compiles
miohtama Jan 14, 2024
2fbdb1d
Integrate terms of service
miohtama Jan 15, 2024
d64976a
Terms of service integration to deposit tested
miohtama Jan 15, 2024
2281b11
Guard deployes
miohtama Jan 15, 2024
3bb592d
USDC-WETH pool properly deployed for Guarded enzyme
miohtama Jan 15, 2024
4ae3eea
Fixing tests
miohtama Jan 15, 2024
77210d2
Fixing tests
miohtama Jan 15, 2024
d1138ac
Fixing tests
miohtama Jan 15, 2024
adb208f
Guarded swap works
miohtama Jan 15, 2024
81df0db
Check for unauthorised token with GuardV0 + Enzyme
miohtama Jan 15, 2024
a13ed4c
Add check for an unauthorised approve
miohtama Jan 15, 2024
c68e54a
Fix tests
miohtama Jan 15, 2024
692eca6
All tests green
miohtama Jan 15, 2024
43ab92a
Updating CHANGELOG
miohtama Jan 15, 2024
ae15ca7
Bump 1delta
miohtama Jan 15, 2024
cf60d8e
Fix case sensitive with guard/GuardV0 path
miohtama Jan 15, 2024
024e2b5
Fix log message in the fallback provider that if we have only a sing…
miohtama Jan 15, 2024
c3af891
Switch Actions runner
miohtama Jan 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ jobs:

test-python:

runs-on: ubuntu-latest
# Reserved multicore instance for running tests
runs-on:
group: Beefy runners

# Only run the action for the latest push
# See https://docs.github.com/en/actions/using-jobs/using-concurrency#example-only-cancel-in-progress-jobs-or-runs-for-the-current-workflow
Expand Down Expand Up @@ -56,10 +58,9 @@ jobs:
poetry run install-aave-for-testing
# Run tests parallel.
# By default Github gives us only 2 CPUs, but we want to parallerise a bit more.
- name: Run test scripts
run: |
poetry run pytest --tb=native -n 6 --dist loadscope
poetry run pytest --tb=native -n auto --dist loadscope
env:
BNB_CHAIN_JSON_RPC: ${{ secrets.BNB_CHAIN_JSON_RPC }}
JSON_RPC_POLYGON_ARCHIVE: ${{ secrets.JSON_RPC_POLYGON_ARCHIVE }}
Expand Down
10 changes: 10 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[submodule "contracts/enzyme"]
path = contracts/enzyme
url = https://github.com/enzymefinance/protocol.git
ignore = dirty
[submodule "contracts/dhedge"]
path = contracts/dhedge
url = https://github.com/dhedge/V2-Public.git
Expand All @@ -25,3 +26,12 @@
[submodule "contracts/1delta"]
path = contracts/1delta
url = https://github.com/1delta-DAO/contracts-delegation.git
[submodule "contracts/guard/lib/forge-std"]
path = contracts/guard/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "contracts/guard/lib/openzeppelin-contracts"]
path = contracts/guard/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "contracts/terms-of-service"]
path = contracts/terms-of-service
url = https://github.com/tradingstrategy-ai/terms-of-service.git
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Current

- Bump web3.py to 6.12.x
- Add [Terms of Service acceptance manager integration](https://github.com/tradingstrategy-ai/terms-of-service)
- Add GuardV0 and SimpleVaultV0 implementations for creating safe automated asset managers
- Added GuardV0 support for Enzyme vaults and generic adapters
- Improve logging in `wait_and_broadcast_multiple_nodes` for post-mortem analysis
- `hash(SignedTransactionWithNonce)` now is `SignedTransactionWithNonce.hash`, Ethereum transaction hash
- `hash(SignedTransactionWithNonce)` now is `SignedTransactionWithNonce.hash`, Ethereum transaction hash
- Add 1delta price estimation helper `OneDeltaPriceHelper`
- Improve various utility functions
- Fix issues cleaning AST information from Enzyme contracts on certain UNIX shells
- Fix log message in the fallback provider that if we have only a single
provider don't call error handling "switching"

# 0.24.6

Expand Down
29 changes: 27 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,33 @@ in-house: enzyme
-o -name "VaultSpecificGenericAdapter.json" \
-o -name "MockEIP3009Receiver.json" \
-o -name "VaultUSDCPaymentForwarder.json" \
-o -name "TermedVaultUSDCPaymentForwarder.json" \
-o -name "GuardedGenericAdapter.json" \
\) \
-exec cp {} eth_defi/abi \;

# Guard and simple vault contracts
guard:
@mkdir -p eth_defi/abi/guard
@(cd contracts/guard && forge build)
@find contracts/guard/out \
\( \
-name "GuardV0.json" \
-o \
-name "SimpleVaultV0.json" \
\) \
-exec cp {} eth_defi/abi/guard \;

# Terms of service acceptance manager contract
terms-of-service:
@mkdir -p eth_defi/abi/terms-of-service
@(cd contracts/terms-of-service && forge build)
@find contracts/terms-of-service/out \
\( \
-name "TermsOfService.json" \
\) \
-exec cp {} eth_defi/abi/terms-of-service \;

# Compile v3 core and periphery
uniswapv3:
@(cd contracts/uniswap-v3-core && yarn install && yarn compile) > /dev/null
Expand Down Expand Up @@ -66,7 +90,8 @@ enzyme:
@(cd contracts/enzyme && forge build)
@mkdir -p eth_defi/abi/enzyme
@find contracts/enzyme/artifacts -iname "*.json" -exec cp {} eth_defi/abi/enzyme \;
@for abi_file in eth_defi/abi/enzyme/*.json ; do cat <<< $(jq 'del(.ast)' $abi_file) > $abi_file ; done
@scripts/clean-enzyme-abi.sh


# Compile and copy dHEDGE
# npm install also compiles the contracts here
Expand Down Expand Up @@ -101,7 +126,7 @@ clean-abi:
# Compile all contracts we are using
#
# Move ABI files to within a Python package for PyPi distribution
compile-projects-and-prepare-abi: clean-abi sushi in-house copy-uniswapv3-abi aavev3 enzyme dhedge centre 1delta
compile-projects-and-prepare-abi: clean-abi sushi in-house guard copy-uniswapv3-abi aavev3 enzyme dhedge centre 1delta

all: clean-docs compile-projects-and-prepare-abi build-docs

Expand Down
2 changes: 1 addition & 1 deletion contracts/1delta
Submodule 1delta updated 308 files
34 changes: 34 additions & 0 deletions contracts/guard/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: test

on: workflow_dispatch

env:
FOUNDRY_PROFILE: ci

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Run Forge build
run: |
forge --version
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
14 changes: 14 additions & 0 deletions contracts/guard/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
48 changes: 48 additions & 0 deletions contracts/guard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Guard and vault (prototype)

This is a simple implementation of a guard smart contract and a vault smart contract

- [GuardV0](./src/GuardV0.sol) can check whether an asset manager is allowed to do an action on behalf of the asset owners
- [SimpleVaultV0](./src/SimpleVaultV0.sol) is an example vault implementation with two roles
- Owner (who can withdraw assets)
- Asset manager (who can decide on trades)

This code is prototype code for Trading Strategy Protocol Minimal Viable Product version
and not indented for wider distribution.

## Guard activities

Guard will check for activities asset manager perform, all of them which need to be whitelisted by the owner:
- Any smart contract call (contract address, selector)
- Whitelisted token (asset manager cannot trade into an unsupported token)
- Withdrawal (transfer) of assets - assets can be only withdraw back to the owner
- Uniswap v2 router swaps (approval + swap path)

Guard can be used independently from the vault implementation.
It can be used with any asset management protocol e.g. Enzyme.

## Simple vault

- The vault has a guard, an asset manager and an owner
- Initially the vault is configured to allow withdrawals to the owner
- Enabling asset manager allows perform trades
- Each token needs to be separately whitelisted
- Each router needs to be separately whitelisted

Simple vault can be used as a layer of protection for cases where the hot wallet private key
of the asset manager is compromised (asset manager can only perform legit trades, not withdraw any assets).

## Supported protocols

- Uniswap v2 compatibles
- Uniswap v3 compatibles
- Aave v3 compatibles (coming)
- 1delta (coming)

## Development

Compiling

```shell
foundry build
```
6 changes: 6 additions & 0 deletions contracts/guard/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions contracts/guard/lib/forge-std
Submodule forge-std added at 36c303
1 change: 1 addition & 0 deletions contracts/guard/lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at 0d5f54
1 change: 1 addition & 0 deletions contracts/guard/remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@openzeppelin/=lib/openzeppelin-contracts/contracts
12 changes: 12 additions & 0 deletions contracts/guard/script/Counter.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Script, console2} from "forge-std/Script.sol";

contract CounterScript is Script {
function setUp() public {}

function run() public {
vm.broadcast();
}
}
Loading