Skip to content

Commit

Permalink
fix come back period
Browse files Browse the repository at this point in the history
  • Loading branch information
DharmikJagodana committed Apr 18, 2024
1 parent f7a9b53 commit 40be80f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 26 deletions.
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ model RequestLogs {
fid Int
url String
messageHash String
timestamp Int
timestamp DateTime
network Int
address String
buttonIndex Int
Expand All @@ -32,7 +32,7 @@ model Transactions {
userId String
txHash String
messageHash String
timestamp Int
timestamp DateTime
network Int
address String
status String
Expand Down
58 changes: 34 additions & 24 deletions src/app/frames/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,26 @@ export async function POST(request: NextRequest) {
},
});
}

const currentTime = new Date().getTime();
// logs request body
await prisma.requestLogs.create({
data: {
fid: body.untrustedData.fid,
url: body.untrustedData.url,
messageHash: body.untrustedData.messageHash,
timestamp: new Date(currentTime),
network: body.untrustedData.network,
buttonIndex: body.untrustedData.buttonIndex,
address: '',
castId: {
fid: body.untrustedData.castId.fid,
hash: body.untrustedData.castId.hash,
},
trustedData: {
messageBytes: body.trustedData.messageBytes,
},
},
});
if (!follows) {
return mustFollowFrame(userFid, targetFid);
}
Expand All @@ -62,20 +81,29 @@ export async function POST(request: NextRequest) {
const [userAddress] = ethAddresses.split(',');
const isAlreadyMinted = await checkIfAlreadyMinted(userAddress);
if (isAlreadyMinted) {
const lastAllowedMint = currentTime - config.allowMintEveryHours * 60 * 60 * 1000;
// check if 24 passed
const txInLast24Hours = await prisma.transactions.findMany({
where: {
fid: userFid,
timestamp: {
gte:
new Date().getTime() - config.allowMintEveryHours * 60 * 60 * 1000,
gte: new Date(lastAllowedMint),
},
},
});

if (txInLast24Hours.length > 0) {
const mintTime = txInLast24Hours[0].timestamp.getTime();
// calculate how many hours after i can mint again
const comeAfterHours = (
config.allowMintEveryHours * 60 * 60 * 1000 -
(currentTime - mintTime)
) / (60 * 60 * 1000);

return alreadyMinted(
txInLast24Hours[0].txHash,
txInLast24Hours[0].address,
Math.floor(comeAfterHours) + '',
);
}
}
Expand All @@ -90,7 +118,7 @@ export async function POST(request: NextRequest) {
status: 'pending',
network: body.untrustedData.network,
address: userAddress,
timestamp: new Date().getTime(),
timestamp: new Date(currentTime),
},
});
if (txHash) {
Expand All @@ -110,25 +138,6 @@ export async function POST(request: NextRequest) {
}
}, 5000)
}
// logs request body
prisma.requestLogs.create({
data: {
fid: body.untrustedData.fid,
url: body.untrustedData.url,
messageHash: body.untrustedData.messageHash,
timestamp: body.untrustedData.timestamp,
network: body.untrustedData.network,
buttonIndex: body.untrustedData.buttonIndex,
address: '',
castId: {
fid: body.untrustedData.castId.fid,
hash: body.untrustedData.castId.hash,
},
trustedData: {
messageBytes: body.trustedData.messageBytes,
},
},
});
return mintedSuccessFrame(
txHash as string,
userAddress,
Expand Down Expand Up @@ -177,8 +186,9 @@ function mintedSuccessFrame(

function alreadyMinted(txHash: string,
address: string,
comeAfterHours: string
) {
const imageUrl = assets.error.alreadyMinted;
const imageUrl = `${config.host}/og/already-minted/${new Date().getTime()}/${comeAfterHours}/opengraph-image`;
// Use the frame message to build the frame
const frame: Frame = {
version: 'vNext',
Expand Down
68 changes: 68 additions & 0 deletions src/app/og/already-minted/[random]/[time]/opengraph-image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ImageResponse } from 'next/og'

// Image metadata
export const alt = 'About Acme'
export const size = {
width: 1200,
height: 630,
}

export const contentType = 'image/png'

// Image generation
export default async function Image(
{ params }: { params: { time: string } }
) {
// Font


return new ImageResponse(
(
// ImageResponse JSX element
<div
style={{
fontSize: 64,
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
}}
>
<div style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}}>
<p>Already Minted</p>
<p>
<span>
Come back in {params.time} hours
</span>
</p>
</div>
<div style={{
position: 'absolute',
bottom: 0,
fontSize: 34,
textAlign: 'center',
padding: '16px',
background: 'rgba(0, 0, 0, 0.1)',
width: '100%',
display: 'flex',
justifyContent: 'center',
}}>
Meanwhile you can share this page with your friends
</div>
</div>
),
// ImageResponse options
{
// For convenience, we can re-use the exported opengraph-image
// size config to also set the ImageResponse's width and height.
...size,
}
)
}

0 comments on commit 40be80f

Please sign in to comment.