Skip to content

Commit

Permalink
factor out emitting of sendToken
Browse files Browse the repository at this point in the history
  • Loading branch information
alistair-singh committed Jan 17, 2025
1 parent abc1d95 commit 0921ff9
Showing 1 changed file with 54 additions and 65 deletions.
119 changes: 54 additions & 65 deletions contracts/src/Assets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -138,35 +138,27 @@ library Assets {
}
}

// @dev Transfer Ether tokens to Polkadot
function _sendEther(
function _emitSendNativeTokenTicket(
address token,
address sender,
ParaID assetHubParaID,
ParaID destinationChain,
MultiAddress calldata destinationAddress,
uint128 assetHubReserveTransferFee,
uint128 destinationChainFee,
uint128 maxDestinationChainFee,
uint128 amount
) internal returns (Ticket memory ticket) {
AssetsStorage.Layout storage $ = AssetsStorage.layout();

// Lock Native Ether into AssetHub's agent contract
if (msg.value < amount) {
revert InvalidAmount();
}
payable($.assetHubAgent).safeNativeTransfer(amount);
ticket.value = amount;

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

address token = address(0);
// Construct a message payload
if (destinationChain == $.assetHubParaID) {
if (destinationChain == assetHubParaID) {
// The funds will be minted into the receiver's account on AssetHub
if (destinationAddress.isAddress32()) {
// The receiver has a 32-byte account ID
ticket.payload = SubstrateTypes.SendTokenToAssetHubAddress32(
token, destinationAddress.asAddress32(), $.assetHubReserveTransferFee, amount
token, destinationAddress.asAddress32(), assetHubReserveTransferFee, amount
);
} else {
// AssetHub does not support 20-byte account IDs
Expand All @@ -184,7 +176,7 @@ library Assets {
token,
destinationChain,
destinationAddress.asAddress32(),
$.assetHubReserveTransferFee,
assetHubReserveTransferFee,
destinationChainFee,
amount
);
Expand All @@ -194,7 +186,7 @@ library Assets {
token,
destinationChain,
destinationAddress.asAddress20(),
$.assetHubReserveTransferFee,
assetHubReserveTransferFee,
destinationChainFee,
amount
);
Expand All @@ -205,6 +197,38 @@ library Assets {
emit IGateway.TokenSent(token, sender, destinationChain, destinationAddress, amount);
}

// @dev Transfer Ether tokens to Polkadot
function _sendEther(
address sender,
ParaID destinationChain,
MultiAddress calldata destinationAddress,
uint128 destinationChainFee,
uint128 maxDestinationChainFee,
uint128 amount
) internal returns (Ticket memory ticket) {
AssetsStorage.Layout storage $ = AssetsStorage.layout();

// Lock Native Ether into AssetHub's agent contract
if (msg.value < amount) {
revert InvalidAmount();
}

ticket = _emitSendNativeTokenTicket(
address(0),
sender,
$.assetHubParaID,
destinationChain,
destinationAddress,
$.assetHubReserveTransferFee,
destinationChainFee,
maxDestinationChainFee,
amount
);
ticket.value = amount;

payable($.assetHubAgent).safeNativeTransfer(amount);
}

// @dev Transfer ERC20(Ethereum-native) tokens to Polkadot
function _sendNativeToken(
address token,
Expand All @@ -217,56 +241,21 @@ library Assets {
) internal returns (Ticket memory ticket) {
AssetsStorage.Layout storage $ = AssetsStorage.layout();

// Lock the ERC20 into AssetHub's agent contract
_transferToAgent($.assetHubAgent, token, sender, amount);
ticket = _emitSendNativeTokenTicket(
token,
sender,
$.assetHubParaID,
destinationChain,
destinationAddress,
$.assetHubReserveTransferFee,
destinationChainFee,
maxDestinationChainFee,
amount
);
ticket.value = 0;

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

// Construct a message payload
if (destinationChain == $.assetHubParaID) {
// The funds will be minted into the receiver's account on AssetHub
if (destinationAddress.isAddress32()) {
// The receiver has a 32-byte account ID
ticket.payload = SubstrateTypes.SendTokenToAssetHubAddress32(
token, destinationAddress.asAddress32(), $.assetHubReserveTransferFee, amount
);
} else {
// AssetHub does not support 20-byte account IDs
revert Unsupported();
}
} else {
if (destinationChainFee == 0) {
revert InvalidDestinationFee();
}
// The funds will be minted into sovereign account of the destination parachain on AssetHub,
// and then reserve-transferred to the receiver's account on the destination parachain.
if (destinationAddress.isAddress32()) {
// The receiver has a 32-byte account ID
ticket.payload = SubstrateTypes.SendTokenToAddress32(
token,
destinationChain,
destinationAddress.asAddress32(),
$.assetHubReserveTransferFee,
destinationChainFee,
amount
);
} else if (destinationAddress.isAddress20()) {
// The receiver has a 20-byte account ID
ticket.payload = SubstrateTypes.SendTokenToAddress20(
token,
destinationChain,
destinationAddress.asAddress20(),
$.assetHubReserveTransferFee,
destinationChainFee,
amount
);
} else {
revert Unsupported();
}
}
emit IGateway.TokenSent(token, sender, destinationChain, destinationAddress, amount);
// Lock the ERC20 into AssetHub's agent contract
_transferToAgent($.assetHubAgent, token, sender, amount);
}

// @dev Transfer Polkadot-native tokens back to Polkadot
Expand Down

0 comments on commit 0921ff9

Please sign in to comment.