Skip to content

Commit d4a414b

Browse files
authored
smart account / sessions improvements (#247)
1 parent 9c69ee7 commit d4a414b

25 files changed

+356
-244
lines changed

apps/connect/.env.development

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
VITE_HYPERGRAPH_SYNC_SERVER_ORIGIN="http://localhost:3030"
2+
# VITE_HYPERGRAPH_CHAIN="geo-testnet"
3+
# VITE_HYPERGRAPH_API_URL="https://hypergraph-v2-testnet.up.railway.app/graphql"
4+
# VITE_HYPERGRAPH_RPC_URL="https://rpc-geo-test-zc16z3tcvf.t.conduit.xyz"
25
VITE_HYPERGRAPH_CHAIN="geogenesis"
36
VITE_HYPERGRAPH_API_URL="https://hypergraph-v2.up.railway.app/graphql"
47
VITE_HYPERGRAPH_RPC_URL="https://rpc-geo-genesis-h0q2s21xx8.t.conduit.xyz"

apps/connect/src/routes/__root.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Logout } from '@/components/logout';
2+
import { StoreConnect } from '@graphprotocol/hypergraph';
23
import { usePrivy } from '@privy-io/react-auth';
34
import { Link, Outlet, createRootRoute, useLayoutEffect, useRouter } from '@tanstack/react-router';
45
import { TanStackRouterDevtools } from '@tanstack/router-devtools';
6+
import { useSelector } from '@xstate/store/react';
57

68
export const Route = createRootRoute({
79
component: () => {
10+
const accountAddress = useSelector(StoreConnect.store, (state) => state.context.accountAddress);
811
const { authenticated, ready } = usePrivy();
912
const router = useRouter();
1013

@@ -13,15 +16,15 @@ export const Route = createRootRoute({
1316
return;
1417
}
1518

16-
if (ready && !authenticated) {
19+
if (ready && (!authenticated || !accountAddress)) {
1720
if (router.state.location.href.startsWith('/authenticate')) {
1821
localStorage.setItem('geo-connect-authenticate-redirect', router.state.location.href);
1922
}
2023
router.navigate({
2124
to: '/login',
2225
});
2326
}
24-
}, [authenticated, ready]);
27+
}, [authenticated, ready, accountAddress]);
2528

2629
return (
2730
<>

apps/connect/src/routes/authenticate.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ function AuthenticateComponent() {
396396
console.log('smart session created');
397397
const smartAccountClient = await getSmartAccountWalletClient({
398398
owner: walletClient,
399+
address: accountAddress,
399400
chain: CHAIN,
400401
rpcUrl: import.meta.env.VITE_HYPERGRAPH_RPC_URL,
401402
});

apps/connect/src/routes/login.lazy.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function Login() {
2323

2424
const hypergraphLogin = useCallback(
2525
async (walletClient: WalletClient, embeddedWallet: ConnectedWallet) => {
26+
console.log('hypergraphLogin');
2627
if (!identityToken) {
2728
return;
2829
}
@@ -57,12 +58,14 @@ function Login() {
5758
);
5859

5960
useEffect(() => {
61+
console.log('useEffect in login.lazy.tsx');
6062
if (
6163
!hypergraphLoginStarted && // avoid re-running the effect to often
6264
privyAuthenticated && // privy must be authenticated to run it
6365
walletsReady && // wallets must be ready to run it
6466
wallets.length > 0 // wallets must have at least one wallet to run it
6567
) {
68+
console.log('running login effect');
6669
setHypergraphLoginStarted(true);
6770
(async () => {
6871
try {

apps/events/src/Boot.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ declare module '@tanstack/react-router' {
1515

1616
export function Boot() {
1717
return (
18-
<HypergraphAppProvider storage={localStorage} syncServerUri="http://localhost:3030" mapping={mapping}>
18+
<HypergraphAppProvider
19+
storage={localStorage}
20+
syncServerUri="http://localhost:3030"
21+
mapping={mapping}
22+
chainId={80451}
23+
>
1924
<RouterProvider router={router} />
2025
</HypergraphAppProvider>
2126
);

apps/events/src/components/create-events.tsx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { getSmartAccountWalletClient } from '@/lib/smart-account';
2-
import { type GeoSmartAccount, Graph, type Op } from '@graphprotocol/grc-20';
3-
import { publishOps } from '@graphprotocol/hypergraph-react';
1+
import { Graph, type Op } from '@graphprotocol/grc-20';
2+
import type { Connect } from '@graphprotocol/hypergraph';
3+
import { publishOps, useHypergraphApp } from '@graphprotocol/hypergraph-react';
44
import { Button } from './ui/button';
55

66
const createEvents = async ({
7-
smartAccountWalletClient,
7+
smartSessionClient,
88
space,
9-
}: { smartAccountWalletClient: GeoSmartAccount; space: string }) => {
9+
}: { smartSessionClient: Connect.SmartSessionClient; space: string }) => {
1010
try {
1111
const ops: Array<Op> = [];
1212

@@ -46,7 +46,7 @@ const createEvents = async ({
4646
});
4747
ops.push(...createCompanyTypeOps);
4848

49-
const { id: eventTypeId, ops: createEventTypeOps } = Graph.createEntity({
49+
const { ops: createEventTypeOps } = Graph.createEntity({
5050
name: 'My Test Event',
5151
types: ['6b8dbe76-389f-4bde-acdd-db9d5e387882'],
5252
relations: {
@@ -57,10 +57,9 @@ const createEvents = async ({
5757

5858
const result = await publishOps({
5959
ops,
60-
walletClient: smartAccountWalletClient,
60+
walletClient: smartSessionClient,
6161
space,
6262
name: 'Create Job Offers, Companies and Events',
63-
network: 'TESTNET',
6463
});
6564
console.log('result', result);
6665
alert('Events created');
@@ -70,17 +69,17 @@ const createEvents = async ({
7069
};
7170

7271
export const CreateEvents = ({ space }: { space: string }) => {
72+
const { getSmartSessionClient } = useHypergraphApp();
7373
return (
7474
<div>
7575
<Button
7676
onClick={async () => {
77-
const smartAccountWalletClient = await getSmartAccountWalletClient();
78-
if (!smartAccountWalletClient) {
79-
throw new Error('Missing smartAccountWalletClient');
77+
const smartSessionClient = await getSmartSessionClient();
78+
if (!smartSessionClient) {
79+
throw new Error('Missing smartSessionClient');
8080
}
8181
await createEvents({
82-
// @ts-expect-error TODO: in the future we probably only only use one smart account wallet client
83-
smartAccountWalletClient,
82+
smartSessionClient,
8483
space,
8584
});
8685
}}

apps/events/src/components/create-properties-and-types-event.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { getSmartAccountWalletClient } from '@/lib/smart-account';
2-
import { type GeoSmartAccount, Graph, type Op } from '@graphprotocol/grc-20';
3-
import { publishOps } from '@graphprotocol/hypergraph-react';
1+
import { Graph, type Op } from '@graphprotocol/grc-20';
2+
import type { Connect } from '@graphprotocol/hypergraph';
3+
import { publishOps, useHypergraphApp } from '@graphprotocol/hypergraph-react';
44
import { useState } from 'react';
55
import { Button } from './ui/button';
66
import { Card, CardContent } from './ui/card';
77

88
const createPropertiesAndTypesEvent = async ({
9-
smartAccountWalletClient,
9+
smartSessionClient,
1010
space,
11-
}: { smartAccountWalletClient: GeoSmartAccount; space: string }) => {
11+
}: { smartSessionClient: Connect.SmartSessionClient; space: string }) => {
1212
const ops: Array<Op> = [];
1313
const { id: salaryPropertyId, ops: createSalaryPropertyOps } = Graph.createProperty({
1414
dataType: 'NUMBER',
@@ -50,10 +50,9 @@ const createPropertiesAndTypesEvent = async ({
5050

5151
const result = await publishOps({
5252
ops,
53-
walletClient: smartAccountWalletClient,
53+
walletClient: smartSessionClient,
5454
space,
5555
name: 'Create properties and types',
56-
network: 'TESTNET',
5756
});
5857
return {
5958
result,
@@ -68,6 +67,7 @@ const createPropertiesAndTypesEvent = async ({
6867

6968
export const CreatePropertiesAndTypesEvent = ({ space }: { space: string }) => {
7069
const [mapping, setMapping] = useState<string>('');
70+
const { getSmartSessionClient } = useHypergraphApp();
7171

7272
return (
7373
<div>
@@ -80,9 +80,9 @@ export const CreatePropertiesAndTypesEvent = ({ space }: { space: string }) => {
8080
)}
8181
<Button
8282
onClick={async () => {
83-
const smartAccountWalletClient = await getSmartAccountWalletClient();
84-
if (!smartAccountWalletClient) {
85-
throw new Error('Missing smartAccountWalletClient');
83+
const smartSessionClient = await getSmartSessionClient();
84+
if (!smartSessionClient) {
85+
throw new Error('Missing smartSessionClient');
8686
}
8787
const {
8888
eventTypeId,
@@ -92,8 +92,7 @@ export const CreatePropertiesAndTypesEvent = ({ space }: { space: string }) => {
9292
jobOffersRelationTypeId,
9393
sponsorsRelationTypeId,
9494
} = await createPropertiesAndTypesEvent({
95-
// @ts-expect-error TODO: in the future we probably only only use one smart account wallet client
96-
smartAccountWalletClient,
95+
smartSessionClient,
9796
space,
9897
});
9998

apps/events/src/components/create-properties-and-types-todos.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { getSmartAccountWalletClient } from '@/lib/smart-account';
2-
import { type GeoSmartAccount, Graph, type Op } from '@graphprotocol/grc-20';
3-
import { publishOps } from '@graphprotocol/hypergraph-react';
1+
import { Graph, type Op } from '@graphprotocol/grc-20';
2+
import type { Connect } from '@graphprotocol/hypergraph';
3+
import { publishOps, useHypergraphApp } from '@graphprotocol/hypergraph-react';
44
import { useState } from 'react';
55
import { Button } from './ui/button';
66
import { Card, CardContent } from './ui/card';
77

88
const createPropertiesAndTypesTodos = async ({
9-
smartAccountWalletClient,
9+
smartSessionClient,
1010
space,
11-
}: { smartAccountWalletClient: GeoSmartAccount; space: string }) => {
11+
}: { smartSessionClient: Connect.SmartSessionClient; space: string }) => {
1212
const ops: Array<Op> = [];
1313
const { id: checkedPropertyId, ops: createCheckedPropertyOps } = Graph.createProperty({
1414
dataType: 'CHECKBOX',
@@ -67,10 +67,9 @@ const createPropertiesAndTypesTodos = async ({
6767

6868
const result = await publishOps({
6969
ops,
70-
walletClient: smartAccountWalletClient,
70+
walletClient: smartSessionClient,
7171
space,
7272
name: 'Create properties and types',
73-
network: 'TESTNET',
7473
});
7574
return {
7675
result,
@@ -87,7 +86,7 @@ const createPropertiesAndTypesTodos = async ({
8786

8887
export const CreatePropertiesAndTypesTodos = ({ space }: { space: string }) => {
8988
const [mapping, setMapping] = useState<string>('');
90-
89+
const { getSmartSessionClient } = useHypergraphApp();
9190
return (
9291
<div>
9392
{mapping && (
@@ -99,9 +98,9 @@ export const CreatePropertiesAndTypesTodos = ({ space }: { space: string }) => {
9998
)}
10099
<Button
101100
onClick={async () => {
102-
const smartAccountWalletClient = await getSmartAccountWalletClient();
103-
if (!smartAccountWalletClient) {
104-
throw new Error('Missing smartAccountWalletClient');
101+
const smartSessionClient = await getSmartSessionClient();
102+
if (!smartSessionClient) {
103+
throw new Error('Missing smartSessionClient');
105104
}
106105
const {
107106
todoTypeId,
@@ -113,8 +112,7 @@ export const CreatePropertiesAndTypesTodos = ({ space }: { space: string }) => {
113112
websitePropertyId,
114113
amountPropertyId,
115114
} = await createPropertiesAndTypesTodos({
116-
// @ts-expect-error - TODO: fix the types error
117-
smartAccountWalletClient,
115+
smartSessionClient,
118116
space,
119117
});
120118

apps/events/src/components/playground.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { getSmartAccountWalletClient } from '@/lib/smart-account';
2-
import { _useCreateEntityPublic, _useDeleteEntityPublic, useQuery } from '@graphprotocol/hypergraph-react';
1+
import {
2+
_useCreateEntityPublic,
3+
_useDeleteEntityPublic,
4+
useHypergraphApp,
5+
useQuery,
6+
} from '@graphprotocol/hypergraph-react';
37
import { useState } from 'react';
48
import { Event } from '../schema';
59
import { Button } from './ui/button';
@@ -15,6 +19,7 @@ export const Playground = () => {
1519
});
1620
const [isDeleting, setIsDeleting] = useState(false);
1721
const [isCreating, setIsCreating] = useState(false);
22+
const { getSmartSessionClient } = useHypergraphApp();
1823

1924
const deleteEntity = _useDeleteEntityPublic(Event, {
2025
space: '1c954768-7e14-4f0f-9396-0fe9dcd55fe8',
@@ -34,7 +39,7 @@ export const Playground = () => {
3439
disabled={isCreating}
3540
onClick={async () => {
3641
setIsCreating(true);
37-
const walletClient = await getSmartAccountWalletClient();
42+
const walletClient = await getSmartSessionClient();
3843
if (!walletClient) {
3944
alert('Wallet client not found');
4045
setIsCreating(false);
@@ -60,14 +65,13 @@ export const Playground = () => {
6065
<Button
6166
onClick={async () => {
6267
setIsDeleting(true);
63-
const walletClient = await getSmartAccountWalletClient();
68+
const walletClient = await getSmartSessionClient();
6469
if (!walletClient) {
6570
alert('Wallet client not found');
6671
return;
6772
}
6873
await deleteEntity({
6974
id: event.id,
70-
// @ts-expect-error - TODO: fix the types error
7175
walletClient,
7276
});
7377
setIsDeleting(false);

apps/events/src/components/todo/todos-public.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { getSmartAccountWalletClient } from '@/lib/smart-account';
21
import { Id } from '@graphprotocol/grc-20';
32
import { _generateDeleteOps, publishOps, useCreateEntity, useQuery, useSpace } from '@graphprotocol/hypergraph-react';
3+
import { useHypergraphApp } from '@graphprotocol/hypergraph-react';
44
import { useGenerateCreateOps } from '@graphprotocol/hypergraph-react/internal/use-generate-create-ops';
55
import { Todo2 } from '../../schema';
66
import { Spinner } from '../spinner';
77
import { Button } from '../ui/button';
88

99
export const TodosPublic = () => {
1010
const { id: spaceId } = useSpace({ mode: 'public' });
11+
const { getSmartSessionClient } = useHypergraphApp();
1112
const {
1213
data: dataPublic,
1314
isLoading: isLoadingPublic,
@@ -44,15 +45,14 @@ export const TodosPublic = () => {
4445

4546
<Button
4647
onClick={async () => {
47-
const smartAccountWalletClient = await getSmartAccountWalletClient();
48-
if (!smartAccountWalletClient) {
49-
throw new Error('Missing smartAccountWalletClient');
48+
const smartSessionClient = await getSmartSessionClient();
49+
if (!smartSessionClient) {
50+
throw new Error('Missing smartSessionClient');
5051
}
5152
const ops = await _generateDeleteOps({ id: todo.id, space: spaceId });
5253
const result = await publishOps({
5354
ops,
54-
// @ts-expect-error - TODO: fix the types error
55-
walletClient: smartAccountWalletClient,
55+
walletClient: smartSessionClient,
5656
space: spaceId,
5757
name: 'Delete Todo',
5858
});
@@ -65,9 +65,9 @@ export const TodosPublic = () => {
6565
))}
6666
<Button
6767
onClick={async () => {
68-
const smartAccountWalletClient = await getSmartAccountWalletClient();
69-
if (!smartAccountWalletClient) {
70-
throw new Error('Missing smartAccountWalletClient');
68+
const smartSessionClient = await getSmartSessionClient();
69+
if (!smartSessionClient) {
70+
throw new Error('Missing smartSessionClient');
7171
}
7272
const userId = Id.Id('8zPJjTGLBDPtUcj6q2tghg');
7373
const todo = createTodo({
@@ -84,8 +84,7 @@ export const TodosPublic = () => {
8484
console.log('ops', ops);
8585
const result = await publishOps({
8686
ops,
87-
// @ts-expect-error - TODO: fix the types error
88-
walletClient: smartAccountWalletClient,
87+
walletClient: smartSessionClient,
8988
space: spaceId,
9089
name: 'Create Todo',
9190
});

0 commit comments

Comments
 (0)