Skip to content

Commit

Permalink
FIX: optimize script, reset fork state at each iteration, support sha…
Browse files Browse the repository at this point in the history
…ngai for morpho tranches
  • Loading branch information
bugduino committed Oct 4, 2024
1 parent e2931b8 commit e577ac5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
4 changes: 4 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
src = "src"
out = "out"
libs = ["lib"]
evm_version = 'shanghai'

[rpc_endpoints]
mainnet = "${TENDERLY_RPC_URL}"

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
43 changes: 21 additions & 22 deletions script/SimulateAllocations.s.sol
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/console.sol";
import {Script} from "forge-std/Script.sol";
import {IIdleToken} from "../src/IIdleToken.sol";

contract SimulateAllocations is Script {
address public constant REBALANCER = 0xB3C8e5534F0063545CBbb7Ce86854Bf42dB8872B;
address public constant IDLE_USDC = 0x5274891bEC421B39D23760c04A6755eCB444797C;
address public constant IDLE_DAI = 0x3fE7940616e5Bc47b0775a0dccf6237893353bB4;
address public constant IDLE_USDT = 0xF34842d05A1c888Ca02769A633DF37177415C2f8;
// @notice run with
// forge script ./script/SimulateAllocations.s.sol:SimulateAllocations --fork-url $TENDERLY_RPC_URL_2 -v --sig "testIdleDAI(uint256[][])" "[[100000,0,0],[0,100000,0]]"
// forge script ./script/SimulateAllocations.s.sol:SimulateAllocations -v --sig "testIdleDAI(uint256,uint256[][])" "[[100000,0,0],[0,100000,0]]"
function testNextRates(
uint256 blockNumber,
address _idleToken,
uint256[][] memory allocations
) public returns (uint256[] memory aprs) {
aprs = new uint256[](allocations.length);
for (uint256 i = 0; i < allocations.length; i++) {
// logArray("allocations", allocations[i]);
) internal returns (uint256[] memory aprs) {
uint256 len = allocations.length;
aprs = new uint256[](len);
for (uint256 i = 0; i < len;) {
// we need to reset the forked state after each rebalance
vm.createSelectFork("mainnet", blockNumber);
aprs[i] = getAprForAllocations(_idleToken, allocations[i]);
// console.log("apr", aprs[i]);
// console.log('---');
unchecked {
++i;
}
}
}

function getAprForAllocations(
address _idleToken,
uint256[] memory allocations
) public returns (uint256) {
) internal returns (uint256) {
IIdleToken it = IIdleToken(_idleToken);

vm.prank(REBALANCER);
it.setAllocations(allocations);
it.rebalance();
return it.getAvgAPR();
}

function testIdleUSDC(uint256[][] memory allocations) public returns (uint256[] memory aprs) {
return testNextRates(0x5274891bEC421B39D23760c04A6755eCB444797C, allocations);
}
function testIdleDAI(uint256[][] memory allocations) public returns (uint256[] memory aprs) {
return testNextRates(0x3fE7940616e5Bc47b0775a0dccf6237893353bB4, allocations);
function testIdleUSDC(uint256 blockNumber, uint256[][] memory allocations) external returns (uint256[] memory) {
return testNextRates(blockNumber, IDLE_USDC, allocations);
}
function testIdleUSDT(uint256[][] memory allocations) public returns (uint256[] memory aprs) {
return testNextRates(0xF34842d05A1c888Ca02769A633DF37177415C2f8, allocations);
function testIdleDAI(uint256 blockNumber, uint256[][] memory allocations) external returns (uint256[] memory) {
return testNextRates(blockNumber, IDLE_DAI, allocations);
}

function logArray(string memory label, uint256[] memory arr) internal view {
for (uint256 i = 0; i < arr.length; i++) {
console.log(label, i, arr[i]);
}
function testIdleUSDT(uint256 blockNumber, uint256[][] memory allocations) external returns (uint256[] memory) {
return testNextRates(blockNumber, IDLE_USDT, allocations);
}
}

0 comments on commit e577ac5

Please sign in to comment.