@@ -9,10 +9,10 @@ This module defines the client API for the Web extension.
99use std:: { collections:: HashMap , future:: Future , sync:: Arc } ;
1010
1111use futures:: { lock:: Mutex as AsyncMutex , stream:: StreamExt } ;
12- use linera_base:: identifiers:: ApplicationId ;
12+ use linera_base:: identifiers:: { AccountOwner , ApplicationId } ;
1313use linera_client:: {
1414 chain_listener:: { ChainListener , ChainListenerConfig , ClientContext as _} ,
15- client_options:: ClientOptions ,
15+ client_options:: ClientContextOptions ,
1616 wallet:: Wallet ,
1717} ;
1818use linera_core:: {
@@ -33,8 +33,8 @@ type JsResult<T> = Result<T, JsError>;
3333
3434async fn get_storage ( ) -> Result < WebStorage , <linera_views:: memory:: MemoryStore as WithError >:: Error >
3535{
36- linera_storage:: DbStorage :: initialize (
37- linera_views:: memory:: MemoryStoreConfig :: new ( 1 ) ,
36+ linera_storage:: DbStorage :: maybe_create_and_connect (
37+ & linera_views:: memory:: MemoryStoreConfig :: new ( 1 ) ,
3838 "linera" ,
3939 Some ( linera_execution:: WasmRuntime :: Wasmer ) ,
4040 )
@@ -47,15 +47,11 @@ type ChainClient =
4747 linera_core:: client:: ChainClient < linera_rpc:: node_provider:: NodeProvider , WebStorage > ;
4848
4949// TODO(#13): get from user
50- pub const OPTIONS : ClientOptions = ClientOptions {
50+ pub const OPTIONS : ClientContextOptions = ClientContextOptions {
5151 send_timeout : std:: time:: Duration :: from_millis ( 4000 ) ,
5252 recv_timeout : std:: time:: Duration :: from_millis ( 4000 ) ,
5353 max_pending_message_bundles : 10 ,
54- wasm_runtime : Some ( linera_execution:: WasmRuntime :: Wasmer ) ,
55- max_concurrent_queries : None ,
5654 max_loaded_chains : nonzero_lit:: usize!( 40 ) ,
57- max_stream_queries : 10 ,
58- cache_size : 1000 ,
5955 retry_delay : std:: time:: Duration :: from_millis ( 1000 ) ,
6056 max_retries : 10 ,
6157 wait_for_outgoing_messages : false ,
@@ -68,10 +64,7 @@ pub const OPTIONS: ClientOptions = ClientOptions {
6864 // TODO(linera-protocol#2944): separate these out from the
6965 // `ClientOptions` struct, since they apply only to the CLI/native
7066 // client
71- tokio_threads : Some ( 1 ) ,
72- command : linera_client:: client_options:: ClientCommand :: Keygen ,
7367 wallet_state_path : None ,
74- storage_config : None ,
7568 with_wallet : None ,
7669} ;
7770
@@ -107,12 +100,15 @@ impl JsFaucet {
107100 /// - if we fail to get the list of current validators from the faucet
108101 /// - if we fail to claim the chain from the faucet
109102 /// - if we fail to persist the new chain or keypair to the wallet
103+ ///
104+ /// # Panics
105+ /// If an error occurs in the chain listener task.
110106 #[ wasm_bindgen( js_name = claimChain) ]
111107 pub async fn claim_chain ( & self , client : & mut Client ) -> JsResult < String > {
112108 use linera_client:: persistent:: LocalPersistExt as _;
113109 let mut context = client. client_context . lock ( ) . await ;
114110 let key_pair = context. wallet . generate_key_pair ( ) ;
115- let owner: linera_base :: identifiers :: Owner = key_pair. public ( ) . into ( ) ;
111+ let owner: AccountOwner = key_pair. public ( ) . into ( ) ;
116112 tracing:: info!(
117113 "Requesting a new chain for owner {} using the faucet at address {}" ,
118114 owner,
@@ -203,7 +199,7 @@ pub struct Frontend(Client);
203199
204200#[ derive( serde:: Deserialize ) ]
205201struct TransferParams {
206- donor : Option < linera_base :: identifiers :: Owner > ,
202+ donor : Option < AccountOwner > ,
207203 amount : u64 ,
208204 recipient : linera_base:: identifiers:: Account ,
209205}
@@ -228,14 +224,18 @@ impl Client {
228224 OPTIONS ,
229225 wallet,
230226 ) ) ) ;
231- ChainListener :: new ( ChainListenerConfig :: default ( ) )
232- . run ( client_context. clone ( ) , storage)
233- . await ;
227+ ChainListener :: new (
228+ ChainListenerConfig :: default ( ) ,
229+ client_context. clone ( ) ,
230+ storage,
231+ )
232+ . run ( )
233+ . await ;
234234 log:: info!( "Linera Web client successfully initialized" ) ;
235235 Ok ( Self { client_context } )
236236 }
237237
238- /// Set a callback to be called when a notification is received
238+ /// Sets a callback to be called when a notification is received
239239 /// from the network.
240240 ///
241241 /// # Panics
@@ -301,6 +301,15 @@ impl Client {
301301 result
302302 }
303303
304+ /// Transfers funds from one account to another.
305+ ///
306+ /// `options` should be an options object of the form `{ donor,
307+ /// recipient, amount }`; omitting `donor` will cause the funds to
308+ /// come from the chain balance.
309+ ///
310+ /// # Errors
311+ /// - if the options object is of the wrong form
312+ /// - if the transfer fails
304313 #[ wasm_bindgen]
305314 pub async fn transfer ( & self , options : wasm_bindgen:: JsValue ) -> JsResult < ( ) > {
306315 let params: TransferParams = serde_wasm_bindgen:: from_value ( options) ?;
@@ -309,7 +318,7 @@ impl Client {
309318 let _hash = self
310319 . apply_client_command ( & chain_client, || {
311320 chain_client. transfer (
312- params. donor ,
321+ params. donor . unwrap_or ( AccountOwner :: CHAIN ) ,
313322 linera_base:: data_types:: Amount :: from_tokens ( params. amount . into ( ) ) ,
314323 linera_execution:: system:: Recipient :: Account ( params. recipient ) ,
315324 )
@@ -319,6 +328,10 @@ impl Client {
319328 Ok ( ( ) )
320329 }
321330
331+ /// Gets the identity of the default chain.
332+ ///
333+ /// # Errors
334+ /// If the chain couldn't be established.
322335 pub async fn identity ( & self ) -> JsResult < JsValue > {
323336 Ok ( serde_wasm_bindgen:: to_value (
324337 & self . default_chain_client ( ) . await ?. identity ( ) . await ?,
0 commit comments