blk
Ethereum and Web3 client - JSON-RPC calls, wallet key-pairs, transaction signing, ABI encoding, IPFS add/get, and ENS name resolution.
Load with: use blk
What this module does
blk is an Ethereum/EVM client. You point it at a JSON-RPC endpoint
(Infura, Alchemy, a local node, etc.) and can then check balances, send transactions,
call smart contracts, estimate gas, and query blocks. It also has a simple wallet
(key-pair generation and message signing), basic ABI encoding for
uint256 and address parameters, IPFS add/get via the
HTTP API, and ENS name resolution.
Quick example
use blk
# Connect to a node
c = ethne("https://mainnet.infura.io/v3/YOUR_KEY")
# Check an account balance
bal = ethgb(c, "0xYourAddress")
prn(bal)
# Generate a wallet
w = walne()
prn(walad(w)) # derived public address
# Sign a message
sig = walsg(w, "hello")
# Create and sign a transaction
tx = txne(walad(w), "0xRecipient", 1000000, nil)
tx = txsg(tx, w.ac)
# Get current gas price
prn(ethgg(c))
Functions
JSON-RPC client
ethne(url)
Creates a client pointing at the given JSON-RPC URL. Sets an incrementing request ID counter starting at 1.
ethcl(c, method, params)
Makes a JSON-RPC call. Sends a POST request with {jsonrpc: "2.0", id, method, params}, increments the ID counter, and returns the result field. Returns nil on HTTP error or JSON-RPC error.
Account and chain
ethgb(c, addr)
Gets the ETH balance of addr at the latest block via eth_getBalance.
ethec(c, tx)
Sends a raw transaction object via eth_sendTransaction. Returns the transaction hash.
ethgs(c, txhash)
Gets a transaction receipt for the given hash via eth_getTransactionReceipt.
ethnb(c)
Returns the current block number via eth_blockNumber.
ethg0(c, n)
Gets a full block by number (with transaction objects) via eth_getBlockByNumber.
Gas
ethgg(c)
Returns the current gas price via eth_gasPrice.
ethge(c, tx)
Estimates the gas required for a transaction via eth_estimateGas.
Smart contracts
ethcc(c, binary_addr, abi)
Creates a contract instance from a deployed address and an ABI list. Returns a contract object that holds the client, address, and ABI.
ethc0(contract, method, params)
Calls a contract function (read-only, no gas) via eth_call. Encodes the call data using blknc.
ethcs(contract, method, params, account)
Sends a state-changing transaction to a contract method via eth_sendTransaction. Encodes call data using blknc.
blknc(abi, method, params)
Encodes a contract call. Looks up the function in the ABI by name, computes the 4-byte function selector (first 4 bytes of keccak256(signature) via cry.sh4), and encodes each parameter - uint256 as a zero-padded 64-character hex string, address with the 0x prefix stripped and zero-padded to 64 characters.
Wallet
walne()
Generates a new wallet with a 32-byte random private key via cry.cryrn.
walad(w)
Derives the public address from the wallet's private key via cry.cryfp.
walsg(w, msg)
Signs a message with the wallet's private key (SHA-256 of private key + message via cry.sh256, then signs with cry.crys2).
Transactions
txne(from, to, value, data)
Creates a transaction object with default gas limit 21000, gas price "0x1", and nonce 0. data defaults to "0x" if nil.
txsg(tx, priv_key)
Signs a transaction by computing SHA-256 of from + to + value + data and signing with cry.crys2. Sets tx.sg. Returns the updated transaction.
IPFS
ipfsn(url)
Creates an IPFS client pointing at the given API URL (e.g. "http://localhost:5001").
ipfsd(c, data)
Adds data to IPFS via the /api/v0/add endpoint. Returns the content hash (CID), or nil on failure.
ipfsg(c, hash)
Retrieves content from IPFS by hash via /api/v0/cat?arg=hash. Returns the content, or nil on failure.
ENS
ensre(c, name)
Resolves an ENS name to an address via the ens_resolver JSON-RPC method. Returns the addr field of the response, or nil.
Notes
- ABI encoding only handles
uint256andaddressparameter types - other types (strings, arrays, tuples, etc.) are not implemented. - All JSON-RPC errors return
nil- check your node endpoint and parameters if you get unexpected nils. - Transaction signing uses
cry.sh256andcry.crys2- this is a simplified signing scheme and is not compatible with standard Ethereum ECDSA signing (EIP-155). - Requires
trl,txt,cry, andreq.