Build a DeCharge Machine
Last updated
Last updated
Before proceeding, ensure you have cloned the repository:
The DeCharge machine demonstrates a continuous charging model, where a device operates over time and charges users incrementally. Its messaging network leverages Nostr for event-driven communication, with Solana handling payments via lock
and settle
.
An online demo of these examples is available at:
Run DePHY vending machine workers by: docker compose up
The docker compose
setup and the App demo application integrate both DeCharge and Gacha use cases.
Ensure all dependencies are installed before running the application.
Message Definition: Messages are defined as DephyDechargeMessage
(in message.rs
), with two variants:
Request
: Initiates a state change (e.g., user requests the machine to start).
Status
: Confirms a state update (e.g., machine is now Working).
Mention Tag (Machine Pubkey): The p
tag identifies the target machine using its Nostr public key. For example, PublicKey::parse("machine_pubkey")
specifies which device receives the message.
Session Tag: The s
tag scopes events to a specific session (e.g., "dephy-decharge-controller
"), ensuring messages are isolated to the current context.
Publishing and Subscription: The RelayClient
publishes messages to a Nostr relay and subscribes to events using filters:
Filter: Retrieves events since started_at
, scoped to the session and machine pubkey.
Sender: Typically a user or admin via the dApp or CLI.
Receiver: The DeCharge node controller handling the machine.
Message Handling: The MessageHandler processes events, updating machine states and coordinating with Solana.
Node: Listens for user Request
events, verifies eligibility with check_eligible
, and updates machine state (e.g., Available
to Working
). It notifies the server via Status
events.
Server: Monitors Status
events, locks funds with lock
when the machine starts, and settles the final amount with settle
when it stops. This split ensures state management and payment processing are separate but coordinated.
Controller: Initiates state change and verifies eligibility
Server: Locks and settles funds