Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ add bot update rate0 function #116

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions contracts/amo/DynamicDutyCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ contract DynamicDutyCalculator is IDynamicDutyCalculator, Initializable, AccessC

bytes32 public constant INTERACTION = keccak256("INTERACTION");

bytes32 public constant BOT = keccak256("BOT");

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
Expand Down Expand Up @@ -109,19 +111,9 @@ contract DynamicDutyCalculator is IDynamicDutyCalculator, Initializable, AccessC
require(beta > 3e5 && beta < 1e8, "AggMonetaryPolicy/invalid-beta");

ilks[collateral].beta = beta;
ilks[collateral].rate0 = rate0;
ilks[collateral].enabled = enabled;

uint256 price = oracle.peek(lisUSD);
ilks[collateral].lastPrice = price;

uint256 duty = calculateRate(price, beta, rate0) + 1e27;
if (duty > maxDuty) duty = maxDuty;
if (duty < minDuty) duty = minDuty;

IDao(interaction).setCollateralDuty(collateral, duty);

emit CollateralParamsUpdated(collateral, beta, rate0, enabled);
_setCollateralRate0(collateral, beta, rate0, enabled, false);
}

/**
Expand Down Expand Up @@ -281,4 +273,41 @@ contract DynamicDutyCalculator is IDynamicDutyCalculator, Initializable, AccessC
return 1e18;
}
}

/**
* @dev Set rate0 for a collateral tokens.
* @param collaterals The collateral token address list.
* @param rates0 The rate list when the price is equal to PEG.
tedw-lista marked this conversation as resolved.
Show resolved Hide resolved
*/
function setCollateralRate0(address[] memory collaterals, uint256[] memory rates0) external onlyRole(BOT) {
require(collaterals.length > 0 && collaterals.length == rates0.length, "AggMonetaryPolicy/invalid-params");

for (uint256 i = 0; i < collaterals.length; i++) {
address collateral = collaterals[i];
require(collateral != address(0), "AggMonetaryPolicy/invalid-address");
require(ilks[collateral].beta > 3e5 && ilks[collateral].beta < 1e8, "AggMonetaryPolicy/invalid-beta");

_setCollateralRate0(collateral, ilks[collateral].beta, rates0[i], ilks[collateral].enabled, true);
}
}

function _setCollateralRate0(address collateral, uint256 beta, uint256 rate0, bool enabled, bool checkRange) private {
ilks[collateral].rate0 = rate0;

uint256 price = oracle.peek(lisUSD);
ilks[collateral].lastPrice = price;

uint256 duty = calculateRate(price, beta, rate0) + 1e27;
if (duty > maxDuty) {
require(!checkRange, "AggMonetaryPolicy/invalid-rate0");
duty = maxDuty;
}
if (duty < minDuty) {
require(!checkRange, "AggMonetaryPolicy/invalid-rate0");
duty = minDuty;
}

IDao(interaction).setCollateralDuty(collateral, duty);
emit CollateralParamsUpdated(collateral, beta, rate0, enabled);
}
}
2 changes: 1 addition & 1 deletion scripts/upgrades/deploy_impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {deployImplementation, verifyImpContract} = require('./utils/upgrade_utils

const oldContractAddress = ''
const oldContractName = ''
const contractName = 'FlashBuy'
const contractName = 'DynamicDutyCalculator'


async function main() {
Expand Down
77 changes: 77 additions & 0 deletions test/foundry/DynamicDutyCalculator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ contract DynamicDutyCalculatorTest is Test {
uint256 delta = 200000;

address public proxyAdminOwner = address(0x2A11AA);
address public bot = address(0x3A11AA);

address public collateral = address(0x5A11AA); // random address
address public collateral1 = address(0x6A11AA); // random address

uint256 beta = 1e6;
uint256 rate0 = 3309234382829741600; // 11% APY
uint256 rate0_15p = 4431822000000000000; // 15% APY
uint256 rate0_400p = 51034942716352291304; // 400% APY

address admin;

Expand Down Expand Up @@ -54,6 +59,10 @@ contract DynamicDutyCalculatorTest is Test {
)
);
dynamicDutyCalculator = DynamicDutyCalculator(address(dynamicDutyCalculatorProxy));

vm.startPrank(msg.sender);
dynamicDutyCalculator.grantRole(dynamicDutyCalculator.BOT(), bot);
vm.stopPrank();
}

function testRevert_initialize() public {
Expand Down Expand Up @@ -706,5 +715,73 @@ contract DynamicDutyCalculatorTest is Test {
dynamicDutyCalculator.file("proxy", address(0xAA));
vm.stopPrank();
}

function test_setCollateralRate0() public {
test_setCollateralParams();

vm.mockCall(
address(oracle),
abi.encodeWithSelector(ResilientOracle.peek.selector, lisUSD),
abi.encode(uint256(99500000)) // returns $0.995
);

vm.mockCall(
address(interaction),
abi.encodeWithSelector(Interaction.setCollateralDuty.selector),
abi.encode(uint256(0))
);

vm.startPrank(bot);
address[] memory collaterals = new address[](1);
collaterals[0] = collateral;
uint256[] memory rates = new uint256[](1);
rates[0] = rate0_15p;
dynamicDutyCalculator.setCollateralRate0(collaterals, rates);
vm.stopPrank();

(bool _enabled, uint256 _lastPrice, uint256 _rate0, uint256 _beta) = dynamicDutyCalculator.ilks(collateral);

assertEq(_beta, beta);
assertEq(_rate0, rate0_15p);
assertEq(_enabled, true);
assertEq(_lastPrice, 99500000);
}

function test_setCollateralRate0_revert() public {
test_setCollateralParams();

address[] memory collaterals = new address[](1);
collaterals[0] = collateral;
uint256[] memory rates = new uint256[](1);
rates[0] = rate0_15p;

vm.startPrank(bot);
vm.expectRevert("AggMonetaryPolicy/invalid-params");
dynamicDutyCalculator.setCollateralRate0(collaterals, new uint256[](2));
vm.stopPrank();

vm.startPrank(address(1));
vm.expectRevert("AccessControl: account 0x0000000000000000000000000000000000000001 is missing role 0x902cbe3a02736af9827fb6a90bada39e955c0941e08f0c63b3a662a7b17a4e2b");
dynamicDutyCalculator.setCollateralRate0(collaterals, rates);
vm.stopPrank();

rates[0] = rate0_400p;
vm.startPrank(bot);
vm.expectRevert("AggMonetaryPolicy/invalid-rate0");
dynamicDutyCalculator.setCollateralRate0(collaterals, rates);
vm.stopPrank();

collaterals[0] = address(0);
vm.startPrank(bot);
vm.expectRevert("AggMonetaryPolicy/invalid-address");
dynamicDutyCalculator.setCollateralRate0(collaterals, rates);
vm.stopPrank();

collaterals[0] = collateral1;
vm.startPrank(bot);
vm.expectRevert("AggMonetaryPolicy/invalid-beta");
dynamicDutyCalculator.setCollateralRate0(collaterals, rates);
vm.stopPrank();
}
}

Loading