Skip to content

Commit

Permalink
Made eth price usd configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Mar 25, 2024
1 parent 299b209 commit 3dcef00
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 85 deletions.
54 changes: 14 additions & 40 deletions config/optimism.json
@@ -1,43 +1,17 @@
{
"simulation": {
"num_users": "2",
"time_step": "300",
"tvl_usd": "1000000",
"time_step": "1800",
"tvl_usd": "5000000",
"verbosity": "1",
"duration_draws": "6",

"eth_price_usd_per_draw" : [
"30"
],

"duration_draws": "30",
"apr_for_each_draw" : [
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"50000000000000000",
"200000000000000000",
"100000000000000000",
"10000000000000000"
"30000000000000000"
]
},

Expand All @@ -64,14 +38,14 @@
},

"claimer": {
"claimer_min_fee": "100000000000",
"claimer_max_fee": "1000000000000000000"
"claimer_min_fee": "100",
"claimer_max_fee": "10000000"
},

"gas": {
"start_draw_cost_in_eth": "3500000000000",
"award_draw_cost_in_eth": "3500000000000",
"claim_cost_in_eth": "1000000000000",
"liquidation_cost_in_eth": "3500000000000"
"start_draw_cost_in_usd": "10",
"award_draw_cost_in_usd": "10",
"claim_cost_in_usd": "10",
"liquidation_cost_in_usd": "10"
}
}
4 changes: 2 additions & 2 deletions src/agent/ClaimerAgent.sol
Expand Up @@ -92,7 +92,7 @@ contract ClaimerAgent is Utils {
// see if any are worth claiming
{
uint claimFees = claimer.computeTotalFees(tier, tierPrizes);
uint cost = tierPrizes * env.config().gas().claimCostInEth;
uint cost = env.config().usdToWeth(tierPrizes * env.config().gas().claimCostInUsd);
if (isLogging(3)) {
console2.log(
"\tclaimFees for drawId %s tier %s with prize size %e:",
Expand Down Expand Up @@ -312,7 +312,7 @@ contract ClaimerAgent is Utils {
if (isLogging(2)) {
console2.log(
"+++++++++++++++++++++ Prize Claim Cost:",
env.config().gas().claimCostInEth
env.config().gas().claimCostInUsd
);
console2.log(
"+++++++++++++++++++++ Draw",
Expand Down
6 changes: 2 additions & 4 deletions src/agent/DrawAgent.sol
Expand Up @@ -44,7 +44,7 @@ contract DrawAgent is StdCheats, Utils {
}

function willFinishDraw() public view returns (bool) {
uint256 finishDrawCost = env.config().gas().finishDrawCostInEth;
uint256 finishDrawCost = env.config().usdToWeth(env.config().gas().finishDrawCostInUsd);
uint256 minimumFinishDrawProfit = getMinimumProfit(finishDrawCost);
uint256 finishDrawProfit;

Expand All @@ -69,9 +69,7 @@ contract DrawAgent is StdCheats, Utils {

if (drawManager.canStartDraw()) {
uint fee = drawManager.startDrawReward();
// console2.log("fee %e", fee);
uint256 startDrawCost = env.config().gas().startDrawCostInEth;
// console2.log("cost %e", startDrawCost);
uint256 startDrawCost = env.config().usdToWeth(env.config().gas().startDrawCostInUsd);
uint256 startDrawProfit = fee < startDrawCost ? 0 : fee - startDrawCost;
if (startDrawProfit >= getMinimumProfit(startDrawCost)) {
// console2.log("started draw", drawId);
Expand Down
3 changes: 2 additions & 1 deletion src/agent/LiquidatorAgent.sol
Expand Up @@ -154,7 +154,8 @@ contract LiquidatorAgent is Utils {
if (amountIn > 57896044618658097711785492504343953926634992332820282019728) {
return wrap(0);
}
SD59x18 cost = tokenInValueUsd.mul(convert(int256(amountIn))).add(computeGasCostInUsd(ethValueUsd, env.config().gas().liquidationCostInEth));
SD59x18 capitalCost = tokenInValueUsd.mul(convert(int256(amountIn)));//.add(convert(int256(env.config().gas().liquidationCostInUsd)));
SD59x18 cost = capitalCost.add(convert(int256(env.config().gas().liquidationCostInUsd)));
return cost.lt(amountOutInUsd) ? amountOutInUsd.sub(cost) : wrap(0);
}

Expand Down
3 changes: 2 additions & 1 deletion src/environment/SingleChainEnvironment.sol
Expand Up @@ -137,6 +137,7 @@ contract SingleChainEnvironment is Utils, StdCheats {
claimerConfig.maxFeePortionOfPrize
);

console2.log("Claimer DecayConstant ", claimer.decayConstant().unwrap());
// console2.log("SingleChainEnvironment constructor 6");

underlyingToken.mint(address(this), vaultFactory.YIELD_BUFFER());
Expand Down Expand Up @@ -190,7 +191,7 @@ contract SingleChainEnvironment is Utils, StdCheats {
address(prizeToken),
address(vault),
config.prizePool().drawPeriodSeconds / 3,
0.001e18,
0.00001e18,
0 // no smoothing
)
)
Expand Down
44 changes: 29 additions & 15 deletions src/utils/Config.sol
Expand Up @@ -53,12 +53,14 @@ struct DrawManagerConfig {

// Gas configs
struct GasConfig {
uint256 startDrawCostInEth;
uint256 finishDrawCostInEth;
uint256 claimCostInEth;
uint256 liquidationCostInEth;
uint256 startDrawCostInUsd;
uint256 finishDrawCostInUsd;
uint256 claimCostInUsd;
uint256 liquidationCostInUsd;
}

uint constant USD_DECIMALS = 3;

contract Config is CommonBase {
using SafeCast for uint256;

Expand All @@ -85,17 +87,12 @@ contract Config is CommonBase {
function load(string memory filepath) public {
string memory config = vm.readFile(filepath);

uint usdDecimals = 3;

wethUsdValueOverTime = new SD59x18OverTime();
wethUsdValueOverTime.add(block.timestamp, convert(int(3000 * 10**usdDecimals)).div(convert(1e18))); // USD / WETH

poolUsdValueOverTime = new SD59x18OverTime();
poolUsdValueOverTime.add(block.timestamp, convert(int(10**usdDecimals)).div(convert(1e18))); // USD / POOL
poolUsdValueOverTime.add(block.timestamp, convert(int(10**USD_DECIMALS)).div(convert(1e18))); // USD / POOL

_simulation.numUsers = vm.parseJsonUint(config, "$.simulation.num_users");
_simulation.timeStep = vm.parseJsonUint(config, "$.simulation.time_step");
_simulation.totalValueLocked = uint(convert(convert(int(vm.parseJsonUint(config, "$.simulation.tvl_usd"))).mul(convert(int(10**usdDecimals))).div(poolUsdValueOverTime.get(block.timestamp))));
_simulation.totalValueLocked = uint(convert(convert(int(vm.parseJsonUint(config, "$.simulation.tvl_usd"))).mul(convert(int(10**USD_DECIMALS))).div(poolUsdValueOverTime.get(block.timestamp))));
_simulation.verbosity = vm.parseJsonUint(config, "$.simulation.verbosity");
_simulation.durationDraws = vm.parseJsonUint(config, "$.simulation.duration_draws");

Expand All @@ -121,10 +118,21 @@ contract Config is CommonBase {
_claimer.timeToReachMaxFee = getClaimerTimeToReachMaxFee();
_claimer.maxFeePortionOfPrize = getClaimerMaxFeePortionOfPrize();

_gas.startDrawCostInEth = vm.parseJsonUint(config, "$.gas.start_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");
_gas.startDrawCostInUsd = vm.parseJsonUint(config, "$.gas.start_draw_cost_in_usd");
_gas.finishDrawCostInUsd = vm.parseJsonUint(config, "$.gas.award_draw_cost_in_usd");
_gas.claimCostInUsd = vm.parseJsonUint(config, "$.gas.claim_cost_in_usd");
_gas.liquidationCostInUsd = vm.parseJsonUint(config, "$.gas.liquidation_cost_in_usd");

wethUsdValueOverTime = new SD59x18OverTime();
uint[] memory ethPrices = vm.parseJsonUintArray(config, "$.simulation.eth_price_usd_per_draw");
if (ethPrices.length > 0) {
for (uint i = 0; i < ethPrices.length; i++) {
wethUsdValueOverTime.add(
block.timestamp + (i * _prizePool.drawPeriodSeconds),
convert(int(ethPrices[i] * 10**USD_DECIMALS)).div(convert(1e18))
);
}
}

aprOverTime = new UintOverTime();
uint[] memory aprs = vm.parseJsonUintArray(config, "$.simulation.apr_for_each_draw");
Expand All @@ -136,6 +144,12 @@ contract Config is CommonBase {

}

/// @notice Convert USD to WETH. NOTE: USD must be fixed point USD decimals
/// @dev see USD_DECIMALS above
function usdToWeth(uint usdFixedPointDecimals) public view returns (uint256) {
return uint256(convert(convert(int(usdFixedPointDecimals)).div(wethUsdValueOverTime.get(block.timestamp))));
}

/**
* @notice Get Liquidation Pair decay constant.
* @dev This is approximately the maximum decay constant, as the CGDA formula requires computing e^(decayConstant * time).
Expand Down
4 changes: 0 additions & 4 deletions src/utils/Utils.sol
Expand Up @@ -52,10 +52,6 @@ contract Utils is CommonBase {
// }
// }

function computeGasCostInUsd(SD59x18 ethValueUsd, uint256 gasCostInEth) public view returns (SD59x18) {
return ethValueUsd.mul(convert(int(gasCostInEth)));
}

// Logging
// Clears and logs the CSV headers to the file
function initOutputFileCsv(string memory csvFile, string memory csvColumns) public {
Expand Down
8 changes: 4 additions & 4 deletions test/agent/ClaimerAgent.t.sol
Expand Up @@ -30,10 +30,10 @@ contract ClaimerAgentTest is Test {
vm.etch(address(claimer), "claimer");

GasConfig memory gasConfig = GasConfig({
startDrawCostInEth: 0.3 gwei * 152_473,
finishDrawCostInEth: 0.3 gwei * 405_000,
claimCostInEth: 0.3 gwei * 150_000,
liquidationCostInEth: 0.3 gwei * 500_000
startDrawCostInUsd: 10,
finishDrawCostInUsd: 10,
claimCostInUsd: 10,
liquidationCostInUsd: 10
});

vm.mockCall(
Expand Down
27 changes: 13 additions & 14 deletions test/environment/SingleChain.t.sol
Expand Up @@ -21,7 +21,7 @@ import { StdCheats } from "forge-std/StdCheats.sol";
import { Test } from "forge-std/Test.sol";
import { Vm } from "forge-std/Vm.sol";

import { Config } from "../../src/utils/Config.sol";
import { Config, USD_DECIMALS } from "../../src/utils/Config.sol";
import { Utils } from "../../src/utils/Utils.sol";

import { DrawLog, Logger } from "../../src/utils/Logger.sol";
Expand Down Expand Up @@ -194,10 +194,10 @@ contract SingleChainTest is CommonBase, StdCheats, Test, Utils {
console2.log("");
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().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)));
console2.log("Start draw cost (USD): ", formatUsd(config.gas().startDrawCostInUsd));
console2.log("Award draw cost (USD): ", formatUsd(config.gas().finishDrawCostInUsd));
console2.log("Claim cost (USD): \t ", formatUsd(config.gas().claimCostInUsd));
console2.log("Liq. cost (USD): \t ", formatUsd(config.gas().liquidationCostInUsd));
}

function printPrizeSummary() public view {
Expand Down Expand Up @@ -293,25 +293,24 @@ contract SingleChainTest is CommonBase, StdCheats, Test, Utils {
decimalPart = string.concat("0", decimalPart);
}

return string.concat(wholePart, ".", decimalPart, " ($", formatUsd(value, exchangeRate), " USD)");
return string.concat(wholePart, ".", decimalPart, " (", formatUsd(value, exchangeRate), ")");
}

function formatUsd(uint256 tokens, SD59x18 usdPerToken) public view returns(string memory) {
uint256 amountInUSD = uint256(convert(convert(int256(tokens)).mul(usdPerToken)));
return formatUsd(amountInUSD);
}

uint8 numberOfDecimals = 3;

uint256 usdWhole = amountInUSD / (10**numberOfDecimals);
uint256 usdCentsWhole = amountInUSD % (10**numberOfDecimals);
function formatUsd(uint256 amountInUSD) public view returns(string memory) {
uint256 usdWhole = amountInUSD / (10**USD_DECIMALS);
uint256 usdCentsWhole = amountInUSD % (10**USD_DECIMALS);
string memory usdCentsPart = vm.toString(usdCentsWhole);

while(bytes(usdCentsPart).length < numberOfDecimals) {
while(bytes(usdCentsPart).length < USD_DECIMALS) {
usdCentsPart = string.concat("0", usdCentsPart);
}

return string.concat(vm.toString(usdWhole), ".", usdCentsPart);
return string.concat("$", vm.toString(usdWhole), ".", usdCentsPart, " USD");
}



}

0 comments on commit 3dcef00

Please sign in to comment.