-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove farcaster references replace w/ neynar api (#59)
Co-authored-by: Rob Polak <[email protected]>
- Loading branch information
1 parent
cd67802
commit caa2788
Showing
12 changed files
with
314 additions
and
854 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,59 @@ | ||
import { FetchError } from '../exceptions/FetchError'; | ||
import { neynarFrameValidation } from './neynarFrameFunctions'; | ||
|
||
describe('neynar frame functions', () => { | ||
let fetchMock = jest.fn(); | ||
let status = 200; | ||
|
||
beforeEach(() => { | ||
status = 200; | ||
global.fetch = jest.fn(() => | ||
Promise.resolve({ | ||
status, | ||
json: fetchMock, | ||
}), | ||
) as jest.Mock; | ||
}); | ||
|
||
it('should return fetch response correctly', async () => { | ||
const mockedResponse = { | ||
valid: true, | ||
action: { | ||
tapped_button: { | ||
index: 1, | ||
}, | ||
cast: { | ||
viewer_context: { | ||
recasted: true, | ||
}, | ||
}, | ||
interactor: { | ||
fid: 1234, | ||
verifications: ['0x00123'], | ||
viewer_context: { | ||
following: true, | ||
followed_by: true, | ||
}, | ||
}, | ||
}, | ||
}; | ||
fetchMock.mockResolvedValue(mockedResponse); | ||
const message = '0x00'; | ||
const resp = await neynarFrameValidation(message); | ||
|
||
expect(resp?.valid).toEqual(true); | ||
expect(resp?.recasted).toEqual(mockedResponse.action.cast.viewer_context.recasted); | ||
expect(resp?.button).toEqual(mockedResponse.action.tapped_button.index); | ||
expect(resp?.interactor?.fid).toEqual(mockedResponse.action.interactor.fid); | ||
expect(resp?.interactor?.verified_accounts).toEqual( | ||
mockedResponse.action.interactor.verifications, | ||
); | ||
expect(resp?.following).toEqual(mockedResponse.action.interactor.viewer_context.following); | ||
}); | ||
|
||
it('fails on a non-200', async () => { | ||
status = 401; | ||
const resp = neynarFrameValidation('0x00'); | ||
await expect(resp).rejects.toThrow(FetchError); | ||
}); | ||
}); |
This file contains 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,28 @@ | ||
import { FetchError } from '../exceptions/FetchError'; | ||
import { convertToNeynarResponseModel, NeynarFrameValidationResponse } from './neynarFrameModels'; | ||
|
||
export const NEYNAR_DEFAULT_API_KEY = 'NEYNAR_ONCHAIN_KIT'; | ||
|
||
export async function neynarFrameValidation( | ||
messageBytes: string, | ||
apiKey: string = NEYNAR_DEFAULT_API_KEY, | ||
castReactionContext = true, | ||
followContext = true, | ||
): Promise<NeynarFrameValidationResponse | undefined> { | ||
const options = { | ||
method: 'POST', | ||
url: `https://api.neynar.com/v2/farcaster/frame/validate`, | ||
headers: { accept: 'application/json', api_key: apiKey, 'content-type': 'application/json' }, | ||
body: JSON.stringify({ | ||
message_bytes_in_hex: messageBytes, | ||
cast_reaction_context: castReactionContext, // Returns if the user has liked/recasted | ||
follow_context: followContext, // Returns if the user is Following | ||
}), | ||
}; | ||
const resp = await fetch(options.url, options); | ||
if (resp.status !== 200) { | ||
throw new FetchError(`non-200 status returned from neynar : ${resp.status}`); | ||
} | ||
const responseBody = await resp.json(); | ||
return convertToNeynarResponseModel(responseBody); | ||
} |
Oops, something went wrong.