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

Update sendMessageToL2 function and add AaveStarknetBridgeUpgradePayload contract #135

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 31 additions & 0 deletions contracts/l1/AaveStarknetBridgeUpgradePayload.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.8.0;

import {IBridge} from "./interfaces/IBridge.sol";
import {ITransparentUpgradeableProxy} from "./interfaces/IProxy.sol";

/**
* @title AaveStarknetBridgeUpgradePayload
* @author Aave on Starknet
* @notice Aave governance proposal payload, upgrading the Aave <> Starknet Aave v2 Ethereum Bridge on Ethereum side
*/
contract AaveStarknetBridgeUpgradePayload {
IBridge public constant L1_BRIDGE_NEW_IMPLEMENTATION = IBridge(address(0));
ITransparentUpgradeableProxy public constant L1_BRIDGE_PROXY =
ITransparentUpgradeableProxy(
0x25c0667E46a704AfCF5305B0A586CC24c171E94D
);

function execute() external {
try
L1_BRIDGE_PROXY.upgradeToAndCall(
address(L1_BRIDGE_NEW_IMPLEMENTATION),
abi.encodeWithSelector(
L1_BRIDGE_NEW_IMPLEMENTATION.initialize.selector
)
)
{} catch (bytes memory) {
// Do nothing.
}
}
}
84 changes: 49 additions & 35 deletions contracts/l1/Bridge.sol
kyzia551 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract Bridge is IBridge, Initializable {
IERC20 public _rewardToken;
IAaveIncentivesController public _incentivesController;
mapping(address => ATokenData) public _aTokenData;
uint256 public constant BRIDGE_REVISION = 0x1;
uint256 public constant BRIDGE_REVISION = 0x2;

/**
* @dev Only valid l2 token addresses will can be approved if the function is marked by this modifier.
Expand All @@ -50,25 +50,7 @@ contract Bridge is IBridge, Initializable {
}

/// @inheritdoc IBridge
function initialize(
uint256 l2Bridge,
address messagingContract,
address incentivesController,
address[] calldata l1Tokens,
uint256[] calldata l2Tokens,
uint256[] calldata ceilings
) external virtual onlyValidL2Address(l2Bridge) initializer {
require(
address(incentivesController) != address(0),
Errors.B_INVALID_INCENTIVES_CONTROLLER_ADDRESS
);
_messagingContract = IStarknetMessaging(messagingContract);
_l2Bridge = l2Bridge;
_incentivesController = IAaveIncentivesController(incentivesController);
_rewardToken = IERC20(_incentivesController.REWARD_TOKEN());

_approveBridgeTokens(l1Tokens, l2Tokens, ceilings);
}
function initialize() external virtual initializer {}

/// @inheritdoc IBridge
function deposit(
Expand All @@ -77,7 +59,13 @@ contract Bridge is IBridge, Initializable {
uint256 amount,
uint16 referralCode,
bool fromUnderlyingAsset
) external override onlyValidL2Address(l2Recipient) returns (uint256) {
)
external
payable
override
onlyValidL2Address(l2Recipient)
returns (uint256)
{
require(
IERC20(l1AToken).balanceOf(address(this)) + amount <=
_aTokenData[l1AToken].ceiling,
Expand Down Expand Up @@ -117,14 +105,14 @@ contract Bridge is IBridge, Initializable {
address(underlyingAsset),
lendingPool
);
uint256 l2MsgNonce = _messagingContract.l1ToL2MessageNonce();
_sendDepositMessage(
uint256 l2MsgNonce = _sendDepositMessage(
l1AToken,
msg.sender,
l2Recipient,
staticAmount,
block.number,
rewardsIndex
rewardsIndex,
msg.value
);
emit Deposit(
msg.sender,
Expand All @@ -147,7 +135,7 @@ contract Bridge is IBridge, Initializable {
uint256 staticAmount,
uint256 l2RewardsIndex,
bool toUnderlyingAsset
) external override {
) external payable override {
require(recipient != address(0), Errors.B_INVALID_ADDRESS);
require(staticAmount > 0, Errors.B_INSUFFICIENT_AMOUNT);

Expand Down Expand Up @@ -186,7 +174,8 @@ contract Bridge is IBridge, Initializable {
l1AToken,
msg.sender,
block.number,
l1CurrentRewardsIndex
l1CurrentRewardsIndex,
msg.value
);

emit L2StateUpdated(l1AToken, l1CurrentRewardsIndex);
Expand All @@ -205,14 +194,15 @@ contract Bridge is IBridge, Initializable {
}

/// @inheritdoc IBridge
function updateL2State(address l1AToken) external override {
function updateL2State(address l1AToken) external payable override {
uint256 rewardsIndex = _getCurrentRewardsIndex(l1AToken);

_sendIndexUpdateMessage(
l1AToken,
msg.sender,
block.number,
rewardsIndex
rewardsIndex,
msg.value
);

emit L2StateUpdated(l1AToken, rewardsIndex);
Expand Down Expand Up @@ -302,8 +292,9 @@ contract Bridge is IBridge, Initializable {
uint256 l2Recipient,
uint256 amount,
uint256 blockNumber,
uint256 currentRewardsIndex
) internal {
uint256 currentRewardsIndex,
uint256 fee
) internal returns (uint256) {
uint256[] memory payload = new uint256[](9);
payload[0] = uint256(uint160(from));
payload[1] = l2Recipient;
Expand All @@ -312,32 +303,55 @@ contract Bridge is IBridge, Initializable {
(payload[5], payload[6]) = Cairo.toSplitUint(blockNumber);
(payload[7], payload[8]) = Cairo.toSplitUint(currentRewardsIndex);

_messagingContract.sendMessageToL2(
uint256 nonce;
(, nonce) = _safeSendMessageToL2(
_l2Bridge,
Cairo.DEPOSIT_HANDLER,
payload
payload,
fee
);
return nonce;
}

function _sendIndexUpdateMessage(
address l1Token,
address from,
uint256 blockNumber,
uint256 currentRewardsIndex
uint256 currentRewardsIndex,
uint256 fee
) internal {
uint256[] memory payload = new uint256[](6);
payload[0] = uint256(uint160(from));
payload[1] = _aTokenData[l1Token].l2TokenAddress;
(payload[2], payload[3]) = Cairo.toSplitUint(blockNumber);
(payload[4], payload[5]) = Cairo.toSplitUint(currentRewardsIndex);

_messagingContract.sendMessageToL2(
_safeSendMessageToL2(
_l2Bridge,
Cairo.INDEX_UPDATE_HANDLER,
payload
payload,
fee
);
}

function _safeSendMessageToL2(
uint256 to,
uint256 selector,
uint256[] memory payload,
uint256 fee
) internal returns (bytes32, uint256) {
require(
fee < 0.1 ether,
"Fee to send a message to Starknet can not be higher than 0.1 ETH."
);
return
_messagingContract.sendMessageToL2{value: fee}(
to,
selector,
payload
);
}

function _consumeMessage(
address l1Token,
uint256 l2sender,
Expand Down
13 changes: 7 additions & 6 deletions contracts/l1/governance/CrosschainForwarderStarknet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ interface StarkNetLike {
uint256 to,
uint256 selector,
uint256[] calldata payload
) external returns (bytes32);
) external payable returns (bytes32, uint256);

function consumeMessageFromL2(uint256 from, uint256[] calldata payload)
external
returns (bytes32);
function consumeMessageFromL2(
uint256 from,
uint256[] calldata payload
) external returns (bytes32);

function startL1ToL2MessageCancellation(
uint256 toAddress,
Expand Down Expand Up @@ -39,10 +40,10 @@ contract CrosschainForwarderStarknet {
l2GovernanceRelay = _l2GovernanceRelay;
}

function execute(uint256 spell) public {
function execute(uint256 spell) external payable {
uint256[] memory payload = new uint256[](1);
payload[0] = spell;
StarkNetLike(starkNet).sendMessageToL2(
StarkNetLike(starkNet).sendMessageToL2{value: msg.value}(
l2GovernanceRelay,
RELAY_SELECTOR,
payload
Expand Down
22 changes: 5 additions & 17 deletions contracts/l1/interfaces/IBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,9 @@ interface IBridge {
/**
* @notice Initializes the Bridge
* @dev Function is invoked by the proxy contract when the bridge contract is added
* @param l2Bridge L2 bridge address
* @param messagingContract Starknet messaging contract address
* @param incentivesController Address of Aave IncentivesController
* @param l1Tokens Array of l1 tokens
* @param l2Tokens Array of l2 tokens
* @param ceilings Array of max amount that can be bridged for each aToken without taking into account the interest growth
* @dev This function is empty since contract has been already initialized in a previous version.
**/
function initialize(
uint256 l2Bridge,
address messagingContract,
address incentivesController,
address[] calldata l1Tokens,
uint256[] calldata l2Tokens,
uint256[] calldata ceilings
) external;
function initialize() external;

/**
* @notice allows deposits of aTokens or their underlying assets on L2
Expand All @@ -84,7 +72,7 @@ interface IBridge {
uint256 amount,
uint16 referralCode,
bool fromUnderlyingAsset
) external returns (uint256);
) external payable returns (uint256);

/**
* @notice allows withdraw of aTokens or their underlying assets from L2
Expand All @@ -101,7 +89,7 @@ interface IBridge {
uint256 staticAmount,
uint256 l2RewardsIndex,
bool toUnderlyingAsset
) external;
) external payable;

/**
* @notice Returns bridge's available rewards
Expand All @@ -126,7 +114,7 @@ interface IBridge {
* @notice updates the rewards index of tokens on l2
* @param l1AToken aToken address
**/
function updateL2State(address l1AToken) external;
function updateL2State(address l1AToken) external payable;

/**
* @notice starts AToken deposit cancellation if unsuccessful
Expand Down
2 changes: 1 addition & 1 deletion contracts/l1/interfaces/ICrosschainForwarderStarknet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
pragma solidity ^0.8.0;

interface ICrosschainForwarderStarknet {
function execute(uint256 spell) external;
function execute(uint256 spell) external payable;
}
57 changes: 57 additions & 0 deletions contracts/l1/interfaces/IProxy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface ITransparentUpgradeableProxy {
/**
* @dev Returns the current admin.
*
* NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyAdmin}.
*
* TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
* https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
* `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
*/
function admin() external payable returns (address admin_);

/**
* @dev Returns the current implementation.
*
* NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyImplementation}.
*
* TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
* https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
* `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`
*/
function implementation()
external
payable
returns (address implementation_);

/**
* @dev Changes the admin of the proxy.
*
* Emits an {AdminChanged} event.
*
* NOTE: Only the admin can call this function. See {ProxyAdmin-changeProxyAdmin}.
*/
function changeAdmin(address newAdmin) external payable;

/**
* @dev Upgrade the implementation of the proxy.
*
* NOTE: Only the admin can call this function. See {ProxyAdmin-upgrade}.
*/
function upgradeTo(address newImplementation) external payable;

/**
* @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified
* by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the
* proxied contract.
*
* NOTE: Only the admin can call this function. See {ProxyAdmin-upgradeAndCall}.
*/
function upgradeToAndCall(
address newImplementation,
bytes calldata data
) external payable;
}
Loading