import type * as Identity from 'mppx/identity'
import { Mppx, tempo } from 'mppx/server'
import { type Address, createClient, http } from 'viem'
import { tempoModerato } from 'viem/chains'
export function createHandler(options: {
recipient: Address
secretKey: string
write: (identity: Identity.Results | undefined) => Promise<string[]>
}) {
const client = createClient({ chain: tempoModerato, transport: http() })
const mppx = Mppx.create({
methods: [tempo({
chainId: tempoModerato.id, // 42431: Tempo Moderato testnet
currency: '0x20c0000000000000000000006a37DA5C996874BE', // OUSD on Tempo
getClient: () => client,
recipient: options.recipient,
})],
secretKey: options.secretKey,
})
return async (request: Request): Promise<Response> => {
const result = await mppx.charge({
amount: '0.03',
})(request)
if (result.status === 402) return result.challenge
const haiku = await options.write(result.identity)
return result.withReceipt(Response.json({ haiku }))
}
}