Skip to content

Commit

Permalink
Merge pull request #6 from router-protocol/feat/liquidStaking
Browse files Browse the repository at this point in the history
Feat/liquid staking
  • Loading branch information
Shivam78288 authored Dec 22, 2023
2 parents 656b37d + b886fa0 commit dd36d31
Show file tree
Hide file tree
Showing 49 changed files with 3,669 additions and 26 deletions.
14 changes: 9 additions & 5 deletions contracts/intent-adapters/liquid-staking/Ankr/AnkrStakeAvax.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ contract AnkrStakeAvax is RouterIntentAdapter, NitroMessageHandler {
address private immutable _ankrAvax;
IAnkrStakeAvax private immutable _ankrPool;

event AnkrStakeAvaxDest(address _recipient, uint256 _amount, uint256 _returnAmount);
event AnkrStakeAvaxDest(
address _recipient,
uint256 _amount,
uint256 _returnAmount
);

constructor(
address __native,
Expand Down Expand Up @@ -112,10 +116,10 @@ contract AnkrStakeAvax is RouterIntentAdapter, NitroMessageHandler {
) internal returns (address[] memory tokens, bytes memory logData) {
_ankrPool.stakeAndClaimCerts{value: _amount}();
uint256 returnAmount = withdrawTokens(
_ankrAvax,
_recipient,
type(uint256).max
);
_ankrAvax,
_recipient,
type(uint256).max
);

tokens = new address[](2);
tokens[0] = native();
Expand Down
14 changes: 9 additions & 5 deletions contracts/intent-adapters/liquid-staking/Ankr/AnkrStakeBsc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ contract AnkrStakeBsc is RouterIntentAdapter, NitroMessageHandler {
address private immutable _ankrBsc;
IAnkrStakeBsc private immutable _ankrPool;

event AnkrStakeBscDest(address _recipient, uint256 _amount, uint256 _returnAmount);
event AnkrStakeBscDest(
address _recipient,
uint256 _amount,
uint256 _returnAmount
);

constructor(
address __native,
Expand Down Expand Up @@ -112,10 +116,10 @@ contract AnkrStakeBsc is RouterIntentAdapter, NitroMessageHandler {
) internal returns (address[] memory tokens, bytes memory logData) {
_ankrPool.stakeCerts{value: _amount}();
uint256 returnAmount = withdrawTokens(
_ankrBsc,
_recipient,
type(uint256).max
);
_ankrBsc,
_recipient,
type(uint256).max
);

tokens = new address[](2);
tokens[0] = native();
Expand Down
14 changes: 9 additions & 5 deletions contracts/intent-adapters/liquid-staking/Ankr/AnkrStakeEth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ contract AnkrStakeEth is RouterIntentAdapter, NitroMessageHandler {
address private immutable _ankrEth;
IAnkrStakeEth private immutable _ankrPool;

event AnkrStakeEthDest(address _recipient, uint256 _amount, uint256 _returnAmount);
event AnkrStakeEthDest(
address _recipient,
uint256 _amount,
uint256 _returnAmount
);

constructor(
address __native,
Expand Down Expand Up @@ -112,10 +116,10 @@ contract AnkrStakeEth is RouterIntentAdapter, NitroMessageHandler {
) internal returns (address[] memory tokens, bytes memory logData) {
_ankrPool.stake{value: _amount}();
uint256 returnAmount = withdrawTokens(
_ankrEth,
_recipient,
type(uint256).max
);
_ankrEth,
_recipient,
type(uint256).max
);

tokens = new address[](2);
tokens[0] = native();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ contract AnkrStakeFantom is RouterIntentAdapter, NitroMessageHandler {
address private immutable _ankrFtm;
IAnkrStakeFtm private immutable _ankrPool;

event AnkrStakeFantomDest(address _recipient, uint256 _amount, uint256 _returnAmount);
event AnkrStakeFantomDest(
address _recipient,
uint256 _amount,
uint256 _returnAmount
);

constructor(
address __native,
Expand Down Expand Up @@ -112,10 +116,10 @@ contract AnkrStakeFantom is RouterIntentAdapter, NitroMessageHandler {
) internal returns (address[] memory tokens, bytes memory logData) {
_ankrPool.stakeAndClaimCerts{value: _amount}();
uint256 returnAmount = withdrawTokens(
_ankrFtm,
_recipient,
type(uint256).max
);
_ankrFtm,
_recipient,
type(uint256).max
);

tokens = new address[](2);
tokens[0] = native();
Expand Down
14 changes: 9 additions & 5 deletions contracts/intent-adapters/liquid-staking/Ankr/AnkrStakeMatic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ contract AnkrStakeMatic is RouterIntentAdapter, NitroMessageHandler {
address private immutable _matic;
IAnkrStakeMatic private immutable _ankrPool;

event AnkrStakeMaticDest(address _recipient, uint256 _amount, uint256 _returnAmount);
event AnkrStakeMaticDest(
address _recipient,
uint256 _amount,
uint256 _returnAmount
);

constructor(
address __native,
Expand Down Expand Up @@ -118,10 +122,10 @@ contract AnkrStakeMatic is RouterIntentAdapter, NitroMessageHandler {
IERC20(_matic).safeIncreaseAllowance(address(_ankrPool), _amount);
_ankrPool.stakeAndClaimCerts(_amount);
uint256 returnAmount = withdrawTokens(
_ankrMatic,
_recipient,
type(uint256).max
);
_ankrMatic,
_recipient,
type(uint256).max
);

tokens = new address[](2);
tokens[0] = native();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ interface IAnkrStakePolygon {
uint256 amountIn,
address receiver
) external payable returns (uint256 amountOut);
}
}
136 changes: 136 additions & 0 deletions contracts/intent-adapters/liquid-staking/Benqi/BenqiStakeAvax.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

import {IBenqiStakeAvax} from "./Interfaces.sol";
import {RouterIntentAdapter, Errors} from "router-intents/contracts/RouterIntentAdapter.sol";
import {NitroMessageHandler} from "router-intents/contracts/NitroMessageHandler.sol";
import {IERC20, SafeERC20} from "../../../utils/SafeERC20.sol";

/**
* @title BenqiStakeAvax
* @author Yashika Goyal
* @notice Staking AVAX to receive BenqiAVAX on Benqi.
* @notice This contract is only for Avalanche chain.
*/
contract BenqiStakeAvax is RouterIntentAdapter, NitroMessageHandler {
using SafeERC20 for IERC20;

address private immutable _benqiSAvax;

event BenqiStakeAvaxDest(
address _recipient,
uint256 _amount,
uint256 _receivedSAvax
);

constructor(
address __native,
address __wnative,
address __owner,
address __assetForwarder,
address __dexspan,
address __benqiSAvax
)
RouterIntentAdapter(__native, __wnative, __owner)
NitroMessageHandler(__assetForwarder, __dexspan)
{
_benqiSAvax = __benqiSAvax;
}

function benqiSAvax() public view returns (address) {
return _benqiSAvax;
}

function name() public pure override returns (string memory) {
return "BenqiStakeAvax";
}

/**
* @inheritdoc RouterIntentAdapter
*/
function execute(
address,
address,
bytes calldata data
) external payable override returns (address[] memory tokens) {
(address _recipient, uint256 _amount) = parseInputs(data);

// If the adapter is called using `call` and not `delegatecall`
if (address(this) == self()) {
require(
msg.value == _amount,
Errors.INSUFFICIENT_NATIVE_FUNDS_PASSED
);
}

bytes memory logData;

(tokens, logData) = _stake(_recipient, _amount);

emit ExecutionEvent(name(), logData);
return tokens;
}

/**
* @inheritdoc NitroMessageHandler
*/
function handleMessage(
address tokenSent,
uint256 amount,
bytes memory instruction
) external override onlyNitro nonReentrant {
address recipient = abi.decode(instruction, (address));

if (tokenSent != native()) {
withdrawTokens(tokenSent, recipient, amount);
emit OperationFailedRefundEvent(tokenSent, recipient, amount);
return;
}

try IBenqiStakeAvax(_benqiSAvax).submit{value: amount}() {
uint256 _receivedSAvax = withdrawTokens(
_benqiSAvax,
recipient,
type(uint256).max
);

emit BenqiStakeAvaxDest(recipient, amount, _receivedSAvax);
} catch {
withdrawTokens(tokenSent, recipient, amount);
emit OperationFailedRefundEvent(tokenSent, recipient, amount);
}
}

//////////////////////////// ACTION LOGIC ////////////////////////////

function _stake(
address _recipient,
uint256 _amount
) internal returns (address[] memory tokens, bytes memory logData) {
IBenqiStakeAvax(_benqiSAvax).submit{value: _amount}();
uint256 _receivedSAvax = withdrawTokens(
_benqiSAvax,
_recipient,
type(uint256).max
);

tokens = new address[](2);
tokens[0] = native();
tokens[1] = benqiSAvax();

logData = abi.encode(_recipient, _amount, _receivedSAvax);
}

/**
* @dev function to parse input data.
* @param data input data.
*/
function parseInputs(
bytes memory data
) public pure returns (address, uint256) {
return abi.decode(data, (address, uint256));
}

// solhint-disable-next-line no-empty-blocks
receive() external payable {}
}
6 changes: 6 additions & 0 deletions contracts/intent-adapters/liquid-staking/Benqi/Interfaces.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

interface IBenqiStakeAvax {
function submit() external payable returns (uint);
}
Loading

0 comments on commit dd36d31

Please sign in to comment.