Skip to content

Instantly share code, notes, and snippets.

@akrolsmir
Created November 21, 2023 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akrolsmir/01251dc557a880317300dc6a63b60fd4 to your computer and use it in GitHub Desktop.
Save akrolsmir/01251dc557a880317300dc6a63b60fd4 to your computer and use it in GitHub Desktop.
Working snippet of route.ts
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 hi@manifund.org
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' })
}
@akrolsmir
Copy link
Author

(note that the error in the comments was just setting a Manifund ID instead of Manifold ID for the managram)

@akrolsmir
Copy link
Author

Bonus: list-txns/route.ts

import { createEdgeClient } from '@/pages/api/_db'
import { NextRequest } from 'next/server'

export async function GET(req: NextRequest) {
  const supabase = createEdgeClient(req)

  // List the last 10 txns
  const { data } = await supabase
    .from('txns')
    .select('*')
    .order('created_at', { ascending: false })
    .limit(10)
    .throwOnError()

  return new Response(JSON.stringify(data, null, 2), { status: 200 })
  // Return as a JSON object instead of a string:
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment