-
Notifications
You must be signed in to change notification settings - Fork 18
feat(farcaster): enable offchain resolver to farcaster names #194 #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
developerfred
wants to merge
13
commits into
snapshot-labs:master
Choose a base branch
from
developerfred:feat/farcaster-resolver-offchain
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ba119ea
feat(farcaster): enable offchain resolver to farcaster names #194
developerfred c852b1d
chore(farcaster): fix neynar api url users
developerfred f04f71c
Update farcaster.ts
developerfred d404b89
Merge branch 'master' into feat/farcaster-resolver-offchain
developerfred 215654f
chore(fetch): fixed farcaster script
developerfred 5027da0
chore(farcaster): fix function to running test
developerfred de688be
chore(farcaster): update types
developerfred e0a88b8
chore(farcaster): add path alias and DRY functions
developerfred fddb55f
chore(farcaster): fix code review
developerfred 57e67dd
Merge branch 'master' into feat/farcaster-resolver-offchain
developerfred b24f960
chore(farcaster): fix tscofing and package.json
developerfred 3d78986
Merge branch 'master' into feat/farcaster-resolver-offchain
developerfred 3444e4a
Merge branch 'master' into feat/farcaster-resolver-offchain
developerfred File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { capture } from '@snapshot-labs/snapshot-sentry'; | ||
| import { graphQlCall, Address, Handle, FetchError, isSilencedError, isEvmAddress } from './utils'; | ||
|
developerfred marked this conversation as resolved.
Outdated
|
||
|
|
||
|
developerfred marked this conversation as resolved.
|
||
| export const NAME = 'Farcaster'; | ||
| const FNAMES_API_URL = 'https://fnames.farcaster.xyz/transfers?name='; | ||
| const NEYNAR_API_URL = 'https://api.neynar.com/v2/farcaster/user/'; | ||
| const API_KEY = process.env.NEYNAR_API_KEY ?? ''; | ||
|
|
||
|
|
||
| interface UserDetails { | ||
| username: string; | ||
| verified_addresses: { | ||
| eth_addresses: string[]; | ||
| sol_addresses: string[]; | ||
| }; | ||
| pfp_url: string; | ||
| } | ||
|
|
||
| interface ApiResponse { | ||
| [address: string]: UserDetails[]; | ||
| } | ||
|
|
||
| interface UserResult { | ||
| username?: string; | ||
| eth_addresses?: string[]; | ||
| sol_addresses?: string[]; | ||
| pfp_url?: string; | ||
| } | ||
|
|
||
|
|
||
| async function fetchData<T>(url: string, method: string = 'GET'): Promise<T> { | ||
|
developerfred marked this conversation as resolved.
Outdated
|
||
| const headers = { | ||
| Accept: 'application/json', | ||
| api_key: API_KEY | ||
| }; | ||
| const response = await fetch(url, { method, headers }); | ||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch data from the API. Status: ${response.status}`); | ||
| } | ||
| return response.json() as Promise<T>; | ||
| } | ||
|
|
||
| export async function lookupAddresses( | ||
|
developerfred marked this conversation as resolved.
Outdated
|
||
| addresses: string[] | ||
| ): Promise<{ [key: string]: UserResult | string }> { | ||
| const results: { [key: string]: UserResult | string } = {}; | ||
| try { | ||
| const addressesQuery = addresses.join(','); | ||
|
developerfred marked this conversation as resolved.
Outdated
|
||
| const url = `${NEYNAR_API_URL}bulk-by-address?addresses=${addressesQuery}`; | ||
| const userDetails = await fetchData<ApiResponse>(url); | ||
|
|
||
| for (const [address, data] of Object.entries(userDetails)) { | ||
| if (Array.isArray(data) && data.length > 0) { | ||
| const { | ||
| username, | ||
| verified_addresses: { eth_addresses = [], sol_addresses = [] }, | ||
| pfp_url | ||
| } = data[0]; | ||
| results[address] = { username, eth_addresses, sol_addresses, pfp_url }; | ||
| } else { | ||
| results[address] = 'No user found for this address.'; | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error(`Error fetching address details:`, error); | ||
|
developerfred marked this conversation as resolved.
Outdated
|
||
| throw new Error(`Error fetching address details.`); | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| async function fetchUserDetailsByUsername(username: string): Promise<UserResult | null> { | ||
| try { | ||
| const transferData = await fetchData<{ transfers: any[] }>(`${FNAMES_API_URL}${username}`); | ||
| if (transferData.transfers.length > 0) { | ||
| const userDetails = await fetchData<{ result: { users: UserDetails[] } }>( | ||
| `${NEYNAR_API_URL}search?q=${username}&viewer_fid=197049` | ||
| ); | ||
| if (userDetails.result && userDetails.result.users.length > 0) { | ||
| const { username, verified_addresses, pfp_url } = userDetails.result.users[0]; | ||
| return { | ||
| eth_addresses: verified_addresses.eth_addresses, | ||
| username | ||
| }; | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error(`Error fetching user details for ${username}:`, error); | ||
| throw new Error(`Error fetching user details for ${username}.`); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export async function resolveNames( | ||
|
developerfred marked this conversation as resolved.
Outdated
|
||
| handles: string[] | ||
| ): Promise<{ [handle: string]: UserResult | string }> { | ||
| const results: { [handle: string]: UserResult | string } = {}; | ||
| for (const handle of handles) { | ||
| const normalizedHandle = handle.replace('.fcast.id', ''); | ||
| const userDetails = await fetchUserDetailsByUsername(normalizedHandle); | ||
|
developerfred marked this conversation as resolved.
Outdated
|
||
| if (userDetails) { | ||
| results[handle] = userDetails; | ||
| } else { | ||
| results[handle] = 'User not found or error searching for details.'; | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { resolveNames, lookupAddresses } from '../../../src/addressResolvers/farcaster'; | ||
| import testAddressResolver from './helper'; | ||
|
|
||
| testAddressResolver({ | ||
| name: 'Farcaster', | ||
| lookupAddresses, | ||
| resolveNames, | ||
| validAddress: '0xd1a8Dd23e356B9fAE27dF5DeF9ea025A602EC81e', | ||
| validDomain: 'codingsh.fcast.id', | ||
| blankAddress: '0x0000000000000000000000000000000000000000', | ||
| invalidDomains: ['domain.crypto', 'domain.eth', 'domain.com'] | ||
| }); | ||
|
developerfred marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.