1use alloy::{
2 primitives::{Address, B256, U256},
3 providers::DynProvider,
4};
5use async_trait::async_trait;
6use jsonrpsee::{core::RpcResult, proc_macros::rpc, types::error::INTERNAL_ERROR_CODE};
7use reth_rpc_server_types::result::rpc_err;
8use tempo_alloy::TempoNetwork;
9use tempo_precompiles::tip20::ITIP20;
10
11#[rpc(server, namespace = "tempo")]
12pub trait TempoFaucetExtApi {
13 #[method(name = "fundAddress")]
14 async fn fund_address(&self, address: Address) -> RpcResult<Vec<B256>>;
15}
16
17pub struct TempoFaucetExt {
18 faucet_token_addresses: Vec<Address>,
19 funding_amount: U256,
20 provider: DynProvider<TempoNetwork>,
21}
22
23impl TempoFaucetExt {
24 pub fn new(
25 faucet_token_addresses: Vec<Address>,
26 funding_amount: U256,
27 provider: DynProvider<TempoNetwork>,
28 ) -> Self {
29 Self {
30 faucet_token_addresses,
31 funding_amount,
32 provider,
33 }
34 }
35}
36
37#[async_trait]
38impl TempoFaucetExtApiServer for TempoFaucetExt {
39 async fn fund_address(&self, address: Address) -> RpcResult<Vec<B256>> {
40 let mut tx_hashes = Vec::new();
41 for token in &self.faucet_token_addresses {
42 let tx_hash = *ITIP20::new(*token, &self.provider)
43 .mint(address, self.funding_amount)
44 .send()
45 .await
46 .map_err(|err| rpc_err(INTERNAL_ERROR_CODE, err.to_string(), None))?
47 .tx_hash();
48
49 tx_hashes.push(tx_hash);
50 }
51
52 Ok(tx_hashes)
53 }
54}