## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## ----setup--------------------------------------------------------------------
# library(multichainr)
# 
# # Set the path to your MultiChain binaries
# mc_set_path(Sys.getenv("MULTICHAIN_PATH"))

## ----init---------------------------------------------------------------------
# chain_name <- "vars_demo_chain"
# 
# # Create and start the node
# mc_node_init(chain_name)
# mc_node_start(chain_name)
# 
# # Wait for the node to initialize
# Sys.sleep(3)
# 
# # Connect to the local node
# config <- mc_get_config(chain_name)
# conn <- mc_connect(config)

## ----create_var---------------------------------------------------------------
# var_name <- "system_config"
# 
# # Create a variable that is open for updates
# # We can also provide an optional initial value
# initial_val <- list(version = "1.0.0", status = "initializing")
# 
# mc_create_variable(conn, var_name, open = TRUE, value = initial_val)
# 
# # Retrieve basic info about the variable
# info <- mc_get_variable_info(conn, var_name)
# print(info)

## ----updates------------------------------------------------------------------
# # Update 1: Change status to 'active'
# mc_set_variable_value(conn, var_name, list(version = "1.0.0", status = "active"))
# 
# Sys.sleep(1) # Brief pause to simulate time passing
# 
# # Update 2: Upgrade version and add new fields
# new_config <- list(
#   version = "1.1.0",
#   status = "active",
#   last_maintenance = "2025-04-02",
#   threshold = 85
# )
# 
# mc_set_variable_value(conn, var_name, new_config)

## ----current_state------------------------------------------------------------
# current_val <- mc_get_variable_value(conn, var_name)
# 
# cat("Current System Version:", current_val$version, "\n")
# cat("Current Status:", current_val$status, "\n")

## ----history------------------------------------------------------------------
# # Retrieve the full history of changes
# history_df <- mc_get_variable_history(conn, var_name, verbose = TRUE)
# 
# # The data frame includes the value, the blocktime, and the transaction ID
# print(history_df[, c("blocktime", "value")])
# 
# # You can see exactly how the 'threshold' or 'status' changed over time
# # for auditing or debugging purposes.

## ----cleanup------------------------------------------------------------------
# # Stop the node
# mc_node_stop(conn)
# Sys.sleep(2)
# 
# # Determine data directory
# 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)
# }

