|
| 1 | +// todo(network tls): |
| 2 | +// - canConnectToPreviewnetWithTLS |
| 3 | +// - canConnectToTestnetWithTLS |
| 4 | +// - canConnectToMainnetWithTLS |
| 5 | +// - cannotConnectToPreviewnetWhenNetworkNameIsNullAndCertificateVerificationIsEnabled |
| 6 | + |
| 7 | +use assert_matches::assert_matches; |
| 8 | +use hedera::{ |
| 9 | + AccountBalanceQuery, |
| 10 | + AccountId, |
| 11 | + Hbar, |
| 12 | + Status, |
| 13 | + TokenBurnTransaction, |
| 14 | + TokenCreateTransaction, |
| 15 | + TokenDeleteTransaction, |
| 16 | +}; |
| 17 | +use time::{ |
| 18 | + Duration, |
| 19 | + OffsetDateTime, |
| 20 | +}; |
| 21 | + |
| 22 | +use crate::account::Account; |
| 23 | +use crate::common::{ |
| 24 | + setup_global, |
| 25 | + setup_nonfree, |
| 26 | + TestEnvironment, |
| 27 | +}; |
| 28 | + |
| 29 | +#[tokio::test] |
| 30 | +async fn query() -> anyhow::Result<()> { |
| 31 | + let TestEnvironment { config, client } = setup_global(); |
| 32 | + |
| 33 | + let Some(op) = &config.operator else { |
| 34 | + log::debug!("skipping test due to lack of operator"); |
| 35 | + |
| 36 | + return Ok(()) |
| 37 | + }; |
| 38 | + |
| 39 | + let balance = AccountBalanceQuery::new().account_id(op.account_id).execute(&client).await?; |
| 40 | + |
| 41 | + log::trace!("successfully queried balance: {balance:?}"); |
| 42 | + |
| 43 | + anyhow::ensure!(balance.account_id == op.account_id); |
| 44 | + anyhow::ensure!(balance.hbars.to_tinybars() > 0); |
| 45 | + |
| 46 | + Ok(()) |
| 47 | +} |
| 48 | + |
| 49 | +#[tokio::test] |
| 50 | +async fn query_cost() -> anyhow::Result<()> { |
| 51 | + let TestEnvironment { config, client } = setup_global(); |
| 52 | + |
| 53 | + let Some(op) = &config.operator else { |
| 54 | + log::debug!("skipping test due to lack of operator"); |
| 55 | + return Ok(()) |
| 56 | + }; |
| 57 | + |
| 58 | + let mut query = AccountBalanceQuery::new(); |
| 59 | + |
| 60 | + query.account_id(op.account_id).max_payment_amount(Hbar::new(1)); |
| 61 | + |
| 62 | + let cost = query.get_cost(&client).await?; |
| 63 | + |
| 64 | + assert_eq!(cost, Hbar::ZERO); |
| 65 | + |
| 66 | + let balance = query.payment_amount(cost).execute(&client).await?; |
| 67 | + |
| 68 | + anyhow::ensure!(balance.account_id == op.account_id); |
| 69 | + anyhow::ensure!(balance.hbars.to_tinybars() > 0); |
| 70 | + |
| 71 | + Ok(()) |
| 72 | +} |
| 73 | + |
| 74 | +#[tokio::test] |
| 75 | +async fn query_cost_big_max() -> anyhow::Result<()> { |
| 76 | + let TestEnvironment { config, client } = setup_global(); |
| 77 | + |
| 78 | + let Some(op) = &config.operator else { |
| 79 | + log::debug!("skipping test due to lack of operator"); |
| 80 | + return Ok(()) |
| 81 | + }; |
| 82 | + |
| 83 | + let mut query = AccountBalanceQuery::new(); |
| 84 | + |
| 85 | + query.account_id(op.account_id).max_payment_amount(Hbar::new(1000000)); |
| 86 | + |
| 87 | + let cost = query.get_cost(&client).await?; |
| 88 | + |
| 89 | + assert_eq!(cost, Hbar::ZERO); |
| 90 | + |
| 91 | + let balance = query.payment_amount(cost).execute(&client).await?; |
| 92 | + |
| 93 | + anyhow::ensure!(balance.account_id == op.account_id); |
| 94 | + anyhow::ensure!(balance.hbars.to_tinybars() > 0); |
| 95 | + |
| 96 | + Ok(()) |
| 97 | +} |
| 98 | + |
| 99 | +#[tokio::test] |
| 100 | +async fn query_cost_small_max() -> anyhow::Result<()> { |
| 101 | + let TestEnvironment { config, client } = setup_global(); |
| 102 | + |
| 103 | + let Some(op) = &config.operator else { |
| 104 | + log::debug!("skipping test due to lack of operator"); |
| 105 | + return Ok(()) |
| 106 | + }; |
| 107 | + |
| 108 | + let mut query = AccountBalanceQuery::new(); |
| 109 | + |
| 110 | + query.account_id(op.account_id).max_payment_amount(Hbar::from_tinybars(1)); |
| 111 | + |
| 112 | + let cost = query.get_cost(&client).await?; |
| 113 | + |
| 114 | + assert_eq!(cost, Hbar::ZERO); |
| 115 | + |
| 116 | + let balance = query.payment_amount(cost).execute(&client).await?; |
| 117 | + |
| 118 | + anyhow::ensure!(balance.account_id == op.account_id); |
| 119 | + anyhow::ensure!(balance.hbars.to_tinybars() > 0); |
| 120 | + |
| 121 | + Ok(()) |
| 122 | +} |
| 123 | + |
| 124 | +#[tokio::test] |
| 125 | +async fn invalid_account_id_fails() -> anyhow::Result<()> { |
| 126 | + let TestEnvironment { config: _, client } = setup_global(); |
| 127 | + |
| 128 | + let res = AccountBalanceQuery::new() |
| 129 | + .account_id(AccountId { |
| 130 | + shard: 1, |
| 131 | + realm: 0, |
| 132 | + num: 3, |
| 133 | + alias: None, |
| 134 | + evm_address: None, |
| 135 | + checksum: None, |
| 136 | + }) |
| 137 | + .execute(&client) |
| 138 | + .await; |
| 139 | + |
| 140 | + assert_matches!( |
| 141 | + res, |
| 142 | + Err(hedera::Error::QueryNoPaymentPreCheckStatus { status: Status::InvalidAccountId }) |
| 143 | + ); |
| 144 | + |
| 145 | + Ok(()) |
| 146 | +} |
| 147 | + |
| 148 | +#[tokio::test] |
| 149 | +async fn query_token_balances() -> anyhow::Result<()> { |
| 150 | + let Some(TestEnvironment { config: _, client }) = setup_nonfree() else { |
| 151 | + return Ok(()) |
| 152 | + }; |
| 153 | + |
| 154 | + let account = Account::create(Hbar::new(10), &client).await?; |
| 155 | + |
| 156 | + let token_id = TokenCreateTransaction::new() |
| 157 | + .name("ffff") |
| 158 | + .symbol("f") |
| 159 | + .initial_supply(10000) |
| 160 | + .decimals(50) |
| 161 | + .treasury_account_id(account.id) |
| 162 | + .expiration_time(OffsetDateTime::now_utc() + Duration::minutes(5)) |
| 163 | + .admin_key(account.key.public_key()) |
| 164 | + .supply_key(account.key.public_key()) |
| 165 | + .freeze_default(false) |
| 166 | + .sign(account.key.clone()) |
| 167 | + .execute(&client) |
| 168 | + .await? |
| 169 | + .get_receipt(&client) |
| 170 | + .await? |
| 171 | + .token_id |
| 172 | + .unwrap(); |
| 173 | + |
| 174 | + let balance = AccountBalanceQuery::new().account_id(account.id).execute(&client).await?; |
| 175 | + |
| 176 | + #[allow(deprecated)] |
| 177 | + { |
| 178 | + assert_eq!(balance.tokens.get(&token_id).copied(), Some(10000)); |
| 179 | + assert_eq!(balance.token_decimals.get(&token_id).copied(), Some(50)); |
| 180 | + } |
| 181 | + |
| 182 | + TokenBurnTransaction::new() |
| 183 | + .token_id(token_id) |
| 184 | + .amount(10000_u64) |
| 185 | + .sign(account.key.clone()) |
| 186 | + .execute(&client) |
| 187 | + .await? |
| 188 | + .get_receipt(&client) |
| 189 | + .await?; |
| 190 | + |
| 191 | + let _ = TokenDeleteTransaction::new() |
| 192 | + .token_id(token_id) |
| 193 | + .sign(account.key.clone()) |
| 194 | + .execute(&client) |
| 195 | + .await? |
| 196 | + .get_receipt(&client) |
| 197 | + .await?; |
| 198 | + |
| 199 | + account.delete(&client).await?; |
| 200 | + |
| 201 | + Ok(()) |
| 202 | +} |
0 commit comments