Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes committed Apr 14, 2024
1 parent dd4ca4b commit 3985ce5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
34 changes: 17 additions & 17 deletions contracts/src/Assets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ library Assets {
IERC20(token).safeTransferFrom(sender, agent, amount);
}

function sendTokenCosts(address token, ParaID destinationChain, uint128 destinationChainFee)
function sendTokenCosts(address token, ParaID destinationChain, uint128 destinationChainFee, uint128 maxDestinationChainFee)
external
view
returns (Costs memory costs)
Expand All @@ -53,10 +53,10 @@ library Assets {
revert TokenNotRegistered();
}

return _sendTokenCosts(destinationChain, destinationChainFee);
return _sendTokenCosts(destinationChain, destinationChainFee, maxDestinationChainFee);
}

function _sendTokenCosts(ParaID destinationChain, uint128 destinationChainFee)
function _sendTokenCosts(ParaID destinationChain, uint128 destinationChainFee, uint128 maxDestinationChainFee)
internal
view
returns (Costs memory costs)
Expand All @@ -65,6 +65,19 @@ library Assets {
if ($.assetHubParaID == destinationChain) {
costs.foreign = $.assetHubReserveTransferFee;
} else {
// Reduce the ability for users to perform arbitrage by exploiting a
// favourable exchange rate. For example supplying Ether
// and gaining a more valuable amount of DOT on the destination chain.
//
// Also prevents users from mistakenly sending more fees than would be required
// which has negative effects like draining AssetHub's sovereign account.
//
// For safety, `maxDestinationChainFee` should be less valuable
// than the gas cost to send tokens.
if (destinationChainFee > maxDestinationChainFee) {
revert InvalidDestinationFee();
}

// If the final destination chain is not AssetHub, then the fee needs to additionally
// include the cost of executing an XCM on the final destination parachain.
costs.foreign = $.assetHubReserveTransferFee + destinationChainFee;
Expand All @@ -89,24 +102,11 @@ library Assets {
revert TokenNotRegistered();
}

// Reduce the ability for users to perform arbitrage by exploiting a
// favourable exchange rate. For example supplying Ether
// and gaining a more valuable amount of DOT on the destination chain.
//
// Also prevents users from mistakenly sending more fees than would be required
// which has negative effects like draining AssetHub's sovereign account.
//
// For safety, `maxDestinationChainFee` should be less valuable
// than the gas cost to send tokens.
if (destinationChainFee > maxDestinationChainFee) {
revert InvalidDestinationFee();
}

// Lock the funds into AssetHub's agent contract
_transferToAgent($.assetHubAgent, token, sender, amount);

ticket.dest = $.assetHubParaID;
ticket.costs = _sendTokenCosts(destinationChain, destinationChainFee);
ticket.costs = _sendTokenCosts(destinationChain, destinationChainFee, maxDestinationChainFee);

// Construct a message payload
if (destinationChain == $.assetHubParaID) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
view
returns (uint256)
{
return _calculateFee(Assets.sendTokenCosts(token, destinationChain, destinationFee));
return _calculateFee(Assets.sendTokenCosts(token, destinationChain, destinationFee, MAX_DESTINATION_FEE));
}

// Transfer ERC20 tokens to a Polkadot parachain
Expand Down
6 changes: 3 additions & 3 deletions contracts/test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,10 @@ contract GatewayTest is Test {
uint256 fee = IGateway(address(gateway)).quoteRegisterTokenFee();
IGateway(address(gateway)).registerToken{value: fee}(address(token));

uint128 largeFee = maxDestinationFee + 1; // greater than 10 DOT, 10 decimal places
fee = IGateway(address(gateway)).quoteSendTokenFee(address(token), destPara, largeFee);
vm.expectRevert(Assets.InvalidDestinationFee.selector);
IGateway(address(gateway)).quoteSendTokenFee(address(token), destPara, maxDestinationFee + 1);

vm.expectRevert(Assets.InvalidDestinationFee.selector);
IGateway(address(gateway)).sendToken{value: fee}(address(token), destPara, recipientAddress32, largeFee, 1);
IGateway(address(gateway)).sendToken{value: fee}(address(token), destPara, recipientAddress32, maxDestinationFee + 1, 1);
}
}

0 comments on commit 3985ce5

Please sign in to comment.