1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! # Bevy Dojo: A Bevy plugin for Starknet integration
//!
//! `bevy_dojo` provides a plugin for integrating Starknet blockchain functionality
//! with the Bevy game engine. It enables executing Starknet transactions from Bevy
//! systems in a non-blocking way, making it ideal for games that want to interact
//! with smart contracts on Starknet.
//!
//! ## Features
//!
//! - Non-blocking Starknet connection management
//! - Transaction execution with automatic status monitoring
//! - Environment variable or explicit configuration options
//! - Seamless integration with Bevy's ECS
//!
//! ## Setup
//!
//! Add the plugin to your Bevy app:
//!
//! ```rust
//! use bevy::prelude::*;
//! use bevy_dojo::prelude::*;
//!
//! fn main() {
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_plugins(BevyDojoPlugin)
//! .add_systems(Update, keyboard_control)
//! .run();
//! }
//! ```
//!
//! ## Environment Variables
//!
//! The plugin uses the following environment variables by default:
//!
//! - `STARKNET_RPC_URL`: URL of your Starknet RPC provider
//! - `STARKNET_ACCOUNT_ADDRESS`: Your Starknet account address (as a hex string)
//! - `STARKNET_PRIVATE_KEY`: Your private key (as a hex string)
//!
//! Alternatively, you can provide these values explicitly by replacing the
//! `DefaultStarknetConfig` resource.
//!
//! ## Example: Keyboard-controlled Connection and Transactions
//!
//! ```no_run
//! use bevy::prelude::*;
//! use bevy_dojo::prelude::*;
//! use starknet::core::types::{Call, Felt};
//! use std::str::FromStr;
//!
//! fn keyboard_control(
//! keys: Res<Input<KeyCode>>,
//! runtime: Res<TokioRuntime>,
//! config: Res<DefaultStarknetConfig>,
//! mut sn: ResMut<StarknetConnection>,
//! ) {
//! // Connect to Starknet when the user presses C
//! if keys.just_pressed(KeyCode::C) {
//! init_starknet_connection(runtime, config, sn);
//! }
//!
//! // Execute a transaction when the user presses T
//! if keys.just_pressed(KeyCode::T) {
//! let calls = vec![
//! Call {
//! to: Felt::from_str("0x123456...").unwrap(), // Contract address
//! selector: Felt::from_str("0x987654...").unwrap(), // Function selector
//! calldata: vec![], // Function arguments
//! },
//! ];
//!
//! execute_transaction(runtime, sn, calls);
//! }
//! }
//! ```
//!
//! ## Connection Status
//!
//! You can check the connection status by examining the `StarknetConnection` resource:
//!
//! ```no_run
//! fn display_connection_status(sn: Res<StarknetConnection>) {
//! if sn.is_connected() {
//! println!("Connected to Starknet");
//! } else if sn.is_connecting() {
//! println!("Connecting to Starknet...");
//! } else {
//! println!("Not connected to Starknet");
//! }
//! }
//! ```
// Re-export modules
// Import and re-export main types for convenience
use *;
// Main prelude module that users can import
/// Starknet integration plugin with default configuration
///
/// This plugin initializes all resources needed for Starknet integration:
/// - Adds the `TokioPlugin` to create a Tokio runtime
/// - Initializes the `StarknetConnection` resource
/// - Initializes the `DefaultStarknetConfig` resource
/// - Registers the `check_sn_task` system to monitor async tasks
///
/// # Example
///
/// ```no_run
/// use bevy::prelude::*;
/// use bevy_dojo::prelude::*;
///
/// fn main() {
/// App::new()
/// .add_plugins(DefaultPlugins)
/// .add_plugins(BevyDojoPlugin)
/// .run();
/// }
/// ```
;