From c6abfd8f970228ad35bf66997ca7dd955030a9e7 Mon Sep 17 00:00:00 2001 From: Green Baneling Date: Sat, 4 May 2024 00:00:29 +0200 Subject: [PATCH] Support non-zero base `AssetId` (#71) --- src/config.rs | 7 ++----- src/constants.rs | 3 --- src/lib.rs | 3 ++- src/routes.rs | 27 ++++++++++++++++++--------- tests/dispense.rs | 18 +++++++++++++----- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/config.rs b/src/config.rs index a41c47c..697f08d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,10 +1,9 @@ use crate::constants::{ CAPTCHA_KEY, CAPTCHA_SECRET, DEFAULT_DISPENSE_INTERVAL, DEFAULT_FAUCET_DISPENSE_AMOUNT, - DEFAULT_NODE_URL, DEFAULT_PORT, DISPENSE_AMOUNT, DISPENSE_INTERVAL, FAUCET_ASSET_ID, - FUEL_NODE_URL, HUMAN_LOGGING, LOG_FILTER, PUBLIC_FUEL_NODE_URL, SERVICE_PORT, TIMEOUT_SECONDS, + DEFAULT_NODE_URL, DEFAULT_PORT, DISPENSE_AMOUNT, DISPENSE_INTERVAL, FUEL_NODE_URL, + HUMAN_LOGGING, LOG_FILTER, PUBLIC_FUEL_NODE_URL, SERVICE_PORT, TIMEOUT_SECONDS, WALLET_SECRET_KEY, }; -use fuels_core::types::AssetId; use secrecy::Secret; use std::env; @@ -19,7 +18,6 @@ pub struct Config { pub public_node_url: String, pub wallet_secret_key: Option>, pub dispense_amount: u64, - pub dispense_asset_id: AssetId, pub dispense_limit_interval: u64, pub timeout: u64, } @@ -44,7 +42,6 @@ impl Default for Config { .unwrap_or_else(|_| DEFAULT_FAUCET_DISPENSE_AMOUNT.to_string()) .parse::() .expect("expected a valid integer for DISPENSE_AMOUNT"), - dispense_asset_id: FAUCET_ASSET_ID, dispense_limit_interval: env::var(DISPENSE_INTERVAL) .unwrap_or_else(|_| DEFAULT_DISPENSE_INTERVAL.to_string()) .parse::() diff --git a/src/constants.rs b/src/constants.rs index bde4d08..db8870e 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,5 +1,3 @@ -use fuels_core::types::AssetId; - pub const LOG_FILTER: &str = "RUST_LOG"; pub const HUMAN_LOGGING: &str = "HUMAN_LOGGING"; pub const CAPTCHA_KEY: &str = "CAPTCHA_KEY"; @@ -14,7 +12,6 @@ pub const DISPENSE_AMOUNT: &str = "DISPENSE_AMOUNT"; pub const DISPENSE_INTERVAL: &str = "DISPENSE_LIMIT_INTERVAL"; pub const DEFAULT_DISPENSE_INTERVAL: u64 = 24 * 60 * 60; pub const DEFAULT_FAUCET_DISPENSE_AMOUNT: u64 = 10_000_000; -pub const FAUCET_ASSET_ID: AssetId = AssetId::new([0; 32]); pub const SERVICE_PORT: &str = "PORT"; pub const DEFAULT_PORT: u16 = 3000; diff --git a/src/lib.rs b/src/lib.rs index 7b952fc..5e65fe8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -97,6 +97,7 @@ pub async fn start_server( let provider = Provider::connect(service_config.node_url.clone()) .await .expect("Should create a provider"); + let base_asset_id = *provider.consensus_parameters().base_asset_id(); // setup wallet let secret = service_config @@ -112,7 +113,7 @@ pub async fn start_server( ); let balance = wallet - .get_coins(service_config.dispense_asset_id) + .get_coins(base_asset_id) .await .expect("Failed to fetch initial balance from fuel core") .into_iter() diff --git a/src/routes.rs b/src/routes.rs index 670dc4a..58b9b2a 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -146,9 +146,13 @@ fn check_and_mark_dispense_limit( Ok(()) } -async fn get_coins(wallet: &WalletUnlocked, amount: u64) -> Result, DispenseError> { +async fn get_coins( + wallet: &WalletUnlocked, + base_asset_id: &AssetId, + amount: u64, +) -> Result, DispenseError> { wallet - .get_spendable_resources(AssetId::BASE, amount) + .get_spendable_resources(*base_asset_id, amount) .await .map_err(|e| { error( @@ -245,6 +249,7 @@ pub async fn dispense_tokens( }); let provider = wallet.provider().expect("client provider"); + let base_asset_id = *provider.consensus_parameters().base_asset_id(); let mut tx_id = None; for _ in 0..5 { @@ -253,7 +258,7 @@ pub async fn dispense_tokens( let coin_type = CoinType::Coin(Coin { amount: previous_coin_output.amount, block_created: 0u32, - asset_id: config.dispense_asset_id, + asset_id: base_asset_id, utxo_id: previous_coin_output.utxo_id, owner: previous_coin_output.owner.into(), status: CoinStatus::Unspent, @@ -261,17 +266,17 @@ pub async fn dispense_tokens( vec![Input::resource_signed(coin_type)] } else { - get_coins(&wallet, config.dispense_amount).await? + get_coins(&wallet, &base_asset_id, config.dispense_amount).await? }; let mut outputs = wallet.get_asset_outputs_for_amount( &address.into(), - config.dispense_asset_id, + base_asset_id, config.dispense_amount, ); let faucet_address: Address = wallet.address().into(); // Add an additional output to store the stable part of the fee change. - outputs.push(Output::coin(faucet_address, 0, config.dispense_asset_id)); + outputs.push(Output::coin(faucet_address, 0, base_asset_id)); let tip = guard.next_tip(); @@ -307,7 +312,7 @@ pub async fn dispense_tokens( "Overflow during calculating `TransactionFee`".to_string(), StatusCode::INTERNAL_SERVER_ERROR, ))?; - let available_balance = available_balance(&tx_builder.inputs, &config.dispense_asset_id); + let available_balance = available_balance(&tx_builder.inputs, &base_asset_id); let stable_fee_change = available_balance .checked_sub(fee.max_fee().saturating_add(config.dispense_amount)) .ok_or(error( @@ -316,7 +321,7 @@ pub async fn dispense_tokens( ))?; *tx_builder.outputs.last_mut().unwrap() = - Output::coin(faucet_address, stable_fee_change, config.dispense_asset_id); + Output::coin(faucet_address, stable_fee_change, base_asset_id); let script = tx_builder.build(provider).await.expect("Valid script"); @@ -388,10 +393,14 @@ pub async fn dispense_tokens( #[tracing::instrument(skip_all)] pub async fn dispense_info( Extension(config): Extension, + Extension(wallet): Extension, ) -> Result { + let provider = wallet.provider().expect("client provider"); + let base_asset_id = *provider.consensus_parameters().base_asset_id(); + Ok(DispenseInfoResponse { amount: config.dispense_amount, - asset_id: config.dispense_asset_id.to_string(), + asset_id: base_asset_id.to_string(), }) } diff --git a/tests/dispense.rs b/tests/dispense.rs index d0ad50f..9b56bd4 100644 --- a/tests/dispense.rs +++ b/tests/dispense.rs @@ -10,7 +10,7 @@ use fuel_faucet::config::Config; use fuel_faucet::models::DispenseInfoResponse; use fuel_faucet::{start_server, Clock, THE_BIGGEST_AMOUNT}; use fuel_tx::ConsensusParameters; -use fuel_types::{Address, AssetId}; +use fuel_types::Address; use fuels_accounts::provider::Provider; use fuels_accounts::wallet::WalletUnlocked; use fuels_core::types::bech32::Bech32Address; @@ -60,6 +60,7 @@ impl TestContext { let dispense_amount = rng.gen_range(1..10000u64); let secret_key: SecretKey = SecretKey::random(&mut rng); let wallet = WalletUnlocked::new_from_private_key(secret_key, None); + let base_asset_id = [1; 32].into(); let mut generator = CoinConfigGenerator::new(); let mut coins: Vec<_> = (0..10000) @@ -77,7 +78,7 @@ impl TestContext { coins.push(CoinConfig { owner: wallet.address().into(), amount: 1 << 50, - asset_id: Default::default(), + asset_id: base_asset_id, ..generator.generate() }); @@ -89,6 +90,7 @@ impl TestContext { let mut consensus_parameters = ConsensusParameters::default(); consensus_parameters .set_fee_params(fuel_tx::FeeParameters::default().with_gas_price_factor(1)); + consensus_parameters.set_base_asset_id(base_asset_id); let chain_config = ChainConfig { consensus_parameters, @@ -121,7 +123,6 @@ impl TestContext { node_url: format!("http://{}", fuel_node.bound_address), wallet_secret_key: Some(Secret::new(format!("{secret_key:x}"))), dispense_amount, - dispense_asset_id: AssetId::default(), ..Default::default() }; @@ -157,7 +158,11 @@ async fn can_start_server() { assert_eq!(response.amount, context.faucet_config.dispense_amount); assert_eq!( response.asset_id, - context.faucet_config.dispense_asset_id.to_string() + context + .provider + .consensus_parameters() + .base_asset_id() + .to_string() ); } @@ -208,7 +213,10 @@ async fn _dispense_sends_coins_to_valid_address( let test_balance: u64 = context .provider - .get_coins(&recipient_address, context.faucet_config.dispense_asset_id) + .get_coins( + &recipient_address, + *context.provider.consensus_parameters().base_asset_id(), + ) .await .unwrap() .iter()