Foundry¶
Warning
Consider that features like Anvil (Foundry's local blockchain) and forge test
(for running Solidity tests) are not yet supported in foundry-polkadot
.
Overview¶
Foundry is a fast, modular, and extensible toolkit for Ethereum application development written in Rust. It provides a suite of command-line tools, including forge
for compiling, testing, and deploying smart contracts and cast
for interacting with blockchains.
foundry-polkadot
is an adaptation explicitly engineered for the Polkadot Hub, tailored for developers already familiar with Foundry who seek to leverage its capabilities within the Polkadot ecosystem. Additionally, this guide offers detailed information on the forge
and cast
commands supported within foundry-polkadot
, complete with simple, runnable examples for quick reference.
Installation¶
The installation process is tailored for the Polkadot variant:
-
foundry-polkadot
is installed viafoundryup-polkadot
, its dedicated installer. To get started, open your terminal and execute:curl -L https://raw.githubusercontent.com/paritytech/foundry-polkadot/refs/heads/master/foundryup/install | bash
This command starts the installation of
foundryup-polkadot
. After installation, run the following command to download the precompiledfoundry-polkadot
binaries:This command will install the
forge
andcast
binaries, which are explained below. Windows users must use a Unix-like terminal environment such as Git BASH or Windows Subsystem for Linux (WSL), as PowerShell and Command Prompt are not currently supported byfoundryup
.
Compiler Integration¶
A core divergence lies in the underlying Solidity compiler.
foundry
is built to interface with thesolc
compiler, which targets Ethereum's Ethereum Virtual Machine (EVM).-
foundry-polkadot
, in contrast, introduces and primarily utilizes theresolc
compiler to compile down Solidity contracts into PolkaVM bytecode.-
Command-Line Flag: For commands that involve compilation (e.g.,
forge build
), you can use the--resolc
flag to enableresolc
compilation. For example:This command instructs Forge to use
resolc
instead ofsolc
, generating bytecode compatible with PolkaVM. -
Configuration File: Alternatively, you can configure
resolc
usage in thefoundry.toml
file by adding:Setting
resolc_compile = false
reverts to usingsolc
, ensuring compatibility with Ethereum projects. By default,foundry-polkadot
usessolc
unlessresolc
is explicitly enabled.resolc
also exposes specific options for fine-tuning the compilation process, such as--use-resolc <RESOLC_VERSION>
for specifying a compiler version or path,-O, --resolc-optimizer-mode <LEVEL>
for setting optimization levels, and--heap-size <SIZE>
and--stack-size <SIZE>
for configuring contract memory.
-
Command-Line Interface (CLI)¶
foundry-polkadot
preserves the familiar forge
and cast
subcommand structure. However, it's crucial to note that commands which involve compilation (such as create
, bind
, build
, and inspect
) will yield different output when resolc
is utilized, as the generated bytecode is specifically designed for PolkaVM rather than the EVM.
Unsupported or Modified Features¶
Not all functionalities from the original Foundry are present or behave identically in foundry-polkadot
:
- Currently Unsupported:
- Compilation of Yul code is not yet supported.
- Support for factory contracts deployment is a known issue that is currently unresolved.
- Broader Feature Limitations: Integration with
Anvil
andChisel
(Foundry's local blockchain and EVM toolkit, respectively) is not available. This limitation directly impacts the support for several key commands, includingforge test
for running tests,forge snapshot
for creating blockchain state snapshots, andforge script
for complex deployment and interaction scripts. - Modified Feature: The most notable modification is in the compilation output. When
resolc
is employed, the resulting bytecode will fundamentally differ from that generated bysolc
, reflecting PolkaVM's distinct architectural requirements.
Set up a Project¶
Initialize a new project using forge init
:
This command creates a complete project structure with the following components:
src/
- Contains the Solidity smart contracts (includes a sampleCounter.sol
contract by default)lib/
- Houses external dependencies and libraries (forge-std
testing library is included)script/
- Stores deployment and interaction scripts (includesCounter.s.sol
deployment script by default)test/
- Contains your contract tests (includesCounter.t.sol
test file by default)foundry.toml
- Main configuration file for compiler settings, network configurations, and project preferences
The default project includes a simple Counter
contract that demonstrates basic state management through increment and decrement functions, along with corresponding tests and deployment scripts to help you get started quickly.
Compile a Project¶
Compile contracts using forge build
:
Note
You can still use forge build
for compiling to regular EVM bytecode.
PolkaVM bytecode starts with 0x505
prefix. Inspect compiled artifacts with:
If successful, you will see the following output:
Deploy a Contract¶
Deploy contracts using forge create
:
If the operation completes successfully, you'll see the following output (for example, to deploy to the Passet Hub chain):
[:] Compiling... Compiler run successful!
For contracts with constructor arguments:
forge create MyToken \
--rpc-url <INSERT_RPC_URL> \
--private-key <INSERT_PRIVATE_KEY> \
--constructor-args "MyToken" "MTK" 1000000 \
--resolc
Network Compatibility
Use the --resolc
flag when deploying to PolkaVM-compatible networks. Omit it for Ethereum-compatible networks.
Supported foundry-polkadot
Commands¶
This section provides a detailed breakdown of the forge
and cast
commands supported in foundry-polkadot
.
Forge Commands¶
-
init
- Command:
forge init <PROJECT_NAME>
- Description: Initializes a new Foundry project in the current directory, setting up the basic project structure and installing standard libraries.
- Command:
-
bind
- Command:
forge bind [--resolc]
- Description: Generates type-safe Rust bindings for your Solidity contracts. Use
--resolc
to ensure compilation with theresolc
compiler for PolkaVM compatibility.
- Command:
-
bind-json
- Command:
forge bind-json [--resolc]
- Description: Generates JSON bindings for your Solidity contracts. Use
--resolc
forresolc
-based compilation.
- Command:
-
build
- Command:
forge build [--resolc]
- Description: Compiles all Solidity contracts in your project. Specify
--resolc
to compile for PolkaVM.
- Command:
-
cache clean
- Command:
forge cache clean
- Description: Clears the Foundry cache directory.
- Command:
-
cache ls
- Command:
forge cache ls
- Description: Lists the contents of the Foundry cache.
- Command:
-
clean
- Command:
forge clean
- Description: Removes all build artifacts from the project's
out
directory.
- Command:
-
compiler resolve
- Command:
forge compiler resolve [--resolc]
- Description: Resolves and displays the versions of Solidity compilers Foundry is using. Use
--resolc
to also check forresolc
.
- Command:
-
config
- Command:
forge config
- Description: Displays the current Foundry project configuration, including settings from
foundry.toml
.
- Command:
-
create
- Command:
forge create [OPTIONS] <CONTRACT>
- Required Parameters:
<CONTRACT>
(the name of the contract to deploy) - Description: Deploys a new contract to a specified blockchain network. The
--resolc
flag ensures it's compiled for PolkaVM. You'll typically need to provide an RPC URL, a private key for the deployer account, and potentially constructor arguments.
- Command:
-
doc
- Command:
forge doc
- Description: Generates documentation for your Solidity contracts.
- Command:
-
flatten
- Command:
forge flatten [OPTIONS] <PATH>
- Required Parameters:
<PATH>
(the path to the Solidity file) - Description: Combines all imports of a Solidity file into a single file, useful for deployment or verification.
- Command:
-
fmt
- Command:
forge fmt
- Description: Formats Solidity code according to a predefined style.
- Command:
-
geiger
- Command:
forge geiger <PATH>
- Required Parameters:
<PATH>
(the path to the Solidity file) - Description: Analyzes Solidity code for potential security vulnerabilities and gas inefficiencies.
- Command:
-
generate test
- Command:
forge generate test --contract-name <CONTRACT_NAME>
- Required Parameters:
--contract-name <CONTRACT_NAME>
(the name of the contract for which to generate a test) - Description: Creates a new test file with boilerplate code for a specified contract.
- Command:
-
generate-fig-spec
- Command:
forge generate-fig-spec
- Description: Generates a Fig specification for CLI autocompletion tools.
- Command:
-
inspect
- Command:
forge inspect <CONTRACT_NAME> <ARTIFACT> [--resolc]
- Required Parameters:
<CONTRACT_NAME>
(the contract to inspect),<ARTIFACT>
(e.g.,bytecode
,abi
,methods
,events
) - Description: Displays various artifacts of a compiled contract. Use
--resolc
to inspectresolc
-compiled artifacts; the bytecode will start with0x505
.
- Command:
-
install
- Command:
forge install <REPOSITORY>
- Description: Installs a Solidity library or dependency from a Git repository.
- Command:
-
update
- Command:
forge update [<REPOSITORY>]
- Description: Updates installed dependencies. If a repository is specified, only that one is updated.
- Command:
-
remappings
- Command:
forge remappings
- Description: Lists the currently configured Solidity compiler remappings.
- Command:
-
remove
- Command:
forge remove <REPOSITORY>
- Description: Removes an installed Solidity dependency. Use
--force
to remove without confirmation.
- Command:
-
selectors upload
- Command:
forge selectors upload [--all]
- Description: Uploads function selectors from compiled contracts to OpenChain. Use
--all
to upload for all contracts.
- Command:
-
selectors list
- Command:
forge selectors list
- Description: Lists all known function selectors for contracts in the project.
- Command:
-
selectors find
- Command:
forge selectors find <SELECTOR>
- Description: Searches for a function signature given its 4-byte selector.
- Command:
-
selectors cache
- Command:
forge selectors cache
- Description: Caches function selectors for faster lookup.
- Command:
-
tree
- Command:
forge tree
- Description: Displays the dependency tree of your Solidity contracts.
- Command:
Non-working Commands
Consider that some foundry commands are not yet supported in foundry-polkadot
:
clone
: This command is not supported infoundry-polkadot
.coverage
: Code coverage analysis is not supported.snapshot
: Creating blockchain state snapshots is not supported.test
: Running Solidity tests is not supported.
Cast Commands¶
-
4byte
- Command:
cast 4byte [OPTIONS] [TOPIC_0]
- Description: Decodes a 4-byte function selector into its human-readable function signature.
- Command:
-
4byte-event
- Command:
cast 4byte-event [OPTIONS] [TOPIC_0]
- Description: Decodes a 4-byte event topic into its human-readable event signature.
- Command:
-
abi-encode
- Command:
cast abi-encode <SIG> [ARGS]...
- Required Parameters:
<SIG>
(the function signature),[ARGS]
(arguments to encode) - Description: ABI-encodes function arguments according to a given signature.
- Command:
-
address-zero
- Command:
cast address-zero
- Description: Returns the zero address (0x00...00).
- Command:
-
age
- Command:
cast age [OPTIONS] [BLOCK]
- Description: Converts a block number or tag (e.g.,
latest
) into its timestamp.
- Command:
-
balance
- Command:
cast balance [OPTIONS] <WHO>
- Required Parameters:
<WHO>
(the address to check) - Description: Retrieves the native token balance of a given address on the specified RPC network.
- Command:
-
base-fee
- Command:
cast base-fee [OPTIONS] [BLOCK]
- Description: Retrieves the base fee per gas for a specific block (defaults to
latest
).
- Command:
-
block
- Command:
cast block [OPTIONS] [BLOCK]
- Description: Retrieves comprehensive details about a specific block (defaults to
latest
).
- Command:
-
block-number
- Command:
cast block-number [OPTIONS] [BLOCK]
- Description: Retrieves the number of the latest or a specified block.
- Command:
-
call
- Command:
cast call [OPTIONS] <TO> <SIG> [ARGS]...
- Description: Executes a read-only (constant) function call on a contract. No transaction is sent to the network.
- Command:
-
chain
- Command:
cast chain [OPTIONS]
- Description: Displays the human-readable name of the connected blockchain.
- Command:
-
chain-id
- Command:
cast chain-id [OPTIONS]
- Description: Displays the chain ID of the connected blockchain.
- Command:
-
client
- Command:
cast client [OPTIONS]
- Description: Retrieves information about the connected RPC client (node software).
- Command:
-
code
- Command:
cast code [OPTIONS] <WHO>
- Required Parameters:
<WHO>
(the contract address) - Description: Retrieves the bytecode deployed at a given contract address.
- Command:
-
codesize
- Command:
cast codesize [OPTIONS] <WHO>
- Required Parameters:
<WHO>
(the contract address) - Description: Retrieves the size of the bytecode deployed at a given contract address.
- Command:
-
compute-address
- Command:
cast compute-address [OPTIONS] <WHO>
- Required Parameters:
<WHO>
(the deployer's address) - Description: Computes the predicted contract address based on the deployer's address and nonce.
- Command:
-
decode-abi
- Command:
cast decode-abi <SIG> <CALLDATA>
- Required Parameters:
<SIG>
(the function signature),<CALLDATA>
(the ABI-encoded data) - Description: Decodes ABI-encoded output data from a contract call given its signature.
- Command:
-
decode-calldata
- Command:
cast decode-calldata <SIG> <CALLDATA>
- Required Parameters:
<SIG>
(the function signature),<CALLDATA>
(the raw calldata) - Description: Decodes raw calldata into human-readable arguments using a function signature.
- Command:
-
decode-error
- Command:
cast decode-error <DATA> [--sig <SIGNATURE>]
- Required Parameters:
<DATA>
(the error data) - Description: Decodes a custom error message from a transaction revert. You may need to provide the error signature.
- Command:
-
decode-event
- Command:
cast decode-event <DATA> [--sig <SIGNATURE>]
- Required Parameters:
<DATA>
(the event data) - Description: Decodes event data from a transaction log.
- Command:
-
estimate
- Command:
cast estimate [OPTIONS] [TO] [SIG] [ARGS]...
- Required Parameters:
[TO]
(the recipient address or contract),[SIG]
(function signature),[ARGS]
(arguments) - Description: Estimates the gas cost for a transaction or function call.
- Command:
-
find-block
- Command:
cast find-block [OPTIONS] <TIMESTAMP>
- Required Parameters:
<TIMESTAMP>
(a Unix timestamp) - Description: Finds the closest block number to a given Unix timestamp.
- Command:
-
gas-price
- Command:
cast gas-price [OPTIONS]
- Description: Retrieves the current average gas price on the network.
- Command:
-
generate-fig-spec
- Command:
cast generate-fig-spec
- Description: Generates a Fig specification for CLI autocompletion.
- Command:
-
index-string
- Command:
cast index-string <STRING> <INDEX>
- Description: Computes the Keccak-256 hash of a string, useful for event topics.
- Command:
-
index-erc7201
- Command:
cast index-erc7201 <VALUE>
- Description: Computes the hash for an ERC-7201 identifier.
- Command:
-
logs
- Command:
cast logs [OPTIONS] [SIG_OR_TOPIC] [TOPICS_OR_ARGS]...
- Required Parameters:
[SIG_OR_TOPIC]
(a signature or topic hash) - Description: Filters and displays event logs from transactions.
- Command:
-
max-int
- Command:
cast max-int
- Description: Displays the maximum value for a signed 256-bit integer.
- Command:
-
max-uint
- Command:
cast max-uint
- Description: Displays the maximum value for an unsigned 256-bit integer.
- Command:
-
min-int
- Command:
cast min-int
- Description: Displays the minimum value for a signed 256-bit integer.
- Command:
-
mktx
- Command:
cast mktx [OPTIONS] [TO] [SIG] [ARGS]...
- Required Parameters:
[TO]
(the recipient address or contract) - Description: Creates a raw, signed transaction that can be broadcast later.
- Command:
-
decode-transaction
- Command:
cast decode-transaction [OPTIONS] [TX]
- Required Parameters:
[TX]
(the raw transaction hex string) - Description: Decodes a raw transaction hex string into its human-readable components.
- Command:
-
namehash increment
- Command:
cast namehash <NAME>
- Description: Computes the ENS (Ethereum Name Service) namehash for a given name.
- Command:
-
nonce
- Command:
cast nonce [OPTIONS] <WHO>
- Required Parameters:
<WHO>
(the address to check) - Description: Retrieves the transaction count (nonce) for a given address.
- Command:
-
parse-bytes32-address
- Command:
cast parse-bytes32-address <VALUE>
- Description: Parses a 32-byte hex string (e.g., from
bytes32
) into an Ethereum address.
- Command:
-
parse-bytes32-string
- Command:
cast parse-bytes32-string <VALUE>
- Description: Parses a 32-byte hex string into a human-readable string.
- Command:
-
parse-units
- Command:
cast parse-units <AMOUNT> [UNIT]
- Description: Converts a human-readable amount into its smallest unit (e.g., Ether to Wei). Defaults to
ether
.
- Command:
-
pretty-calldata
- Command:
cast pretty-calldata [OPTIONS] <DATA>
- Required Parameters:
<DATA>
(the calldata hex string) - Description: Attempts to pretty-print and decode a raw calldata string into possible function calls.
- Command:
-
publish
- Command:
cast publish [OPTIONS] <RAW_TX>
- Description: Broadcasts a raw, signed transaction to the network.
- Command:
-
receipt
- Command:
cast receipt [OPTIONS] <TX_HASH>
- Description: Retrieves the transaction receipt for a given transaction hash, including status, gas usage, and logs.
- Command:
-
rpc
- Command:
cast rpc [OPTIONS] <METHOD> [PARAMS]...
- Required Parameters:
<METHOD>
(the RPC method to call),[PARAMS]
(parameters for the method) - Description: Makes a direct RPC call to the connected blockchain node.
- Command:
-
send
- Command:
cast send [OPTIONS] <TO> <SIG> [ARGS]...
- Required Parameters:
<TO>
(the recipient address or contract) - Description: Sends a transaction to a contract or address, executing a function or transferring value.
- Command:
-
sig
- Command:
cast sig <FUNCTION_SIGNATURE>
- Required Parameters:
<FUNCTION_SIGNATURE>
(the full function signature string) - Description: Computes the 4-byte function selector for a given function signature.
- Command:
-
sig-event
- Command:
cast sig-event <EVENT_SIGNATURE>
- Required Parameters:
<EVENT_SIGNATURE>
(the full event signature string) - Description: Computes the Keccak-256 hash (topic) for a given event signature.
- Command:
-
storage
- Command:
cast storage [OPTIONS] <ADDRESS> [SLOT]
- Required Parameters:
<ADDRESS>
(the contract address) - Description: Retrieves the raw value stored at a specific storage slot of a contract.
- Command:
-
tx
- Command:
cast tx [OPTIONS] <TX_HASH>
- Description: Retrieves comprehensive details about a specific transaction.
- Command:
-
upload-signature
- Command:
cast upload-signature [OPTIONS] <SIGNATURE_STRING>
- Required Parameters:
<SIGNATURE_STRING>
(the function or event signature) - Description: Uploads a function or event signature to the OpenChain registry.
- Command:
-
wallet
- Command:
cast wallet new
- Description: Generates a new random Ethereum keypair (private key and address).
- Command:
-
wallet new-mnemonic
- Command:
cast wallet new-mnemonic
- Description: Generates a new BIP-39 mnemonic phrase and derives the first account from it.
- Command:
-
wallet address
- Command:
cast wallet address [OPTIONS]
- Description: Derives and displays the Ethereum address from a private key or mnemonic (if provided).
- Command:
Non-working Commands
Consider that some foundry commands are not yet supported in foundry-polkadot
:
proof
: This command, used for generating Merkle proofs, is not supported.storage-root
: This command, used for retrieving the storage root of a contract, is not supported.
| Created: July 1, 2025