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

Self route flashloan #70

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1338e6f
update transfer fee
KaymasJain May 6, 2022
0f4be40
added event
KaymasJain May 6, 2022
e0996b5
Implementations created
shriyatyagii May 9, 2022
e88ad6a
flashloan func added in implementations
shriyatyagii May 10, 2022
0c5c158
changed file names
shriyatyagii May 10, 2022
c7d3a69
minor update
shriyatyagii May 10, 2022
2ba7c66
added common helper and interface
shriyatyagii May 13, 2022
c549c3a
Added Implementation proxy - Arbitrum
shriyatyagii May 14, 2022
4c64449
Added implementation proxy - Avalanche
shriyatyagii May 14, 2022
2ab8019
Added implementation proxy - Optimism
shriyatyagii May 15, 2022
907b1ec
Added implementation proxy - Polygon
shriyatyagii May 15, 2022
cd8a484
code refactored
shriyatyagii May 15, 2022
7c7e650
arbitrum updates + testcases
shriyatyagii May 19, 2022
4c232b1
added sortTokens for uniswap key sorting
shriyatyagii May 20, 2022
57b3aca
minor updates + optimism testcases
shriyatyagii May 20, 2022
8ddef00
bubbleSort added in new implementations
shriyatyagii May 20, 2022
6cd9969
minor updates on all chains
shriyatyagii May 20, 2022
76ab257
polygon testcase + polygon updates
shriyatyagii May 20, 2022
cc7ca71
Avalanche testcases + new updates
shriyatyagii May 20, 2022
3d93a57
minor updates
shriyatyagii May 20, 2022
1277d61
Uni v3 flashloan added
shriyatyagii May 25, 2022
f6b80f4
testcases added
shriyatyagii May 25, 2022
baccd8c
self-route set up
shriyatyagii May 27, 2022
78327ea
testcases added
shriyatyagii May 27, 2022
2afdaa0
added route 9 - polygon
shriyatyagii May 29, 2022
a92298f
testcases added - polygon
shriyatyagii May 29, 2022
4d72798
minor updates
shriyatyagii May 30, 2022
e110eb3
route 9 added- Arbitrum
shriyatyagii May 30, 2022
8e8ec28
testcases added - arbitrum
shriyatyagii May 30, 2022
3d13e8a
route 9 added - Avalanche
shriyatyagii May 30, 2022
e506911
testcases added - Avalanche
shriyatyagii May 30, 2022
66a2a5d
route 9 added - optimism
shriyatyagii May 30, 2022
153a7b0
testcases added - optimism
shriyatyagii May 30, 2022
f0e5ac1
updated changes - mainnet
shriyatyagii May 31, 2022
fba997d
updated testcases -mainnet
shriyatyagii May 31, 2022
bf09018
minor updates
shriyatyagii May 31, 2022
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.
85 changes: 4 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 All @@ -170,6 +141,8 @@ contract Helper is Variables {
.getFlashLoanFeePercentage()
) *
100;
} else if (_route == 9) {
BPS_ = InstaFeeBPS;
} else {
revert("Invalid source");
}
Expand All @@ -179,56 +152,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