-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawAgent.sol
98 lines (77 loc) · 3.01 KB
/
DrawAgent.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.19;
import "forge-std/console2.sol";
import { StdCheats } from "forge-std/StdCheats.sol";
import {
RngBlockhash,
DrawManager,
SingleChainEnvironment,
PrizePool
} from "../environment/SingleChainEnvironment.sol";
import { Config } from "../utils/Config.sol";
import { Utils } from "../utils/Utils.sol";
struct DrawDetail {
uint8 numberOfTiers;
uint256 startDrawReward;
uint256 finishDrawReward;
}
contract DrawAgent is StdCheats, Utils {
string relayCostCsvFile = string.concat(vm.projectRoot(), "/data/relayCost.csv");
string relayCostCsvColumns =
"Draw ID, Timestamp, Awarding Cost, Awarding Profit, Relay Cost, Relay Profit";
SingleChainEnvironment public env;
uint256 public drawCount;
uint256 public constant SEED = 0x23423;
mapping(uint24 drawId => DrawDetail drawInfo) internal _drawDetails;
constructor(SingleChainEnvironment _env) {
env = _env;
initOutputFileCsv(relayCostCsvFile, relayCostCsvColumns);
}
function drawDetails(uint24 drawId) public view returns (DrawDetail memory) {
return _drawDetails[drawId];
}
function willFinishDraw() public view returns (bool) {
uint256 finishDrawCost = env.config().usdToWeth(env.config().gas().finishDrawCostInUsd);
uint256 minimumFinishDrawProfit = getMinimumProfit(finishDrawCost);
uint256 finishDrawProfit;
if (env.drawManager().canFinishDraw()) {
// console2.log("Draw CAN AWARD");
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;
}
function check() public returns (bool) {
DrawManager drawManager = env.drawManager();
RngBlockhash rng = env.rng();
PrizePool prizePool = env.prizePool();
uint24 drawId = prizePool.getDrawIdToAward();
if (drawManager.canStartDraw()) {
uint fee = drawManager.startDrawReward();
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);
(uint32 requestId,) = rng.requestRandomNumber();
drawManager.startDraw(address(this), requestId);
_drawDetails[drawId].startDrawReward = fee;
}
} else {
}
if (willFinishDraw()) {
_drawDetails[drawId].numberOfTiers = prizePool.numberOfTiers();
_drawDetails[drawId].finishDrawReward = drawManager.finishDrawReward();
// console2.log("Awarding draw ", drawId);
drawManager.finishDraw(address(this));
drawCount++;
return true;
}
return false;
}
function getMinimumProfit(uint256 _cost) public pure returns (uint256) {
return (_cost + (_cost / 10)) - _cost; // require 10% profit
}
}