Skip to content
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

Feat: Registration Discount codes #1084

Merged
merged 22 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/web/app/(basenames)/names/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ export const metadata: Metadata = {
export default async function Page() {
return (
<ErrorsProvider context="registration">
<RegistrationProviders>
<Suspense>
<Suspense>
<RegistrationProviders>
<main>
<RegistrationFlow />
<RegistrationValueProp />
<PoweredByEns />
<RegistrationFAQ />
</main>
</Suspense>
</RegistrationProviders>
</RegistrationProviders>
</Suspense>
</ErrorsProvider>
);
}
36 changes: 36 additions & 0 deletions apps/web/pages/api/proofs/discountCode/consume/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { logger } from 'apps/web/src/utils/logger';
import { withTimeout } from 'apps/web/pages/api/decorators';
import { incrementDiscountCodeUsage } from 'apps/web/src/utils/proofs/discount_code_storage';

/*
this endpoint will increment the discount code usage to prevent abuse
*/

type DiscountCodeRequest = {
code: string;
};

async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}

try {
const { code } = req.body as DiscountCodeRequest;

if (!code || typeof code !== 'string') {
return res.status(500).json({ error: 'Invalid request' });
}

await incrementDiscountCodeUsage(code);

return res.status(200).json({ success: true });
} catch (error: unknown) {
logger.error('error incrementing the discount code', error);
}
// If error is not an instance of Error, return a generic error message
return res.status(500).json({ error: 'An unexpected error occurred' });
}

export default withTimeout(handler);
80 changes: 80 additions & 0 deletions apps/web/pages/api/proofs/discountCode/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { proofValidation, signDiscountMessageWithTrustedSigner } from 'apps/web/src/utils/proofs';
import { logger } from 'apps/web/src/utils/logger';
import { withTimeout } from 'apps/web/pages/api/decorators';
import { Address, Hash, stringToHex } from 'viem';
import { USERNAME_DISCOUNT_CODE_VALIDATORS } from 'apps/web/src/addresses/usernames';
import { baseSepolia } from 'viem/chains';
import { getDiscountCode } from 'apps/web/src/utils/proofs/discount_code_storage';

export type DiscountCodeResponse = {
discountValidatorAddress: Address;
address: Address;
signedMessage: Hash;
};

/*
this endpoint returns whether or a discount code is valid
*/
async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'method not allowed' });
}
const { address, chain, code } = req.query;
const validationErr = proofValidation(address, chain);
if (validationErr) {
return res.status(validationErr.status).json({ error: validationErr.error });
}

if (!code || typeof code !== 'string') {
return res.status(500).json({ error: 'Discount code invalid' });
}

try {
// 1. get the database model
const discountCodes = await getDiscountCode(code);

// 2. Validation: Coupon exists
if (!discountCodes || discountCodes.length === 0) {
return res.status(500).json({ error: 'Discount code invalid' });
}

const discountCode = discountCodes[0];

// 2.1 Validation: Coupon is expired
if (new Date(discountCode.expires_at) < new Date()) {
return res.status(500).json({ error: 'Discount code invalid' });
}

// 2.2 Validation: Coupon can be redeemed
if (Number(discountCode.usage_count) >= Number(discountCode.usage_limit)) {
return res.status(500).json({ error: 'Discount code invalid' });
}

// 3. Sign the validationData
const couponCodeUuid = stringToHex(discountCode.code, { size: 32 });
const expirationTimeUnix = Math.floor(discountCode.expires_at.getTime() / 1000);

const signature = await signDiscountMessageWithTrustedSigner(
address as Address,
couponCodeUuid,
USERNAME_DISCOUNT_CODE_VALIDATORS[baseSepolia.id],
expirationTimeUnix,
);

// 4. Return the discount data
const result: DiscountCodeResponse = {
discountValidatorAddress: USERNAME_DISCOUNT_CODE_VALIDATORS[baseSepolia.id],
address: address as Address,
signedMessage: signature,
};

return res.status(200).json(result);
} catch (error: unknown) {
logger.error('error getting proofs for discount code', error);
}
// If error is not an instance of Error, return a generic error message
return res.status(500).json({ error: 'An unexpected error occurred' });
}

export default withTimeout(handler);
5 changes: 5 additions & 0 deletions apps/web/src/addresses/usernames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ export const EXPONENTIAL_PREMIUM_PRICE_ORACLE: AddressMap = {
[baseSepolia.id]: '0x2B73408052825e17e0Fe464f92De85e8c7723231',
[base.id]: '0xd53B558e1F07289acedf028d226974AbBa258312',
};

export const USERNAME_DISCOUNT_CODE_VALIDATORS: AddressMap = {
[baseSepolia.id]: '0x52acEeB464F600437a3681bEC087fb53F3f75638',
// [base.id]: '0x6E89d99643DB1223697C77A9F8B2Cb07E898e743',
kirkas marked this conversation as resolved.
Show resolved Hide resolved
};
Loading
Loading