sui_graphql_client/
faucet.rsuse sui_types::Address;
use sui_types::ObjectId;
use sui_types::TransactionDigest;
use anyhow::anyhow;
use anyhow::bail;
use reqwest::StatusCode;
use reqwest::Url;
use serde::Deserialize;
use serde::Serialize;
use serde_json::json;
use std::time::Duration;
use tracing::error;
use tracing::info;
pub const FAUCET_DEVNET_HOST: &str = "https://faucet.devnet.sui.io";
pub const FAUCET_TESTNET_HOST: &str = "https://faucet.testnet.sui.io";
pub const FAUCET_LOCAL_HOST: &str = "http://localhost:9123";
const FAUCET_REQUEST_TIMEOUT: Duration = Duration::from_secs(120);
const FAUCET_POLL_INTERVAL: Duration = Duration::from_secs(2);
pub struct FaucetClient {
faucet_url: Url,
inner: reqwest::Client,
}
#[derive(serde::Deserialize)]
struct FaucetResponse {
task: Option<String>,
error: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct BatchStatusFaucetResponse {
pub status: Option<BatchSendStatus>,
pub error: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "UPPERCASE")]
pub enum BatchSendStatusType {
Inprogress,
Succeeded,
Discarded,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BatchSendStatus {
pub status: BatchSendStatusType,
pub transferred_gas_objects: Option<FaucetReceipt>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FaucetReceipt {
pub sent: Vec<CoinInfo>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
struct BatchFaucetReceipt {
pub task: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CoinInfo {
pub amount: u64,
pub id: ObjectId,
pub transfer_tx_digest: TransactionDigest,
}
impl FaucetClient {
pub fn new(faucet_url: &str) -> Self {
let inner = reqwest::Client::new();
let faucet_url = Url::parse(faucet_url).expect("Invalid faucet URL");
FaucetClient { faucet_url, inner }
}
pub fn local() -> Self {
Self {
faucet_url: Url::parse(FAUCET_LOCAL_HOST).expect("Invalid faucet URL"),
inner: reqwest::Client::new(),
}
}
pub fn devnet() -> Self {
Self {
faucet_url: Url::parse(FAUCET_DEVNET_HOST).expect("Invalid faucet URL"),
inner: reqwest::Client::new(),
}
}
pub fn testnet() -> Self {
Self {
faucet_url: Url::parse(FAUCET_TESTNET_HOST).expect("Invalid faucet URL"),
inner: reqwest::Client::new(),
}
}
pub async fn request(&self, address: Address) -> Result<Option<String>, anyhow::Error> {
self.request_impl(address).await
}
async fn request_impl(&self, address: Address) -> Result<Option<String>, anyhow::Error> {
let address = address.to_string();
let json_body = json![{
"FixedAmountRequest": {
"recipient": &address
}
}];
let url = format!("{}v1/gas", self.faucet_url);
info!(
"Requesting gas from faucet for address {} : {}",
address, url
);
let resp = self
.inner
.post(url)
.header("content-type", "application/json")
.json(&json_body)
.send()
.await?;
match resp.status() {
StatusCode::ACCEPTED | StatusCode::CREATED => {
let faucet_resp: FaucetResponse = resp.json().await?;
if let Some(err) = faucet_resp.error {
error!("Faucet request was unsuccessful: {err}");
bail!("Faucet request was unsuccessful: {err}")
} else {
info!("Request succesful: {:?}", faucet_resp.task);
Ok(faucet_resp.task)
}
}
StatusCode::TOO_MANY_REQUESTS => {
error!("Faucet service received too many requests from this IP address.");
bail!("Faucet service received too many requests from this IP address. Please try again after 60 minutes.");
}
StatusCode::SERVICE_UNAVAILABLE => {
error!("Faucet service is currently overloaded or unavailable.");
bail!("Faucet service is currently overloaded or unavailable. Please try again later.");
}
status_code => {
error!("Faucet request was unsuccessful: {status_code}");
bail!("Faucet request was unsuccessful: {status_code}");
}
}
}
pub async fn request_and_wait(
&self,
address: Address,
) -> Result<Option<FaucetReceipt>, anyhow::Error> {
let request_id = self.request(address).await?;
if let Some(request_id) = request_id {
let poll_response = tokio::time::timeout(FAUCET_REQUEST_TIMEOUT, async {
let mut interval = tokio::time::interval(FAUCET_POLL_INTERVAL);
loop {
interval.tick().await;
info!("Polling faucet request status: {request_id}");
let req = self.request_status(request_id.clone()).await;
if let Ok(Some(poll_response)) = req {
match poll_response.status {
BatchSendStatusType::Succeeded => {
info!("Faucet request {request_id} succeeded");
break Ok(poll_response);
}
BatchSendStatusType::Discarded => {
break Ok(BatchSendStatus {
status: BatchSendStatusType::Discarded,
transferred_gas_objects: None,
});
}
BatchSendStatusType::Inprogress => {
continue;
}
}
} else if let Some(err) = req.err() {
error!("Faucet request {request_id} failed. Error: {:?}", err);
break Err(anyhow!(
"Faucet request {request_id} failed. Error: {:?}",
err
));
}
}
})
.await
.map_err(|_| {
error!(
"Faucet request {request_id} timed out. Timeout set to {} seconds",
FAUCET_REQUEST_TIMEOUT.as_secs()
);
anyhow!("Faucet request timed out")
})??;
Ok(poll_response.transferred_gas_objects)
} else {
Ok(None)
}
}
pub async fn request_status(
&self,
id: String,
) -> Result<Option<BatchSendStatus>, anyhow::Error> {
let status_url = format!("{}v1/status/{}", self.faucet_url, id);
info!("Checking status of faucet request: {status_url}");
let response = self.inner.get(&status_url).send().await?;
if response.status() == StatusCode::TOO_MANY_REQUESTS {
bail!("Cannot fetch request status due to too many requests from this IP address.");
} else if response.status() == StatusCode::BAD_GATEWAY {
bail!("Cannot fetch request status due to a bad gateway.")
}
let json = response
.json::<BatchStatusFaucetResponse>()
.await
.map_err(|e| {
error!("Failed to parse faucet response: {:?}", e);
anyhow!("Failed to parse faucet response: {:?}", e)
})?;
Ok(json.status)
}
}