Skip to content

Commit

Permalink
Refactor faucet.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed Feb 12, 2025
1 parent 74735cd commit 3ce9f34
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 21 deletions.
11 changes: 1 addition & 10 deletions gmp/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,7 @@ impl IChain for Connector {
(config.currency_decimals, config.currency_symbol)
}
/// Uses a faucet to fund the account when possible.
async fn faucet(&self) -> Result<()> {
let balance = match self.network_id() {
6 => 10u128.pow(25), // astar
2 => 10u128.pow(29), // ethereum
3 => 10u128.pow(29), // ethereum
network_id => {
tracing::info!("network {network_id} doesn't support faucet");
return Ok(());
},
};
async fn faucet(&self, balance: u128) -> Result<()> {
self.wallet.faucet(balance, None).await?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions gmp/grpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ impl IChain for Connector {
self.address
}
/// Uses a faucet to fund the account when possible.
async fn faucet(&self) -> Result<()> {
let request = Request::new(proto::FaucetRequest {});
async fn faucet(&self, balance: u128) -> Result<()> {
let request = Request::new(proto::FaucetRequest { balance });
self.client.lock().await.faucet(request).await?;
Ok(())
}
Expand Down
7 changes: 5 additions & 2 deletions gmp/grpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ impl Gmp for ConnectorWrapper {
&self,
request: Request<proto::FaucetRequest>,
) -> GmpResult<proto::FaucetResponse> {
let (connector, _) = self.connector(request)?;
connector.faucet().await.map_err(|err| Status::unknown(err.to_string()))?;
let (connector, msg) = self.connector(request)?;
connector
.faucet(msg.balance)
.await
.map_err(|err| Status::unknown(err.to_string()))?;
Ok(Response::new(proto::FaucetResponse {}))
}

Expand Down
4 changes: 3 additions & 1 deletion gmp/grpc/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use time_primitives::{
};

#[derive(Serialize, Deserialize)]
pub struct FaucetRequest {}
pub struct FaucetRequest {
pub balance: u128,
}

#[derive(Serialize, Deserialize)]
pub struct FaucetResponse {}
Expand Down
6 changes: 2 additions & 4 deletions gmp/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use time_primitives::{

const BLOCK_TIME: u64 = 1;
const FINALIZATION_TIME: u64 = 2;
const FAUCET: u128 = 1_000_000_000;

const BLOCKS: TableDefinition<u64, u64> = TableDefinition::new("blocks");
const BALANCE: TableDefinition<Address, u128> = TableDefinition::new("balance");
Expand Down Expand Up @@ -193,12 +192,11 @@ impl IChain for Connector {
currency()
}

async fn faucet(&self) -> Result<()> {
async fn faucet(&self, balance: u128) -> Result<()> {
let tx = self.db.begin_write()?;
{
let mut t = tx.open_table(BALANCE)?;
let balance = read_balance(&t, self.address)?;
t.insert(self.address, balance + FAUCET)?;
t.insert(self.address, balance)?;
}
tx.commit()?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/gmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ pub trait IChain: Send + Sync + 'static {
/// Human readable connector account identifier.
fn address(&self) -> Address;
/// Uses a faucet to fund the account when possible.
async fn faucet(&self) -> Result<()>;
async fn faucet(&self, balance: u128) -> Result<()>;
/// Transfers an amount to an account.
async fn transfer(&self, address: Address, amount: u128) -> Result<()>;
/// Queries the account balance.
Expand Down
1 change: 1 addition & 0 deletions tc-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub struct NetworkConfig {
pub route_base_fee: u128,
pub shard_size: u16,
pub shard_threshold: u16,
pub faucet: Option<u128>,
}

#[cfg(test)]
Expand Down
4 changes: 3 additions & 1 deletion tc-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ impl Tc {
}

pub async fn faucet(&self, network: NetworkId) -> Result<()> {
self.connector(network)?.faucet().await
let config = self.config.network(network)?;
let faucet = config.faucet.context("faucet not supported, please prefund account")?;
self.connector(network)?.faucet(faucet).await
}

pub async fn balance(&self, network: Option<NetworkId>, address: Address) -> Result<u128> {
Expand Down

0 comments on commit 3ce9f34

Please sign in to comment.