Skip to Content
DocumentationAnalog GMPDevelopers' GuideTutorialsSetup Your Project with Foundry

Setup Your Project with Foundry

This tutorial will show you how to install and use Analog GMP contracts with Foundry.

Getting Started

  1. Ensure you’ve installed Foundry .
  2. To start a new project with Foundry, use forge init:
forge init hello_gmp

This creates a new directory called “hello_gmp” from the default Foundry template and initializes a new GIT repository.

  1. You may have to add your remappings in remappings.txt by running this command:
forge remappings > remappings.txt
  1. Your project should now be correctly set up to work with Analog GMP contracts. From now on, simply import the Analog GMP dependencies into your contract from @analog-gmp as follows:
import {IGmpReceiver} from "@analog-gmp/interfaces/IGmpReceiver.sol"; import {IGateway} from "@analog-gmp/interfaces/IGateway.sol";

For example, create a sample contract in the src directory, such as counter.sol, using the following code:

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import {IGmpReceiver} from "@analog-gmp/interfaces/IGmpReceiver.sol"; contract Counter is IGmpReceiver { address private immutable _gateway; uint256 public number; constructor(address gateway) { _gateway = gateway; } function onGmpReceived(bytes32, uint128, bytes32, bytes calldata) external payable returns (bytes32) { require(msg.sender == _gateway, "unauthorized"); number++; return bytes32(number); } }
  1. Now run:
forge build

This should display a successful compilation message, as shown here:

[⠊] Compiling... [⠊] Compiling 28 files with Solc 0.8.13 [⠒] Solc 0.8.13 finished in 949.34ms Compiler run successful! --> src/Counter.sol

Writing Tests

You can easily write cross-chain unit tests using Analog testing tools at @analog-gmp-testing.

import {GmpTestTools} from "@analog-gmp-testing/GmpTestTools.sol"; // Deploy gateway contracts and create forks for all supported networks GmpTestTools.setup(); // Set `account` balance in all networks GmpTestTools.deal(address(account), 100 ether); // Switch to Sepolia network GmpTestTools.switchNetwork(5); // Switch to Shibuya network and set `account` as `msg.sender` and `tx.origin` GmpTestTools.switchNetwork(7, address(account)); // Relay all pending GMP messages. GmpTestTools.relayMessages();

Build

forge build

Test

forge test

This should display a successful test message, as shown here:

[⠊] Compiling... No files changed, compilation skipped Ran 2 tests for test/Counter.t.sol:CounterTest [PASS] testFuzz_SetNumber(uint256) (runs: 256, μ: 30977, ~: 31288) [PASS] test_Increment() (gas: 31303) Suite result: ok. 2 passed; 0 failed; 0 skipped; finished in 19.97ms (9.69ms CPU time) Ran 1 test suite in 145.98ms (19.97ms CPU time): 2 tests passed, 0 failed, 0 skipped (2 total tests)