Skip to content

Commit

Permalink
make chainId and account methods in wallet interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MananTank committed Feb 16, 2024
1 parent 211a2c4 commit d480357
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 17 deletions.
5 changes: 3 additions & 2 deletions packages/thirdweb/src/react/ui/ConnectWallet/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ const ActiveDot = /* @__PURE__ */ StyledDiv(() => {
});

function ConnectedToSmartWallet() {
const activeAccount = useActiveAccount();
const activeWallet = useActiveWallet();
const chainId = useActiveWalletChainId();
const locale = useTWLocale().connectWallet;
Expand All @@ -774,14 +775,14 @@ function ConnectedToSmartWallet() {
</Container>
);

if (chainId && activeWallet?.account && isSmartWallet) {
if (chainId && activeAccount && isSmartWallet) {
return (
<>
{isSmartWalletDeployed ? (
<Link
color="secondaryText"
hoverColor="primaryText"
href={`https://thirdweb.com/${chainId}/${activeWallet.account.address}/account`}
href={`https://thirdweb.com/${chainId}/${activeAccount.address}/account`}
target="_blank"
size="sm"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const SmartWalletConnecting = (props: {

const [personalWalletChainId, setPersonalWalletChainId] = useState<
bigint | undefined
>(personalWallet.chainId);
>(personalWallet.getChainId());

useEffect(() => {
function handleChainChanged(chain: string) {
Expand Down
28 changes: 26 additions & 2 deletions packages/thirdweb/src/wallets/injected/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export function injectedWallet(options?: InjectedWalletOptions) {
*/
export class InjectedWallet implements Wallet {
metadata: Wallet["metadata"];
chainId: Wallet["chainId"];
private chainId: bigint | undefined;
private account?: Account | undefined;
events: Wallet["events"];
account?: Account | undefined;

// NOTE: can't use `#` notation unless we want to use `tslib` (which we don't because it adds overhead)
private options?: InjectedWalletOptions;
Expand Down Expand Up @@ -98,6 +98,30 @@ export class InjectedWallet implements Wallet {
};
}

/**
* Get the `chainId` that the wallet is connected to.
* @returns The chainId
* @example
* ```ts
* const chainId = wallet.getChainId();
* ```
*/
getChainId(): bigint | undefined {
return this.chainId;
}

/**
* Get the connected `Account` from the wallet.
* @returns The connected account
* @example
* ```ts
* const account = wallet.getAccount();
* ```
*/
getAccount(): Account | undefined {
return this.account;
}

/**
* Auto connect to already connected wallet provider
* @example
Expand Down
5 changes: 2 additions & 3 deletions packages/thirdweb/src/wallets/interfaces/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ export type Wallet = {
connect: (options?: any) => Promise<Account>;
autoConnect: (options?: any) => Promise<Account>;
disconnect: () => Promise<void>;
getAccount(): Account | undefined;
getChainId(): bigint | undefined;

// OPTIONAL
chainId?: bigint;

events?: {
addListener: WalletEventListener;
removeListener: WalletEventListener;
};

switchChain?: (newChainId: bigint | number) => Promise<void>;
account?: Account;
};

export interface WalletWithPersonalWallet extends Wallet {
Expand Down
4 changes: 2 additions & 2 deletions packages/thirdweb/src/wallets/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function createConnectionManager() {
};

const setActiveWallet = (wallet: Wallet) => {
const account = wallet.account;
const account = wallet.getAccount();
if (!account) {
throw new Error("Can not a wallet without an account as active");
}
Expand All @@ -92,7 +92,7 @@ export function createConnectionManager() {
// update active states
activeWallet.setValue(wallet);
activeAccount.setValue(account);
activeWalletChainId.setValue(wallet.chainId);
activeWalletChainId.setValue(wallet.getChainId());
activeWalletConnectionStatus.setValue("connected");

// setup listeners
Expand Down
37 changes: 32 additions & 5 deletions packages/thirdweb/src/wallets/smart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ export const personalWalletToSmartAccountMap = new WeakMap<Wallet, Wallet>();
*/
export class SmartWallet implements WalletWithPersonalWallet {
private options: SmartWalletOptions;
private chainId?: bigint | undefined;
private account?: Account | undefined;

personalWallet: Wallet | undefined;
metadata: Wallet["metadata"];
chainId?: bigint | undefined;
isSmartWallet: true;
account?: Account | undefined;

/**
* Create an instance of the SmartWallet.
Expand All @@ -81,6 +82,30 @@ export class SmartWallet implements WalletWithPersonalWallet {
this.chainId = getChainIdFromChain(options.chain);
}

/**
* Get the `chainId` that the wallet is connected to.
* @returns The chainId
* @example
* ```ts
* const chainId = wallet.getChainId();
* ```
*/
getChainId(): bigint | undefined {
return this.chainId;
}

/**
* Get the connected `Account` from the wallet.
* @returns The connected account
* @example
* ```ts
* const account = wallet.getAccount();
* ```
*/
getAccount(): Account | undefined {
return this.account;
}

/**
* Connect to the smart wallet using a personal account.
* @param connectionOptions - The options for connecting to the smart wallet.
Expand All @@ -99,11 +124,13 @@ export class SmartWallet implements WalletWithPersonalWallet {

const { personalWallet } = connectionOptions;

if (!personalWallet.account) {
const personalAccount = personalWallet.getAccount();

if (!personalAccount) {
throw new Error("Personal wallet does not have an account");
}

if (personalWallet.chainId !== chainId) {
if (personalWallet.getChainId() !== chainId) {
throw new Error(
"Personal account's wallet is on a different chain than the smart wallet.",
);
Expand All @@ -119,7 +146,7 @@ export class SmartWallet implements WalletWithPersonalWallet {

const account = await smartAccount({
...this.options,
personalAccount: personalWallet.account,
personalAccount: personalAccount,
});

personalWalletToSmartAccountMap.set(connectionOptions.personalWallet, this);
Expand Down
28 changes: 26 additions & 2 deletions packages/thirdweb/src/wallets/wallet-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ export function walletConnect(options: WalletConnectCreationOptions) {
export class WalletConnect implements Wallet {
private options: WalletConnectCreationOptions;
private provider: InstanceType<typeof EthereumProvider> | undefined;
private chainId: bigint | undefined;
private account?: Account | undefined;

chainId: Wallet["chainId"];
events: Wallet["events"];
metadata: Wallet["metadata"];
account?: Account | undefined;

/**
* Create a new WalletConnect instance to connect to a wallet using WalletConnect protocol.
Expand All @@ -111,6 +111,30 @@ export class WalletConnect implements Wallet {
this.metadata = options?.metadata || walletConnectMetadata;
}

/**
* Get the `chainId` that the wallet is connected to.
* @returns The chainId
* @example
* ```ts
* const chainId = wallet.getChainId();
* ```
*/
getChainId(): bigint | undefined {
return this.chainId;
}

/**
* Get the connected `Account` from the wallet.
* @returns The connected account
* @example
* ```ts
* const account = wallet.getAccount();
* ```
*/
getAccount(): Account | undefined {
return this.account;
}

/**
* Auto connect to already connected wallet connect session.
* @example
Expand Down

0 comments on commit d480357

Please sign in to comment.