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 2 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
26 changes: 26 additions & 0 deletions contracts/l1/AaveStarknetBridgeUpgradePayload.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 {
address public constant L1_BRIDGE_NEW_IMPLEMENTATION_ADDRESS = address(0);
ITransparentUpgradeableProxy public constant L1_BRIDGE_PROXY =
ITransparentUpgradeableProxy(
0x25c0667E46a704AfCF5305B0A586CC24c171E94D
);

function execute() external {
try
L1_BRIDGE_PROXY.upgradeTo(L1_BRIDGE_NEW_IMPLEMENTATION_ADDRESS)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put upgradeToAndCall here to update revision

{} catch (bytes memory) {
// Do nothing.
}
}
}
55 changes: 33 additions & 22 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 @@ -77,7 +77,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 +123,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 +153,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 +192,8 @@ contract Bridge is IBridge, Initializable {
l1AToken,
msg.sender,
block.number,
l1CurrentRewardsIndex
l1CurrentRewardsIndex,
msg.value
);

emit L2StateUpdated(l1AToken, l1CurrentRewardsIndex);
Expand All @@ -205,14 +212,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 +310,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,26 +321,29 @@ contract Bridge is IBridge, Initializable {
(payload[5], payload[6]) = Cairo.toSplitUint(blockNumber);
(payload[7], payload[8]) = Cairo.toSplitUint(currentRewardsIndex);

_messagingContract.sendMessageToL2(
uint256 nonce;
(, nonce) = _messagingContract.sendMessageToL2{value: fee}(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our opinion, there should be some hard limit of this fee ie, 0.1ETH, and revert in case a user sends more. Because, most likely, it can happen only in the case of some bug.

So I would wrap this logic into some internal function because it is used many times in a code.

_l2Bridge,
Cairo.DEPOSIT_HANDLER,
payload
);
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(
_messagingContract.sendMessageToL2{value: fee}(
_l2Bridge,
Cairo.INDEX_UPDATE_HANDLER,
payload
Expand Down Expand Up @@ -380,11 +392,9 @@ contract Bridge is IBridge, Initializable {
* @notice gets the latest rewards index of the given aToken on L1.
**/

function _getCurrentRewardsIndex(address l1AToken)
internal
view
returns (uint256)
{
function _getCurrentRewardsIndex(
address l1AToken
) internal view returns (uint256) {
(
uint256 index,
uint256 emissionPerSecond,
Expand All @@ -407,7 +417,7 @@ contract Bridge is IBridge, Initializable {
: block.timestamp;
uint256 timeDelta = currentTimestamp - lastUpdateTimestamp;
return
(emissionPerSecond * timeDelta * 10**uint256(18)) /
(emissionPerSecond * timeDelta * 10 ** uint256(18)) /
totalSupply +
index;
}
Expand Down Expand Up @@ -440,9 +450,10 @@ contract Bridge is IBridge, Initializable {
* @param recipient of rewards tokens
* @param rewardsAmount to be transferred to recipient
**/
function _transferRewards(address recipient, uint256 rewardsAmount)
internal
{
function _transferRewards(
address recipient,
uint256 rewardsAmount
) internal {
address self = address(this);
uint256 rewardBalance = _rewardToken.balanceOf(self);

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
6 changes: 3 additions & 3 deletions contracts/l1/interfaces/IBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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 +101,7 @@ interface IBridge {
uint256 staticAmount,
uint256 l2RewardsIndex,
bool toUnderlyingAsset
) external;
) external payable;

/**
* @notice Returns bridge's available rewards
Expand All @@ -126,7 +126,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;
}
20 changes: 10 additions & 10 deletions contracts/l1/interfaces/IStarknetMessaging.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ pragma solidity 0.8.10;
import "./IStarknetMessagingEvents.sol";

interface IStarknetMessaging is IStarknetMessagingEvents {
/**
Returns current nonce

*/
function l1ToL2MessageNonce() external returns (uint256);

/**
Sends a message to an L2 contract.
Returns the hash of the message.
This function is payable, the payed amount is the message fee.

Returns the hash of the message and the nonce of the message.
*/
function sendMessageToL2(
uint256 toAddress,
uint256 selector,
uint256[] calldata payload
) external returns (bytes32);
) external payable returns (bytes32, uint256);

/**
Consumes a message that was sent from an L2 contract.

Returns the hash of the message.
*/
function consumeMessageFromL2(
Expand All @@ -32,6 +29,7 @@ interface IStarknetMessaging is IStarknetMessagingEvents {
/**
Starts the cancellation of an L1 to L2 message.
A message can be canceled messageCancellationDelay() seconds after this function is called.

Note: This function may only be called for a message that is currently pending and the caller
must be the sender of the that message.
*/
Expand All @@ -40,16 +38,18 @@ interface IStarknetMessaging is IStarknetMessagingEvents {
uint256 selector,
uint256[] calldata payload,
uint256 nonce
) external;
) external returns (bytes32);

/**
Cancels an L1 to L2 message, this function should be called messageCancellationDelay() seconds
after the call to startL1ToL2MessageCancellation().

Note that the message fee is not refunded.
*/
function cancelL1ToL2Message(
uint256 toAddress,
uint256 selector,
uint256[] calldata payload,
uint256 nonce
) external;
) external returns (bytes32);
}
3 changes: 2 additions & 1 deletion contracts/l1/interfaces/IStarknetMessagingEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ interface IStarknetMessagingEvents {
uint256 indexed toAddress,
uint256 indexed selector,
uint256[] payload,
uint256 nonce
uint256 nonce,
uint256 fee
);

// An event that is raised when a message from L2 to L1 is consumed.
Expand Down
6 changes: 0 additions & 6 deletions scripts/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
export const LENDING_POOL = "0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9";
export const INCENTIVES_CONTROLLER_MAINNET =
"0xd784927Ff2f95ba542BfC824c8a8a98F3495f6b5";
export const INCENTIVES_CONTROLLER_GOERLI =
"0x8b68bBf62c8DDE10868Fe531aA8c3cB1B55Cbe7b";
export const A_DAI = "0x028171bCA77440897B824Ca71D1c56caC55b68A3";
export const A_USDC = "0xBcca60bB61934080951369a648Fb03DF4F96263C";
export const A_USDT = "0x3Ed3B47Dd13EC9a98b44e6204A523E766B225811";
Expand All @@ -12,14 +10,10 @@ export const DAI_WHALE = "0xe78388b4ce79068e89bf8aa7f218ef6b9ab0e9d0";
export const USDC_WHALE = "0x47ac0fb4f2d84898e4d9e7b4dab3c24507a6d503";
export const STARKNET_MESSAGING_CONTRACT_MAINNET =
"0xc662c410C0ECf747543f5bA90660f6ABeBD9C8c4";
export const STARKNET_MESSAGING_CONTRACT_GOERLI =
"0xde29d060D45901Fb19ED6C6e959EB22d8626708e";
export const EMISSION_MANAGER = "0xEE56e2B3D491590B5b31738cC34d5232F378a8D5";
export const DAI = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
export const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
export const TRANSPARENT_PROXY_FACTORY_MAINNET =
"0xc354ce29aa85e864e55277ef47fc6a92532dd6ca";
export const TRANSPARENT_PROXY_FACTORY_GOERLI =
"0x323b732db732ac1316a77345a360fb2751b3bafd";
export const AAVE_SHORT_EXECUTOR_MAINNET =
"0xee56e2b3d491590b5b31738cc34d5232f378a8d5";
Loading