The multichainr package provides a high-level R
interface to the MultiChain API. This guide walks you through setting up
binary paths, creating a temporary test blockchain, connecting to a
node, and performing basic cleanup.
To interact with the blockchain, the package needs to locate the
MultiChain executable files (multichaind and
multichain-util).
The most convenient way is to set the path in your environment
variables. Open your .Renviron file (you can use
usethis::edit_r_environ()) and add the following line:
MULTICHAIN_PATH="C:/path/to/multichain"
(Replace the path with the actual directory containing your MultiChain binaries.)
Let’s create a “sandbox” — a temporary blockchain named
vignette_chain for testing purposes.
Once initialized, we can launch the MultiChain node. By default, it starts in daemon mode (background process).
To communicate with the node, we need to retrieve its configuration
(RPC port, username, and password) and create a connection object.
multichainr handles this by reading the node’s
configuration files automatically.
# Get the configuration for the specific chain
config <- mc_get_config(chain_name)
# Create a connection object
conn <- mc_connect(config)
# Verify the node status
info <- mc_get_info(conn)
cat("Connected to chain:", info$chainname, "\n")
cat("Protocol Version:", info$protocolversion, "\n")
cat("Current Block Height:", info$blocks, "\n")After finishing your work, it is important to stop the node and, if the blockchain was temporary, delete its data directory to free up disk space.
# Send the stop signal to the node
mc_node_stop(conn)
# Brief pause to allow the process to finalize file writing
Sys.sleep(2)
# Deleting the blockchain files (WARNING: This is irreversible!)
# Determine the default MultiChain data directory based on the OS
if (.Platform$OS.type == "windows") {
base_dir <- file.path(Sys.getenv("APPDATA"), "MultiChain")
} else if (Sys.info()["sysname"] == "Darwin") {
base_dir <- file.path(Sys.getenv("HOME"), "Library/Application Support/MultiChain")
} else {
base_dir <- file.path(Sys.getenv("HOME"), ".multichain")
}
chain_dir <- file.path(base_dir, chain_name)
if (dir.exists(chain_dir)) {
unlink(chain_dir, recursive = TRUE)
message("Temporary blockchain files deleted successfully.")
}In this guide, we covered:
MULTICHAIN_PATH or mc_set_path().In the next vignettes, we will explore how to manage assets, issue tokens, and store data in streams.
MULTICHAIN_PATH, making the user’s workflow much
smoother.eval = FALSE
setting in the first chunk ensures that the vignette can be built into a
package website (like pkgdown) even if the server building
it doesn’t have MultiChain installed.