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

Fantom support #73

Open
wants to merge 58 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
18c167f
Aave v3 implementation added
shriyatyagii Jun 19, 2022
9364f29
Aave v3 testcases added
shriyatyagii Jun 19, 2022
d973f67
Fantom resolver added
shriyatyagii Jun 19, 2022
0b5a8dd
Resolver testcases added
shriyatyagii Jun 19, 2022
f668840
common helpers and interface added
shriyatyagii Jun 19, 2022
ea740e4
hardhat & test script updated
shriyatyagii Jun 19, 2022
9069c53
Fla route added
shriyatyagii Jun 19, 2022
c911d33
fla route - testcases
shriyatyagii Jun 19, 2022
4cdde6c
receiver updated: minor fix
shriyatyagii Jun 19, 2022
07ad993
Resolver updated - fla route
shriyatyagii Jun 20, 2022
fa19dac
Removed logs
shriyatyagii Jun 21, 2022
87f02cf
Revert "Removed logs"
shriyatyagii Jun 21, 2022
80a4c95
code refactored
shriyatyagii Jun 21, 2022
bcddc8e
route var removed
shriyatyagii Jun 21, 2022
9a8043a
Update contracts/aggregator/fantom/flashloan/implAaveV3/main.sol
shriyatyagii Jun 21, 2022
12fcaa6
transferFee updated + added admin module
shriyatyagii Jun 21, 2022
d45e8ed
removed logs
shriyatyagii Jun 21, 2022
0cda7c5
Added flaImpl
shriyatyagii Jun 21, 2022
b33d9c6
removed spell
shriyatyagii Jun 22, 2022
5a7dd18
Added fallback and addNewRoutes func
shriyatyagii Jun 22, 2022
b2b93bb
added routeToImpl mapping
shriyatyagii Jun 22, 2022
f35dc55
updated testcases
shriyatyagii Jun 22, 2022
b10b35a
Added functions to add, update and disable routes
shriyatyagii Jun 22, 2022
d19d790
minor update
shriyatyagii Jun 22, 2022
e3c0ccd
code refactored
shriyatyagii Jun 22, 2022
1a3a361
calculateFeeBPS added in implementations
shriyatyagii Jun 23, 2022
8992343
created common contracts for all chains
shriyatyagii Jun 23, 2022
7c71aad
refactored code
shriyatyagii Jun 24, 2022
1ef3d18
Added implementations in resolver contracts
shriyatyagii Jun 24, 2022
0608c27
Added admin module & func to update impls
shriyatyagii Jun 24, 2022
4b2dd21
Removed routeToFee mapping
shriyatyagii Jun 25, 2022
ec1e9ab
removed bubbleSort from main flashloan
shriyatyagii Jun 25, 2022
5b33657
updated getEnabledRoutes
shriyatyagii Jun 25, 2022
9524224
updated getEnabledRoutes + resolver fixes
shriyatyagii Jun 25, 2022
b1d6665
removed ownerStatus
shriyatyagii Jun 25, 2022
27027b1
added deleteRoute
shriyatyagii Jun 25, 2022
2d12a4c
lint added
shriyatyagii Jun 25, 2022
c3d0f1f
minor fix
shriyatyagii Jun 26, 2022
e32696e
fixes
shriyatyagii Jun 26, 2022
78c7605
deleteRoute updated
shriyatyagii Jun 26, 2022
18f9f02
timeout updated
shriyatyagii Jun 27, 2022
b929202
implToCall renamed to fallbackImplementation
shriyatyagii Jun 27, 2022
f4a2fe1
routeToImplementation renamed
shriyatyagii Jun 27, 2022
05b0bce
Update contracts/aggregator/common/main.sol
shriyatyagii Jun 27, 2022
58c5a32
Update contracts/aggregator/common/main.sol
shriyatyagii Jun 27, 2022
25acf39
contract structure changed
shriyatyagii Jun 27, 2022
f88e19f
Update contracts/aggregator/fantom/flashloan/variable.sol
shriyatyagii Jun 28, 2022
bf1d9f3
Refactored code
thrilok209 Jun 28, 2022
a3ecf21
Merge branch 'Fantom-support-FLA' into fantom-support-changes
thrilok209 Jun 28, 2022
b407e45
removed 1 file
thrilok209 Jun 28, 2022
5424260
Merge branch 'fantom-support-changes' of https://github.com/Instadapp…
thrilok209 Jun 28, 2022
9bf5170
minor change
thrilok209 Jun 28, 2022
537df13
moved onlyOwner modifier
thrilok209 Jun 28, 2022
04fe872
minor fix
shriyatyagii Jun 28, 2022
6b2c8f3
Merge pull request #75 from Instadapp/fantom-support-changes
shriyatyagii Jul 5, 2022
441fd3a
minor edits
shriyatyagii Jul 5, 2022
bbc47f8
minor fix
shriyatyagii Jul 5, 2022
9c77316
minor update
shriyatyagii Jul 5, 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
74 changes: 74 additions & 0 deletions contracts/aggregator/fantom/flashloan/helper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "../../../common/helpers.sol";
import "./variable.sol";

contract Helper is Variables, TokenHelpers, FlashloanHelpers {
using SafeERC20 for IERC20;

/**
* @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.
* @param _account account to check for, if DSA.
*/
function checkIfDsa(address _account) internal view returns (bool) {
return instaList.accountID(_account) > 0;
}

/**
* @dev Returns fee for the passed route in BPS.
* @notice Returns fee for the passed route in BPS. 1 BPS == 0.01%.
* @param _route route number for flashloan.
*/
function calculateFeeBPS(uint256 _route)
public
view
virtual
returns (uint256 BPS_)
{
bytes memory _output = Address.functionStaticCall(
routeToImplementation[_route],
msg.data,
"calculateFeeBPS-call-failed"
);
BPS_ = abi.decode(_output, (uint256));
}

/**
* @dev Function to get the list of all routes.
* @notice Function to get the list of all routes.
*/
function getRoutes() public view returns (uint256[] memory) {
return routes;
}

/**
* @dev Function to get the list of enabled routes.
* @notice Function to get the list of enabled routes.
*/
function getEnabledRoutes()
public
view
returns (uint16[] memory routesEnabled_)
{
uint256[] memory routesAll_ = getRoutes();
uint256 length = routesAll_.length;
uint256 _count = 0;

for (uint256 i = 0; i < length; i++) {
if (routeStatus[routesAll_[i]] == true) {
_count++;
}
}

routesEnabled_ = new uint16[](_count);
uint256 k = 0;

for (uint256 j = 0; j < length; j++) {
if (routeStatus[routesAll_[j]]) {
routesEnabled_[k] = uint16(routesAll_[j]);
k++;
}
}
}
}
200 changes: 200 additions & 0 deletions contracts/aggregator/fantom/flashloan/main.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "./helper.sol";

/**
* @title Flashloan.
* @dev Flashloan aggregator for Fantom.
*/

contract AdminModule is Helper {
using SafeERC20 for IERC20;

event updateOwnerLog(address indexed oldOwner, address indexed newOwner);

event updateWhitelistLog(
address indexed account,
bool indexed isWhitelisted_
);

event LogCollectRevenue(address to, address[] tokens, uint256[] amounts);

/**
* @dev Update owner.
* @notice Update owner.
* @param newOwner_ address of new owner.
*/
function updateOwner(address newOwner_) external onlyOwner {
address oldOwner_ = owner;
owner = newOwner_;
emit updateOwnerLog(oldOwner_, newOwner_);
}

/**
* @dev Function to add new routes.
* @notice Function to add new routes and implementations.
* @param _routes routes to add.
* @param _impls implementations of their respective routes.
*/
function addNewRoutesAndEnable(
uint256[] memory _routes,
address[] memory _impls
) public onlyOwner {
require(_routes.length == _impls.length, "lengths-dont-match");
uint256 length = _routes.length;
for (uint256 i = 0; i < length; i++) {
require(
routeToImplementation[_routes[i]] == address(0),
"route-already-exists"
);
routeToImplementation[_routes[i]] = _impls[i];
routeStatus[_routes[i]] = true;
routes.push(_routes[i]);
}
}

/**
* @dev Function to update existing routes.
* @notice Function to update existing routes and implementations.
* @param _routes routes to update.
* @param _impls implementations of their respective routes.
*/
function updateRouteImplementations(
uint256[] memory _routes,
address[] memory _impls
) public onlyOwner {
require(_routes.length == _impls.length, "lengths-dont-match");
uint256 length = _routes.length;
for (uint256 i = 0; i < length; i++) {
require(
routeToImplementation[_routes[i]] != address(0),
"route-doesnt-exist"
);
routeToImplementation[_routes[i]] = _impls[i];
}
}

/**
* @dev Function to change route status.
* @notice Function to enable and disable routes.
* @param _routes routes those status we want to change.
* @param _statuses new statuses.
*/
function changeRouteStatus(
uint256[] memory _routes,
bool[] memory _statuses
) public onlyOwner {
require(_routes.length == _statuses.length, "lengths-dont-match");
uint256 length = _routes.length;
for (uint256 i = 0; i < length; i++) {
routeStatus[_routes[i]] = _statuses[i];
}
}

/**
* @dev Function to delete route.
* @notice Function to delete route.
* @param _route routes to delete.
*/
function deleteRoute(uint256 _route) public onlyOwner {
uint256 length = routes.length;
for (uint256 i = 0; i < length; i++) {
if (routes[i] == _route) {
routes[i] = routes[length - 1];
routes.pop();
delete routeToImplementation[_route];
delete routeStatus[_route];
}
}
}

/**
* @dev Function to transfer fee to the treasury.
* @notice Function to transfer fee to the treasury. Will be called manually.
* @param _tokens token addresses for transferring fee to treasury.
* @param _to treasury address.
*/
function transferFee(address[] memory _tokens, address _to)
public
onlyOwner
{
uint256[] memory _amts = new uint256[](_tokens.length);
for (uint256 i = 0; i < _tokens.length; i++) {
IERC20 token_ = IERC20(_tokens[i]);
uint256 decimals_ = TokenInterface(_tokens[i]).decimals();
uint256 amtToSub_ = decimals_ == 18 ? 1e10 : decimals_ > 12
? 10000
: decimals_ > 7
? 100
: 10;
_amts[i] = token_.balanceOf(address(this)) > amtToSub_
? (token_.balanceOf(address(this)) - amtToSub_)
: 0;
if (_amts[i] > 0) token_.safeTransfer(_to, _amts[i]);
}
emit LogCollectRevenue(_to, _tokens, _amts);
}
}

contract FlashAggregatorFantom is AdminModule {
event LogFlashloan(address indexed account, uint256 indexed route, address[] tokens, uint256[] amounts);

/**
* @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 calldata // kept for future use by instadapp. Currently not used anywhere.
) external {
require(_tokens.length == _amounts.length, "array-lengths-not-same");
require(routeStatus[_route] == true, "route-disabled");

fallbackImplementation = routeToImplementation[_route];

Address.functionDelegateCall(
fallbackImplementation,
msg.data,
"call-to-impl-failed"
);

delete fallbackImplementation;
emit LogFlashloan(msg.sender, _route, _tokens, _amounts);
}
}

contract InstaFlashAggregatorFantom is FlashAggregatorFantom {
function initialize(
address owner_,
address aave_,
address fla_
) public {
require(status == 0, "cannot-call-again");
owner = owner_;
status = 1;
routeToImplementation[9] = aave_;
routeStatus[9] = true;
routes.push(9);
routeToImplementation[10] = fla_;
routeStatus[10] = true;
routes.push(10);
}

// Fallback function
fallback(bytes calldata input) external payable returns (bytes memory output) {
Vaibhav20201 marked this conversation as resolved.
Show resolved Hide resolved
output = Address.functionDelegateCall(
fallbackImplementation,
input,
"fallback-impl-call-failed"
);
}

receive() external payable {}
}
Loading