Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/thirdweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"url": "https://github.com/thirdweb-dev/js/issues"
},
"dependencies": {
"@base-org/account": "2.5.0",
"@coinbase/wallet-sdk": "4.3.0",
"@emotion/react": "11.14.0",
"@emotion/styled": "11.14.1",
Expand Down
5 changes: 5 additions & 0 deletions packages/thirdweb/src/exports/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export type {
InjectedSupportedWalletIds,
WCSupportedWalletIds,
} from "../wallets/__generated__/wallet-ids.js";
export type {
BaseAccountSDKWalletConnectionOptions,
BaseAccountWalletCreationOptions,
} from "../wallets/base-account/base-account-web.js";
export { isBaseAccountSDKWallet } from "../wallets/base-account/base-account-web.js";
export type {
CoinbaseSDKWalletConnectionOptions,
CoinbaseWalletCreationOptions,
Expand Down
116 changes: 116 additions & 0 deletions packages/thirdweb/src/wallets/base-account/base-account-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* internal helper functions for Base Account SDK wallet
*/

import { trackConnect } from "../../analytics/track/connect.js";
import type { Chain } from "../../chains/types.js";
import { getCachedChainIfExists } from "../../chains/utils.js";
import { BASE_ACCOUNT } from "../constants.js";
import type { Account, Wallet } from "../interfaces/wallet.js";
import { createWalletEmitter } from "../wallet-emitter.js";
import type { CreateWalletArgs } from "../wallet-types.js";
import type { BaseAccountProvider } from "./base-account-web.js";

/**
* @internal
*/
export function baseAccountWalletSDK(args: {
createOptions?: CreateWalletArgs<typeof BASE_ACCOUNT>[1];
providerFactory: () => Promise<BaseAccountProvider>;
}): Wallet<typeof BASE_ACCOUNT> {
const { createOptions } = args;
const emitter = createWalletEmitter<typeof BASE_ACCOUNT>();
let account: Account | undefined;
let chain: Chain | undefined;

function reset() {
account = undefined;
chain = undefined;
}

let handleDisconnect = async () => {};

let handleSwitchChain = async (newChain: Chain) => {
chain = newChain;
};

const unsubscribeChainChanged = emitter.subscribe(
"chainChanged",
(newChain) => {
chain = newChain;
},
);

const unsubscribeDisconnect = emitter.subscribe("disconnect", () => {
reset();
unsubscribeChainChanged();
unsubscribeDisconnect();
});

emitter.subscribe("accountChanged", (_account) => {
account = _account;
});

return {
autoConnect: async (options) => {
const { autoConnectBaseAccountSDK } = await import(
"./base-account-web.js"
);
const provider = await args.providerFactory();
const [connectedAccount, connectedChain, doDisconnect, doSwitchChain] =
await autoConnectBaseAccountSDK(options, emitter, provider);
// set the states
account = connectedAccount;
chain = connectedChain;
handleDisconnect = doDisconnect;
handleSwitchChain = doSwitchChain;
trackConnect({
chainId: chain.id,
client: options.client,
walletAddress: account.address,
walletType: BASE_ACCOUNT,
});
// return account
return account;
},
connect: async (options) => {
const { connectBaseAccountSDK } = await import("./base-account-web.js");
const provider = await args.providerFactory();
const [connectedAccount, connectedChain, doDisconnect, doSwitchChain] =
await connectBaseAccountSDK(options, emitter, provider);

// set the states
account = connectedAccount;
chain = connectedChain;
handleDisconnect = doDisconnect;
handleSwitchChain = doSwitchChain;
trackConnect({
chainId: chain.id,
client: options.client,
walletAddress: account.address,
walletType: BASE_ACCOUNT,
});
// return account
return account;
},
disconnect: async () => {
reset();
await handleDisconnect();
},
getAccount: () => account,
getChain() {
if (!chain) {
return undefined;
}

chain = getCachedChainIfExists(chain.id) || chain;
return chain;
},
getConfig: () => createOptions,
id: BASE_ACCOUNT,
subscribe: emitter.subscribe,
switchChain: async (newChain) => {
await handleSwitchChain(newChain);
},
};
}
Loading