Created
November 21, 2023 01:08
-
-
Save akrolsmir/01251dc557a880317300dc6a63b60fd4 to your computer and use it in GitHub Desktop.
Working snippet of route.ts
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
import { getUser } from '@/db/profile' | |
import { createEdgeClient } from '@/pages/api/_db' | |
import { NextRequest, NextResponse } from 'next/server' | |
import uuid from 'react-uuid' | |
export const runtime = 'edge' | |
// Manifold user ID for [email protected] | |
const MANAGRAM_DEST_ID = 'pyBueUg9y3hrDIUtrus5uAkPHCr1' | |
export type DepositManaProps = { | |
manifoldApiKey: string | |
manaToDeposit: number | |
} | |
// TODO options: | |
// 1) give up and rewrite using pages router | |
// 2) debug what's causing "An unknown error occurred." (might be in Manifold cloud function...?) | |
// 3) try server actions??? | |
// Also: Consider upgrading to Next 14; replace auth with supabase https://supabase.com/blog/supabase-is-now-compatible-with-nextjs-14 | |
export async function POST(req: NextRequest) { | |
const { manifoldApiKey, manaToDeposit } = | |
(await req.json()) as DepositManaProps | |
const supabase = createEdgeClient(req) | |
const user = await getUser(supabase) | |
if (!user) { | |
return NextResponse.json({ error: 'no user' }, { status: 400 }) | |
} | |
// 1. Make the managram | |
const response = await fetch('https://manifold.markets/api/v0/managram', { | |
// const response = await fetch('http://localhost:3001/api/v0/managram', { | |
method: 'POST', | |
headers: { | |
Authorization: `Key ${manifoldApiKey}`, | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ | |
toIds: [MANAGRAM_DEST_ID], // This expects an array of IDs | |
amount: manaToDeposit, | |
message: `mana => Manifund, for Manifund user ${user.id}`, | |
}), | |
}) | |
console.log('despoit-mana: sent managram') | |
const body = await response.text() | |
// console.log('deposit-mana: response body', body) | |
// const json = await response.json() | |
// console.log('deposit-mana json', json) | |
// Expected result: {"message":"Mana sent"} | |
const successfulManagram = body === '{"message":"Mana sent"}' | |
console.log('successfulManagram', successfulManagram) | |
if (!successfulManagram) { | |
return NextResponse.json( | |
{ error: `Managram failed: ${body}` }, | |
{ status: 400 } | |
) | |
} | |
// 2. If successful, create a txn adding that much money to the user's account | |
// const txnId = uuid() | |
await supabase | |
.from('txns') | |
.insert({ | |
from_id: process.env.NEXT_PUBLIC_PROD_BANK_ID ?? '', | |
to_id: user.id, | |
amount: manaToDeposit / 100, | |
token: 'USD', | |
project: null, | |
}) | |
.throwOnError() | |
return NextResponse.json({ message: 'Mana deposited' }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bonus: list-txns/route.ts