|
| 1 | +# pragma version 0.4.0 |
| 2 | +""" |
| 3 | +@title CurveXGovTaikoBroadcaster |
| 4 | +@author Curve.Fi |
| 5 | +@license Copyright (c) Curve.Fi, 2020-2024 - all rights reserved |
| 6 | +@custom:version 0.0.1 |
| 7 | +@notice Taiko governance message broadcaster |
| 8 | +""" |
| 9 | + |
| 10 | +version: public(constant(String[8])) = "0.0.1" |
| 11 | + |
| 12 | +import contracts.Broadcaster as Broadcaster |
| 13 | + |
| 14 | +initializes: Broadcaster |
| 15 | + |
| 16 | + |
| 17 | +interface Bridge: |
| 18 | + def sendMessage(_message: Message) -> (bytes32, Message): payable |
| 19 | + |
| 20 | + |
| 21 | +event SetDestinationData: |
| 22 | + chain_id: indexed(uint256) |
| 23 | + destination_data: DestinationData |
| 24 | + |
| 25 | + |
| 26 | +struct Message: |
| 27 | + id: uint64 # Message ID whose value is automatically assigned. |
| 28 | + fee: uint64 # The max processing fee for the relayer. This fee has 3 parts: |
| 29 | + # - the fee for message calldata. |
| 30 | + # - the minimal fee reserve for general processing, excluding function call. |
| 31 | + # - the invocation fee for the function call. |
| 32 | + # Any unpaid fee will be refunded to the destOwner on the destination chain. |
| 33 | + # Note that fee must be 0 if gasLimit is 0, or large enough to make the invocation fee |
| 34 | + # non-zero. |
| 35 | + gasLimit: uint32 # gasLimit that the processMessage call must have. |
| 36 | + _from: address # The address, EOA or contract, that interacts with this bridge. |
| 37 | + # The value is automatically assigned. |
| 38 | + srcChainId: uint64 # Source chain ID whose value is automatically assigned. |
| 39 | + srcOwner: address # The owner of the message on the source chain. |
| 40 | + destChainId: uint64 # Destination chain ID where the `to` address lives. |
| 41 | + destOwner: address # The owner of the message on the destination chain. |
| 42 | + to: address # The destination address on the destination chain. |
| 43 | + value: uint256 # value to invoke on the destination chain. |
| 44 | + data: Bytes[MAX_MESSAGE_RECEIVED] # callData to invoke on the destination chain. |
| 45 | + |
| 46 | + |
| 47 | +struct DestinationData: |
| 48 | + gas_price: uint256 |
| 49 | + gas_limit: uint256 |
| 50 | + dest_owner: address # FeeCollector or Curve Vault |
| 51 | + relayer: address |
| 52 | + allow_manual_parameters: bool |
| 53 | + |
| 54 | +struct ManualParameters: |
| 55 | + dest_owner: address |
| 56 | + |
| 57 | + |
| 58 | +MAX_MESSAGE_RECEIVED: constant(uint256) = 9400 |
| 59 | + |
| 60 | +BRIDGE: public(constant(Bridge)) = Bridge(0xd60247c6848B7Ca29eDdF63AA924E53dB6Ddd8EC) |
| 61 | + |
| 62 | +destination_data: public(HashMap[uint256, DestinationData]) |
| 63 | + |
| 64 | +manual_parameters: transient(ManualParameters) |
| 65 | + |
| 66 | + |
| 67 | +@deploy |
| 68 | +def __init__(_admins: Broadcaster.AdminSet): |
| 69 | + Broadcaster.__init__(_admins) |
| 70 | + |
| 71 | +exports: Broadcaster.__interface__ |
| 72 | + |
| 73 | + |
| 74 | +@external |
| 75 | +def __default__(): |
| 76 | + pass |
| 77 | + |
| 78 | + |
| 79 | +@internal |
| 80 | +def _applied_destination_data(data: DestinationData) -> DestinationData: |
| 81 | + """ |
| 82 | + @notice Apply manual parameters |
| 83 | + """ |
| 84 | + if data.allow_manual_parameters: |
| 85 | + dest_owner: address = self.manual_parameters.dest_owner |
| 86 | + if dest_owner != empty(address): |
| 87 | + data.dest_owner = dest_owner |
| 88 | + |
| 89 | + return data |
| 90 | + |
| 91 | + |
| 92 | +@payable |
| 93 | +@external |
| 94 | +def broadcast(_chain_id: uint256, _messages: DynArray[Broadcaster.agent_lib.Message, Broadcaster.agent_lib.MAX_MESSAGES], _destination_data: DestinationData=empty(DestinationData)): |
| 95 | + """ |
| 96 | + @notice Broadcast a sequence of messages. |
| 97 | + @dev Save `depositCount` from POLYGON_ZKEVM_BRIDGE.BridgeEvent to claim message on destination chain |
| 98 | + @param _chain_id Chain ID of L2 |
| 99 | + @param _messages The sequence of messages to broadcast. |
| 100 | + @param _destination_data Specific DestinationData (self.destination_data by default) |
| 101 | + """ |
| 102 | + agent: Broadcaster.agent_lib.Agent = Broadcaster._broadcast_check(_chain_id, _messages) |
| 103 | + |
| 104 | + destination: DestinationData = _destination_data |
| 105 | + if destination.relayer == empty(address): |
| 106 | + destination = self.destination_data[_chain_id] |
| 107 | + assert destination.relayer != empty(address) |
| 108 | + |
| 109 | + data: DestinationData = self._applied_destination_data(destination) |
| 110 | + |
| 111 | + fee: uint256 = data.gas_price * data.gas_limit |
| 112 | + extcall BRIDGE.sendMessage( |
| 113 | + Message( |
| 114 | + id=0, # Message ID whose value is automatically assigned. |
| 115 | + fee=convert(fee, uint64), |
| 116 | + gasLimit=convert(data.gas_limit, uint32), |
| 117 | + _from=empty(address), # The value is automatically assigned. |
| 118 | + srcChainId=0, # Source chain ID whose value is automatically assigned. |
| 119 | + srcOwner=msg.sender, |
| 120 | + destChainId=convert(_chain_id, uint64), |
| 121 | + destOwner=data.dest_owner, |
| 122 | + to=data.relayer, |
| 123 | + value=0, |
| 124 | + data=abi_encode( |
| 125 | + abi_encode( # relay(uint256,(address,bytes)[]) |
| 126 | + agent, |
| 127 | + _messages, |
| 128 | + ), |
| 129 | + method_id=method_id("onMessageInvocation(bytes)") |
| 130 | + ), # callData to invoke on the destination chain. |
| 131 | + ), |
| 132 | + value=fee, |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +@view |
| 137 | +@external |
| 138 | +def cost(_chain_id: uint256) -> uint256: |
| 139 | + """ |
| 140 | + @notice Cost in ETH to bridge |
| 141 | + """ |
| 142 | + data: DestinationData = self.destination_data[_chain_id] |
| 143 | + return data.gas_price * data.gas_limit |
| 144 | + |
| 145 | + |
| 146 | +@external |
| 147 | +def set_manual_parameters(_manual_parameters: ManualParameters): |
| 148 | + """ |
| 149 | + @notice Set manual parameters that will be actual within current transaction |
| 150 | + """ |
| 151 | + self.manual_parameters = _manual_parameters |
| 152 | + |
| 153 | + |
| 154 | +@external |
| 155 | +def set_destination_data(_chain_id: uint256, _destination_data: DestinationData): |
| 156 | + """ |
| 157 | + @notice Set custom destination data. In order to turn off chain id set bridge=0xdead |
| 158 | + """ |
| 159 | + assert msg.sender == Broadcaster.admins.ownership |
| 160 | + self.destination_data[_chain_id] = _destination_data |
| 161 | + log SetDestinationData(_chain_id, _destination_data) |
0 commit comments