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

added allowance setter #199

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/strategies/any/v2/AnyCompounderNaiveV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pragma solidity ^0.8.25;

import {SafeERC20} from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol";
import {AnyConverterV2, IERC20Metadata, ERC20, IERC20, Math, CallStruct, PendingCallAllowance} from "./AnyConverterV2.sol";
import {AnyConverterV2, IERC20Metadata, ERC20, IERC20, Math, CallStruct, PendingTarget} from "./AnyConverterV2.sol";

/**
* @title BaseStrategy
Expand Down
2 changes: 1 addition & 1 deletion src/strategies/any/v2/AnyCompounderV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pragma solidity ^0.8.25;

import {AnyCompounderNaiveV2, AnyConverterV2, CallStruct, PendingCallAllowance, IERC20Metadata, ERC20, IERC20, Math} from "./AnyCompounderNaiveV2.sol";
import {AnyCompounderNaiveV2, AnyConverterV2, CallStruct, PendingTarget, IERC20Metadata, ERC20, IERC20, Math} from "./AnyCompounderNaiveV2.sol";

/**
* @title BaseStrategy
Expand Down
203 changes: 114 additions & 89 deletions src/strategies/any/v2/AnyConverterV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct CallStruct {
bytes data;
}

struct PendingCallAllowance {
struct PendingTarget {
address target;
bytes4 selector;
bool allowed;
Expand Down Expand Up @@ -43,11 +43,6 @@ abstract contract AnyConverterV2 is BaseStrategy {
address[] public tokens;
IPriceOracle public oracle;

uint256 public outstandingAllowance;

bytes4 public constant APPROVE_SELECTOR =
bytes4(keccak256("approve(address,uint256)"));

/*//////////////////////////////////////////////////////////////
INITIALIZATION
//////////////////////////////////////////////////////////////*/
Expand All @@ -67,18 +62,42 @@ abstract contract AnyConverterV2 is BaseStrategy {
) internal onlyInitializing {
__BaseStrategy_init(asset_, owner_, autoDeposit_);

address oracle_;
(yieldToken, oracle_, slippage) = abi.decode(
strategyInitData_,
(address, address, uint256)
);
if (oracle_ == address(0)) revert Misconfigured();
if (yieldToken == address(0)) revert Misconfigured();
(
address yieldToken_,
address oracle_,
uint256 slippage_,
PendingTarget[] memory pendingTargets_,
CallStruct[] memory pendingAllowances_
) = abi.decode(
strategyInitData_,
(address, address, uint256, PendingTarget[], CallStruct[])
);

oracle = IPriceOracle(oracle_);
if (yieldToken_ == address(0)) revert Misconfigured();
if (oracle_ == address(0)) revert Misconfigured();

yieldToken = yieldToken_;
tokens.push(asset_);
tokens.push(yieldToken);

oracle = IPriceOracle(oracle_);
slippage = slippage_;

for (uint256 i; i < pendingTargets.length; i++) {
isAllowed[pendingTargets[i].target][
pendingTargets[i].selector
] = pendingTargets[i].allowed;

emit TargetUpdated(
pendingTargets[i].target,
pendingTargets[i].selector,
pendingTargets[i].allowed
);
}

for (uint256 i; i < pendingAllowances.length; i++) {
pendingAllowances[i].target.call(pendingAllowances[i].data);
}
}

/*//////////////////////////////////////////////////////////////
Expand All @@ -90,14 +109,12 @@ abstract contract AnyConverterV2 is BaseStrategy {
* @dev Return assets held by adapter if paused.
*/
function _totalAssets() internal view virtual override returns (uint256) {
uint256 _outstandingAllowance = outstandingAllowance;
uint256 _totalAssets = oracle.getQuote(
IERC20(yieldToken).balanceOf(address(this)),
yieldToken,
asset()
);
if (_outstandingAllowance > _totalAssets) return 0;
return _totalAssets - _outstandingAllowance;
return
oracle.getQuote(
IERC20(yieldToken).balanceOf(address(this)),
yieldToken,
asset()
);
}

function convertToUnderlyingShares(
Expand Down Expand Up @@ -162,7 +179,7 @@ abstract contract AnyConverterV2 is BaseStrategy {
function pushFunds(
uint256,
bytes memory data
) external virtual override onlyKeeperOrOwner {
) external virtual override onlyKeeperOrOwner whenNotPaused {
uint256[6] memory stats = _execute(data);

// Total assets should stay the same or increase (with slippage)
Expand Down Expand Up @@ -214,44 +231,15 @@ abstract contract AnyConverterV2 is BaseStrategy {
);

CallStruct[] memory calls = abi.decode(data, (CallStruct[]));
CallStruct[] memory allowanceCalls = new CallStruct[](calls.length);
for (uint256 i; i < calls.length; i++) {
if (!isAllowed[calls[i].target][bytes4(calls[i].data)])
revert("Not allowed");

if (bytes4(calls[i].data) == APPROVE_SELECTOR) {
(address to, ) = abi.decode(
calls[i].data.slice(4, calls[i].data.length - 4),
(address, uint256)
);
allowanceCalls[i] = CallStruct({
target: calls[i].target,
data: abi.encodeWithSelector(
bytes4(keccak256("allowance(address,address)")),
address(this),
to
)
});
}

(bool success, ) = calls[i].target.call(calls[i].data);
if (!success) revert("Call failed");
}

uint256 _outstandingAllowance;
for (uint256 i; i < allowanceCalls.length; i++) {
if (allowanceCalls[i].target != address(0)) {
(bool success, bytes memory result) = allowanceCalls[i]
.target
.call(allowanceCalls[i].data);
if (!success) revert("Call failed");

_outstandingAllowance += abi.decode(result, (uint256));
}
}

uint256 outstandingAllowance = _outstandingAllowance;
uint256 postTotalAssets = totalAssets() - _outstandingAllowance;
uint256 postTotalAssets = totalAssets();
uint256 postAssetBalance = IERC20(_asset).balanceOf(address(this));
uint256 postYieldTokenBalance = IERC20(_yieldToken).balanceOf(
address(this)
Expand Down Expand Up @@ -317,63 +305,100 @@ abstract contract AnyConverterV2 is BaseStrategy {
}

/*//////////////////////////////////////////////////////////////
ALLOWED FUNCTION LOGIC
SET TARGET LOGIC
//////////////////////////////////////////////////////////////*/

event CallAllowanceProposed(
address target,
bytes4 selector,
bool isAllowed
);
event CallAllowanceChanged(address target, bytes4 selector, bool isAllowed);
event TargetProposed(address target, bytes4 selector, bool isAllowed);
event TargetUpdated(address target, bytes4 selector, bool isAllowed);

PendingCallAllowance[] public pendingCallAllowances;
uint256 public pendingCallAllowanceTime;
PendingTarget[] public pendingTargets;
uint256 public pendingTargetTime;
mapping(address => mapping(bytes4 => bool)) public isAllowed;

function proposeCallAllowance(
PendingCallAllowance[] calldata callAllowances
function proposeTargets(
PendingTarget[] calldata targets
) external onlyOwner {
for (uint256 i; i < callAllowances.length; i++) {
pendingCallAllowances.push(callAllowances[i]);
for (uint256 i; i < targets.length; i++) {
pendingTargets.push(targets[i]);

emit CallAllowanceProposed(
callAllowances[i].target,
callAllowances[i].selector,
callAllowances[i].allowed
emit TargetProposed(
targets[i].target,
targets[i].selector,
targets[i].allowed
);
}
pendingCallAllowanceTime = block.timestamp + 3 days;
pendingTargetTime = block.timestamp + 3 days;
}

function changeCallAllowances() external onlyOwner {
if (
pendingCallAllowanceTime == 0 ||
pendingCallAllowanceTime > block.timestamp
) revert Misconfigured();
function updateTargets() external onlyOwner {
if (pendingTargetTime == 0 || pendingTargetTime > block.timestamp)
revert Misconfigured();

for (uint256 i; i < pendingCallAllowances.length; i++) {
isAllowed[pendingCallAllowances[i].target][
pendingCallAllowances[i].selector
] = pendingCallAllowances[i].allowed;
for (uint256 i; i < pendingTargets.length; i++) {
isAllowed[pendingTargets[i].target][
pendingTargets[i].selector
] = pendingTargets[i].allowed;

emit CallAllowanceChanged(
pendingCallAllowances[i].target,
pendingCallAllowances[i].selector,
pendingCallAllowances[i].allowed
emit TargetUpdated(
pendingTargets[i].target,
pendingTargets[i].selector,
pendingTargets[i].allowed
);
}

delete pendingCallAllowances;
delete pendingCallAllowanceTime;
delete pendingTargets;
delete pendingTargetTime;
}

function getProposedTargets()
external
view
returns (uint256, PendingTarget[] memory)
{
return (pendingTargetTime, pendingTargets);
}

/*//////////////////////////////////////////////////////////////
ALLOWANCE LOGIC
//////////////////////////////////////////////////////////////*/

CallStruct[] public pendingAllowances;
uint256 public pendingAllowanceTime;

event AllowanceProposed(address target, bytes data);

function proposeAllowances(
CallStruct[] calldata allowanceCalls
) external onlyOwner {
for (uint256 i; i < allowanceCalls.length; i++) {
pendingAllowances.push(allowanceCalls[i]);

emit AllowanceProposed(
allowanceCalls[i].target,
allowanceCalls[i].data
);
}
pendingAllowanceTime = block.timestamp + 3 days;
}

function updateAllowances() external onlyOwner {
if (pendingAllowanceTime == 0 || pendingAllowanceTime > block.timestamp)
revert Misconfigured();

for (uint256 i; i < pendingAllowances.length; i++) {
pendingAllowances[i].target.call(pendingAllowances[i].data);
}

delete pendingAllowances;
delete pendingAllowanceTime;
}

function getProposedCallAllowance()
function getProposedAllowances()
external
view
returns (uint256, PendingCallAllowance[] memory)
returns (uint256, CallStruct[] memory)
{
return (pendingCallAllowanceTime, pendingCallAllowances);
return (pendingAllowanceTime, pendingAllowances);
}

/*//////////////////////////////////////////////////////////////
Expand Down
47 changes: 2 additions & 45 deletions src/strategies/any/v2/AnyLBTManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pragma solidity ^0.8.25;

import {AnyCompounderNaiveV2, AnyConverterV2, CallStruct, PendingCallAllowance, IERC20Metadata, ERC20, IERC20, Math} from "./AnyCompounderNaiveV2.sol";
import {AnyCompounderNaiveV2, AnyConverterV2, CallStruct, PendingTarget, IERC20Metadata, ERC20, IERC20, Math} from "./AnyCompounderNaiveV2.sol";
import {ILBT} from "src/interfaces/external/lfj/ILBT.sol";
import {BytesLib} from "bitlib/BytesLib.sol";

Expand Down Expand Up @@ -100,10 +100,6 @@ contract AnyLBTManager is AnyCompounderNaiveV2 {
_asset
);
}

uint256 _outstandingAllowance = outstandingAllowance;
if (_outstandingAllowance > totalAssets) return 0;
totalAssets -= _outstandingAllowance;
}

function setDepositIds(uint24[] memory ids) external onlyKeeperOrOwner {
Expand Down Expand Up @@ -151,8 +147,6 @@ contract AnyLBTManager is AnyCompounderNaiveV2 {
emit PulledFunds(0, 0);
}

event LogUint(string, uint);

function _execute(
bytes memory data
) internal override returns (uint256[6] memory) {
Expand All @@ -163,41 +157,14 @@ contract AnyLBTManager is AnyCompounderNaiveV2 {
uint256 preTotalAssets = totalAssets();

CallStruct[] memory calls = abi.decode(data, (CallStruct[]));
CallStruct[] memory allowanceCalls = new CallStruct[](calls.length);
for (uint256 i; i < calls.length; i++) {
if (!isAllowed[calls[i].target][bytes4(calls[i].data)])
revert("Not allowed");

if (bytes4(calls[i].data) == APPROVE_SELECTOR) {
(address to, ) = abi.decode(
calls[i].data.slice(4, calls[i].data.length - 4),
(address, uint256)
);
allowanceCalls[i] = CallStruct({
target: calls[i].target,
data: abi.encodeWithSelector(
bytes4(keccak256("allowance(address,address)")),
address(this),
to
)
});
}

(bool success, ) = calls[i].target.call(calls[i].data);
if (!success) revert("Call failed");
}

uint256 outstandingAllowance;
for (uint256 i; i < allowanceCalls.length; i++) {
if (allowanceCalls[i].target != address(0)) {
(bool success, bytes memory result) = allowanceCalls[i]
.target
.call(allowanceCalls[i].data);
if (!success) revert("Call failed");

outstandingAllowance += abi.decode(result, (uint256));
}
}
uint24 activeId = ILBT(yieldToken).getActiveId();

uint256 amountY = 0;
Expand All @@ -221,17 +188,7 @@ contract AnyLBTManager is AnyCompounderNaiveV2 {
}
}

emit LogUint("amountY", amountY);
emit LogUint("amountX", amountX);
emit LogUint("balance", IERC20(_asset).balanceOf(address(this)));
emit LogUint(
"balance tokenY",
IERC20(0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7).balanceOf(
address(this)
)
);
emit LogUint("totalAssets", totalAssets());
uint256 postTotalAssets = totalAssets() - outstandingAllowance;
uint256 postTotalAssets = totalAssets();

return ([preTotalAssets, postTotalAssets, 0, 0, 0, 0]);
}
Expand Down
Loading
Loading