Skip to content

Commit

Permalink
add event
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoch05 committed Jan 22, 2024
1 parent 0f048fc commit 8867e0e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
4 changes: 2 additions & 2 deletions helix-contract/address/ln-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"LnOppositeBridgeLogic": "0x08F2a6B3F8f67E6604aBb731FC318cf1f3EAaF53",
"LnOppositeBridgeProxy": "0xbA96d83E2A04c4E50F2D6D7eCA03D70bA2426e5f",
"LnV3BridgeLogic": {
"zksync": "0x93944493105771aaa13B93fcb6c9a0642118d675",
"others": "0xC4Cecb7d4c0eA6c7AA88CbdE56612Cdc2DE2E756"
"zksync": "0x67C3C81113Afb9A73d7ce5868046D97D0e44db59",
"others": "0x6719A06B38829Ddad83B0Ec5e69bf7C47b6a0E72"
},
"LnV3BridgeProxy": {
"zksync": "0xDc55fF59F82AA50D8A4A61dB8CcaDffD26Fb7dD2",
Expand Down
56 changes: 34 additions & 22 deletions helix-contract/contracts/ln/base/LnBridgeTargetV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
pragma solidity ^0.8.17;

import "../interface/ILnBridgeSourceV3.sol";
import "./LnBridgeHelper.sol";
import "../../utils/TokenTransferHelper.sol";

contract LnBridgeTargetV3 {
uint256 constant public SLASH_EXPIRE_TIME = 60 * 60;
// timestamp: the time when transfer filled, this is also the flag that the transfer is filled(relayed or slashed)
// provider: the transfer lnProvider
struct FillTransfer {
Expand Down Expand Up @@ -40,6 +41,7 @@ contract LnBridgeTargetV3 {

event TransferFilled(bytes32 transferId, address provider);
event SlashRequest(bytes32 transferId, uint256 remoteChainId, address provider, address sourceToken, address targetToken, address slasher);
event LiquidityWithdrawRequested(bytes32[] transferIds, uint256 remoteChainId);

function _sendMessageToSource(uint256 _remoteChainId, bytes memory _payload, uint256 feePrepaid, bytes memory _extParams) internal virtual {}

Expand Down Expand Up @@ -73,10 +75,10 @@ contract LnBridgeTargetV3 {

if (_params.targetToken == address(0)) {
require(msg.value == _params.targetAmount, "invalid amount");
LnBridgeHelper.safeTransferNative(_params.receiver, _params.targetAmount);
TokenTransferHelper.safeTransferNative(_params.receiver, _params.targetAmount);
} else {
require(msg.value == 0, "value not need");
LnBridgeHelper.safeTransferFrom(_params.targetToken, msg.sender, _params.receiver, uint256(_params.targetAmount));
TokenTransferHelper.safeTransferFrom(_params.targetToken, msg.sender, _params.receiver, uint256(_params.targetAmount));
}
emit TransferFilled(transferId, _params.provider);
}
Expand Down Expand Up @@ -109,24 +111,18 @@ contract LnBridgeTargetV3 {

// suppose source chain and target chain has the same block timestamp
// event the timestamp is not sync exactly, this TIMEOUT is also verified on source chain
require(_params.timestamp < block.timestamp - LnBridgeHelper.SLASH_EXPIRE_TIME, "time not expired");
require(_params.timestamp < block.timestamp - SLASH_EXPIRE_TIME, "time not expired");
fillTransfers[transferId] = FillTransfer(uint64(block.timestamp), _params.provider);
slashInfos[transferId] = SlashInfo(_params.remoteChainId, msg.sender);

if (_params.targetToken == address(0)) {
require(msg.value == _params.targetAmount + _feePrepaid, "invalid value");
LnBridgeHelper.safeTransferNative(_params.receiver, _params.targetAmount);
TokenTransferHelper.safeTransferNative(_params.receiver, _params.targetAmount);
} else {
require(msg.value == _feePrepaid, "value too large");
LnBridgeHelper.safeTransferFrom(_params.targetToken, msg.sender, _params.receiver, uint256(_params.targetAmount));
TokenTransferHelper.safeTransferFrom(_params.targetToken, msg.sender, _params.receiver, uint256(_params.targetAmount));
}
bytes memory message = abi.encodeWithSelector(
ILnBridgeSourceV3.slash.selector,
block.chainid,
transferId,
_params.provider,
msg.sender
);
bytes memory message = encodeSlashRequest(transferId, _params.provider, msg.sender);
_sendMessageToSource(_params.remoteChainId, message, _feePrepaid, _extParams);
emit SlashRequest(transferId, _params.remoteChainId, _params.provider, _params.sourceToken, _params.targetToken, msg.sender);
}
Expand All @@ -139,13 +135,7 @@ contract LnBridgeTargetV3 {
SlashInfo memory slashInfo = slashInfos[transferId];
require(slashInfo.slasher == msg.sender, "invalid slasher");
// send message
bytes memory message = abi.encodeWithSelector(
ILnBridgeSourceV3.slash.selector,
block.chainid,
transferId,
fillTransfer.provider,
slashInfo.slasher
);
bytes memory message = encodeSlashRequest(transferId, fillTransfer.provider, slashInfo.slasher);
_sendMessageToSource(slashInfo.remoteChainId, message, msg.value, _extParams);
}

Expand All @@ -163,13 +153,35 @@ contract LnBridgeTargetV3 {
// make sure that each transfer has the same provider
require(fillTransfer.provider == _provider, "provider invalid");
}
bytes memory message = abi.encodeWithSelector(
bytes memory message = encodeWithdrawLiquidityRequest(_transferIds, _provider);
_sendMessageToSource(_remoteChainId, message, msg.value, _extParams);
emit LiquidityWithdrawRequested(_transferIds, _remoteChainId);
}

function encodeWithdrawLiquidityRequest(
bytes32[] calldata _transferIds,
address _provider
) public view returns(bytes memory message) {
message = abi.encodeWithSelector(
ILnBridgeSourceV3.withdrawLiquidity.selector,
_transferIds,
block.chainid,
_provider
);
_sendMessageToSource(_remoteChainId, message, msg.value, _extParams);
}

function encodeSlashRequest(
bytes32 _transferId,
address _provider,
address _slasher
) public view returns(bytes memory message) {
message = abi.encodeWithSelector(
ILnBridgeSourceV3.slash.selector,
block.chainid,
_transferId,
_provider,
_slasher
);
}
}

0 comments on commit 8867e0e

Please sign in to comment.