Skip to content

Commit

Permalink
fix(platforms): update lens provider to v2 api (#1976)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-schultz authored Dec 8, 2023
1 parent 7f19729 commit cf791cf
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
34 changes: 24 additions & 10 deletions platforms/src/Lens/Providers/lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import axios from "axios";
// ----- Utils
import { handleProviderAxiosError } from "../../utils/handleProviderAxiosError";

interface DefaultProfile {
interface Profile {
id: string;
handle: string;
fullHandle: string;
ownedBy: string;
}

interface Data {
defaultProfile: DefaultProfile;
ownedHandles: {
items: Profile[];
};
}

interface GraphQlResponse {
Expand All @@ -27,26 +30,37 @@ interface LensProfileResponse {
errors: string[];
}

const lensApiEndpoint = "https://api.lens.dev";
const lensApiEndpoint = "https://api-v2.lens.dev/";

async function getLensProfile(userAddress: string): Promise<LensProfileResponse> {
try {
const query = `
query DefaultProfile {
defaultProfile(request: { ethereumAddress: "${userAddress}"}) {
id
handle
query OwnedHandles {
ownedHandles(request: {
for: "${userAddress}"
}) {
items {
id
ownedBy
fullHandle
}
}
}
`;
const result: { data: GraphQlResponse } = await axios.post(lensApiEndpoint, {
query,
});

if (result?.data?.data?.defaultProfile?.handle) {
const handles = result?.data?.data?.ownedHandles?.items;

const validHandle = handles?.find(
(handle) => handle.ownedBy.toLocaleLowerCase() === userAddress.toLocaleLowerCase()
);

if (validHandle) {
return {
valid: true,
handle: result?.data?.data?.defaultProfile?.handle,
handle: validHandle.fullHandle,
errors: [],
};
} else {
Expand Down
36 changes: 33 additions & 3 deletions platforms/src/Lens/__tests__/lens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ describe("Attempt verification", function () {
mockedAxios.post.mockResolvedValueOnce({
data: {
data: {
defaultProfile: {
id: MOCK_ADDRESS,
handle: MOCK_HANDLE,
ownedHandles: {
items: [
{
id: MOCK_ADDRESS,
fullHandle: MOCK_HANDLE,
ownedBy: MOCK_ADDRESS,
},
],
},
},
},
Expand All @@ -43,6 +48,31 @@ describe("Attempt verification", function () {
});
});

it("should return false if owner addresses do not match", async () => {
mockedAxios.post.mockResolvedValueOnce({
data: {
data: {
ownedHandles: {
items: [
{
id: MOCK_ADDRESS,
fullHandle: MOCK_HANDLE,
ownedBy: MOCK_FAKE_ADDRESS,
},
],
},
},
},
});

const lens = new LensProfileProvider();
const verifiedPayload = await lens.verify({
address: MOCK_ADDRESS_LOWER,
} as RequestPayload);

expect(verifiedPayload.valid).toEqual(false);
});

it("should return false for an address without a lens handle", async () => {
mockedAxios.post.mockResolvedValueOnce({
data: {
Expand Down

0 comments on commit cf791cf

Please sign in to comment.