Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
add api call for getConnectedAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazeeh21 committed Mar 9, 2024
1 parent 7ba1d05 commit 29ebacb
Showing 1 changed file with 123 additions and 77 deletions.
200 changes: 123 additions & 77 deletions src/functions/getConnectedAddresses.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,58 @@
import { queryClient } from "../queryClient";

import { Provider, HubProvider, NeynarProvider } from "../providers";
import { ConnectedAddresses } from "../types";

export default function getConnectedAddresses(provider: Provider, fid: number, ethereum?: boolean, solana?: boolean): ConnectedAddresses {
let addresses: ConnectedAddresses;

if (provider instanceof HubProvider) {
if (provider.psqlUrl)
addresses = getConnectedAddressesFromReplicator(provider.psqlUrl, fid, ethereum, solana);
else
addresses = getConnectedAddressesFromHub(provider.hubUrl, fid, ethereum, solana);
}
else if (provider instanceof NeynarProvider) {
addresses = getConnectedAddressesFromNeynar(provider, fid, ethereum, solana);
}
else {
throw new Error("Provider not supported");
}

return addresses;
import { queryClient } from '../queryClient';

import { Provider, HubProvider, NeynarProvider } from '../providers';
import { ConnectedAddresses } from '../types';

export default function getConnectedAddresses(
provider: Provider,
fid: number,
ethereum?: boolean,
solana?: boolean
): ConnectedAddresses {
let addresses: ConnectedAddresses;

if (provider instanceof HubProvider) {
if (provider.psqlUrl)
addresses = getConnectedAddressesFromReplicator(
provider.psqlUrl,
fid,
ethereum,
solana
);
else
addresses = getConnectedAddressesFromHub(
provider.hubUrl,
fid,
ethereum,
solana
);
} else if (provider instanceof NeynarProvider) {
addresses = getConnectedAddressesFromNeynar(
provider,
fid,
ethereum,
solana
);
} else {
throw new Error('Provider not supported');
}

return addresses;
}

function getConnectedAddressesFromReplicator(psqlUrl: string, fid: number, ethereum?: boolean, solana?: boolean): ConnectedAddresses {
let addresses: ConnectedAddresses = {
all: [],
ethereum: [],
solana: [],
};
// ...
throw new Error("Not implemented");
function getConnectedAddressesFromReplicator(
psqlUrl: string,
fid: number,
ethereum?: boolean,
solana?: boolean
): ConnectedAddresses {
let addresses: ConnectedAddresses = {
all: [],
ethereum: [],
solana: [],
};
// ...
throw new Error('Not implemented');
}

/**
Expand All @@ -41,58 +64,81 @@ function getConnectedAddressesFromReplicator(psqlUrl: string, fid: number, ether
* @param solana is ignored as hubs return all addresses anyway
* @returns ConnectedAddresses for the qiven fid
*/
function getConnectedAddressesFromHub(hubUrl: string, fid: number, ethereum?: boolean, solana?: boolean): ConnectedAddresses {
let addresses: ConnectedAddresses = {
all: [],
ethereum: [],
solana: [],
function getConnectedAddressesFromHub(
hubUrl: string,
fid: number,
ethereum?: boolean,
solana?: boolean
): ConnectedAddresses {
let addresses: ConnectedAddresses = {
all: [],
ethereum: [],
solana: [],
};

try {
const fetchVerificationsByFid = async (fid: string) => {
const response = await fetch(
`${hubUrl}/v1/verificationsByFid?fid=${fid}`
);

if (!response.ok) {
throw new Error('Network response was not ok');
}

return response.json();
};

try {
const queryUrl = `${hubUrl}/v1/verificationsByFid?fid=${fid}`;

/*
* TODO: use the queryClient to fetch data from the Hub API endpoint
* sample curl command: curl --request GET --url https://hub.pinata.cloud/v1/verificationsByFid?fid=2
*/

const verificationResponse = queryClient.somehowGetResponseFrom(queryUrl);


if (verificationResponse.isOk() && verificationResponse.value) {
verificationResponse.messages.forEach((verification) => {

if (verification.data?.verificationAddAddressBody?.protocol === 0) { // protocol === 0 guarantees only ETH addresses
const addressBytes = verification.data?.verificationAddAddressBody.address;
const address = `0x${Buffer.from(addressBytes).toString('hex')}`;
addresses.all.push(address);
addresses.ethereum.push(address);
}

if (verification.data?.verificationAddAddressBody?.protocol === 1) { // protocol === 1 guarantees only SOL addresses
const addressBytes = verification.data?.verificationAddAddressBody.address;
const address = `${Buffer.from(addressBytes).toString('hex')}`;
addresses.all.push(address);
addresses.solana.push(address);
}
/*
* TODO: use the queryClient to fetch data from the Hub API endpoint
* sample curl command: curl --request GET --url https://hub.pinata.cloud/v1/verificationsByFid?fid=2
*/

const verificationResponse = queryClient.fetchQuery({
queryKey: ['verificationsByFid', fid],
queryFn: () => fetchVerificationsByFid(fid.toString()),
});

if (verificationResponse.isOk() && verificationResponse.value) {
verificationResponse.messages.forEach(verification => {
if (verification.data?.verificationAddAddressBody?.protocol === 0) {
// protocol === 0 guarantees only ETH addresses
const addressBytes =
verification.data?.verificationAddAddressBody.address;
const address = `0x${Buffer.from(addressBytes).toString('hex')}`;
addresses.all.push(address);
addresses.ethereum.push(address);
}

});
if (verification.data?.verificationAddAddressBody?.protocol === 1) {
// protocol === 1 guarantees only SOL addresses
const addressBytes =
verification.data?.verificationAddAddressBody.address;
const address = `${Buffer.from(addressBytes).toString('hex')}`;
addresses.all.push(address);
addresses.solana.push(address);
}
} catch (e) {
console.error(e);
throw new Error("Error getting verifications from hub");
});
}
return addresses;
} catch (e) {
console.error(e);
throw new Error('Error getting verifications from hub');
}
return addresses;
}

function getConnectedAddressesFromNeynar(provider: NeynarProvider, fid: number, ethereum?: boolean, solana?: boolean): ConnectedAddresses {
let addresses: ConnectedAddresses = {
all: [],
ethereum: [],
solana: [],
};
// ...


throw new Error("Not implemented");
function getConnectedAddressesFromNeynar(
provider: NeynarProvider,
fid: number,
ethereum?: boolean,
solana?: boolean
): ConnectedAddresses {
let addresses: ConnectedAddresses = {
all: [],
ethereum: [],
solana: [],
};
// ...

throw new Error('Not implemented');
}

0 comments on commit 29ebacb

Please sign in to comment.