Skip to content

Commit 38e753a

Browse files
committed
await async createActions in demo apps
1 parent 23ef8a4 commit 38e753a

File tree

6 files changed

+43
-23
lines changed

6 files changed

+43
-23
lines changed

packages/demo/backend/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ActionsApp extends App {
3030

3131
protected async preMain(): Promise<void> {
3232
// Initialize Actions SDK once at startup
33-
initializeActions()
33+
await initializeActions()
3434
}
3535

3636
protected async main(): Promise<void> {

packages/demo/backend/src/config/actions.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { BASE_SEPOLIA, OPTIMISM_SEPOLIA, UNICHAIN } from './chains.js'
77
import { env } from './env.js'
88
import { AaveETH, GauntletUSDCDemo } from './markets.js'
99

10-
let actionsInstance: ReturnType<typeof createActions<'privy'>>
10+
let actionsInstance: Awaited<ReturnType<typeof createActions<'privy'>>>
1111

1212
export function createActionsConfig(): NodeActionsConfig<'privy'> {
1313
return {
@@ -44,9 +44,11 @@ export function createActionsConfig(): NodeActionsConfig<'privy'> {
4444
}
4545
}
4646

47-
export function initializeActions(config?: NodeActionsConfig<'privy'>): void {
47+
export async function initializeActions(
48+
config?: NodeActionsConfig<'privy'>,
49+
): Promise<void> {
4850
const actionsConfig = config || createActionsConfig()
49-
actionsInstance = createActions(actionsConfig)
51+
actionsInstance = await createActions(actionsConfig)
5052
}
5153

5254
export function getActions() {

packages/demo/frontend/src/components/earn/EarnWithFrontendWallet.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
WALLET_PROVIDER_CONFIGS,
1313
type FrontendWalletProviderType,
1414
} from '@/constants/walletProviders'
15-
import { useMemo } from 'react'
15+
import { useState, useEffect, useMemo } from 'react'
1616
import { createActions } from '@eth-optimism/actions-sdk/react'
1717
import { createActionsConfig } from '@/config/actions'
1818
import { actionsApi } from '@/api/actionsApi'
@@ -25,11 +25,22 @@ export interface EarnWithFrontendWalletProps {
2525
}
2626

2727
function useActions<T extends ReactProviderTypes>(hostedWalletProviderType: T) {
28-
const config = useMemo(
29-
() => createActionsConfig(hostedWalletProviderType),
30-
[hostedWalletProviderType],
31-
)
32-
return useMemo(() => createActions(config), [config])
28+
const [actions, setActions] = useState<Awaited<
29+
ReturnType<typeof createActions<T>>
30+
> | null>(null)
31+
32+
useEffect(() => {
33+
let cancelled = false
34+
const config = createActionsConfig(hostedWalletProviderType)
35+
createActions(config).then((instance) => {
36+
if (!cancelled) setActions(instance)
37+
})
38+
return () => {
39+
cancelled = true
40+
}
41+
}, [hostedWalletProviderType])
42+
43+
return actions
3344
}
3445

3546
/**
@@ -48,7 +59,7 @@ export function EarnWithFrontendWallet({
4859
const operations = useMemo<LendProviderOperations>(
4960
() => ({
5061
getTokenBalances: async () => wallet!.getBalance(),
51-
getMarkets: async () => actions.lend.getMarkets(),
62+
getMarkets: async () => actions!.lend.getMarkets(),
5263
getPosition: async (marketId) => wallet!.lend!.getPosition({ marketId }),
5364
mintAsset: async (asset: Asset) => {
5465
const walletAddress = wallet!.address
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useMemo } from 'react'
1+
import { useState, useEffect } from 'react'
22
import {
33
createActions,
44
type ReactProviderTypes,
@@ -10,14 +10,20 @@ export function useActions<T extends ReactProviderTypes>({
1010
}: {
1111
hostedWalletProviderType: T
1212
}) {
13-
// Memoize the config to prevent recreating it on every render
14-
const config = useMemo(
15-
() => createActionsConfig(hostedWalletProviderType),
16-
[hostedWalletProviderType],
17-
)
13+
const [actions, setActions] = useState<Awaited<
14+
ReturnType<typeof createActions<T>>
15+
> | null>(null)
1816

19-
// Memoize the actions instance to prevent recreating on every render
20-
const actions = useMemo(() => createActions(config), [config])
17+
useEffect(() => {
18+
let cancelled = false
19+
const config = createActionsConfig(hostedWalletProviderType)
20+
createActions(config).then((instance) => {
21+
if (!cancelled) setActions(instance)
22+
})
23+
return () => {
24+
cancelled = true
25+
}
26+
}, [hostedWalletProviderType])
2127

2228
return { actions }
2329
}

packages/demo/frontend/src/hooks/useDynamicWallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function useDynamicWallet() {
1616

1717
useEffect(() => {
1818
const createSmartWallet = async () => {
19-
if (!primaryWallet) {
19+
if (!primaryWallet || !actions) {
2020
setSmartWallet(null)
2121
setError(null)
2222
return

packages/demo/frontend/src/hooks/useTurnkeyWallet.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ export function useTurnkeyWallet() {
6969
setIsCreating(true)
7070
setError(null)
7171

72-
const signer = await actions.wallet.createSigner({
72+
const signer = await actions!.wallet.createSigner({
7373
client: httpClient!,
7474
organizationId: session!.organizationId,
7575
signWith: embeddedWallet!.accounts[0].address,
7676
})
77-
const result = await actions.wallet.createSmartWallet({
77+
const result = await actions!.wallet.createSmartWallet({
7878
signer: signer,
7979
})
8080

@@ -93,7 +93,8 @@ export function useTurnkeyWallet() {
9393
authState === AuthState.Authenticated &&
9494
embeddedWallet &&
9595
httpClient &&
96-
session?.organizationId
96+
session?.organizationId &&
97+
actions
9798
) {
9899
createSmartWallet()
99100
}

0 commit comments

Comments
 (0)