Skip to content

Commit

Permalink
Merge 099a4f5 into 3cc5077
Browse files Browse the repository at this point in the history
  • Loading branch information
parodime authored Oct 16, 2024
2 parents 3cc5077 + 099a4f5 commit 3bb8ae0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
38 changes: 29 additions & 9 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,17 @@ contract FastBridgeV2 is Admin, IFastBridgeV2, IFastBridgeV2Errors {
bridgeTxDetails[transactionId].status = BridgeStatus.REFUNDED;

// transfer origin collateral back to original sender
uint256 amount = transaction.originAmount + transaction.originFeeAmount;
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 (token == UniversalTokenLib.ETH_ADDRESS) {
Address.sendValue(payable(to), amount);
} else {
IERC20(token).safeTransfer(to, amount);
}

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

/// @inheritdoc IFastBridge
Expand Down Expand Up @@ -166,8 +173,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 +307,23 @@ 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);

emit BridgeDepositClaimed(transactionId, bridgeTxDetails[transactionId].proofRelayer, to, token, amount);
// transfer origin collateral to specified address (protocol fee was pre-deducted at deposit)
if (token == UniversalTokenLib.ETH_ADDRESS) {
Address.sendValue(payable(to), amount);
} else {
IERC20(token).safeTransfer(to, amount);
}

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 3bb8ae0

Please sign in to comment.