瀏覽代碼

faucetd: Allow minting arbitrary tokens.

parazyd 4 年之前
父節點
當前提交
e648db374d
共有 2 個文件被更改,包括 23 次插入21 次删除
  1. 3 3
      bin/faucetd/src/error.rs
  2. 20 18
      bin/faucetd/src/main.rs

+ 3 - 3
bin/faucetd/src/error.rs

@@ -10,9 +10,9 @@ pub enum RpcError {
 
 
 fn to_tuple(e: RpcError) -> (i64, String) {
 fn to_tuple(e: RpcError) -> (i64, String) {
     let msg = match e {
     let msg = match e {
-        RpcError::AmountExceedsLimit => "Amount requested is higher than the faucet limit",
-        RpcError::TimeLimitReached => "Timeout not expired. Try again later",
-        RpcError::ParseError => "Parse error",
+        RpcError::AmountExceedsLimit => "Amount requested is higher than the faucet limit.",
+        RpcError::TimeLimitReached => "Timeout not expired. Try again later.",
+        RpcError::ParseError => "Parse error.",
     };
     };
 
 
     (e as i64, msg.to_string())
     (e as i64, msg.to_string())

+ 20 - 18
bin/faucetd/src/main.rs

@@ -21,7 +21,7 @@ use darkfi::{
         ValidatorState, ValidatorStatePtr, MAINNET_GENESIS_HASH_BYTES, MAINNET_GENESIS_TIMESTAMP,
         ValidatorState, ValidatorStatePtr, MAINNET_GENESIS_HASH_BYTES, MAINNET_GENESIS_TIMESTAMP,
         TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP,
         TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP,
     },
     },
-    crypto::{address::Address, keypair::PublicKey, token_list::DrkTokenList},
+    crypto::{address::Address, keypair::PublicKey, token_id},
     net,
     net,
     net::P2pPtr,
     net::P2pPtr,
     node::Client,
     node::Client,
@@ -37,7 +37,7 @@ use darkfi::{
         decode_base10, expand_path,
         decode_base10, expand_path,
         path::get_config_path,
         path::get_config_path,
         serial::serialize,
         serial::serialize,
-        sleep, NetworkName,
+        sleep,
     },
     },
     wallet::walletdb::init_wallet,
     wallet::walletdb::init_wallet,
     Error, Result,
     Error, Result,
@@ -165,12 +165,16 @@ impl Faucetd {
     }
     }
 
 
     // RPCAPI:
     // RPCAPI:
-    // Processes an airdrop request and airdrops requested amount to address.
+    // Processes an airdrop request and airdrops requested token and amount to address.
     // Returns the transaction ID upon success.
     // Returns the transaction ID upon success.
-    // --> {"jsonrpc": "2.0", "method": "airdrop", "params": ["1DarkFi...", 1.42], "id": 1}
+    // --> {"jsonrpc": "2.0", "method": "airdrop", "params": ["1DarkFi...", 1.42, "1F00b4r..."], "id": 1}
     // <-- {"jsonrpc": "2.0", "result": "txID", "id": 1}
     // <-- {"jsonrpc": "2.0", "result": "txID", "id": 1}
     async fn airdrop(&self, id: Value, params: &[Value]) -> JsonResult {
     async fn airdrop(&self, id: Value, params: &[Value]) -> JsonResult {
-        if params.len() != 2 || !params[0].is_string() || !params[1].is_f64() {
+        if params.len() != 3 ||
+            !params[0].is_string() ||
+            !params[1].is_f64() ||
+            !params[2].is_string()
+        {
             return JsonError::new(InvalidParams, None, id).into()
             return JsonError::new(InvalidParams, None, id).into()
         }
         }
 
 
@@ -208,6 +212,16 @@ impl Faucetd {
             return server_error(RpcError::AmountExceedsLimit, id)
             return server_error(RpcError::AmountExceedsLimit, id)
         }
         }
 
 
+        // Here we allow the faucet to mint arbitrary token IDs.
+        // TODO: Revert this to native token when we have contracts for minting tokens.
+        let token_id = match token_id::parse_b58(params[2].as_str().unwrap()) {
+            Ok(v) => v,
+            Err(_) => {
+                error!("airdrop(): Failed parsing token id from string");
+                return server_error(RpcError::ParseError, id)
+            }
+        };
+
         // Check if there as a previous airdrop and the timeout has passed.
         // Check if there as a previous airdrop and the timeout has passed.
         let now = Utc::now().timestamp();
         let now = Utc::now().timestamp();
         let map = self.airdrop_map.lock().await;
         let map = self.airdrop_map.lock().await;
@@ -218,11 +232,6 @@ impl Faucetd {
         };
         };
         drop(map);
         drop(map);
 
 
-        let token_id = self.client.tokenlist.by_net[&NetworkName::DarkFi]
-            .get("DRK".to_string())
-            .unwrap()
-            .drk_address;
-
         let amnt: u64 = match amount.try_into() {
         let amnt: u64 = match amount.try_into() {
             Ok(v) => v,
             Ok(v) => v,
             Err(e) => {
             Err(e) => {
@@ -321,16 +330,9 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
         }
         }
     };
     };
 
 
-    let tokenlist = Arc::new(DrkTokenList::new(&[
-        ("drk", include_bytes!("../../../contrib/token/darkfi_token_list.min.json")),
-        ("btc", include_bytes!("../../../contrib/token/bitcoin_token_list.min.json")),
-        ("eth", include_bytes!("../../../contrib/token/erc20_token_list.min.json")),
-        ("sol", include_bytes!("../../../contrib/token/solana_token_list.min.json")),
-    ])?);
-
     // TODO: sqldb init cleanup
     // TODO: sqldb init cleanup
     // Initialize client
     // Initialize client
-    let client = Arc::new(Client::new(wallet.clone(), tokenlist).await?);
+    let client = Arc::new(Client::new(wallet.clone()).await?);
 
 
     // Parse cashier addresses
     // Parse cashier addresses
     let mut cashier_pubkeys = vec![];
     let mut cashier_pubkeys = vec![];