Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling account creation request errors #366

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion workspaces/src/rpc/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) async fn url_create_account(
let helper_url = helper_url.join("account").unwrap();

// TODO(maybe): need this in near-jsonrpc-client as well:
let _resp = reqwest::Client::new()
let response = reqwest::Client::new()
.post(helper_url)
.header("Content-Type", "application/json")
.body(
Expand All @@ -57,6 +57,40 @@ pub(crate) async fn url_create_account(
.await
.map_err(|e| RpcErrorCode::HelperAccountCreationFailure.custom(e))?;

if response.status() >= reqwest::StatusCode::BAD_REQUEST {
return Err(ErrorKind::Other.message(format!(
"The faucet (helper service) server failed with status code <{}>",
response.status()
)));
}

let account_creation_transaction = response
.json::<near_jsonrpc_client::methods::tx::RpcTransactionResponse>()
.await
.map_err(|err| ErrorKind::DataConversion.custom(err))?
.final_execution_outcome
.map(|outcome| outcome.into_outcome())
.ok_or_else(|| {
ErrorKind::Other.message(
"The faucet (helper service) server did not return a transaction response.",
)
})?;

match account_creation_transaction.status {
near_primitives::views::FinalExecutionStatus::SuccessValue(ref value) => {
if value == b"false" {
return Err(ErrorKind::Other.message(format!(
"The new account <{}> could not be created successfully.",
&account_id
)));
}
}
near_primitives::views::FinalExecutionStatus::Failure(err) => {
return Err(ErrorKind::Execution.custom(err));
}
_ => unreachable!(),
}

Ok(())
}

Expand Down
Loading