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

Carbon Vortex 2.0 #140

Merged
merged 13 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
6 changes: 5 additions & 1 deletion contracts/helpers/TestReenterCarbonVortex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ contract TestReenterCarbonVortex {
_carbonVortex.execute(tokens);
}

function tryReenterCarbonVortex(Token[] calldata tokens) external {
function tryReenterCarbonVortexExecute(Token[] calldata tokens) external {
_carbonVortex.execute(tokens);
}

function tryReenterCarbonVortexTrade(Token token, uint128 targetAmount) external payable {
_carbonVortex.trade{ value: msg.value }(token, targetAmount);
}
}
81 changes: 81 additions & 0 deletions contracts/helpers/TestVault.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import { Address } from "@openzeppelin/contracts/utils/Address.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { ReentrancyGuard } from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import { AccessControlEnumerable } from "@openzeppelin/contracts/access/AccessControlEnumerable.sol";

import { Token } from "../token/Token.sol";

import { Utils, AccessDenied } from "../utility/Utils.sol";

contract TestVault is ReentrancyGuard, AccessControlEnumerable, Utils {
using Address for address payable;
using SafeERC20 for IERC20;

// the admin role is used to grant and revoke the asset manager role
bytes32 internal constant ROLE_ADMIN = keccak256("ROLE_ADMIN");
// the asset manager role is required to access all the funds
bytes32 private constant ROLE_ASSET_MANAGER = keccak256("ROLE_ASSET_MANAGER");

/**
* @dev triggered when tokens have been withdrawn from the vault
*/
event FundsWithdrawn(Token indexed token, address indexed caller, address indexed target, uint256 amount);

/**
* @dev used to initialize the implementation
*/
constructor() {
// set up administrative roles
_setRoleAdmin(ROLE_ADMIN, ROLE_ADMIN);
_setRoleAdmin(ROLE_ASSET_MANAGER, ROLE_ADMIN);
// allow the deployer to initially be the admin of the contract
_setupRole(ROLE_ADMIN, msg.sender);
}

// solhint-enable func-name-mixedcase

/**
* @dev authorize the contract to receive the native token
lbeder marked this conversation as resolved.
Show resolved Hide resolved
*/
receive() external payable {}

/**
* @dev returns the admin role
*/
function roleAdmin() external pure returns (bytes32) {
return ROLE_ADMIN;
}

/**
* @dev returns the asset manager role
*/
function roleAssetManager() external pure returns (bytes32) {
return ROLE_ASSET_MANAGER;
}

/**
* @dev withdraws funds held by the contract and sends them to an account
*/
function withdrawFunds(
Token token,
address payable target,
uint256 amount
) external validAddress(target) nonReentrant {
if (!hasRole(ROLE_ASSET_MANAGER, msg.sender)) {
revert AccessDenied();
}

if (amount == 0) {
return;
}

// safe due to nonReentrant modifier (forwards all available gas in case of ETH)
token.unsafeTransfer(target, amount);

emit FundsWithdrawn({ token: token, caller: msg.sender, target: target, amount: amount });
}
}
9 changes: 5 additions & 4 deletions contracts/utility/ExpDecayMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ library ExpDecayMath {
* @dev returns the amount required for a token after a given time period since trading has been enabled
*
* the returned value is calculated as `amount / 2 ^ (timeElapsed / halfLife)`
* note that because the exponentiation function is limited to an input of up to (and excluding)
* 16 / ln 2, the input value to this function is limited by `timeElapsed / halfLife < 16 / ln 2`
* note that the input value to this function is limited by `timeElapsed / halfLife < 129`
*/
function calcExpDecay(uint256 amount, uint32 timeElapsed, uint32 halfLife) internal pure returns (uint256) {
Fraction memory input = Fraction({ n: timeElapsed, d: halfLife });
uint256 integerPart = timeElapsed / halfLife;
uint256 fractionPart = timeElapsed % halfLife;
Fraction memory input = Fraction({ n: fractionPart, d: halfLife });
Fraction memory output = MathEx.exp2(input);
return MathEx.mulDivF(amount, output.d, output.n);
return MathEx.mulDivF(amount, output.d, output.n * 2 ** integerPart);
}
}
18 changes: 18 additions & 0 deletions contracts/utility/interfaces/IVault.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.0;

import { Token } from "../../token/Token.sol";

/**
* @dev an interface for a vault
*/
interface IVault {
/**
* @dev withdraws funds held by the contract and sends them to an account
*/
function withdrawFunds(
Token token,
address payable target,
uint256 amount
) external;
}
Loading
Loading