Skip to content
32 changes: 32 additions & 0 deletions contracts/external/interfaces/ICoreDepositWallet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2025 Circle Internet Group, Inc. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
pragma solidity ^0.8.0;

/**
* @title ICoreDepositWallet
* @notice Interface for the core deposit wallet
* @dev Source: https://developers.circle.com/cctp/coredepositwallet-contract-interface#deposit-function
*/
interface ICoreDepositWallet {
/**
* @notice Deposits tokens for the sender.
* @param amount The amount of tokens being deposited.
* @param destinationDex The destination dex on HyperCore.
*/
function deposit(uint256 amount, uint32 destinationDex) external;
}
32 changes: 27 additions & 5 deletions contracts/libraries/HyperCoreLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.0;

import { IERC20 } from "@openzeppelin/contracts-v4/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts-v4/token/ERC20/utils/SafeERC20.sol";
import { ICoreDepositWallet } from "../external/interfaces/ICoreDepositWallet.sol";

interface ICoreWriter {
function sendRawAction(bytes calldata data) external;
Expand Down Expand Up @@ -51,11 +52,18 @@ library HyperCoreLib {
address public constant TOKEN_INFO_PRECOMPILE_ADDRESS = 0x000000000000000000000000000000000000080C;
address public constant CORE_WRITER_PRECOMPILE_ADDRESS = 0x3333333333333333333333333333333333333333;

// USDC
address public constant USDC_CORE_DEPOSIT_WALLET_ADDRESS = 0x6B9E773128f453f5c2C60935Ee2DE2CBc5390A24;
uint64 public constant USDC_CORE_INDEX = 0;

// CoreWriter action headers
bytes4 public constant LIMIT_ORDER_HEADER = 0x01000001; // version=1, action=1
bytes4 public constant SPOT_SEND_HEADER = 0x01000006; // version=1, action=6
bytes4 public constant CANCEL_BY_CLOID_HEADER = 0x0100000B; // version=1, action=11

// HyperCore protocol constants
uint32 private constant CORE_SPOT_DEX_ID = type(uint32).max;

// Errors
error LimitPxIsZero();
error OrderSizeIsZero();
Expand Down Expand Up @@ -87,9 +95,7 @@ library HyperCoreLib {
(uint256 _amountEVMToSend, uint64 _amountCoreToReceive) = maximumEVMSendAmountToAmounts(amountEVM, decimalDiff);

if (_amountEVMToSend != 0) {
// Transfer the tokens to this contract's address on HyperCore
IERC20(erc20EVMAddress).safeTransfer(toAssetBridgeAddress(erc20CoreIndex), _amountEVMToSend);

transferToCore(erc20EVMAddress, erc20CoreIndex, _amountEVMToSend);
// Transfer the tokens from this contract on HyperCore to the `to` address on HyperCore
transferERC20CoreToCore(erc20CoreIndex, to, _amountCoreToReceive);
}
Expand Down Expand Up @@ -117,8 +123,7 @@ library HyperCoreLib {
(uint256 _amountEVMToSend, uint64 _amountCoreToReceive) = maximumEVMSendAmountToAmounts(amountEVM, decimalDiff);

if (_amountEVMToSend != 0) {
// Transfer the tokens to this contract's address on HyperCore
IERC20(erc20EVMAddress).safeTransfer(toAssetBridgeAddress(erc20CoreIndex), _amountEVMToSend);
transferToCore(erc20EVMAddress, erc20CoreIndex, _amountEVMToSend);
}

return (_amountEVMToSend, _amountCoreToReceive);
Expand All @@ -137,6 +142,23 @@ library HyperCoreLib {
ICoreWriter(CORE_WRITER_PRECOMPILE_ADDRESS).sendRawAction(payload);
}

/**
* @notice Transfers tokens from this contract on HyperEVM to this contract's address on HyperCore
* @param erc20EVMAddress The address of the ERC20 token on HyperEVM
* @param erc20CoreIndex The HyperCore index id of the token to transfer
* @param amountEVMToSend The amount to transfer on HyperEVM
*/
function transferToCore(address erc20EVMAddress, uint64 erc20CoreIndex, uint256 amountEVMToSend) internal {
// USDC requires a special transfer to core
if (erc20CoreIndex == USDC_CORE_INDEX) {
IERC20(erc20EVMAddress).forceApprove(USDC_CORE_DEPOSIT_WALLET_ADDRESS, amountEVMToSend);
ICoreDepositWallet(USDC_CORE_DEPOSIT_WALLET_ADDRESS).deposit(amountEVMToSend, CORE_SPOT_DEX_ID);
} else {
// For all other tokens, transfer to the asset bridge address on HyperCore
IERC20(erc20EVMAddress).safeTransfer(toAssetBridgeAddress(erc20CoreIndex), amountEVMToSend);
}
}

/**
* @notice Submit a limit order on HyperCore.
* @dev Expects price & size already scaled by 1e8 per HyperCore spec.
Expand Down
13 changes: 7 additions & 6 deletions contracts/periphery/mintburn/HyperCoreFlowExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,9 @@ contract HyperCoreFlowExecutor is AccessControlUpgradeable, AuthorizedFundedFlow
}

if (amountToSponsor > 0) {
if (!_availableInDonationBox(params.quoteNonce, coreTokenInfo.tokenInfo.evmContract, amountToSponsor)) {
if (!_availableInDonationBox(params.quoteNonce, finalToken, amountToSponsor)) {
// If the full amount is not available in the donation box, use the balance of the token in the donation box
amountToSponsor = IERC20(coreTokenInfo.tokenInfo.evmContract).balanceOf(address(donationBox));
amountToSponsor = IERC20(finalToken).balanceOf(address(donationBox));
}
}
}
Expand Down Expand Up @@ -556,7 +556,7 @@ contract HyperCoreFlowExecutor is AccessControlUpgradeable, AuthorizedFundedFlow

if (amountToSponsor > 0) {
// This will succeed because we checked the balance earlier
donationBox.withdraw(IERC20(coreTokenInfo.tokenInfo.evmContract), amountToSponsor);
donationBox.withdraw(IERC20(finalToken), amountToSponsor);
}

$.cumulativeSponsoredAmount[finalToken] += amountToSponsor;
Expand Down Expand Up @@ -750,6 +750,7 @@ contract HyperCoreFlowExecutor is AccessControlUpgradeable, AuthorizedFundedFlow
limitOrderOuts[finalized],
finalCoreTokenInfo,
finalTokenInfo.swapHandler,
finalToken,
availableBalance
);
if (!success) {
Expand Down Expand Up @@ -780,7 +781,7 @@ contract HyperCoreFlowExecutor is AccessControlUpgradeable, AuthorizedFundedFlow
) {
// We expect this situation to be so rare and / or intermittend that we're willing to rely on admin to sweep the funds if this leads to
// swaps being impossible to finalize
revert UnsafeToBridgeError(finalCoreTokenInfo.tokenInfo.evmContract, totalAdditionalToSend);
revert UnsafeToBridgeError(finalToken, totalAdditionalToSend);
}

$.cumulativeSponsoredAmount[finalToken] += totalAdditionalToSendEVM;
Expand All @@ -804,12 +805,13 @@ contract HyperCoreFlowExecutor is AccessControlUpgradeable, AuthorizedFundedFlow
uint64 limitOrderOut,
CoreTokenInfo memory finalCoreTokenInfo,
SwapHandler swapHandler,
address finalToken,
uint64 availableBalance
) internal returns (bool success, uint64 additionalToSend, uint64 balanceRemaining) {
SwapFlowState storage swap = _getMainStorage().swaps[quoteNonce];
if (swap.finalRecipient == address(0)) revert SwapDoesNotExist();
if (swap.finalized) revert SwapAlreadyFinalized();
if (swap.finalToken != finalCoreTokenInfo.tokenInfo.evmContract) revert WrongSwapFinalizationToken(quoteNonce);
if (swap.finalToken != finalToken) revert WrongSwapFinalizationToken(quoteNonce);

uint64 totalToSend;
(totalToSend, additionalToSend) = _calcSwapFlowSendAmounts(
Expand Down Expand Up @@ -946,7 +948,6 @@ contract HyperCoreFlowExecutor is AccessControlUpgradeable, AuthorizedFundedFlow
uint64 bridgeSafetyBufferCore
) internal {
HyperCoreLib.TokenInfo memory tokenInfo = HyperCoreLib.tokenInfo(coreIndex);
require(tokenInfo.evmContract == token, "Token mismatch");

(uint256 accountActivationFeeEVM, ) = HyperCoreLib.minimumCoreReceiveAmountToAmounts(
accountActivationFeeCore,
Expand Down
8 changes: 4 additions & 4 deletions script/mintburn/cctp/createSponsoredDeposit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ contract CreateSponsoredDeposit is DeploymentUtils {
SponsoredCCTPInterface.SponsoredCCTPQuote memory quote = SponsoredCCTPInterface.SponsoredCCTPQuote({
sourceDomain: config.get("cctpDomainId").toUint32(), // Arbitrum CCTP domain
destinationDomain: 19, // HyperEVM CCTP domain
mintRecipient: address(0x83e245941BefbDe29682dF068Bcda006A804eb0C).toBytes32(), // Destination handler contract
mintRecipient: address(0xb63c02e60C05F05975653edC83F876C334E07C6d).toBytes32(), // Destination handler contract
amount: 10000, // 100 USDC (6 decimals)
burnToken: config.get("usdc").toAddress().toBytes32(), // USDC on Arbitrum
destinationCaller: address(0x83e245941BefbDe29682dF068Bcda006A804eb0C).toBytes32(), // Destination handler contract
destinationCaller: address(0xb63c02e60C05F05975653edC83F876C334E07C6d).toBytes32(), // Destination handler contract
maxFee: 1, // 0 max fee
minFinalityThreshold: 1000, // Minimum finality threshold
nonce: keccak256(abi.encodePacked(block.timestamp, deployer, vm.getNonce(deployer))), // Generate nonce
Expand All @@ -97,8 +97,8 @@ contract CreateSponsoredDeposit is DeploymentUtils {
maxUserSlippageBps: 0, // 4% max user slippage (400 basis points)
finalRecipient: address(0x9A8f92a830A5cB89a3816e3D267CB7791c16b04D).toBytes32(), // Final recipient
finalToken: address(0xb88339CB7199b77E23DB6E890353E22632Ba630f).toBytes32(), // USDC on HyperEVM
executionMode: uint8(SponsoredCCTPInterface.ExecutionMode.ArbitraryActionsToEVM), // DirectToCore mode
actionData: actionDataEmpty // Empty for DirectToCore mode
executionMode: uint8(SponsoredCCTPInterface.ExecutionMode.DirectToCore), // DirectToCore mode
actionData: emptyActionData // Empty for DirectToCore mode
});

console.log("SponsoredCCTPQuote created:");
Expand Down
6 changes: 2 additions & 4 deletions test/evm/foundry/local/HyperCoreFlowExecutor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,15 @@ contract HyperCoreFlowExecutorTest is BaseSimulatorTest {

finalRecipient = makeAddr("finalRecipient");

token = new MockERC20();
token = MockERC20(0xb88339CB7199b77E23DB6E890353E22632Ba630f);
donationBox = new DonationBox();
handler = new TestHyperCoreHandler(address(donationBox), address(token));

// Make the handler the owner of DonationBox so it can withdraw during sponsorship
vm.prank(donationBox.owner());
donationBox.transferOwnership(address(handler));

// Link token to HyperCore and set token info in the module via delegatecall
// name, weiDecimals=8, szDecimals=8, evmExtraWeiDecimals=0
hyperCore.forceTokenInfo(CORE_INDEX, "MOCK", address(token), 8, 8, 0);
// Set token info in the module via delegatecall
handler.callSetCoreTokenInfo(address(token), CORE_INDEX, true, 1e6, 1e6);

// Ensure recipient has an active HyperCore account for sponsored simple transfer path
Expand Down
10 changes: 5 additions & 5 deletions test/evm/foundry/local/SponsorredCCTPDstPeriphery.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { HyperCoreMockHelper } from "./HyperCoreMockHelper.sol";
import { BaseSimulatorTest } from "./external/hyper-evm-lib/test/BaseSimulatorTest.sol";
import { PrecompileLib } from "./external/hyper-evm-lib/src/PrecompileLib.sol";
import { CoreWriterLib } from "./external/hyper-evm-lib/src/CoreWriterLib.sol";
import { CoreSimulatorLib } from "./external/hyper-evm-lib/test/simulation/CoreSimulatorLib.sol";

contract MockMessageTransmitter is IMessageTransmitterV2 {
bool internal shouldSucceed = true;
Expand Down Expand Up @@ -104,12 +106,10 @@ contract SponsoredCCTPDstPeripheryTest is BaseSimulatorTest {
// Deploy mock contracts
messageTransmitter = new MockMessageTransmitter();
donationBox = new MockDonationBox();
usdc = new MockUSDC();
usdc = MockUSDC(0xb88339CB7199b77E23DB6E890353E22632Ba630f);

// Setup HyperCore precompile mocks using the helper
// setupDefaultHyperCoreMocks(address(usdc), "Mock USDC", 6);
hyperCore.forceAccountActivation(finalRecipient);
hyperCore.forceTokenInfo(CORE_INDEX, "USDC", address(usdc), 8, 8, 0);

// Deploy periphery
vm.startPrank(admin);
Expand All @@ -124,8 +124,8 @@ contract SponsoredCCTPDstPeripheryTest is BaseSimulatorTest {
IHyperCoreFlowExecutor(address(periphery)).setCoreTokenInfo(address(usdc), CORE_INDEX, true, 1e6, 1e6);
vm.stopPrank();

// Mint USDC to periphery for testing
usdc.mint(address(periphery), 10000e6);
// Deal USDC to periphery for testing
deal(address(usdc), address(periphery), 10000e6);
}

/// @dev Helper function to create a valid CCTP message
Expand Down
Loading