2
2
////!
3
3
////! This module provides utility functions for common Ethereum operations in the Hyperware
4
4
////! ecosystem, particularly focusing on integrating with Hypermap for name resolution.
5
- ////!
5
+ ////!
6
6
////! The main goals of this module are:
7
7
////! 1. Provide simple, developer-friendly functions for common operations
8
8
////! 2. Abstract away the complexity of blockchain interactions
12
12
////!
13
13
////! ```rust
14
14
////! use hyperware_process_lib::eth_utils;
15
- ////!
15
+ ////!
16
16
////! // Send ETH to a Hypermap name
17
17
////! let tx_hash = eth_utils::send_eth("alice.hypr", 1.3)?;
18
- ////!
18
+ ////!
19
19
////! // Check if a wallet owns an NFT
20
20
////! let has_token = eth_utils::has_nft(contract_address, token_id, wallet_address)?;
21
- ////!
21
+ ////!
22
22
////! // Get a token balance
23
23
////! let balance = eth_utils::get_token_balance(token_address, wallet_address)?;
24
- ////!
24
+ ////!
25
25
////! // Send tokens to a Hypermap name
26
26
////! let tx_hash = eth_utils::send_token_to_name(token_address, "bob.hypr", amount)?;
27
27
////! ```
28
28
//
29
29
//use crate::eth::{
30
- // Address,
31
- // EthError,
32
- // TxHash,
30
+ // Address,
31
+ // EthError,
32
+ // TxHash,
33
33
// U256
34
34
//};
35
35
//use crate::hypermap::{Hypermap, HYPERMAP_ADDRESS};
56
56
//
57
57
// #[error("Name resolution error: {0}")]
58
58
// NameResolution(String),
59
- //
59
+ //
60
60
// #[error("Transaction error: {0}")]
61
61
// Transaction(String),
62
62
//}
63
63
//
64
64
///// Send Ether to an address
65
- /////
65
+ /////
66
66
///// This function creates, signs, and sends a transaction to send ETH to an address.
67
67
/////
68
68
///// # Parameters
75
75
/////
76
76
///// # Returns
77
77
///// A `Result<TxHash, EthUtilsError>` representing the transaction hash if successful
78
- /////
78
+ /////
79
79
///// # Example
80
80
///// ```rust
81
81
///// use hyperware_process_lib::{eth_utils, wallet, eth};
82
82
///// use alloy_primitives::{Address, U256};
83
83
///// use std::str::FromStr;
84
- /////
84
+ /////
85
85
///// // Create wallet and provider
86
86
///// let wallet = wallet::Wallet::from_private_key(
87
87
///// "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
90
90
///// let provider = eth::Provider::new(8453, 60000);
91
91
///// let to = Address::from_str("0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf")?;
92
92
///// let amount = U256::from(1000000000000000000u64); // 1 ETH
93
- /////
93
+ /////
94
94
///// // Send ETH
95
95
///// let tx_hash = eth_utils::send_eth(&provider, &wallet, to, amount, None, None)?;
96
96
///// println!("Transaction hash: {}", tx_hash);
106
106
// // Create RLP-encoded transaction
107
107
// let nonce = provider.get_transaction_count(wallet.address(), None)?;
108
108
// let nonce_u64 = u64::try_from(nonce).unwrap_or(0);
109
- //
109
+ //
110
110
// // Get gas price if not provided
111
111
// let gas_price_value = if let Some(price) = gas_price {
112
112
// price
113
113
// } else {
114
114
// let current_gas_price = provider.get_gas_price()?;
115
115
// u128::try_from(current_gas_price).unwrap_or(20000000000)
116
116
// };
117
- //
117
+ //
118
118
// // Get gas limit
119
119
// let gas_limit_value = gas_limit.unwrap_or(21000);
120
- //
120
+ //
121
121
// // Create and sign a transaction manually
122
122
// // First, construct the RLP-encoded transaction
123
123
// let mut rlp_data = Vec::new();
126
126
// rlp_data.extend_from_slice(&nonce_u64.to_be_bytes());
127
127
// rlp_data.extend_from_slice(&gas_limit_value.to_be_bytes());
128
128
// rlp_data.extend_from_slice(&gas_price_value.to_be_bytes());
129
- //
129
+ //
130
130
// // Hash the transaction data with keccak256
131
131
// let mut hasher = sha3::Keccak256::new();
132
132
// hasher.update(&rlp_data);
133
133
// let tx_hash = hasher.finalize();
134
- //
134
+ //
135
135
// // Sign the transaction hash
136
136
// let signed_tx = wallet.sign_transaction_hash(&tx_hash)?;
137
- //
137
+ //
138
138
// // Send raw transaction
139
139
// let tx_hash = provider.send_raw_transaction(signed_tx)?;
140
- //
140
+ //
141
141
// Ok(tx_hash)
142
142
//}
143
143
//
144
144
///// Sends Ether to the owner of the specified Hypermap name.
145
- /////
145
+ /////
146
146
///// This function first resolves the name to its owner address using Hypermap,
147
147
///// then sends the specified amount of Ether to that address.
148
148
/////
154
154
/////
155
155
///// # Returns
156
156
///// A `Result<TxHash, EthUtilsError>` representing the transaction hash if successful
157
- /////
157
+ /////
158
158
///// # Example
159
159
///// ```rust
160
160
///// use hyperware_process_lib::{eth_utils, wallet, eth};
161
- /////
161
+ /////
162
162
///// // Create wallet and provider
163
163
///// let wallet = wallet::Wallet::from_private_key(
164
164
///// "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
165
165
///// 8453 // Base chain ID
166
166
///// )?;
167
167
///// let provider = eth::Provider::new(8453, 60000);
168
- /////
168
+ /////
169
169
///// // Send 1.3 ETH to alice.hypr
170
170
///// let tx_hash = eth_utils::send_eth_to_name(&provider, &wallet, "alice.hypr", 1.3)?;
171
171
///// println!("Transaction hash: {}", tx_hash);
172
172
///// ```
173
173
//pub fn send_eth_to_name(
174
174
// provider: &crate::eth::Provider,
175
175
// wallet: &Wallet,
176
- // name: &str,
176
+ // name: &str,
177
177
// amount_eth: f64
178
178
//) -> Result<TxHash, EthUtilsError> {
179
179
// // Get Hypermap instance using our provider
180
- // let hypermap = Hypermap::new(provider.clone(),
180
+ // let hypermap = Hypermap::new(provider.clone(),
181
181
// Address::from_str(HYPERMAP_ADDRESS).unwrap());
182
182
//
183
183
// // Format the name if needed (add .hypr if missing)
186
186
// // Resolve name to owner address
187
187
// let (_, owner, _) = hypermap.get(&formatted_name)
188
188
// .map_err(|e| EthUtilsError::NameResolution(format!("Failed to resolve name '{}': {}", formatted_name, e)))?;
189
- //
189
+ //
190
190
// // Convert amount to wei (1 ETH = 10^18 wei)
191
191
// let amount_wei = (amount_eth * 1e18) as u128;
192
192
// let amount_in_wei = U256::from(amount_wei);
193
- //
193
+ //
194
194
// // Send ETH to the resolved address
195
195
// send_eth(provider, wallet, owner, amount_in_wei, None, None)
196
196
//}
197
197
//
198
198
///// Format a name for Hypermap resolution
199
- /////
199
+ /////
200
200
///// If the name already contains a dot (.), it's returned as is.
201
201
///// Otherwise, ".hypr" is appended to the name.
202
- /////
202
+ /////
203
203
///// # Parameters
204
204
///// - `name`: The name to format
205
- /////
205
+ /////
206
206
///// # Returns
207
207
///// A formatted name suitable for Hypermap resolution
208
208
//fn format_hypermap_name(name: &str) -> String {
209
209
// // If name already has a domain extension, return as is
210
210
// if name.contains('.') {
211
211
// return name.to_string();
212
212
// }
213
- //
213
+ //
214
214
// // Otherwise, add the default .hypr extension
215
215
// format!("{}.hypr", name)
216
216
//}
217
217
//
218
218
///// Resolve a Hypermap name to its owner's Ethereum address
219
- /////
219
+ /////
220
220
///// # Parameters
221
221
///// - `name`: The Hypermap name to resolve
222
222
///// - `chain_id`: Optional chain ID to use (defaults to Base chain)
223
223
///// - `timeout_ms`: Optional timeout in milliseconds (defaults to 60 seconds)
224
- /////
224
+ /////
225
225
///// # Returns
226
226
///// A `Result<Address, EthError>` representing the owner's Ethereum address
227
- /////
227
+ /////
228
228
///// # Example
229
229
///// ```rust
230
230
///// use hyperware_process_lib::eth_utils;
231
- /////
231
+ /////
232
232
///// let owner = eth_utils::resolve_name("alice.hypr", None, None)?;
233
233
///// println!("Owner address: {}", owner);
234
234
///// ```
240
240
// // Use provided chain ID or default
241
241
// let chain_id = chain_id.unwrap_or(DEFAULT_CHAIN_ID);
242
242
// let timeout = timeout_ms.unwrap_or(DEFAULT_TIMEOUT_MS);
243
- //
243
+ //
244
244
// // Create provider
245
245
// let provider = crate::eth::Provider::new(chain_id, timeout);
246
- //
246
+ //
247
247
// // Get Hypermap instance using our provider
248
- // let hypermap = Hypermap::new(provider,
248
+ // let hypermap = Hypermap::new(provider,
249
249
// Address::from_str(HYPERMAP_ADDRESS).unwrap());
250
250
//
251
251
// // Format the name if needed (add .hypr if missing)
252
252
// let formatted_name = format_hypermap_name(name);
253
253
//
254
254
// // Resolve name to owner address
255
255
// let (_, owner, _) = hypermap.get(&formatted_name)?;
256
- //
256
+ //
257
257
// Ok(owner)
258
258
//}
259
259
//
268
268
// // Test with name that already has dot
269
269
// let name_with_dot = "test.hypr";
270
270
// assert_eq!(format_hypermap_name(name_with_dot), name_with_dot);
271
- //
271
+ //
272
272
// // Test with name that doesn't have dot
273
273
// let name_without_dot = "test";
274
274
// assert_eq!(format_hypermap_name(name_without_dot), "test.hypr");
275
275
// }
276
276
//
277
277
// // Note: These tests would need real providers and wallets to run
278
278
// // We'll implement placeholders that describe what should be tested
279
- //
279
+ //
280
280
// #[test]
281
281
// #[ignore] // Ignore this test since it requires network connectivity
282
282
// fn test_resolve_name() {
295
295
// // let result = send_eth_to_name(&provider, &wallet, "test.hypr", 0.001);
296
296
// // assert!(result.is_ok());
297
297
// }
298
- //}
298
+ //}
0 commit comments