Skip to content

Instantly share code, notes, and snippets.

@ioanSL
Last active April 28, 2023 10:15
Show Gist options
  • Save ioanSL/17494077ce133a25e7879bf2a0fbc717 to your computer and use it in GitHub Desktop.
Save ioanSL/17494077ce133a25e7879bf2a0fbc717 to your computer and use it in GitHub Desktop.
L1 smart contract to recursively receive L2 messages and update user's balance.
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.8.17;
interface IStarknetMessaging {
/**
Consumes a message that was sent from an L2 contract.
Returns the hash of the message.
*/
function consumeMessageFromL2(uint256 fromAddress, uint256[] calldata payload)
external
returns (bytes32);
}
contract L1L2 {
// The StarkNet core contract.
address public starknet_address;
IStarknetMessaging starknetCore;
mapping(uint256 => uint256) public userBalances;
constructor() {
starknet_address = 0x70c8A579aD08339cCA19d77d8646F4b6f0fd098A;
starknetCore = IStarknetMessaging(starknet_address);
}
function withdraw(
uint256 l2ContractAddress,
uint256[] calldata payload
) external {
// Consume the message from the StarkNet core contract.
// This will revert the (Ethereum) transaction if the message does not exist.
starknetCore.consumeMessageFromL2(l2ContractAddress, payload);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment