use anyhow::{Context, Result};
use assert_matches::assert_matches;
use miden_client::account::{Account, AccountType};
use miden_client::note::Note;
use miden_client::rpc::{EndpointError, RegisterAccountError, RpcEndpoint, RpcError};
use miden_client::testing::common::*;
use miden_client::transaction::{InputNote, TransactionRequest, TransactionRequestBuilder};
use miden_client::{ClientError, Felt};
pub mod enforcement;
pub mod funding;
pub mod invitations;
pub mod registration;
const FUNDING_NOTE_MAX_BLOCKS: u32 = 20;
async fn funded_deploy_request(
client: &mut TestClient,
account: &Account,
) -> Result<TransactionRequest> {
let notes = funding_notes(client, account).await?;
TransactionRequestBuilder::new()
.build_consume_notes(notes)
.context("failed to build the deploy transaction request")
}
async fn funding_notes(client: &mut TestClient, account: &Account) -> Result<Vec<Note>> {
client
.wait_for_consumable_notes(account.id(), FUNDING_NOTE_MAX_BLOCKS)
.await
.with_context(|| format!("failed to wait for a funding note for account {}", account.id()))?
.into_iter()
.map(|(record, _)| {
let note: InputNote =
record.try_into().context("a committed note should convert to an input note")?;
Ok(note.into_note())
})
.collect()
}
async fn insert_unfunded_wallet(
client: &mut TestClient,
invitation_code: Option<&str>,
) -> Result<Account> {
let mut setup = AccountSetup::wallet(AccountType::Private).unfunded();
if let Some(invitation_code) = invitation_code {
setup = setup.invitation_code(invitation_code);
}
let (account, _) = client.insert_account(setup).await?;
Ok(account)
}
fn assert_rejected_before_submission(error: &ClientError, account: &Account) {
assert_matches!(
error,
ClientError::AccountNotAllowlisted(account_id) if *account_id == account.id(),
"expected the client to refuse to create the unregistered account, got: {error}"
);
}
fn assert_registration_rejected(error: &ClientError, expected: &RegisterAccountError) {
assert_matches!(
error,
ClientError::RpcError(RpcError::RequestError {
endpoint: RpcEndpoint::RegisterAccount,
endpoint_error: Some(EndpointError::RegisterAccount(actual)),
..
}) if actual == expected,
"expected the registration to be rejected with {expected}, got: {error}"
);
}
async fn is_deployed(client: &TestClient, account: &Account) -> Result<bool> {
let nonce = client
.account_reader(account.id())
.nonce()
.await
.with_context(|| format!("account {} is not tracked by the client", account.id()))?;
Ok(nonce != Felt::ZERO)
}