|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import { ERC20 } from '@openzeppelin/contracts/token/ERC20/ERC20.sol'; |
| 5 | +import { Ownable } from '@openzeppelin/contracts/access/Ownable.sol'; |
| 6 | +import { IMessageVerification } from '@matterlabs/zksync-contracts/contracts/l1-contracts/state-transition/chain-interfaces/IMessageVerification.sol'; |
| 7 | +import { L2Message } from '@matterlabs/zksync-contracts/contracts/l1-contracts/common/Messaging.sol'; |
| 8 | + |
| 9 | +contract InteropToken is ERC20, Ownable { |
| 10 | + // mapping of chain IDs to the approved staking contract address |
| 11 | + mapping(uint256 => address) public approvedStakingContracts; |
| 12 | + |
| 13 | + // mapping of addresses to if they have minted tokens or not |
| 14 | + mapping(address => bool) public addressesThatMinted; |
| 15 | + |
| 16 | + // mapping of chain IDs to number of adresses that have minted some tokens |
| 17 | + mapping(uint256 => uint256) public rewardsByChain; |
| 18 | + |
| 19 | + // the chain ID with the most rewards |
| 20 | + uint256 public winningChainId; |
| 21 | + address constant L2_MESSAGE_VERIFICATION_ADDRESS = 0x0000000000000000000000000000000000010009; |
| 22 | + |
| 23 | + IMessageVerification public l2MessageVerifier = IMessageVerification(L2_MESSAGE_VERIFICATION_ADDRESS); |
| 24 | + |
| 25 | + constructor( |
| 26 | + uint256[] memory _chainIds, |
| 27 | + address[] memory _stakingContracts |
| 28 | + ) ERC20('InteropToken', 'INTR') Ownable(msg.sender) { |
| 29 | + require(_chainIds.length == _stakingContracts.length, 'Length mismatch'); |
| 30 | + for (uint256 i = 0; i < _chainIds.length; ++i) { |
| 31 | + require(_stakingContracts[i] != address(0), 'Zero address used'); |
| 32 | + require(approvedStakingContracts[_chainIds[i]] == address(0), 'Staking contract address already set'); |
| 33 | + approvedStakingContracts[_chainIds[i]] = _stakingContracts[i]; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + function mint( |
| 38 | + uint256 _sourceChainId, |
| 39 | + uint256 _l1BatchNumber, |
| 40 | + uint256 _l2MessageIndex, |
| 41 | + L2Message calldata _l2MessageData, |
| 42 | + bytes32[] calldata _proof |
| 43 | + ) external { |
| 44 | + // token can only be minted once per address |
| 45 | + require(addressesThatMinted[msg.sender] == false, 'Token already minted'); |
| 46 | + require(approvedStakingContracts[_sourceChainId] == _l2MessageData.sender, 'Message origin not approved'); |
| 47 | + bool result = checkMessageProof(_sourceChainId, _l1BatchNumber, _l2MessageIndex, _l2MessageData, _proof); |
| 48 | + require(result == true, 'Message not verified'); |
| 49 | + address sender = abi.decode(_l2MessageData.data, (address)); |
| 50 | + require(sender == msg.sender, 'Message sender is not depositor'); |
| 51 | + rewardsByChain[_sourceChainId]++; |
| 52 | + if (rewardsByChain[_sourceChainId] > rewardsByChain[winningChainId]) { |
| 53 | + winningChainId = _sourceChainId; |
| 54 | + } |
| 55 | + addressesThatMinted[msg.sender] = true; |
| 56 | + _mint(msg.sender, 1); |
| 57 | + } |
| 58 | + |
| 59 | + function checkMessageProof( |
| 60 | + uint256 _sourceChainId, |
| 61 | + uint256 _l1BatchNumber, |
| 62 | + uint256 _l2MessageIndex, |
| 63 | + L2Message calldata _l2MessageData, |
| 64 | + bytes32[] calldata _proof |
| 65 | + ) public view returns (bool) { |
| 66 | + bool result = l2MessageVerifier.proveL2MessageInclusionShared( |
| 67 | + _sourceChainId, |
| 68 | + _l1BatchNumber, |
| 69 | + _l2MessageIndex, |
| 70 | + _l2MessageData, |
| 71 | + _proof |
| 72 | + ); |
| 73 | + return result; |
| 74 | + } |
| 75 | +} |
0 commit comments