Skip to content

Instantly share code, notes, and snippets.

@atarpara
Created April 12, 2022 14:37
Show Gist options
  • Save atarpara/6395275e8ba8fa1fcd442753140d418f to your computer and use it in GitHub Desktop.
Save atarpara/6395275e8ba8fa1fcd442753140d418f to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=

// this line is added to create a gist. Empty file is not allowed.

// SPDX-License-Identifier: MIT
pragma solidity >= 0.8.7;
import {IAAVE} from "./IAAVE.sol";
import {IVault} from "./IVAULT.sol";
import {TransferHelper} from "./TransferHelper.sol";
contract FUND {
address public constant VAULT = address(0xBA12222222228d8Ba445958a75a0704d566BF2C8);
address public constant AAVE = address(0xE0fBa4Fc209b4948668006B2bE61711b7f465bAe);
event _deposit(address asset,uint256 amount,address onBehalfOf,uint16 referralCode);
event _withdraw(address asset, uint256 amount, address to);
struct _singleSwap {
IVault.SingleSwap _single;
IVault.FundManagement _fund;
uint256 _limit;
uint256 _deadline;
}
struct Withdraw{
address _atoken;
address _token;
uint256 _amount;
address _to;
}
function deposit(_singleSwap[] memory _singleswap,address _user) public {
for (uint i=0; i < _singleswap.length ; i++ ){
TransferHelper.safeTransferFrom(_singleswap[i]._single.assetIn, msg.sender, address(this), _singleswap[i]._single.amount);
TransferHelper.safeApprove(_singleswap[i]._single.assetIn, VAULT, _singleswap[i]._single.amount);
uint256 amountCalculated = IVault(VAULT).swap(_singleswap[i]._single, _singleswap[i]._fund, _singleswap[i]._limit, _singleswap[i]._deadline);
TransferHelper.safeApprove(_singleswap[i]._single.assetOut, AAVE, amountCalculated) ; //approval
IAAVE(AAVE).deposit(_singleswap[i]._single.assetOut,amountCalculated,_user,0);
emit _deposit(_singleswap[i]._single.assetOut,amountCalculated,_user,0);
}
}
function withdraw(Withdraw[] memory _with) public {
for (uint i =0 ; i < _with.length ; i++){
TransferHelper.safeTransferFrom(_with[i]._atoken , msg.sender, address(this) , _with[i]._amount);
// TransferHelper.safeApprove(_with[i]._token, AAVE, _with[i]._amount);
IAAVE(AAVE).withdraw(_with[i]._token, _with[i]._amount, _with[i]._to);
emit _withdraw(_with[i]._token, _with[i]._amount, _with[i]._to);
}
}
}
interface IAAVE {
function deposit(
address asset,
uint256 amount,
address onBehalfOf,
uint16 referralCode
) external;
function withdraw(
address asset,
uint256 amount,
address to
) external returns (uint256);
}
interface IVault{
enum SwapKind { GIVEN_IN, GIVEN_OUT }
struct SingleSwap {
bytes32 poolId;
SwapKind kind;
address assetIn;
address assetOut;
uint256 amount;
bytes userData;
}
struct FundManagement {
address sender;
bool fromInternalBalance;
address payable recipient;
bool toInternalBalance;
}
function swap(
SingleSwap memory singleSwap,
FundManagement memory funds,
uint256 limit,
uint256 deadline
) external payable returns (uint256);
}
library TransferHelper {
function safeApprove(
address token,
address to,
uint256 value
) internal {
// bytes4(keccak256(bytes('approve(address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::safeApprove: approve failed'
);
}
function safeTransfer(
address token,
address to,
uint256 value
) internal {
// bytes4(keccak256(bytes('transfer(address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::safeTransfer: transfer failed'
);
}
function safeTransferFrom(
address token,
address from,
address to,
uint256 value
) internal {
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::transferFrom: transferFrom failed'
);
}
function safeTransferETH(address to, uint256 value) internal {
(bool success, ) = to.call{value: value}(new bytes(0));
require(success, 'TransferHelper::safeTransferETH: ETH transfer failed');
}
}
@atarpara
Copy link
Author

Initial Commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment