Skip to content

Commit

Permalink
Separate TokenSent and TokenSentExtras events
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroslavNekryach committed Mar 25, 2024
1 parent 6458aff commit 89d79b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
58 changes: 23 additions & 35 deletions contracts/CctpBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ contract CctpBridge is GasUsage {
uint receivedRelayerFeeFromTokens,
uint relayerFee,
uint receivedRelayerFeeTokenAmount,
uint adminFeeTokenAmount,
bytes32 recipientWalletAddress
uint adminFeeTokenAmount
);

event TokenSentExtras(bytes32 recipientWalletAddress);

event RecipientReplaced(address sender, uint nonce, bytes32 newRecipient);

constructor(
Expand Down Expand Up @@ -86,17 +87,15 @@ contract CctpBridge is GasUsage {
* (See the function `getBridgingCostInTokens`).
* @param amount The amount of tokens to send (including `relayerFeeTokenAmount`).
* @param recipient The recipient address.
* @param recipientWalletAddress The recipient wallet address - used to track user for transfers to Solana.
* @param destinationChainId The ID of the destination chain.
* @param relayerFeeTokenAmount The amount of tokens to be deducted from the transferred amount as a bridging fee.
*/
function _bridge(
function bridge(
uint amount,
bytes32 recipient,
bytes32 recipientWalletAddress,
uint destinationChainId,
uint relayerFeeTokenAmount
) internal {
) public payable {
require(amount > relayerFeeTokenAmount, "CCTP: Amount <= relayer fee");
require(recipient != 0, "CCTP: Recipient must be nonzero");
token.safeTransferFrom(msg.sender, address(this), amount);
Expand Down Expand Up @@ -125,11 +124,27 @@ contract CctpBridge is GasUsage {
gasFromStables,
relayerFee,
relayerFeeTokenAmount,
adminFee,
recipientWalletAddress
adminFee
);
}

/**
* @notice Public method to initiate a bridging process of the token to another blockchain. Used for recipients with different wallet address (Solana)
* @dev See full description in the bridge method
* @param recipientWalletAddress The recipient wallet address - used to track user for transfers to Solana.
**/
function bridgeWithWalletAddress(
uint amount,
bytes32 recipient,
bytes32 recipientWalletAddress,
uint destinationChainId,
uint relayerFeeTokenAmount
) external payable {
bridge(amount, recipient, destinationChainId, relayerFeeTokenAmount);

emit TokenSentExtras(recipientWalletAddress);
}

/**
* @notice Public method to replace recipient if it was accidentally incorrectly specified
* @param originalMessage original message bytes (to replace)
Expand All @@ -148,33 +163,6 @@ contract CctpBridge is GasUsage {
emit RecipientReplaced(tx.origin, nonce, newRecipient);

Check warning on line 163 in contracts/CctpBridge.sol

View workflow job for this annotation

GitHub Actions / ⬣ Lint

Avoid to use tx.origin
}

/**
* @notice Public method to initiate a bridging process of the token to another blockchain. Used for recipients with the same wallet address
* @dev See full description in the _bridge method
**/
function bridge(
uint amount,
bytes32 recipient,
uint destinationChainId,
uint relayerFeeTokenAmount
) external payable {
_bridge(amount, recipient, recipient, destinationChainId, relayerFeeTokenAmount);
}

/**
* @notice Public method to initiate a bridging process of the token to another blockchain. Used for recipients with different wallet address (Solana)
* @dev See full description in the _bridge method
**/
function bridgeWithWalletAddress(
uint amount,
bytes32 recipient,
bytes32 recipientWalletAddress,
uint destinationChainId,
uint relayerFeeTokenAmount
) external payable {
_bridge(amount, recipient, recipientWalletAddress, destinationChainId, relayerFeeTokenAmount);
}

/**
* @notice Completes the bridging process by sending the tokens on the destination blockchain to the recipient.
* @param recipient The recipient address.
Expand Down
6 changes: 0 additions & 6 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,9 @@ const config: HardhatUserConfig = {
solidity: {
version: '0.8.18',
settings: {
viaIR: true,
optimizer: {
enabled: true,
runs: 1000,
details: {
yulDetails: {
optimizerSteps: 'u',
},
},
},
outputSelection: {
'*': {
Expand Down
9 changes: 4 additions & 5 deletions test/cctp-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ describe('CctpBridge', () => {
costOfFinalizingTransfer,
relayerFeeTokenAmount,
'0',
recipient,
);
});

Expand Down Expand Up @@ -184,7 +183,6 @@ describe('CctpBridge', () => {
costOfFinalizingTransfer,
relayerFeeTokenAmount,
'0',
recipient,
);
});

Expand Down Expand Up @@ -241,8 +239,11 @@ describe('CctpBridge', () => {
costOfFinalizingTransfer,
relayerFeeTokenAmount,
'0',
recipientWalletAddress,
);

await expect(tx)
.to.emit(cctpBridge, 'TokenSentExtras')
.withArgs(recipientWalletAddress);
});

it('Success: should charge admin fee', async () => {
Expand Down Expand Up @@ -292,7 +293,6 @@ describe('CctpBridge', () => {
costOfFinalizingTransfer,
relayerFeeTokenAmount,
adminFeeAmount,
recipient,
);
});

Expand Down Expand Up @@ -339,7 +339,6 @@ describe('CctpBridge', () => {
costOfFinalizingTransfer,
relayerFeeTokenAmount,
minAdminFeeAmount,
recipient,
);
});

Expand Down

0 comments on commit 89d79b5

Please sign in to comment.