Skip to content

Commit

Permalink
tcap deployer contract
Browse files Browse the repository at this point in the history
  • Loading branch information
gretzke committed Sep 9, 2024
1 parent 7c7588f commit 48a759e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TCAPTargetOracle
[Git Source](https://github.com/cryptexfinance/tcapv2.0/blob/6bc13f590e0d259edfc7844b2201ce75ef760a67/src/oracle/TCAPTargetOracle.sol)
[Git Source](https://github.com/cryptexfinance/tcapv2.0/blob/6715a13c6e4abbc7ab93ee610fd231d4c1654bde/src/oracle/TCAPTargetOracle.sol)

**Inherits:**
[AggregatedChainlinkOracle](/src/oracle/AggregatedChainlinkOracle.sol/contract.AggregatedChainlinkOracle.md)
Expand All @@ -20,7 +20,7 @@ uint256 private immutable DIVISOR;


```solidity
constructor(ITCAPV2 tcap, address feed) AggregatedChainlinkOracle(feed, address(tcap));
constructor(ITCAPV2 tcap, address feed_) AggregatedChainlinkOracle(feed_, address(tcap));
```

### latestPrice
Expand Down
15 changes: 0 additions & 15 deletions script/Deploy.s.sol

This file was deleted.

44 changes: 44 additions & 0 deletions script/DeployTCAP.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import "forge-std/Script.sol";
import "script/deployers/TCAPV2Deployer.s.sol";
import {TCAPTargetOracle} from "../src/oracle/TCAPTargetOracle.sol";
import {ProxyAdmin} from "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol";
import {TransparentUpgradeableProxy, ITransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {AggregatorV3Interface} from "@chainlink/interfaces/feeds/AggregatorV3Interface.sol";

contract Deploy is Script, TCAPV2Deployer {
using stdJson for string;

function run() public {
// Final values for deployment tbd
address proxyAdminOwner = address(1);
address admin = address(2);
AggregatorV3Interface oracleFeed = AggregatorV3Interface(address(3));
vm.startBroadcast(vm.envUint("PRIVATE_KEY"));
new TCAPDeployer(proxyAdminOwner, admin, oracleFeed);
vm.stopBroadcast();
}
}

/// @dev ensures that the contracts and permissions are set up correctly
contract TCAPDeployer {
event Deployed(address indexed tcap, address indexed implementation);

constructor(address proxyAdminOwner, address admin, AggregatorV3Interface oracleFeed) {
address tmpAdmin = address(this);
bytes memory initData = abi.encodeCall(TCAPV2.initialize, (tmpAdmin));

address implementation = address(new TCAPV2());
TCAPV2 tcap = TCAPV2(address(new TransparentUpgradeableProxy(implementation, proxyAdminOwner, initData)));

TCAPV2(tcap).grantRole(TCAPV2(tcap).ORACLE_SETTER_ROLE(), tmpAdmin);
TCAPTargetOracle oracle = new TCAPTargetOracle(ITCAPV2(tcap), address(oracleFeed));
TCAPV2(tcap).setOracle(address(oracle));
TCAPV2(tcap).grantRole(TCAPV2(tcap).DEFAULT_ADMIN_ROLE(), admin);
TCAPV2(tcap).revokeRole(TCAPV2(tcap).ORACLE_SETTER_ROLE(), tmpAdmin);
TCAPV2(tcap).revokeRole(TCAPV2(tcap).DEFAULT_ADMIN_ROLE(), tmpAdmin);
emit Deployed(address(tcap), implementation);
}
}
3 changes: 2 additions & 1 deletion src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ contract Vault is IVault, AccessControl, Multicall {

/// @inheritdoc IVault
function updateOracle(address newOracle) external onlyRole(ORACLE_SETTER_ROLE) {
if (IOracle(newOracle).asset() != address(COLLATERAL)) revert IOracle.InvalidOracle();
_updateOracle(newOracle);
}

Expand Down Expand Up @@ -341,6 +340,7 @@ contract Vault is IVault, AccessControl, Multicall {
}

function _updateInterestRate(uint16 fee) internal {
if (fee > Constants.MAX_FEE) revert InvalidValue(IVault.ErrorCode.MAX_FEE);
VaultStorage storage $ = _getVaultStorage();
$.mintData.setInterestRate(fee);
emit InterestRateUpdated(fee);
Expand Down Expand Up @@ -373,6 +373,7 @@ contract Vault is IVault, AccessControl, Multicall {
}

function _updateOracle(address newOracle) internal {
if (IOracle(newOracle).asset() != address(COLLATERAL)) revert IOracle.InvalidOracle();
VaultStorage storage $ = _getVaultStorage();
$.oracle = IOracle(newOracle);
emit OracleUpdated(newOracle);
Expand Down
1 change: 0 additions & 1 deletion src/lib/FeeCalculatorLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ library FeeCalculatorLib {

function setInterestRate(Vault.MintData storage $, uint16 fee) internal {
updateFeeIndex($);
if (fee > Constants.MAX_FEE) revert IVault.InvalidValue(IVault.ErrorCode.MAX_FEE);
$.feeData.fee = fee;
}

Expand Down
2 changes: 1 addition & 1 deletion src/oracle/TCAPTargetOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {AggregatedChainlinkOracle} from "./AggregatedChainlinkOracle.sol";
contract TCAPTargetOracle is AggregatedChainlinkOracle {
uint256 private immutable DIVISOR;

constructor(ITCAPV2 tcap, address feed) AggregatedChainlinkOracle(feed, address(tcap)) {
constructor(ITCAPV2 tcap, address feed_) AggregatedChainlinkOracle(feed_, address(tcap)) {
DIVISOR = tcap.DIVISOR();
}

Expand Down
25 changes: 25 additions & 0 deletions test/TCAPV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {MockFeed} from "./mock/MockFeed.sol";
import {MockCollateral} from "./mock/MockCollateral.sol";
import {TCAPTargetOracle} from "../src/oracle/TCAPTargetOracle.sol";
import {AggregatedChainlinkOracle} from "../src/oracle/AggregatedChainlinkOracle.sol";
import {TCAPDeployer} from "script/DeployTCAP.s.sol";
import {AggregatorV3Interface} from "@chainlink/interfaces/feeds/AggregatorV3Interface.sol";

abstract contract Uninitialized is Test, TestHelpers, TCAPV2Deployer {
function setUp() public virtual {
Expand Down Expand Up @@ -120,3 +122,26 @@ contract InitializedTest is Initialized {
assertEq(tCAPV2.latestPrice(), 3e12 * 1e18 / tCAPV2.DIVISOR());
}
}

contract DeployerTest is Test {
function test_Deployer() public {
address proxyAdminOwner = makeAddr("proxyAdminOwner");
address admin = makeAddr("admin");
AggregatorV3Interface feed = new MockFeed(3e12 * 1e8);
vm.recordLogs();
address deployer = address(new TCAPDeployer(proxyAdminOwner, admin, feed));
Vm.Log[] memory entries = vm.getRecordedLogs();
TCAPV2 tcap = TCAPV2(address(uint160(uint256(entries[entries.length - 1].topics[1]))));

address actualProxyAdminOwner =
ProxyAdmin(address(uint160(uint256(vm.load(address(tcap), hex"b53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103"))))).owner();
assertEq(actualProxyAdminOwner, proxyAdminOwner, "proxyAdminOwner does not match");

assertFalse(tcap.hasRole(tcap.ORACLE_SETTER_ROLE(), deployer), "deployer should not have oracle setter role");
assertFalse(tcap.hasRole(tcap.DEFAULT_ADMIN_ROLE(), deployer), "deployer should not have default admin role");
assertTrue(tcap.hasRole(tcap.DEFAULT_ADMIN_ROLE(), admin), "admin should have default admin role");
}
}
// 0xa85d42cF1874817895f171C917F6eE2cEa73EC20
// 0x8d2C17FAd02B7bb64139109c6533b7C2b9CADb81
// emit Deployed(tcap: TransparentUpgradeableProxy: [0x8d2C17FAd02B7bb64139109c6533b7C2b9CADb81], implementation: TCAPV2: [0xffD4505B3452Dc22f8473616d50503bA9E1710Ac])

0 comments on commit 48a759e

Please sign in to comment.