Skip to content

Commit

Permalink
Make cbId search lowercase and case sensitive to search by index (#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoMogg authored Aug 17, 2024
1 parent 8a7575e commit 84e8721
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 3 additions & 1 deletion apps/web/pages/api/proofs/cbid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

try {
const responseData = await getWalletProofs(
address as `0x${string}`,
// to lower case to be able to use index on huge dataset
(address as string).toLowerCase() as `0x${string}`,
parseInt(chain as string),
ProofTableNamespace.CBIDDiscount,
false,
);

return res.status(200).json(responseData);
Expand Down
21 changes: 14 additions & 7 deletions apps/web/src/utils/proofs/proofs_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export enum ProofTableNamespace {
UsernamesEarlyAccess = 'usernames_early_access',
BNSDiscount = 'basenames_bns_discount',
BaseEthHolders = 'basenames_base_eth_holders_discount',
CBIDDiscount = 'usernames_cbid_discount',
CBIDDiscount = 'basenames_cbid_discount',
}

type ProofsTable = {
Expand All @@ -26,12 +26,19 @@ const proofTableName = 'proofs';
export async function getProofsByNamespaceAndAddress(
address: Address,
namespace: ProofTableNamespace,
caseInsensitive = true, // set false for big data sets
) {
return createKysely<Database>()
let query = createKysely<Database>()
.selectFrom(proofTableName)
.where('address', 'ilike', address)
.where('namespace', '=', namespace.valueOf())
.selectAll()
.limit(1)
.execute();
.where('namespace', '=', namespace.valueOf());

/**
* use = when possible to search by namespace_address index otherwise it's a text based search.
*/
if (caseInsensitive) {
query = query.where('address', 'ilike', address);
} else {
query = query.where('address', '=', address);
}
return query.selectAll().limit(1).execute();
}
3 changes: 2 additions & 1 deletion apps/web/src/utils/proofs/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ export async function getWalletProofs(
address: `0x${string}`,
chain: number,
namespace: ProofTableNamespace,
caseInsensitive = true, // set false for big data sets
): Promise<MerkleTreeProofResponse> {
const hasPreviouslyRegistered = await hasRegisteredWithDiscount([address], chain);
// if any linked address registered previously return an error
if (hasPreviouslyRegistered) {
throw new ProofsException('This address has already claimed a username.', 400);
}
const [content] = await getProofsByNamespaceAndAddress(address, namespace);
const [content] = await getProofsByNamespaceAndAddress(address, namespace, caseInsensitive);
const proofs = content?.proofs ? (JSON.parse(content.proofs) as `0x${string}`[]) : [];
if (proofs.length === 0) {
throw new ProofsException(`address is not eligible for [${namespace}] this discount.`, 404);
Expand Down

0 comments on commit 84e8721

Please sign in to comment.