Skip to content

Instantly share code, notes, and snippets.

@bronzepickaxe
Created September 4, 2023 08:38
Show Gist options
  • Save bronzepickaxe/60063c47c327a1f2d4ee3dbd6361049b to your computer and use it in GitHub Desktop.
Save bronzepickaxe/60063c47c327a1f2d4ee3dbd6361049b to your computer and use it in GitHub Desktop.
POC
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import {PreciseMathUtils} from "../../contracts/libraries/PreciseMathUtils.sol";
import {MathUtils} from "../../contracts/libraries/MathUtils.sol";
import "forge-std/Test.sol";
import {console} from "forge-std/console.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
// forge install forge-std/forge-std
// Put this in src/test
// forge test --match-contract POC -vvvv
contract POC is Test {
using SafeMath for uint256;
// Set the treasuryRewardCutRate to 10% according to LIP-92.
uint256 treasuryRewardCutRate = 100000000000000000000000000;
function setUp() public {}
// Current implementation of updateTranscoderWithFees
function test_updateTranscoderWithFeesOld() public {
// Let's say rewards is 10
uint256 rewards = 10;
// This should calculate to 10% of 10, resulting in 1.
uint256 treasuryRewards = MathUtils.percOf(rewards, treasuryRewardCutRate);
// As you can see in the console.log, the usage of MathUtils results in: 100000000000000000000
console.log("treasuryRewards is: %s", treasuryRewards);
// This will revert because 10 minus 100000000000000000000 will underflow.
vm.expectRevert();
rewards = rewards.sub(treasuryRewards);
}
// Correct implementation of updateTranscoderWithFees
function test_updateTranscoderWithFeesWithFixed() public {
// Let's say rewards is 10
uint256 rewards = 10;
// This should calculate to 10% of 10, resulting in 1.
uint256 treasuryRewards = PreciseMathUtils.percOf(rewards, treasuryRewardCutRate);
// As you can see in the console.log, the usage of PreciseMathUtils results in: 1
console.log("treasuryRewards is: %s", treasuryRewards);
rewards = rewards.sub(treasuryRewards);
// treasuryRewards should be 10% of 10 -> 1
assertEq(treasuryRewards, 1);
}
}
@zakaria5313
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment