-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #446 from ourzora/frame-transactions
Add frame transactions
- Loading branch information
Showing
5 changed files
with
2,154 additions
and
728 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
/// <reference types="next/navigation-types/compat/navigation" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** @jsxImportSource frog/jsx */ | ||
import { Frog } from 'frog' | ||
import { handle } from 'frog/next' | ||
import { isAddress, parseEther } from 'viem' | ||
|
||
import { auctionAbi, governorAbi } from 'src/data/contract/abis' | ||
import { AddressType, BytesType, CHAIN_ID } from 'src/typings' | ||
|
||
const app = new Frog({ | ||
basePath: '/api/frames', | ||
}) | ||
|
||
app.transaction('/bid', async (c) => { | ||
try { | ||
const { inputText, req } = c | ||
const { chainId, auctionContract, tokenId, amount, referral } = req.query() | ||
|
||
if (!tokenId) return new Response('Invalid tokenId', { status: 400 }) | ||
|
||
const tokenIdParsed = BigInt(tokenId) | ||
const chainIdParsed = parseInt(chainId) | ||
|
||
let bidAmount: bigint | undefined | ||
|
||
if (amount) bidAmount = parseEther(amount) | ||
else if (inputText) bidAmount = parseEther(inputText) | ||
|
||
if (!bidAmount) return new Response('Invalid bid amount', { status: 400 }) | ||
|
||
if ( | ||
chainIdParsed !== CHAIN_ID.OPTIMISM && | ||
chainIdParsed !== CHAIN_ID.ZORA && | ||
chainIdParsed !== CHAIN_ID.BASE | ||
) | ||
return new Response('Invalid chain id', { status: 400 }) | ||
|
||
const contract = { | ||
abi: auctionAbi, | ||
chainId: `eip155:${chainIdParsed}`, | ||
to: auctionContract as AddressType, | ||
} as const | ||
|
||
if (referral) { | ||
if (!isAddress(referral)) return new Response('Invalid referral', { status: 400 }) | ||
|
||
return c.contract({ | ||
...contract, | ||
functionName: 'createBidWithReferral', | ||
value: bidAmount, | ||
args: [tokenIdParsed, referral as AddressType], | ||
}) | ||
} | ||
|
||
return c.contract({ | ||
...contract, | ||
functionName: 'createBid', | ||
value: bidAmount, | ||
args: [tokenIdParsed], | ||
}) | ||
} catch (err) { | ||
return new Response((err as Error).message, { status: 500 }) | ||
} | ||
}) | ||
|
||
app.transaction('/vote', async (c) => { | ||
try { | ||
const { inputText, req } = c | ||
const { chainId, governorContract, support, proposalId, reason } = req.query() | ||
const chainIdParsed = parseInt(chainId) | ||
|
||
let reasonText | ||
if (reason) reasonText = reason | ||
else if (inputText) reasonText = inputText | ||
|
||
const supportParsed = BigInt(support) | ||
|
||
// 0 = Against, 1 = For, 2 = Abstain | ||
if (supportParsed !== 0n && supportParsed !== 1n && supportParsed !== 2n) | ||
return new Response('Invalid support value', { status: 400 }) | ||
|
||
if ( | ||
chainIdParsed !== CHAIN_ID.OPTIMISM && | ||
chainIdParsed !== CHAIN_ID.ZORA && | ||
chainIdParsed !== CHAIN_ID.BASE | ||
) | ||
return new Response('Invalid chain id', { status: 400 }) | ||
|
||
const contract = { | ||
abi: governorAbi, | ||
chainId: `eip155:${chainIdParsed}`, | ||
to: governorContract as AddressType, | ||
} as const | ||
|
||
if (reasonText) { | ||
return c.contract({ | ||
...contract, | ||
functionName: 'castVoteWithReason', | ||
args: [proposalId as BytesType, supportParsed, reasonText], | ||
}) | ||
} | ||
|
||
return c.contract({ | ||
...contract, | ||
functionName: 'castVote', | ||
args: [proposalId as BytesType, supportParsed], | ||
}) | ||
} catch (err) { | ||
return new Response((err as Error).message, { status: 500 }) | ||
} | ||
}) | ||
|
||
export const GET = handle(app) | ||
export const POST = handle(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.