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

Implementations proxy update #67

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 0 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,3 @@ Deploy the contracts to Hardhat Network:
```sh
$ npm run deploy
```

## Syntax Highlighting

If you use VSCode, you can enjoy syntax highlighting for your Solidity code via the
[vscode-solidity](https://github.com/juanfranblanco/vscode-solidity) extension. The recommended approach to set the
compiler version is to add the following fields to your VSCode user settings:

```json
{
"solidity.compileUsingRemoteVersion": "v0.8.4+commit.c7e474f2",
"solidity.defaultCompiler": "remote"
}
```

Where of course `v0.8.4+commit.c7e474f2` can be replaced with any other version.
83 changes: 2 additions & 81 deletions contracts/aggregator/arbitrum/flashloan/helpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
pragma solidity ^0.8.0;

import "./variables.sol";
import "../../common/helpers.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract Helper is Variables {
contract Helper is HelpersCommon, Variables {
using SafeERC20 for IERC20;

/**
Expand Down Expand Up @@ -102,25 +103,6 @@ contract Helper is Variables {
}
}

/**
* @dev Calculates the balances..
* @notice Calculates the balances of the account passed for the tokens.
* @param _tokens list of token addresses to calculate balance for.
* @param _account account to calculate balance for.
*/
function calculateBalances(address[] memory _tokens, address _account)
internal
view
returns (uint256[] memory)
{
uint256 _length = _tokens.length;
uint256[] memory balances_ = new uint256[](_length);
for (uint256 i = 0; i < _length; i++) {
IERC20 token = IERC20(_tokens[i]);
balances_[i] = token.balanceOf(_account);
}
return balances_;
}

/**
* @dev Validates if the receiver sent the correct amounts of funds.
Expand All @@ -141,17 +123,6 @@ contract Helper is Variables {
}
}

/**
* @dev Validates if token addresses are unique. Just need to check adjacent tokens as the array was sorted first
* @notice Validates if token addresses are unique.
* @param _tokens list of token addresses.
*/
function validateTokens(address[] memory _tokens) internal pure {
for (uint256 i = 0; i < _tokens.length - 1; i++) {
require(_tokens[i] != _tokens[i + 1], "non-unique-tokens");
}
}

/**
* @dev Returns fee for the passed route in BPS.
* @notice Returns fee for the passed route in BPS. 1 BPS == 0.01%.
Expand Down Expand Up @@ -179,56 +150,6 @@ contract Helper is Variables {
}
}

/**
* @dev Calculate fees for the respective amounts and fee in BPS passed.
* @notice Calculate fees for the respective amounts and fee in BPS passed. 1 BPS == 0.01%.
* @param _amounts list of amounts.
* @param _BPS fee in BPS.
*/
function calculateFees(uint256[] memory _amounts, uint256 _BPS)
internal
pure
returns (uint256[] memory)
{
uint256 length_ = _amounts.length;
uint256[] memory InstaFees = new uint256[](length_);
for (uint256 i = 0; i < length_; i++) {
InstaFees[i] = (_amounts[i] * _BPS) / (10**4);
}
return InstaFees;
}

/**
* @dev Sort the tokens and amounts arrays according to token addresses.
* @notice Sort the tokens and amounts arrays according to token addresses.
* @param _tokens list of token addresses.
* @param _amounts list of respective amounts.
*/
function bubbleSort(address[] memory _tokens, uint256[] memory _amounts)
internal
pure
returns (address[] memory, uint256[] memory)
{
for (uint256 i = 0; i < _tokens.length - 1; i++) {
for (uint256 j = 0; j < _tokens.length - i - 1; j++) {
if (_tokens[j] > _tokens[j + 1]) {
(
_tokens[j],
_tokens[j + 1],
_amounts[j],
_amounts[j + 1]
) = (
_tokens[j + 1],
_tokens[j],
_amounts[j + 1],
_amounts[j]
);
}
}
}
return (_tokens, _amounts);
}

/**
* @dev Returns to true if the passed address is a DSA else returns false.
* @notice Returns to true if the passed address is a DSA else returns false.
Expand Down
120 changes: 120 additions & 0 deletions contracts/aggregator/arbitrum/flashloan/implBalancer/main.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../helpers.sol";

contract BalancerImplementationArbitrum is Helper {

/**
* @dev Main function for flashloan for all routes. Calls the middle functions according to routes.
* @notice Main function for flashloan for all routes. Calls the middle functions according to routes.
* @param _tokens token addresses for flashloan.
* @param _amounts list of amounts for the corresponding assets.
* @param _route route for flashloan.
* @param _data extra data passed.
*/
function flashLoan(
address[] memory _tokens,
uint256[] memory _amounts,
uint256 _route,
bytes calldata _data,
bytes memory _instadata
) external reentrancy {
require(_route == 5 , "invalid-BALANCER-route");
(_tokens, _amounts) = bubbleSort(_tokens, _amounts);
validateTokens(_tokens);
routeBalancer(_tokens, _amounts, _data);
}

/**
* @dev Callback function for balancer flashloan.
* @notice Fallback function for balancer flashloan.
* @param _amounts list of amounts for the corresponding assets or amount of ether to borrow as collateral for flashloan.
* @param _fees list of fees for the corresponding addresses for flashloan.
* @param _data extra data passed.
*/
function receiveFlashLoan(
IERC20[] memory _tokens,
uint256[] memory _amounts,
uint256[] memory _fees,
bytes memory _data
) external verifyDataHash(_data) {
require(msg.sender == balancerLendingAddr, "not-balancer-sender");

FlashloanVariables memory instaLoanVariables_;

uint256 length_ = _tokens.length;
instaLoanVariables_._tokens = new address[](length_);
for (uint256 i = 0; i < length_; i++) {
instaLoanVariables_._tokens[i] = address(_tokens[i]);
}

(address sender_, bytes memory data_) = abi.decode(
_data,
(address, bytes)
);

instaLoanVariables_._amounts = _amounts;
instaLoanVariables_._iniBals = calculateBalances(
instaLoanVariables_._tokens,
address(this)
);
instaLoanVariables_._instaFees = calculateFees(
_amounts,
calculateFeeBPS(5)
);

safeTransfer(instaLoanVariables_, sender_);

if (checkIfDsa(sender_)) {
Address.functionCall(
sender_,
data_,
"DSA-flashloan-fallback-failed"
);
} else {
InstaFlashReceiverInterface(sender_).executeOperation(
instaLoanVariables_._tokens,
_amounts,
instaLoanVariables_._instaFees,
sender_,
data_
);
}

instaLoanVariables_._finBals = calculateBalances(
instaLoanVariables_._tokens,
address(this)
);
validateFlashloan(instaLoanVariables_);

safeTransferWithFee(instaLoanVariables_, _fees, balancerLendingAddr);
}


/**
* @dev Middle function for route 5.
* @notice Middle function for route 5.
* @param _tokens token addresses for flashloan.
* @param _amounts list of amounts for the corresponding assets.
* @param _data extra data passed.
*/
function routeBalancer(
address[] memory _tokens,
uint256[] memory _amounts,
bytes memory _data
) internal {
bytes memory data_ = abi.encode(msg.sender, _data);
uint256 length_ = _tokens.length;
IERC20[] memory tokens_ = new IERC20[](length_);
for (uint256 i = 0; i < length_; i++) {
tokens_[i] = IERC20(_tokens[i]);
}
dataHash = bytes32(keccak256(data_));
balancerLending.flashLoan(
InstaFlashReceiverInterface(address(this)),
tokens_,
_amounts,
data_
);
}
}
Loading