11// Copyright 2020-2025 IOTA Stiftung
22// SPDX-License-Identifier: Apache-2.0
33
4- use std:: process:: Output ;
5-
6- use anyhow:: Context ;
7- use iota_interaction:: types:: base_types:: { IotaAddress , ObjectID } ;
84use iota_interaction:: IotaClientTrait ;
95use product_common:: network_name:: NetworkName ;
10- use serde:: Deserialize ;
11- #[ cfg( not( target_arch = "wasm32" ) ) ]
12- use tokio:: process:: Command ;
136
147use crate :: error:: Error ;
158use crate :: iota_interaction_adapter:: IotaClientAdapter ;
169
17- const FUND_WITH_ACTIVE_ADDRESS_FUNDING_TX_BUDGET : u64 = 5_000_000 ;
18- const FUND_WITH_ACTIVE_ADDRESS_FUNDING_VALUE : u64 = 500_000_000 ;
19-
2010/// Returns the network-id also known as chain-identifier provided by the specified iota_client
2111pub async fn network_id ( iota_client : & IotaClientAdapter ) -> Result < NetworkName , Error > {
2212 let network_id = iota_client
@@ -26,82 +16,3 @@ pub async fn network_id(iota_client: &IotaClientAdapter) -> Result<NetworkName,
2616 . map_err ( |e| Error :: RpcError ( e. to_string ( ) ) ) ?;
2717 Ok ( network_id. try_into ( ) . expect ( "chain ID is a valid network name" ) )
2818}
29-
30- #[ derive( Deserialize , Debug ) ]
31- #[ serde( rename_all = "camelCase" ) ]
32- struct CoinOutput {
33- gas_coin_id : ObjectID ,
34- nanos_balance : u64 ,
35- }
36-
37- fn unpack_command_output ( output : & Output , task : & str ) -> anyhow:: Result < String > {
38- let stdout = std:: str:: from_utf8 ( & output. stdout ) ?;
39- if !output. status . success ( ) {
40- let stderr = std:: str:: from_utf8 ( & output. stderr ) ?;
41- anyhow:: bail!( "Failed to {task}: {stdout}, {stderr}" ) ;
42- }
43-
44- Ok ( stdout. to_string ( ) )
45- }
46-
47- /// Requests funds from the local IOTA client's configured faucet.
48- ///
49- /// This behavior can be changed to send funds with local IOTA client's active address to the given address.
50- /// For that the env variable `IOTA_IDENTITY_FUND_WITH_ACTIVE_ADDRESS` must be set to `true`.
51- /// Notice, that this is a setting mostly intended for internal test use and must be used with care.
52- /// For details refer to to `identity_iota_core`'s README.md.
53- #[ cfg( not( target_arch = "wasm32" ) ) ]
54- pub async fn request_funds ( address : & IotaAddress ) -> anyhow:: Result < ( ) > {
55- let fund_with_active_address = std:: env:: var ( "IOTA_IDENTITY_FUND_WITH_ACTIVE_ADDRESS" )
56- . map ( |v| !v. is_empty ( ) && v. to_lowercase ( ) == "true" )
57- . unwrap_or ( false ) ;
58-
59- if !fund_with_active_address {
60- let output = Command :: new ( "iota" )
61- . arg ( "client" )
62- . arg ( "faucet" )
63- . arg ( "--address" )
64- . arg ( address. to_string ( ) )
65- . arg ( "--json" )
66- . output ( )
67- . await
68- . context ( "Failed to execute command" ) ?;
69- unpack_command_output ( & output, "request funds from faucet" ) ?;
70- } else {
71- let output = Command :: new ( "iota" )
72- . arg ( "client" )
73- . arg ( "gas" )
74- . arg ( "--json" )
75- . output ( )
76- . await
77- . context ( "Failed to execute command" ) ?;
78- let output_str = unpack_command_output ( & output, "fetch active account's gas coins" ) ?;
79-
80- let parsed: Vec < CoinOutput > = serde_json:: from_str ( & output_str) ?;
81- let min_balance = FUND_WITH_ACTIVE_ADDRESS_FUNDING_VALUE + FUND_WITH_ACTIVE_ADDRESS_FUNDING_TX_BUDGET ;
82- let matching = parsed. into_iter ( ) . find ( |coin| coin. nanos_balance >= min_balance) ;
83- let Some ( coin_to_use) = matching else {
84- anyhow:: bail!( "Failed to find coin object with enough funds to transfer to test account" ) ;
85- } ;
86-
87- let address_string = address. to_string ( ) ;
88- let output = Command :: new ( "iota" )
89- . arg ( "client" )
90- . arg ( "pay-iota" )
91- . arg ( "--recipients" )
92- . arg ( & address_string)
93- . arg ( "--input-coins" )
94- . arg ( coin_to_use. gas_coin_id . to_string ( ) )
95- . arg ( "--amounts" )
96- . arg ( FUND_WITH_ACTIVE_ADDRESS_FUNDING_VALUE . to_string ( ) )
97- . arg ( "--gas-budget" )
98- . arg ( FUND_WITH_ACTIVE_ADDRESS_FUNDING_TX_BUDGET . to_string ( ) )
99- . arg ( "--json" )
100- . output ( )
101- . await
102- . context ( "Failed to execute command" ) ?;
103- unpack_command_output ( & output, & format ! ( "send funds from active account to {address_string}" ) ) ?;
104- }
105-
106- Ok ( ( ) )
107- }
0 commit comments