Skip to content

Commit

Permalink
71: adding gas rebate test
Browse files Browse the repository at this point in the history
  • Loading branch information
jac18281828 committed Jun 26, 2023
1 parent 9c362dd commit 2bdecad
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ jobs:
- name: Run Forge tests
run: |
forge test -vvv
env:
FOUNDRY_PROFILE: fastbuild
id: test

- name: Run Forge geiger
Expand Down
7 changes: 5 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@
{
"label": "test",
"type": "shell",
"command": "forge test -vvv",
"command": "forge test -vvv --fail-fast",
"options": {
"cwd": "${workspaceFolder}"
"cwd": "${workspaceFolder}",
"env": {
"FOUNDRY_PROFILE": "fastbuild"
}
},
"dependsOn": "hint",
"group": {
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ENV PATH=${PATH}:~/.cargo/bin
RUN yarn install
RUN yarn prettier:check
RUN yarn hint
RUN forge test -vvv
RUN FOUNDRY_PROFILE=fastbuild forge test -vvv --fail-fast
RUN forge geiger --check contracts/*

RUN bin/update_abi.sh
6 changes: 5 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ src = 'contracts'
out = 'out'
libs = ['lib']
optimizer = true
optimizer_runs = 200
optimizer_runs = 500
via-ir = true
block_number = 1
# 101 gwei
gas_price = 101000000000
block_timestamp = 100

[profile.fastbuild]
optimizer_runs = 50
fuzz.runs = 10

[profile.default.optimizer_details]
inliner = true
jumpdest_remover = true
Expand Down
19 changes: 1 addition & 18 deletions test/governance/CollectiveGovernance.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { createCommunityBuilder } from "../../contracts/community/CommunityBuild
import { Storage } from "../../contracts/storage/Storage.sol";
import { VoteStrategy } from "../../contracts/governance/VoteStrategy.sol";
import { Governance } from "../../contracts/governance/Governance.sol";
import { CollectiveGovernance, calculateGasRebate } from "../../contracts/governance/CollectiveGovernance.sol";
import { CollectiveGovernance } from "../../contracts/governance/CollectiveGovernance.sol";
import { TimeLocker } from "../../contracts/treasury/TimeLocker.sol";
import { VersionedContract } from "../../contracts/access/VersionedContract.sol";
import { StorageFactory } from "../../contracts/storage/StorageFactory.sol";
Expand All @@ -33,22 +33,6 @@ import { MockERC721 } from "../mock/MockERC721.sol";
import { FlagSet } from "../mock/FlagSet.sol";
import { TestData } from "../mock/TestData.sol";

contract GasRebateTest is Test {
function testGasRebate() public {
uint256 startGas = gasleft();
(uint256 gasRebate, uint256 gasUsed) = calculateGasRebate(startGas, 1 ether, 200 gwei, 200000);
assertApproxEqAbs(gasRebate, 72234 gwei, 5000 gwei);
assertTrue(gasUsed > 0);
}

function testMaximumRebate() public {
uint256 startGas = gasleft();
(uint256 gasRebate, uint256 gasUsed) = calculateGasRebate(startGas, 30 gwei, 200 gwei, 200000);
assertEq(gasRebate, 30 gwei);
assertTrue(gasUsed > 0);
}
}

contract CollectiveGovernanceTest is Test {
address private constant _OWNER = address(0x1);
address private constant _SUPERVISOR = address(0x123);
Expand Down Expand Up @@ -1418,7 +1402,6 @@ contract CollectiveGovernanceTest is Test {
governance.withdrawRebate(_VOTER1);
assertTrue(_VOTER1.balance > 0);
assertEq(_VOTER1.balance, expectBalance);
assertEq(_VOTER1.balance, 10063924 gwei);
}

function testCastVoteAndCancelRefund() public {
Expand Down
45 changes: 45 additions & 0 deletions test/governance/GasRebateTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: BSD-3-Clause
// solhint-disable not-rely-on-time
pragma solidity ^0.8.15;

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

import { calculateGasRebate } from "../../contracts/governance/CollectiveGovernance.sol";

contract GasRebateTest is Test {
function testGasRebate() public {
uint256 startGas = gasleft();
(uint256 gasRebate, uint256 gasUsed) = calculateGasRebate(startGas, 1 ether, 200 gwei, 200000);
assertApproxEqAbs(gasRebate, 72234 gwei, 5000 gwei);
assertTrue(gasUsed > 0);
}

function testMaximumRebate(uint256 gasLimit) public {
vm.assume(gasLimit >= 30 gwei && gasLimit <= 200 gwei);
uint256 testNetGas = 251154;
uint256 startGas = gasleft();
uint i;
do {
i = 3 * 13 * startGas;
} while (startGas - gasleft() < testNetGas);
(uint256 gasRebate, uint256 gasUsed) = calculateGasRebate(startGas, gasLimit, 200 gwei, 200000);
emit log_uint(gasRebate);
assertEq(gasRebate, gasLimit);
emit log_uint(gasUsed);
assertTrue(gasUsed >= testNetGas);
}

function testRebateRealWorldTarget(uint256 gasUsedTarget) public {
vm.assume(gasUsedTarget > 30000 && gasUsedTarget < 255000);
uint256 startGas = gasleft();
uint i;
do {
i = 3 * 13 * startGas;
} while (startGas - gasleft() < gasUsedTarget);
(uint256 gasRebate, uint256 gasUsed) = calculateGasRebate(startGas, 2000 gwei, 200 gwei, 200000);
emit log_uint(gasRebate);
assertEq(gasRebate, 2000 gwei);
emit log_uint(gasUsed);
assertTrue(gasUsed >= gasUsedTarget);
}
}

0 comments on commit 2bdecad

Please sign in to comment.