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 40 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
184 changes: 184 additions & 0 deletions contracts/aggregator/common/helpers.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import './variables.sol';

contract HelpersCommon is VariablesCommon {
using SafeERC20 for IERC20;

/**
* @dev Approves the token to the spender address with allowance amount.
* @notice Approves the token to the spender address with allowance amount.
* @param token_ token for which allowance is to be given.
* @param spender_ the address to which the allowance is to be given.
* @param amount_ amount of token.
*/
function approve(
address token_,
address spender_,
uint256 amount_
) internal {
TokenInterface tokenContract_ = TokenInterface(token_);
try tokenContract_.approve(spender_, amount_) {} catch {
IERC20 token = IERC20(token_);
token.safeApprove(spender_, 0);
token.safeApprove(spender_, amount_);
}
}

/**
* @dev Approves the tokens to the receiver address with allowance (amount + fee).
* @notice Approves the tokens to the receiver address with allowance (amount + fee).
* @param _instaLoanVariables struct which includes list of token addresses and amounts.
* @param _fees list of premiums/fees for the corresponding addresses for flashloan.
* @param _receiver address to which tokens have to be approved.
*/
function safeApprove(
FlashloanVariables memory _instaLoanVariables,
uint256[] memory _fees,
address _receiver
) internal {
uint256 length_ = _instaLoanVariables._tokens.length;
require(length_ == _instaLoanVariables._amounts.length, 'Lengths of parameters not same');
require(length_ == _fees.length, 'Lengths of parameters not same');
for (uint256 i = 0; i < length_; i++) {
approve(_instaLoanVariables._tokens[i], _receiver, _instaLoanVariables._amounts[i] + _fees[i]);
}
}

/**
* @dev Transfers the tokens to the receiver address.
* @notice Transfers the tokens to the receiver address.
* @param _instaLoanVariables struct which includes list of token addresses and amounts.
* @param _receiver address to which tokens have to be transferred.
*/
function safeTransfer(FlashloanVariables memory _instaLoanVariables, address _receiver) internal {
uint256 length_ = _instaLoanVariables._tokens.length;
require(length_ == _instaLoanVariables._amounts.length, 'Lengths of parameters not same');
for (uint256 i = 0; i < length_; i++) {
IERC20 token = IERC20(_instaLoanVariables._tokens[i]);
token.safeTransfer(_receiver, _instaLoanVariables._amounts[i]);
}
}

/**
* @dev Validates if the receiver sent the correct amounts of funds.
* @notice Validates if the receiver sent the correct amounts of funds.
* @param _instaLoanVariables struct which includes list of initial balances, final balances and fees for the respective tokens.
*/
function validateFlashloan(FlashloanVariables memory _instaLoanVariables) internal pure {
for (uint256 i = 0; i < _instaLoanVariables._iniBals.length; i++) {
require(
_instaLoanVariables._iniBals[i] + _instaLoanVariables._instaFees[i] <= _instaLoanVariables._finBals[i],
'amount-paid-less'
);
}
}

/**
* @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 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 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 Sort the tokens and amounts arrays according to token addresses.
* @notice Sort the tokens and amounts arrays according to token addresses.
* @param _token0 address of token0.
* @param _token1 address of token1.
*/
function sortTokens(address _token0, address _token1) internal pure returns (address, address) {
if (_token1 < _token0) {
(_token0, _token1) = (_token1, _token0);
}
return (_token0, _token1);
}

/**
* @dev better checking by double encoding the data.
* @notice better checking by double encoding the data.
* @param data_ data passed.
*/
modifier verifyDataHash(bytes memory data_) {
bytes32 dataHash_ = keccak256(data_);
require(dataHash_ == dataHash && dataHash_ != bytes32(0), 'invalid-data-hash');
require(status == 2, 'already-entered');
dataHash = bytes32(0);
_;
status = 1;
}

/**
* @dev reentrancy gaurd.
* @notice reentrancy gaurd.
*/
modifier reentrancy() {
require(status == 1, 'already-entered');
status = 2;
_;
require(status == 1, 'already-entered');
}
}
45 changes: 45 additions & 0 deletions contracts/aggregator/common/interfaces.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
pragma experimental ABIEncoderV2;

interface InstaFlashReceiverInterface {
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata _data
) external returns (bool);
}

interface IndexInterface {
function master() external view returns (address);

function list() external view returns (address);
}

interface ListInterface {
function accountID(address) external view returns (uint64);
}

interface TokenInterface {
function approve(address, uint256) external;

function transfer(address, uint256) external;

function transferFrom(
address,
address,
uint256
) external;

function deposit() external payable;

function withdraw(uint256) external;

function balanceOf(address) external view returns (uint256);

function decimals() external view returns (uint256);

function totalSupply() external view returns (uint256);
}
Loading