Overview
Cross-Chain Transfer Protocol (CCTP) uses generalized message passing to facilitate the native burning and minting of USDC across supported blockchains, also known as domains. Message passing is a three-step process:
- An on-chain component on source domain emits a message.
- Circle's off-chain attestation service signs the message.
- The on-chain component at the destination domain receives the message, and forwards the message body to the specified recipient.
Architecture
- CCTP on EVM Domains
Usecase: Cross-chain USDC transfer
Tips: The next phrase is gasless, which will replace approval in fulture.
Transfer USDC on testnet from Ethereum to Avalanche,The script has 5 steps:
- In this first step, you initiate a transfer of USDC from one blockchain to another, and specify the recipient wallet address on the destination chain. This step approves the Ethereum Sepolia TokenMessenger contract to withdraw USDC from the provided Ethereum Sepolia wallet address.
const approveTx = await usdcEthContract.methods.approve(ETH_TOKEN_MESSENGER_CONTRACT_ADDRESS, amount).send({gas: approveTxGas})
- In this second step, you facilitate a burn of the specified amount of USDC on the source chain. This step executes depositForBurn function on the Ethereum Sepolia TokenMessenger contract deployed on Sepolia testnet.
const burnTx = await ethTokenMessengerContract.methods.depositForBurn(amount, AVAX_DESTINATION_DOMAIN, destinationAddressInBytes32, USDC_ETH_CONTRACT_ADDRESS).send();
- In this third step, you make sure you have the correct message and hash it. This step extracts messageBytes emitted by MessageSent event from depositForBurn transaction logs and hashes the retrieved messageBytes using the keccak256 hashing algorithm.
const transactionReceipt = await web3.eth.getTransactionReceipt(burnTx.transactionHash);
const eventTopic = web3.utils.keccak256('MessageSent(bytes)')
const log = transactionReceipt.logs.find((l) => l.topics[0] === eventTopic)
const messageBytes = web3.eth.abi.decodeParameters(['bytes'], log.data)[0]
const messageHash = web3.utils.keccak256(messageBytes);
- In this fourth step, you request the attestation from Circle, which provides authorization to mint the specified amount of USDC on the destination chain. This step polls the attestation service to acquire the signature using the messageHash from the previous step.
let attestationResponse = {status: 'pending'};
while(attestationResponse.status != 'complete') {
const response = await fetch(`https://iris-api-sandbox.circle.com/attestations/${messageHash}`);
attestationResponse = await response.json()
await new Promise(r => setTimeout(r, 2000));
}
- In this final step, you enable USDC to be minted on the destination chain. This step calls the receiveMessage function on the Avalanche Fuji MessageTransmitter contract to receive USDC at the Avalanche Fuji wallet address.
const receiveTx = await avaxMessageTransmitterContract.receiveMessage(receivingMessageBytes, signature);