async fn connect_to_external_services<O, A, S, B>(
    service: O,
    account: A,
    store: S,
    btp: BtpOutgoingService<B, A>,
) -> Result<A, warp::reject::Rejection>
where
    O: OutgoingService<A> + Clone + Send + Sync + 'static,
    A: CcpRoutingAccount + BtpAccount + SettlementAccount + Clone + Send + Sync + 'static,
    S: NodeStore<Account = A> + AddressStore + BalanceStore + Clone + Send + Sync + 'static,
    B: OutgoingService<A> + Clone + 'static,
{
    // Try to connect to the account's BTP socket if they have
    // one configured
    if account.get_ilp_over_btp_url().is_some() {
        trace!("Newly inserted account has a BTP URL configured, will try to connect");
        connect_to_service_account(account.clone(), true, btp).await?
    }

    // If we added a parent, get the address assigned to us by
    // them and update all of our routes
    if account.routing_relation() == RoutingRelation::Parent {
        get_address_from_parent_and_update_routes(service, account.clone(), store.clone()).await?;
    }

    // Register the account with the settlement engine
    // if a settlement_engine_url was configured on the account
    // or if there is a settlement engine configured for that
    // account's asset_code
    let default_settlement_engine = store
        .get_asset_settlement_engine(account.asset_code())
        .await?;

    let settlement_engine_url = account
        .settlement_engine_details()
        .map(|details| details.url)
        .or(default_settlement_engine);
    if let Some(se_url) = settlement_engine_url {
        let id = account.id();
        let http_client = SettlementClient::default();
        trace!(
            "Sending account {} creation request to settlement engine: {:?}",
            id,
            se_url.clone()
        );

        let response = http_client
            .create_engine_account(id, se_url.clone())
            .map_err(|err| {
                Rejection::from(ApiError::internal_server_error().detail(err.to_string()))
            })
            .await?;

        if response.status().is_success() {
            trace!("Account {} created on the SE", id);

            // We will pre-fund our account with 0, which will return
            // the current settle_to value
            let (_, amount_to_settle) = store.update_balances_for_fulfill(id, 0u64).await?;

            // prefund the absolute value
            if amount_to_settle > 0 {
                http_client
                    .send_settlement(id, se_url, amount_to_settle, account.asset_scale())
                    .map_err(|err| {
                        Rejection::from(ApiError::internal_server_error().detail(err.to_string()))
                    })
                    .await?;
            }
        } else {
            error!(
                "Error creating account. Settlement engine responded with HTTP code: {}",
                response.status()
            );
        }
    }

    Ok(account)
}
// Helper function which gets called whenever a new account is added or
// modified.
// Performed actions:
// 1. If they have a BTP uri configured: connect to their BTP socket
// 2. If they are a parent:
// 2a. Perform an ILDCP Request to get the address assigned to us by them, and
// update our store's address to that value
// 2b. Perform a RouteControl Request to make them send us any new routes
// 3. If they have a settlement engine endpoitn configured: Make a POST to the
//    engine's account creation endpoint with the account's id
Helper function which gets called whenever a new account is added or
modified.
Performed actions:
1. If they have a BTP uri configured: connect to their BTP socket
2. If they are a parent:
2a. Perform an ILDCP Request to get the address assigned to us by them, and
update our store's address to that value
2b. Perform a RouteControl Request to make them send us any new routes
3. If they have a settlement engine endpoitn configured: Make a POST to the
engine's account creation endpoint with the account's id
Helper function which gets called whenever a new account is added or modified . Performed actions : 1 . If they have a BTP uri configured : connect to their BTP socket 2 . If they are a parent : 2a . Perform an ILDCP Request to get the address assigned to us by them and update our store ' s address to that value 2b . Perform a RouteControl Request to make them send us any new routes 3 . If they have a settlement engine endpoitn configured : Make a POST to the engine ' s account creation endpoint with the account ' s id
async fn connect_to_external_services<O, A, S, B>(
    service: O,
    account: A,
    store: S,
    btp: BtpOutgoingService<B, A>,
) -> Result<A, warp::reject::Rejection>
where
    O: OutgoingService<A> + Clone + Send + Sync + 'static,
    A: CcpRoutingAccount + BtpAccount + SettlementAccount + Clone + Send + Sync + 'static,
    S: NodeStore<Account = A> + AddressStore + BalanceStore + Clone + Send + Sync + 'static,
    B: OutgoingService<A> + Clone + 'static,
{
    // Try to connect to the account's BTP socket if they have
    // one configured
    if account.get_ilp_over_btp_url().is_some() {
        trace!("Newly inserted account has a BTP URL configured, will try to connect");
        connect_to_service_account(account.clone(), true, btp).await?
    }
    // If we added a parent, get the address assigned to us by
    // them and update all of our routes
    if account.routing_relation() == RoutingRelation::Parent {
        get_address_from_parent_and_update_routes(service, account.clone(), store.clone()).await?;
    }
    // Register the account with the settlement engine
    // if a settlement_engine_url was configured on the account
    // or if there is a settlement engine configured for that
    // account's asset_code
    let default_settlement_engine = store
        .get_asset_settlement_engine(account.asset_code())
        .await?;
    let settlement_engine_url = account
        .settlement_engine_details()
        .map(|details| details.url)
        .or(default_settlement_engine);
    if let Some(se_url) = settlement_engine_url {
        let id = account.id();
        let http_client = SettlementClient::default();
        trace!(
            "Sending account {} creation request to settlement engine: {:?}",
            id,
            se_url.clone()
        );
        let response = http_client
            .create_engine_account(id, se_url.clone())
            .map_err(|err| {
                Rejection::from(ApiError::internal_server_error().detail(err.to_string()))
            })
            .await?;
        if response.status().is_success() {
            trace!("Account {} created on the SE", id);
            // We will pre-fund our account with 0, which will return
            // the current settle_to value
            let (_, amount_to_settle) = store.update_balances_for_fulfill(id, 0u64).await?;
            // prefund the absolute value
            if amount_to_settle > 0 {
                http_client
                    .send_settlement(id, se_url, amount_to_settle, account.asset_scale())
                    .map_err(|err| {
                        Rejection::from(ApiError::internal_server_error().detail(err.to_string()))
                    })
                    .await?;
            }
        } else {
            error!(
                "Error creating account. Settlement engine responded with HTTP code: {}",
                response.status()
            );
        }
    }
    Ok(account)
}
async fn connect_to_external_services < O , A , S , B > ( service : O , account : A , store : S , btp : BtpOutgoingService < B , A > , ) -> Result < A , warp :: reject :: Rejection > where O : OutgoingService < A > + Clone + Send + Sync + ' static , A : CcpRoutingAccount + BtpAccount + SettlementAccount + Clone + Send + Sync + ' static , S : NodeStore < Account = A > + AddressStore + BalanceStore + Clone + Send + Sync + ' static , B : OutgoingService < A > + Clone + ' static , { // Try to connect to the account's BTP socket if they have // one configured if account . get_ilp_over_btp_url ( ) . is_some ( ) { trace ! ( " " ) ; connect_to_service_account ( account . clone ( ) , true , btp ) . await ? } // If we added a parent, get the address assigned to us by // them and update all of our routes if account . routing_relation ( ) == RoutingRelation :: Parent { get_address_from_parent_and_update_routes ( service , account . clone ( ) , store . clone ( ) ) . await ? ; } // Register the account with the settlement engine // if a settlement_engine_url was configured on the account // or if there is a settlement engine configured for that // account's asset_code let default_settlement_engine = store . get_asset_settlement_engine ( account . asset_code ( ) ) . await ? ; let settlement_engine_url = account . settlement_engine_details ( ) . map ( | details | details . url ) . or ( default_settlement_engine ) ; if let Some ( se_url ) = settlement_engine_url { let id = account . id ( ) ; let http_client = SettlementClient :: default ( ) ; trace ! ( " " id se_url clone ( ) ) ; let response = http_client . create_engine_account ( id , se_url . clone ( ) ) . map_err ( | err | { Rejection :: from ( ApiError :: internal_server_error ( ) . detail ( err . to_string ( ) ) ) } ) . await ? ; if response . status ( ) . is_success ( ) { trace ! ( " " id ) ; // We will pre-fund our account with 0, which will return // the current settle_to value let ( _ , amount_to_settle ) = store . update_balances_for_fulfill ( id , 0u64 ) . await ? ; // prefund the absolute value if amount_to_settle > 0 { http_client . send_settlement ( id , se_url , amount_to_settle , account . asset_scale ( ) ) . map_err ( | err | { Rejection :: from ( ApiError :: internal_server_error ( ) . detail ( err . to_string ( ) ) ) } ) . await ? ; } } else { error ! ( " " response status ( ) ) ; } } Ok ( account ) }
Helper function which gets called whenever a new account is added or
modified.
Helper function which gets called whenever a new account is added or modified .
service
O
account
A
store
S
btp
BtpOutgoingService<B, A>
service
O
account
A
store
S
btp
BtpOutgoingService<B, A>