This repository was archived by the owner on May 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathindex.ts
More file actions
110 lines (95 loc) · 3.12 KB
/
index.ts
File metadata and controls
110 lines (95 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import {
ActionProvider,
CreateAction,
EvmWalletProvider,
type Network,
} from '@coinbase/agentkit';
import { base } from 'viem/chains';
import type { z } from 'zod';
import { FarcasterUsernameSchema } from './schemas.js';
import type { NeynarUserResponse } from './types.js';
export class FarcasterActionProvider extends ActionProvider<EvmWalletProvider> {
constructor() {
super('farcaster', []);
}
@CreateAction({
name: 'farcaster_username',
description: 'Resolve a Farcaster username to an Ethereum address',
schema: FarcasterUsernameSchema,
})
async farcasterUsername(
walletProvider: EvmWalletProvider,
args: z.infer<typeof FarcasterUsernameSchema>,
) {
const { username } = args;
// Get Neynar API key from environment variables
const neynarApiKey = process.env.NEYNAR_API_KEY;
if (!neynarApiKey) {
throw new Error('NEYNAR_API_KEY environment variable is not set');
}
try {
// Call Neynar API to search for the user
const response = await fetch(
`https://api.neynar.com/v2/farcaster/user/search?q=${encodeURIComponent(username)}`,
{
headers: {
accept: 'application/json',
'x-api-key': neynarApiKey,
},
},
);
if (!response.ok) {
throw new Error(
`Neynar API error: ${response.status} ${response.statusText}`,
);
}
const data = (await response.json()) as NeynarUserResponse;
// Check if any users were found
if (!data.result.users || data.result.users.length === 0) {
return JSON.stringify({
success: false,
message: `No Farcaster user found with username: ${username}`,
});
}
// Find the user with the matching username (case-insensitive)
const user = data.result.users.find(
(u) => u.username.toLowerCase() === username.toLowerCase(),
);
if (!user) {
return JSON.stringify({
success: false,
message: `No Farcaster user found with username: ${username}`,
});
}
// Check if the user has any verified Ethereum addresses
if (
!user.verified_addresses?.eth_addresses ||
user.verified_addresses.eth_addresses.length === 0
) {
return JSON.stringify({
success: false,
message: `User ${username} has no verified Ethereum addresses`,
});
}
// Return the primary Ethereum address if available, otherwise the first one
const ethAddress =
user.verified_addresses.primary?.eth_address ||
user.verified_addresses.eth_addresses[0];
return JSON.stringify({
success: true,
username: user.username,
fid: user.fid,
ethAddress,
});
} catch (error) {
return JSON.stringify({
success: false,
message: `Error resolving Farcaster username: ${error instanceof Error ? error.message : String(error)}`,
});
}
}
supportsNetwork(network: Network): boolean {
return network.chainId === String(base.id);
}
}
export const farcasterActionProvider = () => new FarcasterActionProvider();