Skip to content

Commit

Permalink
feat: use BigInt for on-pay (#4188)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomquirk authored Jan 13, 2024
1 parent 183e667 commit 83e5b7b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ exports[`SummaryExpandedView should render correctly 1`] = `
<div
class="flex items-center justify-center overflow-hidden text-4xl bg-smoke-100 dark:bg-slate-700 h-14 w-14 rounded-full"
>
<img
alt="undefined logo"
class="h-full w-full object-cover object-center"
crossorigin="anonymous"
src="/api/juicebox/pv/undefined/project/undefined/logo"
/>
<span>
🧃
</span>
</div>
<span
class="ml-3 min-w-0 font-medium dark:text-slate-50"
Expand Down
70 changes: 35 additions & 35 deletions src/pages/api/events/on-pay.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber, utils } from 'ethers'
import { utils } from 'ethers'
import { emailServerClient } from 'lib/api/postmark'
import { sudoPublicDbClient } from 'lib/api/supabase/clients'
import { authCheck } from 'lib/auth'
Expand All @@ -20,39 +20,40 @@ const JUICE_API_EVENTS_ENABLED = process.env.JUICE_API_EVENTS_ENABLED === 'true'

const logger = getLogger('api/events/on-pay')

const BigNumberValidator = (errorMessage: string) => {
return Yup.mixed<BigNumber>()
const BigIntValidator = (errorMessage: string) => {
return Yup.mixed<bigint>()
.transform(current => {
try {
return BigNumber.from(current)
return BigInt(current)
} catch (e) {
return undefined
}
})
.test('is-big-number', errorMessage, value => BigNumber.isBigNumber(value))
.test('is-bigint', errorMessage, value => typeof value === 'bigint')
}

const Schema = Yup.object().shape({
fundingCycleConfiguration: BigNumberValidator(
'fundingCycleConfiguration must be an ethers BigNumber',
).required(),
fundingCycleNumber: BigNumberValidator(
'fundingCycleNumber must be an ethers BigNumber',
).required(),
projectId: BigNumberValidator(
'projectId must be an ethers BigNumber',
).required(),
payer: Yup.string().required(),
beneficiary: Yup.string().required(),
amount: BigNumberValidator('amount must be an ethers BigNumber').required(),
beneficiaryTokenCount: BigNumberValidator(
'beneficiaryTokenCount must be an ethers BigNumber',
).required(),
memo: Yup.string(),
metadata: Yup.string().required(),
from: Yup.string().required(),
blockHash: Yup.string().required(),
blockNumber: Yup.number(),
data: Yup.object().shape({
fundingCycleConfiguration: BigIntValidator(
'fundingCycleConfiguration must be a BigInt',
).required(),
fundingCycleNumber: BigIntValidator(
'fundingCycleNumber must be a BigInt',
).required(),
projectId: BigIntValidator('projectId must be a BigInt').required(),
payer: Yup.string().required(),
beneficiary: Yup.string().required(),
amount: BigIntValidator('amount must be a BigInt').required(),
beneficiaryTokenCount: BigIntValidator(
'beneficiaryTokenCount must be a BigInt',
).required(),
memo: Yup.string(),
metadata: Yup.string().required(),
}),
metadata: Yup.object().shape({
transactionHash: Yup.string().required(),
// TODO add more fields if needed
}),
})

type OnPayEvent = Awaited<ReturnType<typeof Schema.validate>>
Expand All @@ -66,11 +67,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {

const event = await Schema.validate(req.body)

const emailMetadata = await compileEmailMetadata(event)

const emailEvents = await findEmailEventsForProjectId(
event.projectId.toNumber(),
event.payer.toLowerCase(),
Number(event.data.projectId),
event.data.payer.toLowerCase(),
)
const emailMetadata = await compileEmailMetadata(event)

await sendEmails(emailMetadata, emailEvents)

Expand Down Expand Up @@ -107,10 +109,8 @@ type EmailMetadata = {
}

const compileEmailMetadata = async ({
projectId,
amount,
payer,
blockHash,
data: { projectId, amount, payer },
metadata: { transactionHash },
}: OnPayEvent): Promise<EmailMetadata> => {
const formattedAmount = fromWad(amount.toString())
const normalizedPayerAddress = utils.getAddress(payer)
Expand All @@ -127,7 +127,7 @@ const compileEmailMetadata = async ({

let projectName = `Project ${projectId.toString()}`
try {
const projectMetadata = await getProjectMetadata(projectId.toNumber())
const projectMetadata = await getProjectMetadata(Number(projectId))
if (projectMetadata?.name) {
projectName = projectMetadata.name
}
Expand All @@ -138,8 +138,8 @@ const compileEmailMetadata = async ({
})
}

const transactionName = blockHash
const transactionUrl = `https://etherscan.io/block/${blockHash}`
const transactionName = transactionHash
const transactionUrl = `https://etherscan.io/tx/${transactionHash}`

return {
amount: formattedAmount,
Expand Down

2 comments on commit 83e5b7b

@vercel
Copy link

@vercel vercel bot commented on 83e5b7b Jan 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 83e5b7b Jan 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.