|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.15; |
| 3 | + |
| 4 | +import {DSTestPlus} from "solmate/test/utils/DSTestPlus.sol"; |
| 5 | + |
| 6 | +import {MockLinearVRGDA} from "../mocks/MockLinearVRGDA.sol"; |
| 7 | +import {toWadUnsafe} from "../../src/utils/SignedWadMath.sol"; |
| 8 | +import {console} from "forge-std/console.sol"; |
| 9 | +import {Vm} from "forge-std/Vm.sol"; |
| 10 | + |
| 11 | +// Differentially fuzz VRGDA solidity implementation against python reference |
| 12 | +contract VRGDACorrectnessTest is DSTestPlus { |
| 13 | + |
| 14 | + //instantiate vm |
| 15 | + address private constant VM_ADDRESS = address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))); |
| 16 | + Vm public constant vm = Vm(VM_ADDRESS); |
| 17 | + |
| 18 | + // sample parameters for differential fuzzing campaign |
| 19 | + uint256 immutable MAX_TIMEFRAME = 356 days * 10; |
| 20 | + uint256 immutable MAX_SELLABLE = 10000; |
| 21 | + int256 immutable TARGET_PRICE = 69.42e18; |
| 22 | + int256 immutable PRICE_DECREASE_PERCENT = 0.31e18; |
| 23 | + int256 immutable PER_UNIT_TIME = 2e18; |
| 24 | + |
| 25 | + MockLinearVRGDA vrgda; |
| 26 | + |
| 27 | + function setUp() public { |
| 28 | + vrgda = new MockLinearVRGDA(TARGET_PRICE, PRICE_DECREASE_PERCENT, PER_UNIT_TIME); |
| 29 | + } |
| 30 | + |
| 31 | + // test correctness of implementation for a single input, as a sanity check |
| 32 | + function testFFICorrectness() public { |
| 33 | + // 10 days in wads |
| 34 | + uint256 timeSinceStart = 10 * 1e18; |
| 35 | + // number sold, slightly ahead of schedule |
| 36 | + uint256 numSold = 25; |
| 37 | + |
| 38 | + uint256 actualPrice = vrgda.getVRGDAPrice(int256(timeSinceStart), numSold); |
| 39 | + uint256 expectedPrice = calculatePrice( |
| 40 | + TARGET_PRICE, |
| 41 | + PRICE_DECREASE_PERCENT, |
| 42 | + PER_UNIT_TIME, |
| 43 | + timeSinceStart, |
| 44 | + numSold |
| 45 | + ); |
| 46 | + |
| 47 | + console.log("actual price", actualPrice); |
| 48 | + console.log("expected price", expectedPrice); |
| 49 | + //check approximate equality |
| 50 | + assertRelApproxEq(expectedPrice, actualPrice, 0.00001e18); |
| 51 | + // sanity check that prices are greater than zero |
| 52 | + assertGt(actualPrice, 0); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + // fuzz to test correctness against multiple inputs |
| 57 | + function testFFICorrectnessFuzz(uint256 timeSinceStart, uint256 numSold) public { |
| 58 | + // Bound fuzzer inputs to acceptable contraints. |
| 59 | + numSold = bound(numSold, 0, MAX_SELLABLE); |
| 60 | + timeSinceStart = bound(timeSinceStart, 0, MAX_TIMEFRAME); |
| 61 | + // Convert to wad days for convenience. |
| 62 | + timeSinceStart = timeSinceStart * 1e18 / 1 days; |
| 63 | + |
| 64 | + // We wrap this call in a try catch because the getVRGDAPrice is expected to revert for |
| 65 | + // degenerate cases. When this happens, we just continue campaign. |
| 66 | + try vrgda.getVRGDAPrice(int256(timeSinceStart), numSold) returns (uint256 actualPrice) { |
| 67 | + uint256 expectedPrice = calculatePrice( |
| 68 | + TARGET_PRICE, |
| 69 | + PRICE_DECREASE_PERCENT, |
| 70 | + PER_UNIT_TIME, |
| 71 | + timeSinceStart, |
| 72 | + numSold |
| 73 | + ); |
| 74 | + if (expectedPrice < 0.0000001e18) return; // For really small prices, we expect divergence, so we skip |
| 75 | + assertRelApproxEq(expectedPrice, actualPrice, 0.00001e18); |
| 76 | + } catch {} |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + // ffi call |
| 83 | + function calculatePrice( |
| 84 | + int256 _targetPrice, |
| 85 | + int256 _priceDecreasePercent, |
| 86 | + int256 _perUnitTime, |
| 87 | + uint256 _timeSinceStart, |
| 88 | + uint256 _numSold |
| 89 | + ) private returns (uint256) { |
| 90 | + string[] memory inputs = new string[](13); |
| 91 | + inputs[0] = "python3"; |
| 92 | + inputs[1] = "test/diff_fuzz/python/compute_price.py"; |
| 93 | + inputs[2] = "linear"; |
| 94 | + inputs[3] = "--time_since_start"; |
| 95 | + inputs[4] = vm.toString(_timeSinceStart); |
| 96 | + inputs[5] = "--num_sold"; |
| 97 | + inputs[6] = vm.toString(_numSold); |
| 98 | + inputs[7] = "--target_price"; |
| 99 | + inputs[8] = vm.toString(uint256(_targetPrice)); |
| 100 | + inputs[9] = "--price_decrease_percent"; |
| 101 | + inputs[10] = vm.toString(uint256(_priceDecreasePercent)); |
| 102 | + inputs[11] = "--per_time_unit"; |
| 103 | + inputs[12] = vm.toString(uint256(_perUnitTime)); |
| 104 | + |
| 105 | + return abi.decode(vm.ffi(inputs), (uint256)); |
| 106 | + } |
| 107 | +} |
0 commit comments