hedera 0.43.0

The SDK for interacting with Hedera Hashgraph.
Documentation
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
use hedera::{
    AccountBalanceQuery,
    AccountCreateTransaction,
    Client,
    CustomFixedFee,
    Hbar,
    Key,
    PrivateKey,
    TokenCreateTransaction,
    TokenId,
    TopicCreateTransaction,
    TopicInfoQuery,
    TopicMessageSubmitTransaction,
    TopicUpdateTransaction,
    TransactionId,
};

use crate::common::{
    setup_nonfree,
    TestEnvironment,
};
use crate::topic::Topic;

#[tokio::test]
async fn basic() -> anyhow::Result<()> {
    let Some(TestEnvironment { config, client }) = setup_nonfree() else {
        return Ok(());
    };

    let Some(op) = &config.operator else {
        log::debug!("skipping test due to missing operator");
        return Ok(());
    };

    let topic_id = TopicCreateTransaction::new()
        .admin_key(op.private_key.public_key())
        .topic_memo("[e2e::TopicCreateTransaction]")
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?
        .topic_id
        .unwrap();

    let topic = Topic { id: topic_id };

    topic.delete(&client).await?;

    Ok(())
}

#[tokio::test]
async fn fieldless() -> anyhow::Result<()> {
    let Some(TestEnvironment { config: _, client }) = setup_nonfree() else {
        return Ok(());
    };

    let _topic_id = TopicCreateTransaction::new()
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?
        .topic_id
        .unwrap();
    Ok(())
}

#[tokio::test]
async fn autoset_auto_renew_account() -> anyhow::Result<()> {
    let Some(TestEnvironment { config: _, client }) = setup_nonfree() else {
        return Ok(());
    };

    let topic_id = TopicCreateTransaction::new()
        .admin_key(client.get_operator_public_key().unwrap())
        .topic_memo("[e2e::TopicCreateTransaction]")
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?
        .topic_id
        .unwrap();

    let info = TopicInfoQuery::new().topic_id(topic_id).execute(&client).await?;
    assert_eq!(info.auto_renew_account_id.unwrap(), client.get_operator_account_id().unwrap());
    Ok(())
}

async fn create_token(client: &Client) -> anyhow::Result<TokenId> {
    let operator_account_id = client.get_operator_account_id().unwrap();
    let operator_key = client.get_operator_public_key().unwrap();

    let receipt = TokenCreateTransaction::new()
        .name("Test Token")
        .symbol("FT")
        .treasury_account_id(operator_account_id)
        .initial_supply(1_000_000)
        .decimals(2)
        .admin_key(operator_key.clone())
        .supply_key(operator_key)
        .execute(client)
        .await?
        .get_receipt(client)
        .await?;

    Ok(receipt.token_id.unwrap())
}

#[tokio::test]
async fn creates_and_updates_revenue_generating_topic() -> anyhow::Result<()> {
    let Some(TestEnvironment { config, client }) = setup_nonfree() else {
        return Ok(());
    };

    let fee_exempt_keys = vec![PrivateKey::generate_ecdsa(), PrivateKey::generate_ecdsa()];

    let token1 = create_token(&client).await?;
    let token2 = create_token(&client).await?;

    let Some(op) = &config.operator else {
        log::debug!("skipping test due to missing operator");
        return Ok(());
    };

    let custom_fixed_fees = vec![
        CustomFixedFee::new(1, Some(token1), Some(op.account_id)),
        CustomFixedFee::new(2, Some(token2), Some(op.account_id)),
    ];

    // Create revenue-generating topic
    let receipt = TopicCreateTransaction::new()
        .fee_schedule_key(op.private_key.public_key())
        .submit_key(op.private_key.public_key())
        .admin_key(op.private_key.public_key())
        .fee_exempt_keys(fee_exempt_keys.iter().map(|key| key.public_key().into()).collect())
        .custom_fees(custom_fixed_fees)
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    let topic_id = receipt.topic_id.unwrap();

    let info = TopicInfoQuery::new().topic_id(topic_id).execute(&client).await?;

    assert_eq!(
        info.fee_schedule_key.unwrap().to_bytes(),
        Key::Single(op.private_key.public_key()).to_bytes()
    );

    // Update the revenue-generating topic
    let new_fee_exempt_keys = vec![PrivateKey::generate_ecdsa(), PrivateKey::generate_ecdsa()];
    let new_fee_schedule_key = PrivateKey::generate_ecdsa();

    let new_token1 = create_token(&client).await?;
    let new_token2 = create_token(&client).await?;

    let new_custom_fixed_fees = vec![
        CustomFixedFee::new(3, Some(new_token1), Some(op.account_id)),
        CustomFixedFee::new(4, Some(new_token2), Some(op.account_id)),
    ];

    TopicUpdateTransaction::new()
        .topic_id(topic_id)
        .fee_exempt_keys(new_fee_exempt_keys.iter().map(|key| key.public_key().into()).collect())
        .fee_schedule_key(new_fee_schedule_key.public_key())
        .custom_fees(new_custom_fixed_fees.clone())
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    let updated_info = TopicInfoQuery::new().topic_id(topic_id).execute(&client).await?;

    assert_eq!(
        updated_info.fee_schedule_key.unwrap().to_bytes(),
        Key::Single(new_fee_schedule_key.public_key()).to_bytes()
    );

    // Validate updated fee exempt keys
    for (idx, key) in new_fee_exempt_keys.iter().enumerate() {
        assert_eq!(
            updated_info.fee_exempt_keys[idx].to_bytes(),
            Key::Single(key.public_key()).to_bytes()
        );
    }

    // Validate updated custom fees
    for (idx, fee) in new_custom_fixed_fees.iter().enumerate() {
        assert_eq!(updated_info.custom_fees[idx].amount, fee.amount);
        assert_eq!(updated_info.custom_fees[idx].denominating_token_id, fee.denominating_token_id);
    }

    Ok(())
}

#[tokio::test]
async fn create_revenue_generating_topic_with_invalid_fee_exempt_key_fails() -> anyhow::Result<()> {
    let Some(TestEnvironment { config, client }) = setup_nonfree() else {
        return Ok(());
    };

    let Some(op) = &config.operator else {
        log::debug!("skipping test due to missing operator");
        return Ok(());
    };

    let fee_exempt_key = PrivateKey::generate_ecdsa();
    let fee_exempt_key_list_with_duplicates =
        vec![Key::Single(fee_exempt_key.public_key()), Key::Single(fee_exempt_key.public_key())];

    let result = TopicCreateTransaction::new()
        .admin_key(op.private_key.public_key())
        .fee_exempt_keys(fee_exempt_key_list_with_duplicates)
        .execute(&client)
        .await;

    assert!(matches!(
        result,
        Err(hedera::Error::TransactionPreCheckStatus {
            status: hedera::Status::FeeExemptKeyListContainsDuplicatedKeys,
            ..
        })
    ));

    // Test exceeding key limit
    let fee_exempt_key_list_exceeding_limit =
        (0..11).map(|_| Key::Single(PrivateKey::generate_ecdsa().public_key())).collect::<Vec<_>>();

    let result = TopicCreateTransaction::new()
        .admin_key(op.private_key.public_key())
        .fee_exempt_keys(fee_exempt_key_list_exceeding_limit)
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await;

    assert!(matches!(
        result.unwrap_err(),
        hedera::Error::ReceiptStatus {
            status: hedera::Status::MaxEntriesForFeeExemptKeyListExceeded,
            ..
        }
    ));

    Ok(())
}

// Continuing with more test conversions...
#[tokio::test]
async fn update_fee_schedule_key_without_permission_fails() -> anyhow::Result<()> {
    let Some(TestEnvironment { config, client }) = setup_nonfree() else {
        return Ok(());
    };

    let Some(op) = &config.operator else {
        log::debug!("skipping test due to missing operator");
        return Ok(());
    };

    let receipt = TopicCreateTransaction::new()
        .admin_key(op.private_key.public_key())
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    let topic_id = receipt.topic_id.unwrap();
    let fee_schedule_key = PrivateKey::generate_ed25519();

    let result = TopicUpdateTransaction::new()
        .topic_id(topic_id)
        .fee_schedule_key(fee_schedule_key.public_key())
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await;

    assert!(matches!(
        result.unwrap_err(),
        hedera::Error::ReceiptStatus {
            status: hedera::Status::FeeScheduleKeyCannotBeUpdated,
            ..
        }
    ));

    Ok(())
}

#[tokio::test]
async fn update_custom_fees_without_fee_schedule_key_fails() -> anyhow::Result<()> {
    let Some(TestEnvironment { config, client }) = setup_nonfree() else {
        return Ok(());
    };

    let Some(op) = &config.operator else {
        log::debug!("skipping test due to missing operator");
        return Ok(());
    };

    // Create a topic without fee schedule key
    let receipt = TopicCreateTransaction::new()
        .admin_key(op.private_key.public_key())
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    let topic_id = receipt.topic_id.unwrap();

    let token1 = create_token(&client).await?;
    let token2 = create_token(&client).await?;

    let custom_fixed_fees = vec![
        CustomFixedFee::new(1, Some(token1), Some(op.account_id)),
        CustomFixedFee::new(2, Some(token2), Some(op.account_id)),
    ];

    let result = TopicUpdateTransaction::new()
        .topic_id(topic_id)
        .custom_fees(custom_fixed_fees)
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await;

    assert!(matches!(
        result.unwrap_err(),
        hedera::Error::ReceiptStatus { status: hedera::Status::FeeScheduleKeyNotSet, .. }
    ));

    Ok(())
}

#[tokio::test]
async fn charges_hbar_fee_with_limits_applied() -> anyhow::Result<()> {
    let Some(TestEnvironment { config, client }) = setup_nonfree() else {
        return Ok(());
    };

    let Some(op) = &config.operator else {
        log::debug!("skipping test due to missing operator");
        return Ok(());
    };

    let hbar_amount: u64 = 100_000_000;
    let private_key = PrivateKey::generate_ecdsa();

    let custom_fixed_fee = CustomFixedFee::new(hbar_amount / 2, None, Some(op.account_id));

    let receipt = TopicCreateTransaction::new()
        .admin_key(op.private_key.public_key())
        .fee_schedule_key(op.private_key.public_key())
        .add_custom_fee(custom_fixed_fee)
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    let topic_id = receipt.topic_id.unwrap();

    let account_receipt = AccountCreateTransaction::new()
        .initial_balance(Hbar::new(1))
        .set_key_without_alias(private_key.public_key())
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    let account_id = account_receipt.account_id.unwrap();

    client.set_operator(account_id, private_key);

    TopicMessageSubmitTransaction::new()
        .topic_id(topic_id)
        .message("Hello, Hieroâ„¢ hashgraph!".as_bytes().to_vec())
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    client.set_operator(op.account_id, PrivateKey::generate_ecdsa());

    let account_info = AccountBalanceQuery::new().account_id(account_id).execute(&client).await?;

    assert!(account_info.hbars.to_tinybars() < (hbar_amount / 2) as i64);

    Ok(())
}

#[tokio::test]
async fn exempts_fee_exempt_keys_from_hbar_fees() -> anyhow::Result<()> {
    let Some(TestEnvironment { config, client }) = setup_nonfree() else {
        return Ok(());
    };

    let Some(op) = &config.operator else {
        log::debug!("skipping test due to missing operator");
        return Ok(());
    };

    let hbar_amount: u64 = 100_000_000;
    let fee_exempt_key1 = PrivateKey::generate_ecdsa();
    let fee_exempt_key2 = PrivateKey::generate_ecdsa();

    let custom_fixed_fee = CustomFixedFee::new(hbar_amount / 2, None, Some(op.account_id));

    let receipt = TopicCreateTransaction::new()
        .admin_key(op.private_key.public_key())
        .fee_schedule_key(op.private_key.public_key())
        .fee_exempt_keys(vec![
            Key::Single(fee_exempt_key1.public_key()),
            Key::Single(fee_exempt_key2.public_key()),
        ])
        .add_custom_fee(custom_fixed_fee)
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    let topic_id = receipt.topic_id.unwrap();

    let payer_account_receipt = AccountCreateTransaction::new()
        .initial_balance(Hbar::new(1))
        .set_key_without_alias(fee_exempt_key1.public_key())
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    let payer_account_id = payer_account_receipt.account_id.unwrap();

    client.set_operator(payer_account_id, fee_exempt_key1);

    TopicMessageSubmitTransaction::new()
        .topic_id(topic_id)
        .message("Hello, Hieroâ„¢ hashgraph!".as_bytes().to_vec())
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    client.set_operator(payer_account_id, PrivateKey::generate_ecdsa());

    let account_info =
        AccountBalanceQuery::new().account_id(payer_account_id).execute(&client).await?;

    assert!(account_info.hbars.to_tinybars() > (hbar_amount / 2) as i64);

    Ok(())
}

// Test temporarily taken out until can figure out a solution for a separate freeze
#[tokio::test]
async fn automatically_assign_auto_renew_account_id_on_topic_create() -> anyhow::Result<()> {
    let Some(TestEnvironment { config: _, client }) = setup_nonfree() else {
        return Ok(());
    };

    let topic_receipt =
        TopicCreateTransaction::new().execute(&client).await?.get_receipt(&client).await?;

    let topic_id = topic_receipt.topic_id.unwrap();

    let info = TopicInfoQuery::new().topic_id(topic_id).execute(&client).await?;

    assert!(info.auto_renew_account_id.is_some());

    Ok(())
}

#[tokio::test]
async fn create_with_transaction_id_assigns_auto_renew_account_id_to_transaction_id_account_id(
) -> anyhow::Result<()> {
    let Some(TestEnvironment { config: _, client }) = setup_nonfree() else {
        return Ok(());
    };

    let private_key = PrivateKey::generate_ecdsa();
    let public_key = private_key.public_key();

    let account_receipt = AccountCreateTransaction::new()
        .set_key_without_alias(public_key)
        .initial_balance(Hbar::new(10))
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    let account_id = account_receipt.account_id.unwrap();

    let topic_receipt = TopicCreateTransaction::new()
        .transaction_id(TransactionId::generate(account_id))
        .freeze_with(&client)?
        .sign(private_key)
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await?;

    let topic_id = topic_receipt.topic_id.unwrap();

    let topic_info = TopicInfoQuery::new().topic_id(topic_id).execute(&client).await?;

    assert_eq!(topic_info.auto_renew_account_id, Some(account_id));

    Ok(())
}