Skip to content

Commit

Permalink
New trpc route that starts transfer workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkidwarrior committed Sep 14, 2024
1 parent 89c82ff commit 1f71bc5
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"private": true,
"dependencies": {
"@my/supabase": "workspace:*",
"@my/temporal": "workspace:*",
"@my/wagmi": "workspace:*",
"@my/workflows": "workspace:*",
"@supabase/supabase-js": "2.44.2",
"@tanstack/react-query": "^5.18.1",
"@trpc/client": "11.0.0-next-beta.264",
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/routers/_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { distributionRouter } from './distribution'
import { tagRouter } from './tag'
import { secretShopRouter } from './secretShop'
import { sendAccountRouter } from './sendAccount'
import { transferRouter } from './transfer'
import { accountRecoveryRouter } from './account-recovery/router'

export const appRouter = createTRPCRouter({
Expand All @@ -16,6 +17,7 @@ export const appRouter = createTRPCRouter({
distribution: distributionRouter,
secretShop: secretShopRouter,
sendAccount: sendAccountRouter,
transfer: transferRouter,
})

export type AppRouter = typeof appRouter
Expand Down
104 changes: 104 additions & 0 deletions packages/api/src/routers/transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { TRPCError } from '@trpc/server'
import debug from 'debug'
import { z } from 'zod'
import { createTRPCRouter, protectedProcedure } from '../trpc'
import { client } from '@my/temporal/client'
import type { UserOperation } from 'permissionless'
import { TransferWorkflow, type transferState } from '@my/workflows'
import type { coinsDict } from 'app/data/coins'

const log = debug('api:transfer')

export const transferRouter = createTRPCRouter({
withUserOp: protectedProcedure
.input(
z.object({
userOp: z.custom<UserOperation<'v0.7'>>(),
token: z.custom<keyof coinsDict>(), //@ todo: might be safer to decode the token from the userOp, to ensure we don't apply the wrong token
})
)
.mutation(async ({ input: { token, userOp } }) => {
const { sender, nonce } = userOp
try {
const handle = await client.workflow.start(TransferWorkflow, {
taskQueue: 'monorepo',
workflowId: `transfer-workflow-${token}-${sender}-${nonce}`,
args: [userOp],
})
log('Started transfer handle', handle.workflowId)
// optional: wait for client result
return await handle.workflowId
} catch (error) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: error instanceof Error ? error.message : 'Unknown error',
})
}
}),
getState: protectedProcedure.input(z.string()).query(async ({ input: workflowId }) => {
try {
const handle = await client.workflow.getHandle(workflowId)
const state = await handle.query<transferState, []>('getTransferState')
return state
} catch (error) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: error instanceof Error ? error.message : 'Unknown error',
})
}
}),
getPending: protectedProcedure
.input(
z.object({
token: z.custom<keyof coinsDict>(),
sender: z.string(),
})
)
.query(async ({ input: { token, sender } }) => {
try {
const states: transferState[] = []
const workflows = await client.workflow.list({
query: `ExecutionStatus = "Running" AND WorkflowId BETWEEN "transfer-workflow-${token}-${sender}-" AND "transfer-workflow-${token}-${sender}-~"`,
})
for await (const workflow of workflows) {
const handle = await client.workflow.getHandle(workflow.workflowId)

const state = await handle.query<transferState, []>('getTransferState')
states.push(state)
}
return states
} catch (error) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: error instanceof Error ? error.message : 'Unknown error',
})
}
}),
getFailed: protectedProcedure
.input(
z.object({
token: z.custom<keyof coinsDict>(),
sender: z.string(),
})
)
.query(async ({ input: { token, sender } }) => {
try {
const states: transferState[] = []
const workflows = await client.workflow.list({
query: `ExecutionStatus = "Failed" AND WorkflowId BETWEEN "transfer-workflow-${token}-${sender}-" AND "transfer-workflow-${token}-${sender}-~"`,
})
for await (const workflow of workflows) {
const handle = await client.workflow.getHandle(workflow.workflowId)
const state = await handle.query<transferState, []>('getTransferState')
states.push(state)
}
return states
} catch (error) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: error instanceof Error ? error.message : 'Unknown error',
cause: error,
})
}
}),
})
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6398,7 +6398,9 @@ __metadata:
resolution: "@my/api@workspace:packages/api"
dependencies:
"@my/supabase": "workspace:*"
"@my/temporal": "workspace:*"
"@my/wagmi": "workspace:*"
"@my/workflows": "workspace:*"
"@supabase/supabase-js": "npm:2.44.2"
"@tanstack/react-query": "npm:^5.18.1"
"@trpc/client": "npm:11.0.0-next-beta.264"
Expand Down

0 comments on commit 1f71bc5

Please sign in to comment.