The CCIP Token Manager is now live. Try it now.

Using CCIP local simulator in your Hardhat project

You can use Chainlink Local to run CCIP in a localhost environment within your Hardhat project. To get started quickly, you will use the CCIP Hardhat Starter Kit. This project is a Hardhat boilerplate that includes the Chainlink Local package and several CCIP examples.

Prerequisites

  1. In a terminal, clone the CCIP Hardhat Starter Kit repository and change directories:

    git clone https://github.com/smartcontractkit/ccip-starter-kit-hardhat && \
    cd ./ccip-starter-kit-hardhat/
    
  2. Install the Chainlink Local package and other required packages:

    npm install
    
  3. Compile the contracts:

    npm run compile
    

Test tokens transfers

You will run a test to transfer tokens between two accounts. The test file Example1.spec.ts is located in the ./test/no-fork directory. This file contains one test case:

Transfer with LINK fees: This test case transfers tokens from the sender account to the receiver account, paying fees in LINK. At the end of the test, it verifies that the sender account was debited and the receiver account was credited.

For a detailed explanation of the test file, refer to the Examine the code section.

In your terminal, run the following command to execute the test:

npx hardhat test test/no-fork/Example1.spec.ts

Example output:

$ npx hardhat test test/no-fork/Example1.spec.ts


Example 1
   ✔ Should transfer CCIP test tokens from EOA to EOA (1057ms)


1 passing (1s)

Examine the code

Setup

To transfer tokens using CCIP, we need the following:

  • Destination chain selector
  • Source CCIP router
  • LINK token for paying CCIP fees
  • A test token contract (such as CCIP-BnM)
  • A sender account (Alice)
  • A receiver account (Bob)

The deployFixture function is used to set up the initial state for the tests. This function deploys the CCIP local simulator contract and initializes the sender and receiver accounts.

  1. Initialize the CCIP local simulator contract:

    const ccipLocalSimulatorFactory = await hre.ethers.getContractFactory("CCIPLocalSimulator")
    const ccipLocalSimulator: CCIPLocalSimulator = await ccipLocalSimulatorFactory.deploy()
    
  2. Initialize the sender and receiver accounts:

    const [alice, bob] = await hre.ethers.getSigners()
    

Next steps

For more advanced scenarios, please refer to other test files in the ./test/no-fork directory. To learn how to use Chainlink local in forked environments, refer to the guide on Using CCIP Local Simulator in your Hardhat project with forked environments.

Get the latest Chainlink content straight to your inbox.