Initial commit: add .gitignore and README
This commit is contained in:
110
contracts/examples/AaveFlashLoanReceiver.sol
Normal file
110
contracts/examples/AaveFlashLoanReceiver.sol
Normal file
@@ -0,0 +1,110 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import "../interfaces/IAavePool.sol";
|
||||
import "../interfaces/IERC20.sol";
|
||||
|
||||
/**
|
||||
* @title AaveFlashLoanReceiver
|
||||
* @notice Example flash loan receiver for Aave v3
|
||||
* @dev This contract receives flash loans and must repay them in executeOperation
|
||||
*/
|
||||
contract AaveFlashLoanReceiver is IFlashLoanReceiver {
|
||||
IAavePool public immutable pool;
|
||||
address public owner;
|
||||
|
||||
modifier onlyOwner() {
|
||||
require(msg.sender == owner, "Not owner");
|
||||
_;
|
||||
}
|
||||
|
||||
constructor(address pool_) {
|
||||
pool = IAavePool(pool_);
|
||||
owner = msg.sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Execute flash loan operation
|
||||
* @param asset The flash loaned asset
|
||||
* @param amount The flash loaned amount
|
||||
* @param premium The premium to repay
|
||||
* @param initiator The initiator of the flash loan
|
||||
* @param params Additional parameters (can encode arbitrage data, etc.)
|
||||
* @return true if successful
|
||||
*/
|
||||
function executeOperation(
|
||||
address asset,
|
||||
uint256 amount,
|
||||
uint256 premium,
|
||||
address initiator,
|
||||
bytes calldata params
|
||||
) external override returns (bool) {
|
||||
// Verify this was called by the pool
|
||||
require(msg.sender == address(pool), "Invalid caller");
|
||||
require(initiator == address(this), "Invalid initiator");
|
||||
|
||||
// Your logic here (e.g., arbitrage, liquidation, etc.)
|
||||
// Example: swap on DEX, arbitrage, etc.
|
||||
|
||||
// Calculate total amount to repay (loan + premium)
|
||||
uint256 amountOwed = amount + premium;
|
||||
|
||||
// Approve pool to take repayment
|
||||
IERC20(asset).approve(address(pool), amountOwed);
|
||||
|
||||
// Return true to indicate successful operation
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Execute flash loan (single asset)
|
||||
* @param asset The asset to flash loan
|
||||
* @param amount The amount to flash loan
|
||||
* @param params Additional parameters for executeOperation
|
||||
*/
|
||||
function flashLoanSimple(
|
||||
address asset,
|
||||
uint256 amount,
|
||||
bytes calldata params
|
||||
) external onlyOwner {
|
||||
pool.flashLoanSimple(
|
||||
address(this),
|
||||
asset,
|
||||
amount,
|
||||
params,
|
||||
0 // referral code
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Execute flash loan (multiple assets)
|
||||
* @param assets The assets to flash loan
|
||||
* @param amounts The amounts to flash loan
|
||||
* @param modes The flash loan modes (0 = no debt, 2 = variable debt)
|
||||
* @param params Additional parameters for executeOperation
|
||||
*/
|
||||
function flashLoan(
|
||||
address[] calldata assets,
|
||||
uint256[] calldata amounts,
|
||||
uint256[] calldata modes,
|
||||
bytes calldata params
|
||||
) external onlyOwner {
|
||||
pool.flashLoan(
|
||||
address(this),
|
||||
assets,
|
||||
amounts,
|
||||
modes,
|
||||
address(this),
|
||||
params,
|
||||
0 // referral code
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Withdraw tokens (emergency)
|
||||
*/
|
||||
function withdrawToken(address token, uint256 amount) external onlyOwner {
|
||||
IERC20(token).transfer(owner, amount);
|
||||
}
|
||||
}
|
||||
|
||||
79
contracts/examples/AaveSupplyBorrow.sol
Normal file
79
contracts/examples/AaveSupplyBorrow.sol
Normal file
@@ -0,0 +1,79 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import "../interfaces/IAavePool.sol";
|
||||
import "../interfaces/IERC20.sol";
|
||||
|
||||
/**
|
||||
* @title AaveSupplyBorrow
|
||||
* @notice Example contract for supplying collateral and borrowing on Aave v3
|
||||
*/
|
||||
contract AaveSupplyBorrow {
|
||||
IAavePool public immutable pool;
|
||||
|
||||
constructor(address pool_) {
|
||||
pool = IAavePool(pool_);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Supply collateral, enable as collateral, and borrow
|
||||
* @param asset The collateral asset to supply
|
||||
* @param amount The amount of collateral to supply
|
||||
* @param debtAsset The asset to borrow
|
||||
* @param borrowAmount The amount to borrow
|
||||
*/
|
||||
function supplyAndBorrow(
|
||||
address asset,
|
||||
uint256 amount,
|
||||
address debtAsset,
|
||||
uint256 borrowAmount
|
||||
) external {
|
||||
// Step 1: Transfer collateral from user
|
||||
IERC20(asset).transferFrom(msg.sender, address(this), amount);
|
||||
|
||||
// Step 2: Approve pool to take collateral
|
||||
IERC20(asset).approve(address(pool), amount);
|
||||
|
||||
// Step 3: Supply collateral
|
||||
pool.supply(asset, amount, address(this), 0);
|
||||
|
||||
// Step 4: Enable as collateral
|
||||
pool.setUserUseReserveAsCollateral(asset, true);
|
||||
|
||||
// Step 5: Borrow (variable rate = 2, stable rate is deprecated)
|
||||
pool.borrow(debtAsset, borrowAmount, 2, 0, address(this));
|
||||
|
||||
// Step 6: Transfer borrowed tokens to user
|
||||
IERC20(debtAsset).transfer(msg.sender, borrowAmount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Repay debt and withdraw collateral
|
||||
* @param debtAsset The debt asset to repay
|
||||
* @param repayAmount The amount to repay
|
||||
* @param collateralAsset The collateral asset to withdraw
|
||||
* @param withdrawAmount The amount to withdraw
|
||||
*/
|
||||
function repayAndWithdraw(
|
||||
address debtAsset,
|
||||
uint256 repayAmount,
|
||||
address collateralAsset,
|
||||
uint256 withdrawAmount
|
||||
) external {
|
||||
// Step 1: Transfer repayment tokens from user
|
||||
IERC20(debtAsset).transferFrom(msg.sender, address(this), repayAmount);
|
||||
|
||||
// Step 2: Approve pool to take repayment
|
||||
IERC20(debtAsset).approve(address(pool), repayAmount);
|
||||
|
||||
// Step 3: Repay debt (variable rate = 2)
|
||||
pool.repay(debtAsset, repayAmount, 2, address(this));
|
||||
|
||||
// Step 4: Withdraw collateral
|
||||
pool.withdraw(collateralAsset, withdrawAmount, address(this));
|
||||
|
||||
// Step 5: Transfer collateral to user
|
||||
IERC20(collateralAsset).transfer(msg.sender, withdrawAmount);
|
||||
}
|
||||
}
|
||||
|
||||
52
contracts/examples/ProtocolinkExecutor.sol
Normal file
52
contracts/examples/ProtocolinkExecutor.sol
Normal file
@@ -0,0 +1,52 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import "../interfaces/IERC20.sol";
|
||||
|
||||
interface IProtocolinkRouter {
|
||||
function execute(
|
||||
bytes calldata data
|
||||
) external payable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @title ProtocolinkExecutor
|
||||
* @notice Example contract for executing Protocolink routes
|
||||
* @dev This contract can execute Protocolink transaction plans
|
||||
*/
|
||||
contract ProtocolinkExecutor {
|
||||
IProtocolinkRouter public immutable router;
|
||||
|
||||
constructor(address router_) {
|
||||
router = IProtocolinkRouter(router_);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Execute a Protocolink route
|
||||
* @param data The encoded Protocolink route data
|
||||
*/
|
||||
function executeRoute(bytes calldata data) external payable {
|
||||
router.execute{value: msg.value}(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Execute a Protocolink route with token approvals
|
||||
* @param tokens The tokens to approve
|
||||
* @param amounts The amounts to approve
|
||||
* @param data The encoded Protocolink route data
|
||||
*/
|
||||
function executeRouteWithApprovals(
|
||||
address[] calldata tokens,
|
||||
uint256[] calldata amounts,
|
||||
bytes calldata data
|
||||
) external payable {
|
||||
// Approve tokens
|
||||
for (uint256 i = 0; i < tokens.length; i++) {
|
||||
IERC20(tokens[i]).approve(address(router), amounts[i]);
|
||||
}
|
||||
|
||||
// Execute route
|
||||
router.execute{value: msg.value}(data);
|
||||
}
|
||||
}
|
||||
|
||||
70
contracts/examples/UniswapV3Swap.sol
Normal file
70
contracts/examples/UniswapV3Swap.sol
Normal file
@@ -0,0 +1,70 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import "../interfaces/IERC20.sol";
|
||||
|
||||
interface ISwapRouter {
|
||||
struct ExactInputSingleParams {
|
||||
address tokenIn;
|
||||
address tokenOut;
|
||||
uint24 fee;
|
||||
address recipient;
|
||||
uint256 deadline;
|
||||
uint256 amountIn;
|
||||
uint256 amountOutMinimum;
|
||||
uint160 sqrtPriceLimitX96;
|
||||
}
|
||||
|
||||
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
|
||||
}
|
||||
|
||||
/**
|
||||
* @title UniswapV3Swap
|
||||
* @notice Example contract for swapping tokens on Uniswap v3
|
||||
*/
|
||||
contract UniswapV3Swap {
|
||||
ISwapRouter public immutable swapRouter;
|
||||
|
||||
constructor(address swapRouter_) {
|
||||
swapRouter = ISwapRouter(swapRouter_);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Swap tokens using Uniswap v3
|
||||
* @param tokenIn The input token
|
||||
* @param tokenOut The output token
|
||||
* @param fee The fee tier (100, 500, 3000, 10000)
|
||||
* @param amountIn The input amount
|
||||
* @param amountOutMinimum The minimum output amount (slippage protection)
|
||||
* @param deadline The transaction deadline
|
||||
*/
|
||||
function swapExactInputSingle(
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint24 fee,
|
||||
uint256 amountIn,
|
||||
uint256 amountOutMinimum,
|
||||
uint256 deadline
|
||||
) external returns (uint256 amountOut) {
|
||||
// Transfer tokens from user
|
||||
IERC20(tokenIn).transferFrom(msg.sender, address(this), amountIn);
|
||||
|
||||
// Approve router
|
||||
IERC20(tokenIn).approve(address(swapRouter), amountIn);
|
||||
|
||||
// Execute swap
|
||||
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
|
||||
tokenIn: tokenIn,
|
||||
tokenOut: tokenOut,
|
||||
fee: fee,
|
||||
recipient: msg.sender,
|
||||
deadline: deadline,
|
||||
amountIn: amountIn,
|
||||
amountOutMinimum: amountOutMinimum,
|
||||
sqrtPriceLimitX96: 0
|
||||
});
|
||||
|
||||
amountOut = swapRouter.exactInputSingle(params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user