Skip to content

Commit

Permalink
Updated simulator with latest code
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Mar 1, 2024
1 parent e685c24 commit bf34ef2
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 43 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -4,6 +4,17 @@ This project simulates PoolTogether V5 in a configurable environment.

## Usage

Run the environment scripts:

```
./scripts/optimism.sh
./scripts/ethereum.sh
```

These scripts run the SingleChain test using the `config/optimism.json` and `config/ethereum.json` config files, respectively. They output to `config/output/optimism-output.csv` and `config/output/ethereum-output.csv`.

### Configuration and Output

The simulator is configured using JSON files in the config directory. The simulation outputs results to a CSV file.

To run the simulation for a certain config and output:
Expand Down
2 changes: 1 addition & 1 deletion config/ethereum.json
Expand Up @@ -5,7 +5,7 @@
"tvl_usd": "10000000",
"verbosity": "1",

"duration_draws": "5",
"duration_draws": "3",
"apr_for_each_draw" : [
"25000000000000000",
"30000000000000000",
Expand Down
2 changes: 1 addition & 1 deletion foundry.toml
Expand Up @@ -3,7 +3,7 @@ src = 'src'
out = 'out'
test = 'test'
libs = ['lib']
solc = "0.8.19"
solc = "0.8.24"
fs_permissions = [{ access = "read", path = "./broadcast"},{ access = "read-write", path = "./data" },{ access = "read-write", path = "./config" }]
gas_reports = ["*"]
memory_limit = 1000000000 # 1 GB
2 changes: 1 addition & 1 deletion lib/pt-v5-claimer
2 changes: 1 addition & 1 deletion src/YieldVaultMintRate.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import { AccessControl } from "openzeppelin/access/AccessControl.sol";
import { ERC20, ERC4626, IERC20 } from "openzeppelin/token/ERC20/extensions/ERC4626.sol";
Expand Down
13 changes: 8 additions & 5 deletions src/agent/ClaimerAgent.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import { console2 } from "forge-std/console2.sol";
import { Vm } from "forge-std/Vm.sol";
Expand Down Expand Up @@ -82,6 +82,8 @@ contract ClaimerAgent is Utils {

while (remainingPrizes > 0) {
(uint8 tier, uint256 tierPrizes) = countContiguousTierPrizes(nextPrizeIndex, remainingPrizes);
// subtract any prizes that went unclaimed due to insufficient liquidity
tierPrizes = tierPrizes - drawTierInsufficientLiquidityPrizeCounts[drawId][tier];

uint targetClaimCount;
uint prizeSize = prizePool.getTierPrizeSize(tier);
Expand Down Expand Up @@ -121,12 +123,13 @@ contract ClaimerAgent is Utils {
uint32 maxPrizesPerLiquidity = uint32(prizePool.getTierRemainingLiquidity(tier) / prizeSize);

if (targetClaimCount > maxPrizesPerLiquidity) {
drawTierInsufficientLiquidityPrizeCounts[drawId][tier] +=
drawTierInsufficientLiquidityPrizeCounts[drawId][tier] =
targetClaimCount -
maxPrizesPerLiquidity;
console2.log("INSUFFICIENT LIQUIDITY: Draw id %s, tier %s, num missing %s ", drawId, tier, targetClaimCount - maxPrizesPerLiquidity);
console2.log("remaining liquidity", prizePool.getTierRemainingLiquidity(tier));
console2.log("prize size: ", prizeSize);
console2.log("-------- INSUFFICIENT LIQUIDITY: Draw id %s, tier %s", drawId, tier);
console2.log("-------- \tprize count %s, possible count %s, target count %s ", targetClaimCount, maxPrizesPerLiquidity, prizePool.getTierPrizeCount(tier));
console2.log("-------- \tremaining liquidity", prizePool.getTierRemainingLiquidity(tier));
console2.log("-------- \tprize size: ", prizeSize);
targetClaimCount = maxPrizesPerLiquidity;
}

Expand Down
30 changes: 15 additions & 15 deletions src/agent/DrawAgent.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import "forge-std/console2.sol";

Expand Down Expand Up @@ -43,18 +43,18 @@ contract DrawAgent is StdCheats, Utils {
return _drawDetails[drawId];
}

function willAwardDraw() public view returns (bool) {
uint256 awardDrawCost = env.config().gas().awardDrawCostInEth;
uint256 minimumAwardDrawProfit = getMinimumProfit(awardDrawCost);
uint256 awardDrawProfit;
function willFinishDraw() public view returns (bool) {
uint256 finishDrawCost = env.config().gas().finishDrawCostInEth;
uint256 minimumFinishDrawProfit = getMinimumProfit(finishDrawCost);
uint256 finishDrawProfit;

if (env.drawManager().canAwardDraw()) {
if (env.drawManager().canFinishDraw()) {
// console2.log("Draw CAN AWARD");
uint fee = env.drawManager().awardDrawFee();
// console2.log("Draw awardDraw fee %e", fee);
awardDrawProfit = fee < awardDrawCost ? 0 : fee - awardDrawCost;
// console2.log("awardDrawCost %e", awardDrawCost);
return awardDrawProfit >= minimumAwardDrawProfit;
uint fee = env.drawManager().finishDrawReward();
// console2.log("Draw finishDraw fee %e", fee);
finishDrawProfit = fee < finishDrawCost ? 0 : fee - finishDrawCost;
// console2.log("finishDrawCost %e", finishDrawCost);
return finishDrawProfit >= minimumFinishDrawProfit;
}

return false;
Expand All @@ -68,7 +68,7 @@ contract DrawAgent is StdCheats, Utils {
uint24 drawId = prizePool.getDrawIdToAward();

if (drawManager.canStartDraw()) {
uint fee = drawManager.startDrawFee();
uint fee = drawManager.startDrawReward();
// console2.log("fee %e", fee);
uint256 startDrawCost = env.config().gas().startDrawCostInEth;
// console2.log("cost %e", startDrawCost);
Expand All @@ -82,11 +82,11 @@ contract DrawAgent is StdCheats, Utils {
} else {
}

if (willAwardDraw()) {
if (willFinishDraw()) {
_drawDetails[drawId].numberOfTiers = prizePool.numberOfTiers();
_drawDetails[drawId].finishDrawReward = drawManager.awardDrawFee();
_drawDetails[drawId].finishDrawReward = drawManager.finishDrawReward();
// console2.log("Awarding draw ", drawId);
drawManager.awardDraw(address(this));
drawManager.finishDraw(address(this));
drawCount++;
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/agent/LiquidatorAgent.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import "forge-std/console2.sol";

Expand Down
6 changes: 3 additions & 3 deletions src/environment/SingleChainEnvironment.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import { StdCheats } from "forge-std/StdCheats.sol";

Expand Down Expand Up @@ -137,7 +137,8 @@ contract SingleChainEnvironment is Utils, StdCheats {

// console2.log("SingleChainEnvironment constructor 6");


underlyingToken.mint(address(this), vaultFactory.YIELD_BUFFER());
underlyingToken.approve(address(vaultFactory), vaultFactory.YIELD_BUFFER());
vault = PrizeVault(
vaultFactory.deployVault(
"PoolTogether Prize USDC",
Expand All @@ -147,7 +148,6 @@ contract SingleChainEnvironment is Utils, StdCheats {
address(claimer),
address(0), // yield fee recipient
0, // yield fee
1e5, // yield buffer
address(this)
)
);
Expand Down
8 changes: 5 additions & 3 deletions src/utils/Config.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import "forge-std/console2.sol";

Expand Down Expand Up @@ -54,7 +54,7 @@ struct DrawManagerConfig {
// Gas configs
struct GasConfig {
uint256 startDrawCostInEth;
uint256 awardDrawCostInEth;
uint256 finishDrawCostInEth;
uint256 claimCostInEth;
uint256 liquidationCostInEth;
}
Expand Down Expand Up @@ -106,6 +106,8 @@ contract Config is CommonBase {
_prizePool.tierShares = vm.parseJsonUint(config, "$.prize_pool.tier_shares").toUint8();
_prizePool.drawTimeout = vm.parseJsonUint(config, "$.prize_pool.draw_timeout").toUint24();
_prizePool.tierLiquidityUtilizationRate = vm.parseJsonUint(config, "$.prize_pool.tier_liquidity_utilization_rate");

console2.log("???? Config.load _prizePool.tierLiquidityUtilizationRate: ", _prizePool.tierLiquidityUtilizationRate);

_drawManager.auctionDuration = vm.parseJsonUint(config, "$.draw_manager.auction_duration").toUint64();
_drawManager.auctionTargetTime = vm.parseJsonUint(config, "$.draw_manager.auction_target_time").toUint64();
Expand All @@ -120,7 +122,7 @@ contract Config is CommonBase {
_claimer.maxFeePortionOfPrize = getClaimerMaxFeePortionOfPrize();

_gas.startDrawCostInEth = vm.parseJsonUint(config, "$.gas.start_draw_cost_in_eth");
_gas.awardDrawCostInEth = vm.parseJsonUint(config, "$.gas.award_draw_cost_in_eth");
_gas.finishDrawCostInEth = vm.parseJsonUint(config, "$.gas.award_draw_cost_in_eth");
_gas.claimCostInEth = vm.parseJsonUint(config, "$.gas.claim_cost_in_eth");
_gas.liquidationCostInEth = vm.parseJsonUint(config, "$.gas.liquidation_cost_in_eth");

Expand Down
2 changes: 1 addition & 1 deletion src/utils/Logger.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import "forge-std/console2.sol";

Expand Down
2 changes: 1 addition & 1 deletion src/utils/SD59x18OverTime.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import { SD59x18 } from "prb-math/SD59x18.sol";

Expand Down
2 changes: 1 addition & 1 deletion src/utils/UintOverTime.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import "forge-std/console2.sol";

Expand Down
2 changes: 1 addition & 1 deletion src/utils/Utils.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import { CommonBase } from "forge-std/Base.sol";
import { SD59x18, wrap, convert } from "prb-math/SD59x18.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/ValuesOverTime.t.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

import "forge-std/Test.sol";

Expand Down
4 changes: 2 additions & 2 deletions test/agent/ClaimerAgent.t.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

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

Expand Down Expand Up @@ -31,7 +31,7 @@ contract ClaimerAgentTest is Test {

GasConfig memory gasConfig = GasConfig({
startDrawCostInEth: 0.3 gwei * 152_473,
awardDrawCostInEth: 0.3 gwei * 405_000,
finishDrawCostInEth: 0.3 gwei * 405_000,
claimCostInEth: 0.3 gwei * 150_000,
liquidationCostInEth: 0.3 gwei * 500_000
});
Expand Down
6 changes: 3 additions & 3 deletions test/environment/SingleChain.t.sol
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
pragma solidity ^0.8.19;

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

Expand Down Expand Up @@ -78,7 +78,7 @@ console2.log("SingleChain setUp 4");
uint256 duration = (config.simulation().durationDraws+2) * config.prizePool().drawPeriodSeconds;
for (uint256 i = startTime; i <= startTime + duration; i += config.simulation().timeStep) {
vm.warp(i);
vm.roll(block.number + 1);
vm.roll(block.number + 2);

// Let agents do their thing
uint apr = env.updateApr();
Expand Down Expand Up @@ -195,7 +195,7 @@ console2.log("SingleChain setUp 4");
console2.log("Average fee per claim (WETH): ", formatTokens(averageFeePerClaim, config.wethUsdValueOverTime().get(block.timestamp)));
console2.log("");
console2.log("Start draw cost (WETH): ", formatTokens(config.gas().startDrawCostInEth, config.wethUsdValueOverTime().get(block.timestamp)));
console2.log("Award draw cost (WETH): ", formatTokens(config.gas().awardDrawCostInEth, config.wethUsdValueOverTime().get(block.timestamp)));
console2.log("Award draw cost (WETH): ", formatTokens(config.gas().finishDrawCostInEth, config.wethUsdValueOverTime().get(block.timestamp)));
console2.log("Claim cost (WETH): \t ", formatTokens(config.gas().claimCostInEth, config.wethUsdValueOverTime().get(block.timestamp)));
console2.log("Liq. cost (WETH): \t ", formatTokens(config.gas().liquidationCostInEth, config.wethUsdValueOverTime().get(block.timestamp)));
}
Expand Down

0 comments on commit bf34ef2

Please sign in to comment.