bm_staking.sh
Overview
bm_staking.sh is a Bash script designed to automate interactions with a blockchain smart contract system related to block manager staking and rewards. It primarily manages the lifecycle of a Block Manager Node Wallet by initializing it, fetching its details, starting the block manager process, and periodically requesting rewards.
The script communicates with smart contracts using the tvm-cli tool, which interacts with TON Virtual Machine (TVM) contracts through their ABI (Application Binary Interface) JSON files. It requires a private key file for authentication and uses an address root constant to locate the relevant contract root.
Key contract artifacts used:
AckiNackiBlockManagerNodeWallet.abi.json — ABI for the Block Manager Node Wallet contract.
BlockManagerContractRoot.abi.json — ABI for the Block Manager Contract Root.
File Constants and Initial Setup
BM_ABIandABIvariables point to the ABI JSON files used for interacting with the respective contracts.ROOT is a fixed address representing the root contract address in the blockchain.
The script accepts an optional parameter
-pto specify the sleep time interval between reward requests.The first positional argument after options is the path to the Block Manager's private key file (
BM_KEY).
Command-Line Parameters
-p: Optional flag to specify sleep time in seconds between successive reward requests.Positional argument:
BM_KEY: File path to the Block Manager private key JSON file.
Parameter Validation
The script verifies the presence of the private key file.
It extracts the public key from the private key JSON using
jq.Formats the public key to JSON format required by the contract call.
Core Functional Steps
Getting the Block Manager Node Wallet Address
Uses
tvm-cli runxto call methodgetAckiNackiBlockManagerNodeWalletAddresson the root contract.Passes the public key JSON to retrieve the unique wallet address for the block manager node.
The wallet address is extracted from the JSON response.
Fetching Wallet Details
Calls
getDetailson the block manager wallet contract using the wallet address and ABI.The details provide current status and configuration of the wallet.
If fetching details fails, the script exits with an error.
Starting the Block Manager Process
Invokes
startBMmethod on the block manager wallet contract using the private key for signing.After starting, the script sleeps for 10 seconds to allow contract processing.
Reward Processing Function
process_bm () {
tvm-cli -j callx --abi $BM_ABI --addr $BM_WALLET --keys $BM_KEY -m getReward
}
Calls the
getRewardmethod on the block manager wallet contract.This method triggers the reward claiming process.
It is called in an infinite loop with a delay controlled by
SLEEP_TIME.
Infinite Reward Loop
The script runs an infinite loop that:
Calls
process_bmto request rewards.Sleeps for the specified interval (
SLEEP_TIME) before repeating.
Error handling is adjusted (
set +e) during the sleep phase to avoid script termination.
Commented Code: Contract Data Decoding
Several commented lines demonstrate how to decode contract data fields such as
_start_bm,_walletLastTouch,_walletTouch,_license_num, and_epochEnd.These fields represent metadata related to the block manager wallet’s operational state.
Decoding is done via
tvm-cli decode account dataandjq, but these lines are currently disabled.
Implementation Details and Algorithms
The script relies on
tvm-clifor all blockchain interactions, which requires proper ABI files and addresses.JSON parsing and formatting is handled with
jqandsed.The infinite loop with sleep ensures continuous reward claiming without manual intervention.
Error handling is strict for critical steps (key reading, contract calls) but tolerant during the sleep cycle.
Interaction with Other System Components
Uses ABI files located in relative paths (
../contracts/bksystem/), indicating coupling with the smart contract artifacts under the project’s contract directory.Calls methods in the smart contracts deployed on the blockchain via the
tvm-clicommand-line interface.The private key file used must correspond to the block manager wallet’s keys.
The script is designed to be run on a node or system with access to the blockchain client environment.
Usage Example
./bm_staking.sh -p 60 /path/to/block_manager_private_key.json
Starts the block manager staking process.
Requests rewards every 60 seconds.
Mermaid Diagram: Flowchart of Main Functionality
flowchart TD
Start --> ParseOpts["Parse -p option"]
ParseOpts --> ValidateKey["Validate BM_KEY file"]
ValidateKey --> ExtractPubKey["Extract public key from BM_KEY"]
ExtractPubKey --> GetWalletAddr["Get BM Wallet Address via getAckiNackiBlockManagerNodeWalletAddress"]
GetWalletAddr --> GetWalletDetails["Get wallet details via getDetails"]
GetWalletDetails --> StartBM["Call startBM method"]
StartBM --> Sleep10s["Sleep 10 seconds"]
Sleep10s --> Loop["Enter infinite loop"]
Loop --> ProcessBM["Call getReward method"]
ProcessBM --> SleepInterval["Sleep SLEEP_TIME seconds"]
SleepInterval --> Loop
This flowchart represents the sequential steps of the script from initialization, contract calls, to the continuous reward claiming loop.