feat: improve WalletAccount class instantiation #1245
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation and Resolution
Currently, the
WalletAccount
class in theaccount.ts
file has the connection logic and account address retrieval mixed with the object construction process in the constructor. This violates the principle of separation of concerns and can lead to potential issues such as race conditions and difficulty in testing.To address this concern, the connection logic and account address retrieval have been separated from the constructor and moved into static
connect
andconnectSilent
methods. This change ensures that the object construction process remains synchronous and focused on initializing the object's state, while the connection logic is handled separately.RPC version (if applicable)
N/A
Usage related changes
No changes to the usage of the WalletAccount class from the user's perspective.
Development related changes
The
WalletAccount
class constructor no longer includes any asynchronous operations or connection logic. It now focuses solely on initializing the object's state and setting up event listeners.The
connect
andconnectSilent
static methods have been introduced to handle the connection logic and account address retrieval from the wallet provider.Developers should now use the
connect
orconnectSilent
methods to create instances ofWalletAccount
with the appropriate account address retrieved from the wallet provider.This approach facilitates better integration with libraries like
starknetkit
andstarknet-react
, which handle the connection logic internally. After establishing a connection, these libraries can return aWalletAccount
instance without attempting to connect again, as the connection logic is now separated from the object construction process.Checklist: