Skip to content

Commit

Permalink
dispute emit relayer not guard. new Claim/Refund transfer & tests [SL…
Browse files Browse the repository at this point in the history
…T-295]
  • Loading branch information
parodime committed Oct 15, 2024
1 parent b09b41c commit ef5aa38
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
40 changes: 28 additions & 12 deletions packages/contracts-rfq/contracts/FastBridgeV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ contract FastBridgeV2 is Admin, IFastBridgeV2, IFastBridgeV2Errors {
revert DisputePeriodPassed();
}

address disputedRelayer = bridgeTxDetails[transactionId].proofRelayer;

// @dev relayer gets slashed effectively if dest relay has gone thru
bridgeTxDetails[transactionId].status = BridgeStatus.REQUESTED;
bridgeTxDetails[transactionId].proofRelayer = address(0);
bridgeTxDetails[transactionId].proofBlockTimestamp = 0;
bridgeTxDetails[transactionId].proofBlockNumber = 0;

emit BridgeProofDisputed(transactionId, msg.sender);
emit BridgeProofDisputed(transactionId, disputedRelayer);
}

/// @inheritdoc IFastBridge
Expand All @@ -111,12 +113,15 @@ contract FastBridgeV2 is Admin, IFastBridgeV2, IFastBridgeV2Errors {
bridgeTxDetails[transactionId].status = BridgeStatus.REFUNDED;

// transfer origin collateral back to original sender
address to = transaction.originSender;
address token = transaction.originToken;
uint256 amount = transaction.originAmount + transaction.originFeeAmount;
token.universalTransfer(to, amount);

emit BridgeDepositRefunded(transactionId, to, token, amount);
if (transaction.originToken == UniversalTokenLib.ETH_ADDRESS) {
Address.sendValue(payable(transaction.originSender), amount);
} else {
IERC20(transaction.originToken).safeTransfer(transaction.originSender, amount);
}

emit BridgeDepositRefunded(transactionId, transaction.originSender, transaction.originToken, amount);
}

/// @inheritdoc IFastBridge
Expand Down Expand Up @@ -166,8 +171,10 @@ contract FastBridgeV2 is Admin, IFastBridgeV2, IFastBridgeV2Errors {

// track amount of origin token owed to protocol
uint256 originFeeAmount;
if (protocolFeeRate > 0) originFeeAmount = (originAmount * protocolFeeRate) / FEE_BPS;
originAmount -= originFeeAmount; // remove from amount used in request as not relevant for relayers
if (protocolFeeRate > 0) {
originFeeAmount = (originAmount * protocolFeeRate) / FEE_BPS;
originAmount -= originFeeAmount; // remove from amount used in request as not relevant for relayers
}

// set status to requested
bytes memory request = abi.encode(
Expand Down Expand Up @@ -298,12 +305,21 @@ contract FastBridgeV2 is Admin, IFastBridgeV2, IFastBridgeV2Errors {
// update protocol fees if origin fee amount exists
if (transaction.originFeeAmount > 0) protocolFees[transaction.originToken] += transaction.originFeeAmount;

// transfer origin collateral less fee to specified address
address token = transaction.originToken;
uint256 amount = transaction.originAmount;
token.universalTransfer(to, amount);
// transfer origin collateral to specified address (protocol fee was pre-deducted at deposit)
if (transaction.originToken == UniversalTokenLib.ETH_ADDRESS) {
Address.sendValue(payable(to), transaction.originAmount);
} else {
IERC20(transaction.originToken).safeTransfer(to, transaction.originAmount);
}

emit BridgeDepositClaimed(transactionId, bridgeTxDetails[transactionId].proofRelayer, to, token, amount);
// solhint-disable-next-line max-line-length
emit BridgeDepositClaimed(
transactionId,
bridgeTxDetails[transactionId].proofRelayer,
to,
transaction.originToken,
transaction.originAmount
);
}

function bridgeStatuses(bytes32 transactionId) public view returns (BridgeStatus status) {
Expand Down
17 changes: 8 additions & 9 deletions packages/contracts-rfq/test/FastBridgeV2.Src.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ contract FastBridgeV2SrcTest is FastBridgeV2SrcBaseTest {
});
}

function expectBridgeProofDisputed(bytes32 txId, address guard) public {
function expectBridgeProofDisputed(bytes32 txId, address relayer) public {
vm.expectEmit(address(fastBridge));
// Note: BridgeProofDisputed event has a mislabeled address parameter, this is actually the guard
emit BridgeProofDisputed({transactionId: txId, relayer: guard});
emit BridgeProofDisputed({transactionId: txId, relayer: relayer});
}

function expectBridgeDepositRefunded(IFastBridge.BridgeParams memory bridgeParams, bytes32 txId) public {
Expand Down Expand Up @@ -384,11 +383,11 @@ contract FastBridgeV2SrcTest is FastBridgeV2SrcBaseTest {
bridge({caller: userA, msgValue: ethParams.originAmount, params: ethParams});
expectBridgeProofProvided({txId: txId, relayer: relayerA, destTxHash: hex"01"});
prove({caller: relayerB, transactionId: txId, destTxHash: hex"01", relayer: relayerA});
expectBridgeProofDisputed(txId, guard);
expectBridgeProofDisputed(txId, relayerA);
dispute(guard, txId);
expectBridgeProofProvided({txId: txId, relayer: relayerA, destTxHash: hex"02"});
prove({caller: relayerB, transactionId: txId, destTxHash: hex"02", relayer: relayerA});
expectBridgeProofDisputed(txId, guard);
expectBridgeProofDisputed(txId, relayerA);
dispute(guard, txId);
expectBridgeProofProvided({txId: txId, relayer: relayerA, destTxHash: hex"03"});
prove({caller: relayerB, transactionId: txId, destTxHash: hex"03", relayer: relayerA});
Expand Down Expand Up @@ -660,7 +659,7 @@ contract FastBridgeV2SrcTest is FastBridgeV2SrcBaseTest {
bytes32 txId = getTxId(tokenTx);
bridge({caller: userA, msgValue: 0, params: tokenParams});
prove({caller: relayerA, bridgeTx: tokenTx, destTxHash: hex"01"});
expectBridgeProofDisputed({txId: txId, guard: guard});
expectBridgeProofDisputed({txId: txId, relayer: relayerA});
dispute({caller: guard, txId: txId});
assertEq(fastBridge.bridgeStatuses(txId), IFastBridgeV2.BridgeStatus.REQUESTED);
assertEq(fastBridge.protocolFees(address(srcToken)), INITIAL_PROTOCOL_FEES_TOKEN);
Expand All @@ -672,7 +671,7 @@ contract FastBridgeV2SrcTest is FastBridgeV2SrcBaseTest {
bridge({caller: userA, msgValue: 0, params: tokenParams});
prove({caller: relayerA, bridgeTx: tokenTx, destTxHash: hex"01"});
skip(CLAIM_DELAY);
expectBridgeProofDisputed({txId: txId, guard: guard});
expectBridgeProofDisputed({txId: txId, relayer: relayerA});
dispute({caller: guard, txId: txId});
assertEq(fastBridge.bridgeStatuses(txId), IFastBridgeV2.BridgeStatus.REQUESTED);
assertEq(fastBridge.protocolFees(address(srcToken)), INITIAL_PROTOCOL_FEES_TOKEN);
Expand All @@ -684,7 +683,7 @@ contract FastBridgeV2SrcTest is FastBridgeV2SrcBaseTest {
bytes32 txId = getTxId(ethTx);
bridge({caller: userA, msgValue: ethParams.originAmount, params: ethParams});
prove({caller: relayerA, bridgeTx: ethTx, destTxHash: hex"01"});
expectBridgeProofDisputed({txId: txId, guard: guard});
expectBridgeProofDisputed({txId: txId, relayer: relayerA});
dispute({caller: guard, txId: txId});
assertEq(fastBridge.bridgeStatuses(txId), IFastBridgeV2.BridgeStatus.REQUESTED);
assertEq(fastBridge.protocolFees(ETH_ADDRESS), INITIAL_PROTOCOL_FEES_ETH);
Expand All @@ -697,7 +696,7 @@ contract FastBridgeV2SrcTest is FastBridgeV2SrcBaseTest {
bridge({caller: userA, msgValue: ethParams.originAmount, params: ethParams});
prove({caller: relayerA, bridgeTx: ethTx, destTxHash: hex"01"});
skip(CLAIM_DELAY);
expectBridgeProofDisputed({txId: txId, guard: guard});
expectBridgeProofDisputed({txId: txId, relayer: relayerA});
dispute({caller: guard, txId: txId});
assertEq(fastBridge.bridgeStatuses(txId), IFastBridgeV2.BridgeStatus.REQUESTED);
assertEq(fastBridge.protocolFees(ETH_ADDRESS), INITIAL_PROTOCOL_FEES_ETH);
Expand Down

0 comments on commit ef5aa38

Please sign in to comment.