Skip to content

Commit 81796d3

Browse files
Merge pull request #27 from euler-xyz/scripts
Scripts
2 parents 54c1af6 + 9660bc2 commit 81796d3

14 files changed

+298
-15
lines changed

.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Private key to interact with contracts using scripts inside /script
2+
WALLET_PRIVATE_KEY=0xa26..
3+
# RPC
4+
MAINNET_RPC_URL="https://"
5+
ARBITRUM_RPC_URL="https://"
6+
BASE_RPC_URL="https://"
7+
8+
# Explorer API Key
9+
ETHERSCAN_MAINNET_API_KEY=adada
10+
ETHERSCAN_ARBITRUM_API_KEY=adada
11+
ETHERSCAN_BASE_API_KEY=adada

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ out/
66
!/broadcast
77
/broadcast/*/31337/
88
/broadcast/**/dry-run/
9+
broadcast/
10+
script/json/out/
911

1012
# Dotenv file
1113
.env

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ forge coverage
4848
forge doc --serve --port 4000
4949
```
5050

51+
## Private Deployment
52+
53+
- EulerSwapFactory: 0x04C54FF83e4BC428FD1eDA2f41cdBd583A2e9cF8
54+
- EulerSwapPeriphery: 0x64A8410D7D2ecF3Aaf32b6C3932e4586f3C42ecE
55+
56+
------
57+
58+
- EulerSwapFactory: 0xF75548aF02f1928CbE9015985D4Fcbf96d728544
59+
- EulerSwapPeriphery: 0x813D74E832b3d9E9451d8f0E871E877edf2a5A5f
60+
- USDT-USDT pool: 0x2bFED8dBEb8e6226a15300AC77eE9130E52410fE
61+
62+
5163
## Safety
5264

5365
This software is experimental and is provided "as is" and "as available".

foundry.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ solc = "0.8.27"
66
optimizer = true
77
optimizer_runs = 10000
88
gas_reports = ["*"]
9+
fs_permissions = [{ access = "read-write", path = "./"}]
10+
11+
[rpc_endpoints]
12+
mainnet = "${MAINNET_RPC_URL}"
13+
base = "${BASE_RPC_URL}"
14+
15+
[etherscan]
16+
mainnet = { key = "${ETHERSCAN_MAINNET_API_KEY}" }
17+
base = { key = "${ETHERSCAN_BASE_API_KEY}" }
918

1019
[doc]
1120
out = "foundry-docs/"

script/DeployPool.s.sol

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
pragma solidity ^0.8.0;
3+
4+
import {ScriptUtil} from "./ScriptUtil.s.sol";
5+
import {IEulerSwapFactory, IEulerSwap, EulerSwapFactory} from "../src/EulerSwapFactory.sol";
6+
import {IEVC, IEulerSwap} from "../src/EulerSwap.sol";
7+
8+
/// @title Script to deploy new pool.
9+
contract DeployPool is ScriptUtil {
10+
function run() public {
11+
// load wallet
12+
uint256 eulerAccountKey = vm.envUint("WALLET_PRIVATE_KEY");
13+
address eulerAccount = vm.rememberKey(eulerAccountKey);
14+
15+
// load JSON file
16+
string memory inputScriptFileName = "DeployPool_input.json";
17+
string memory json = _getJsonFile(inputScriptFileName);
18+
19+
EulerSwapFactory factory = EulerSwapFactory(vm.parseJsonAddress(json, ".factory"));
20+
IEulerSwap.Params memory poolParams = IEulerSwap.Params({
21+
vault0: vm.parseJsonAddress(json, ".vault0"),
22+
vault1: vm.parseJsonAddress(json, ".vault1"),
23+
eulerAccount: eulerAccount,
24+
equilibriumReserve0: uint112(vm.parseJsonUint(json, ".equilibriumReserve0")),
25+
equilibriumReserve1: uint112(vm.parseJsonUint(json, ".equilibriumReserve1")),
26+
currReserve0: uint112(vm.parseJsonUint(json, ".currReserve0")),
27+
currReserve1: uint112(vm.parseJsonUint(json, ".currReserve1")),
28+
fee: vm.parseJsonUint(json, ".fee")
29+
});
30+
IEulerSwap.CurveParams memory curveParams = IEulerSwap.CurveParams({
31+
priceX: vm.parseJsonUint(json, ".priceX"),
32+
priceY: vm.parseJsonUint(json, ".priceY"),
33+
concentrationX: vm.parseJsonUint(json, ".concentrationX"),
34+
concentrationY: vm.parseJsonUint(json, ".concentrationY")
35+
});
36+
bytes32 salt = bytes32(uint256(vm.parseJsonUint(json, ".salt")));
37+
38+
IEVC evc = IEVC(factory.EVC());
39+
address predictedPoolAddress = factory.computePoolAddress(poolParams, curveParams, salt);
40+
41+
IEVC.BatchItem[] memory items = new IEVC.BatchItem[](2);
42+
43+
items[0] = IEVC.BatchItem({
44+
onBehalfOfAccount: address(0),
45+
targetContract: address(evc),
46+
value: 0,
47+
data: abi.encodeCall(evc.setAccountOperator, (eulerAccount, predictedPoolAddress, true))
48+
});
49+
items[1] = IEVC.BatchItem({
50+
onBehalfOfAccount: eulerAccount,
51+
targetContract: address(factory),
52+
value: 0,
53+
data: abi.encodeCall(EulerSwapFactory.deployPool, (poolParams, curveParams, salt))
54+
});
55+
56+
vm.startBroadcast(eulerAccount);
57+
evc.batch(items);
58+
vm.stopBroadcast();
59+
60+
address pool = factory.eulerAccountToPool(eulerAccount);
61+
62+
string memory outputScriptFileName = "DeployPool_output.json";
63+
64+
string memory object;
65+
object = vm.serializeAddress("factory", "deployedPool", pool);
66+
67+
vm.writeJson(object, string.concat(vm.projectRoot(), "/script/json/out/", outputScriptFileName));
68+
}
69+
}

script/DeployProtocol.s.sol

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
pragma solidity ^0.8.0;
3+
4+
import {ScriptUtil} from "./ScriptUtil.s.sol";
5+
import {EulerSwapFactory} from "../src/EulerSwapFactory.sol";
6+
import {EulerSwapPeriphery} from "../src/EulerSwapPeriphery.sol";
7+
8+
/// @title Script to deploy EulerSwapFactory & EulerSwapPeriphery.
9+
contract DeployProtocol is ScriptUtil {
10+
function run() public {
11+
// load wallet
12+
uint256 deployerKey = vm.envUint("WALLET_PRIVATE_KEY");
13+
address deployerAddress = vm.rememberKey(deployerKey);
14+
15+
// load JSON file
16+
string memory inputScriptFileName = "DeployProtocol_input.json";
17+
string memory json = _getJsonFile(inputScriptFileName);
18+
19+
address evc = vm.parseJsonAddress(json, ".evc");
20+
21+
vm.startBroadcast(deployerAddress);
22+
23+
new EulerSwapFactory(evc);
24+
new EulerSwapPeriphery();
25+
26+
vm.stopBroadcast();
27+
}
28+
}

script/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Forge scripts
2+
3+
Every script takes inputs via a `ScriptName_input.json` file inside the json directory.
4+
5+
Before running the scripts, please make sure to fill the `.env` file following the `.env.example`. The main env variables for the script to succefully run, are `WALLET_PRIVATE_KEY` and the `NETWORK_RPC_URL`.
6+
7+
After filling the `.env` file, make sure to run: `source .env` in your terminal.
8+
9+
## Deploy protocol
10+
11+
- Fill the `DeployProtocol_input.json` file with the needed inputs.
12+
- Run `forge script ./script/DeployProtocol.s.sol --rpc-url network_name --broadcast --slow`
13+
14+
## Deploy new pool
15+
16+
- Fill the `DeployPool_input.json` file with the needed inputs.
17+
- In pool deployment, the `eulerAccount` address is the deployer address, so we derive the address from the attached private key in the `.env` file.
18+
- Run `forge script ./script/DeployPool.s.sol --rpc-url network_name --broadcast --slow`
19+
20+
## Exact in swap
21+
22+
- Fill the `SwapExactIn_input.json` file with the needed inputs.
23+
- Run `forge script ./script/SwapExactIn.s.sol --rpc-url network_name --broadcast --slow`
24+

script/ScriptUtil.s.sol

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
pragma solidity ^0.8.0;
3+
4+
import "forge-std/Script.sol";
5+
6+
contract ScriptUtil is Script {
7+
function _getJsonFile(string memory _jsonFile) internal view returns (string memory) {
8+
return vm.readFile(_getJsonFilePath(_jsonFile));
9+
}
10+
11+
function _getJsonFilePath(string memory _jsonFile) private view returns (string memory) {
12+
string memory root = vm.projectRoot();
13+
return string.concat(root, "/script/json/", _jsonFile);
14+
}
15+
}

script/SwapExactIn.s.sol

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
pragma solidity ^0.8.0;
3+
4+
import {ScriptUtil} from "./ScriptUtil.s.sol";
5+
import {IERC20, SafeERC20, EulerSwap} from "../src/EulerSwap.sol";
6+
import {EulerSwapPeriphery} from "../src/EulerSwapPeriphery.sol";
7+
8+
contract SwapExactIn is ScriptUtil {
9+
using SafeERC20 for IERC20;
10+
11+
function run() public {
12+
// load wallet
13+
uint256 swapperKey = vm.envUint("WALLET_PRIVATE_KEY");
14+
address swapperAddress = vm.rememberKey(swapperKey);
15+
16+
// load JSON file
17+
string memory inputScriptFileName = "SwapExactIn_input.json";
18+
string memory json = _getJsonFile(inputScriptFileName);
19+
20+
EulerSwapPeriphery periphery = EulerSwapPeriphery(vm.parseJsonAddress(json, ".periphery"));
21+
EulerSwap pool = EulerSwap(vm.parseJsonAddress(json, ".pool"));
22+
address tokenIn = vm.parseJsonAddress(json, ".tokenIn");
23+
address tokenOut = vm.parseJsonAddress(json, ".tokenOut");
24+
uint256 amountIn = vm.parseJsonUint(json, ".amountIn");
25+
uint256 amountOutMin = vm.parseJsonUint(json, ".amountOutMin");
26+
27+
vm.startBroadcast(swapperAddress);
28+
29+
IERC20(tokenIn).forceApprove(address(periphery), amountIn);
30+
31+
periphery.swapExactIn(address(pool), tokenIn, tokenOut, amountIn, amountOutMin);
32+
33+
vm.stopBroadcast();
34+
}
35+
}

script/json/DeployPool_input.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"factory": "0xF75548aF02f1928CbE9015985D4Fcbf96d728544",
3+
"salt": "1",
4+
"vault0": "0xa66957e58b60d6b92b850c8773a9ff9b0ba96a65",
5+
"vault1": "0x4212e01c7c8e1c21dea6030c74ae2084f5337bd1",
6+
"equilibriumReserve0": 2000e6,
7+
"equilibriumReserve1": 2000e6,
8+
"currReserve0": 2000e6,
9+
"currReserve1": 2000e6,
10+
"fee": 0,
11+
"priceX": 1e18,
12+
"priceY": 1e18,
13+
"concentrationX": 0.97e18,
14+
"concentrationY": 0.97e18
15+
}

0 commit comments

Comments
 (0)