-
Notifications
You must be signed in to change notification settings - Fork 171
feat: add initial smart wallet client actions #1648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
moldy530
wants to merge
1
commit into
main
Choose a base branch
from
moldy/wallet-api-base
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Experimental | ||
|
||
This experimental namespace is meant to contain actions and other core logic that consumes Wallet APIs. These will replace the usual flow found within this repo |
79 changes: 79 additions & 0 deletions
79
account-kit/core/src/experimental/actions/getSmartWalletClient.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { | ||
createSmartWalletClient, | ||
type SmartWalletClient, | ||
} from "@account-kit/wallet-client"; | ||
import type { Address, IsUndefined, JsonRpcAccount } from "viem"; | ||
import { getAlchemyTransport } from "../../actions/getAlchemyTransport.js"; | ||
import { getConnection } from "../../actions/getConnection.js"; | ||
import { getSigner } from "../../actions/getSigner.js"; | ||
import { getSignerStatus } from "../../actions/getSignerStatus.js"; | ||
import { SignerNotConnectedError } from "../../errors.js"; | ||
import type { AlchemyAccountsConfig } from "../../types.js"; | ||
|
||
export type GetSmartWalletClientResult< | ||
TAccount extends JsonRpcAccount<Address> | undefined = | ||
| JsonRpcAccount<`0x${string}`> | ||
| undefined, | ||
> = SmartWalletClient<TAccount>; | ||
|
||
export type GetSmartWalletClientParams< | ||
TAccount extends JsonRpcAccount<Address> | undefined = | ||
| JsonRpcAccount<Address> | ||
| undefined, | ||
> = { mode?: "local" | "remote" } & (IsUndefined<TAccount> extends true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, we should move the mode into the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i do this in a later pr |
||
? { account?: never } | ||
: { account: Address }); | ||
|
||
export function getSmartWalletClient< | ||
TAccount extends JsonRpcAccount<Address> | undefined = | ||
| JsonRpcAccount<Address> | ||
| undefined, | ||
>( | ||
config: AlchemyAccountsConfig, | ||
params?: GetSmartWalletClientParams<TAccount>, | ||
): GetSmartWalletClientResult<TAccount>; | ||
|
||
export function getSmartWalletClient( | ||
config: AlchemyAccountsConfig, | ||
params?: GetSmartWalletClientParams, | ||
): GetSmartWalletClientResult { | ||
const connection = getConnection(config); | ||
const signerStatus = getSignerStatus(config); | ||
const transport = getAlchemyTransport(config); | ||
const signer = getSigner(config); | ||
|
||
if (!signer || !signerStatus.isConnected) { | ||
throw new SignerNotConnectedError(); | ||
} | ||
|
||
const smartWalletClient = | ||
config.store.getState().smartWalletClients[connection.chain.id]; | ||
|
||
if ( | ||
smartWalletClient && | ||
smartWalletClient.account?.address === params?.account | ||
) { | ||
return smartWalletClient; | ||
} | ||
|
||
// TODO: should we still cache the client like we used to? | ||
// honestly that probably caused more problems than it solved | ||
// TBD actually, we might store it in the store so we don't run into issues | ||
// with react | ||
const client = createSmartWalletClient({ | ||
transport, | ||
chain: connection.chain, | ||
signer, | ||
account: params?.account, | ||
mode: params?.mode ?? "local", | ||
}); | ||
|
||
config.store.setState((state) => ({ | ||
smartWalletClients: { | ||
...state.smartWalletClients, | ||
[connection.chain.id]: client, | ||
}, | ||
})); | ||
|
||
return client; | ||
} |
21 changes: 21 additions & 0 deletions
21
account-kit/core/src/experimental/actions/watchSmartWalletClient.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { AlchemyAccountsConfig } from "../../types"; | ||
import { | ||
getSmartWalletClient, | ||
type GetSmartWalletClientResult, | ||
} from "./getSmartWalletClient.js"; | ||
|
||
export function watchSmartWalletClient(config: AlchemyAccountsConfig) { | ||
return (onChange: (client: GetSmartWalletClientResult) => void) => { | ||
return config.store.subscribe( | ||
({ signerStatus, chain }) => ({ signerStatus, chain }), | ||
() => { | ||
onChange(getSmartWalletClient(config)); | ||
}, | ||
{ | ||
equalityFn(a, b) { | ||
return a.signerStatus === b.signerStatus && a.chain === b.chain; | ||
}, | ||
}, | ||
); | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type * from "./actions/getSmartWalletClient.js"; | ||
export { getSmartWalletClient } from "./actions/getSmartWalletClient.js"; | ||
export type * from "./actions/watchSmartWalletClient.js"; | ||
export { watchSmartWalletClient } from "./actions/watchSmartWalletClient.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably the most beneficial place to add support for wallet apis because it means accounts will be index correctly. everything downstream will the touch this flow which is what we want