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

build: deploy scripts #5

Merged
merged 39 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
bf579b0
refactor: remove unused files
ryanycw Jun 17, 2023
0f8a0a3
chore: init environment settings
ryanycw Jun 17, 2023
4736e5b
build: create CI action
ryanycw Jun 17, 2023
7617ded
build: init contracts
ryanycw Jun 17, 2023
1503930
test: init all tests
ryanycw Jun 17, 2023
b3d7051
chore: install packages
ryanycw Jun 18, 2023
061a3dc
chore: adjust gitignore
ryanycw Jun 18, 2023
f3aba49
docs: create pr template
ryanycw Jun 18, 2023
d282a55
chore: setup commitlint
ryanycw Jun 18, 2023
84c9d1e
fix: adjust codecov project name
ryanycw Jun 18, 2023
c1e295d
Merge branch 'chore/setup-repo' into build/core-contracts
ryanycw Jun 18, 2023
63e68f6
chore: adjust router addresses
ryanycw Jun 18, 2023
ba59701
chore: format contracts
ryanycw Jun 18, 2023
bcf65e7
Merge branch 'build/core-contracts' into test/clone-all-tests
ryanycw Jun 18, 2023
2d6cfa6
chore: adjust intermediate tokens and params name
ryanycw Jun 22, 2023
a2fa3ba
chore: adjust testing parameters
ryanycw Jun 22, 2023
b9eac69
chore: adjust foundry settings
ryanycw Jun 22, 2023
e06a0c3
chore: format with forge fmt
ryanycw Jun 22, 2023
36880ec
build: add interfaces for bancor
ryanycw Jun 22, 2023
70b0154
refactor: change parameters name
ryanycw Jun 22, 2023
1e56a9e
build: create storages for bancor
ryanycw Jun 22, 2023
0a7b6a3
build: create bancor swap
ryanycw Jun 22, 2023
e4dff18
chore: adjust name and add constants for bancor
ryanycw Jun 22, 2023
77cbbfd
chore: adjust git ignore
ryanycw Jun 23, 2023
80a4b2a
docs: create documents
ryanycw Jun 23, 2023
b68ce1e
refactor: shorten the code of dex
ryanycw Jun 23, 2023
f457d76
build: setup dex in fixture
ryanycw Jun 23, 2023
face2ba
test: add testing paths in configs
ryanycw Jun 23, 2023
cea106e
test: change bancor testing path
ryanycw Jun 25, 2023
ea61e64
test: bancor functionality unit test
ryanycw Jun 25, 2023
213106d
refactor: adjust gitignore
ryanycw Jul 1, 2023
e6ed407
refactor: adjust name of bancor dex
ryanycw Jul 1, 2023
54a4f99
build: deployment scripts
ryanycw Jul 1, 2023
c0a6853
build: create deployment shell script
ryanycw Jul 1, 2023
392a25e
refactor: adjust optimizer to solve stack too deep
ryanycw Jul 1, 2023
1bd6914
docs: deployment record
ryanycw Jul 1, 2023
5a0c48a
chore: deployment test configuration
ryanycw Jul 1, 2023
de01c5c
docs: update readme
ryanycw Jul 1, 2023
394fa62
Merge branch 'main' into build/deploy-scripts
CryptJS13 Sep 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ cache/
out/

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

# Dotenv file
.env
Expand Down
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,114 @@
# Harvest Universal Liquidator Mainnet

This is the gateway for all Harvest strategies to execute swaps with multiple Dexes.

## Get Started

1. Fill in the environment variables.

```bash
cp .env.example .env
```

2. Set up dependencies.

```bash
yarn
```

## Prerequisites

- [Foundry](https://github.com/foundry-rs/foundry)

## Test locally

```bash
forge test -vvv
```

## Deploy

While deploying locally, set up the local node with `anvil`. Make sure that the address from the private key has enough gas to deploy or use one of the ten addresses with the `MNEMONIC` set up with `anvil`.

```bash
source .env
anvil -m $MNEMONIC
or
anvil -m $MNEMONIC --fork-url <RPC_URL>
```

Create a corresponding network section inside `deployed-addresses.json`.

```json
"<template-for-new-network>": {
"UniversalLiquidator": "",
"UniversalLiquidatorRegistry": "",
"UniV3Dex": "",
"BalancerDex": "",
"SushiswapDex": "",
"CurveDex": "",
"BancorV2Dex": "",
"<new-address-item-to-store>": ""
},
```

Deploy contracts with corresponding scripts.

```bash
./script/_scripts.sh script/<SCRIPTS.s.sol>
```

---

There are 5 scripts to deploy:

- _script/SystemBase.s.sol_
- _script/Dex.s.sol_
- _script/Path.s.sol_
- _script/Pool.s.sol_
- _script/Fee.s.sol_

Executing each script will require a few configurations:

Determine the Network.

```bash
Which network do you want to deploy to?
Options: local, eth-mainnet, eth-sepolia, arb-mainnet, arb-goerli, polygon-mainnet, polygon-mumbai
```

Determine whether or not to broadcast, if the network is not local.

```bash
Broadcast? [y/n]...
```

Determine whether or not to verify, if the network is not local.

```bash
Verify contract? [y/n]...
```

Determine the profile to use.

```bash
Profile? [default/optimized]...
```

(With Dex.s.sol) Determine which Dex to deploy.

```bash
Which dex do you want to deploy? (Ex: UniV3Dex, the contract name)
```

(With Dex.s.sol) Determine the name for the Dex.

```bash
Which name do you want to represent the dex? (Ex: uniV3)
```

(With Path.s.sol, Pool.s.sol, or Fee.s.sol) Determine the config parameters file for setting up. There are three example files for reference: _Paths.0000.json_, _Pools.0000.json_, and _Fees.0000.json_.

```bash
Setup with which file? (Ex: Paths.0000.json)
```
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ optimizer = false
fs_permissions = [{ access = "read-write", path = "./"}]

[profile.optimized]
optimizer = true
optimizer = false
optimizer_runs = 10000
fs_permissions = [{ access = "read-write", path = "./"}]

Expand Down
88 changes: 88 additions & 0 deletions script/Dex.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "forge-std/Script.sol";
import "forge-std/StdJson.sol";
import "openzeppelin/utils/Strings.sol";

import "../src/interfaces/IUniversalLiquidatorRegistry.sol";

import "../src/core/dexes/UniV3Dex.sol";
import "../src/core/dexes/BalancerDex.sol";
import "../src/core/dexes/SushiswapDex.sol";
import "../src/core/dexes/CurveDex.sol";
import "../src/core/dexes/BancorV2Dex.sol";

contract DexScript is Script {
using stdJson for string;
using Strings for uint256;

address _registry;
address _newDex;

function run() public {
vm.startBroadcast();
preDeploy();
deploy();
postDeploy();
vm.stopBroadcast();
}

function deploy() public {
if (keccak256(bytes(vm.envString("DEX"))) == keccak256(bytes("UniV3Dex"))) {
UniV3Dex uniV3Dex = new UniV3Dex();
console.log("UniV3Dex: ", address(uniV3Dex));
IUniversalLiquidatorRegistry(_registry).addDex(bytes32(bytes(vm.envString("DEX_NAME"))), address(uniV3Dex));
_newDex = address(uniV3Dex);
} else if (keccak256(bytes(vm.envString("DEX"))) == keccak256(bytes("BalancerDex"))) {
BalancerDex balancerDex = new BalancerDex();
console.log("BalancerDex: ", address(balancerDex));
IUniversalLiquidatorRegistry(_registry).addDex(bytes32(bytes(vm.envString("DEX_NAME"))), address(balancerDex));
_newDex = address(balancerDex);
} else if (keccak256(bytes(vm.envString("DEX"))) == keccak256(bytes("SushiswapDex"))) {
SushiswapDex sushiswapDex = new SushiswapDex();
console.log("SushiswapDex: ", address(sushiswapDex));
IUniversalLiquidatorRegistry(_registry).addDex(bytes32(bytes(vm.envString("DEX_NAME"))), address(sushiswapDex));
_newDex = address(sushiswapDex);
} else if (keccak256(bytes(vm.envString("DEX"))) == keccak256(bytes("CurveDex"))) {
CurveDex curveDex = new CurveDex();
console.log("CurveDex: ", address(curveDex));
IUniversalLiquidatorRegistry(_registry).addDex(bytes32(bytes(vm.envString("DEX_NAME"))), address(curveDex));
_newDex = address(curveDex);
} else if (keccak256(bytes(vm.envString("DEX"))) == keccak256(bytes("BancorV2Dex"))) {
BancorV2Dex bancorV2Dex = new BancorV2Dex();
console.log("BancorV2Dex: ", address(bancorV2Dex));
IUniversalLiquidatorRegistry(_registry).addDex(bytes32(bytes(vm.envString("DEX_NAME"))), address(bancorV2Dex));
_newDex = address(bancorV2Dex);
} else {
console.log("Dex not found");
}
}

/**
* @notice Check if the key exist in deployed-addresses.json file
*/
function preDeploy() public {
string memory root = vm.projectRoot();
string memory path = string.concat(root, "/script/deployed-addresses.json");
string memory json = vm.readFile(path);

_registry = json.readAddress(string.concat(string.concat(".", vm.envString("NETWORK")), ".UniversalLiquidatorRegistry"));
json.readString(string.concat(string.concat(".", vm.envString("NETWORK")), string.concat(".", vm.envString("DEX"))));
}

/**
* @notice Write the deployed addresses to the deployed-addresses.json file
*/
function postDeploy() public {
string memory root = vm.projectRoot();
string memory path = string.concat(root, "/script/deployed-addresses.json");

string memory key = string.concat(string.concat(".", vm.envString("NETWORK")), string.concat(".", vm.envString("DEX")));
string memory dexId = vm.envString("DEX_NAME");
string memory dexAddr = uint256(uint160(_newDex)).toHexString(20);
key.serialize("id", dexId);
string memory value = key.serialize("address", dexAddr);
value.write(path, key);
}
}
56 changes: 56 additions & 0 deletions script/Fee.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "forge-std/Script.sol";
import "forge-std/StdJson.sol";

struct FeePair {
address buyToken;
string description;
string dexName;
uint256 fee;
address sellToken;
}

contract FeeScript is Script {
using stdJson for string;

string _json;
string _config;

function run() public {
vm.startBroadcast();
preDeploy();
deploy();
vm.stopBroadcast();
}

function deploy() public {
FeePair[] memory _fees = abi.decode(_config.parseRaw(""), (FeePair[]));
for (uint256 i; i < _fees.length;) {
string memory dexName = _fees[i].dexName;
address _dexAddr = _json.readAddress(string.concat(".", vm.envString("NETWORK"), ".", dexName, ".address"));
if (keccak256(bytes(dexName)) == keccak256(bytes("UniV3Dex"))) {
(bool success, bytes memory data) = _dexAddr.call(
abi.encodeWithSignature("setFee(address,address,uint24)", _fees[i].sellToken, _fees[i].buyToken, _fees[i].fee)
);
if (!success) {
console2.log("curve setPool failed: ");
console2.logBytes(data);
}
}

unchecked {
++i;
}
}
}

/**
* @notice Check if the key exist in deployed-addresses.json file
*/
function preDeploy() public {
_json = vm.readFile(string.concat(vm.projectRoot(), "/script/deployed-addresses.json"));
_config = vm.readFile(string.concat(vm.projectRoot(), "/script/config/", vm.envString("SETUP_FILE")));
}
}
63 changes: 63 additions & 0 deletions script/Path.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "forge-std/Script.sol";
import "forge-std/StdJson.sol";

import "../src/interfaces/IUniversalLiquidatorRegistry.sol";

struct DexSetting {
string dexName;
address[] paths;
}

struct TokenPair {
address buyToken;
string description;
DexSetting[] dexSetup;
address sellToken;
}

contract PathScript is Script {
using stdJson for string;

address _registry;
string _json;
string _config;

function run() public {
vm.startBroadcast();
preDeploy();
deploy();
vm.stopBroadcast();
}

function deploy() public {
TokenPair[] memory _tokenPairs = abi.decode(_config.parseRaw(""), (TokenPair[]));
for (uint256 i; i < _tokenPairs.length;) {
for (uint256 j; j < _tokenPairs[i].dexSetup.length;) {
string memory _dexName =
_json.readString(string.concat(".", vm.envString("NETWORK"), ".", _tokenPairs[i].dexSetup[j].dexName, ".id"));
bytes32 dexId = bytes32(bytes(_dexName));
IUniversalLiquidatorRegistry(_registry).setPath(dexId, _tokenPairs[i].dexSetup[j].paths);
unchecked {
++j;
}
}

unchecked {
++i;
}
}
}

/**
* @notice Check if the key exist in deployed-addresses.json file
*/
function preDeploy() public {
_json = vm.readFile(string.concat(vm.projectRoot(), "/script/deployed-addresses.json"));
_config = vm.readFile(string.concat(vm.projectRoot(), "/script/config/", vm.envString("SETUP_FILE")));

_registry = _json.readAddress(string.concat(".", vm.envString("NETWORK"), ".UniversalLiquidatorRegistry"));
}
}
Loading
Loading