| 1 | import { Hono } from 'hono' |
| 2 | import { cors } from 'hono/cors' |
| 3 | import { createTestClient, http, publicActions, walletActions } from 'viem' |
| 4 | import { foundry } from 'viem/chains' |
| 5 | import type { ApiResponse } from 'shared' |
| 6 | import { counterAbi } from 'contracts' |
| 7 | |
| 8 | const app = new Hono() |
| 9 | app.use(cors()) |
| 10 | |
| 11 | const client = createTestClient({ |
| 12 | chain: foundry, |
| 13 | mode: 'anvil', |
| 14 | transport: http() |
| 15 | }) |
| 16 | .extend(publicActions) |
| 17 | .extend(walletActions) |
| 18 | |
| 19 | app.get("/hello", async (c) => { |
| 20 | const response: ApiResponse = { |
| 21 | message: "Hello bhvr!", |
| 22 | success: true |
| 23 | } |
| 24 | return c.json(response) |
| 25 | }) |
| 26 | |
| 27 | app.get('/contracts/:address/counter', async (c) => { |
| 28 | try { |
| 29 | const address = c.req.param('address') as `0x${string}` |
| 30 | |
| 31 | const result = await client.readContract({ |
| 32 | address, |
| 33 | abi: counterAbi, |
| 34 | functionName: 'number' |
| 35 | }) |
| 36 | |
| 37 | const response: ApiResponse = { |
| 38 | message: `Counter value: ${result}`, |
| 39 | success: true |
| 40 | } |
| 41 | |
| 42 | return c.json(response) |
| 43 | } catch (error) { |
| 44 | return c.json({ |
| 45 | message: 'Failed to read contract', |
| 46 | success: false |
| 47 | }, 500) |
| 48 | } |
| 49 | }) |
| 50 | |
| 51 | export default app |