update webhook

master
TZGyn 10 months ago
parent 4e383576a5
commit e3d5e7da7c
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -1,69 +1,46 @@
import { db } from '$lib/db/index.js'
import { json } from '@sveltejs/kit'
import { RestClientV5 } from 'bybit-api'
import { z } from 'zod'
export const POST = async ({ locals, request }) => {
console.log('hit')
console.log(await request.text())
return json({})
const body = await request.json()
const apiKey = 'wrc1w54Zp5JAfXLxY2'
const apiSecret = 'tY7oYPhwSE1gabFS4PmxtmbDOhkYWvPh0khf'
const client = new RestClientV5({
key: apiKey,
secret: apiSecret,
demoTrading: true,
const form = z
.object({
key: z.string(),
secret: z.string(),
symbol: z.string(),
side: z.enum(['Buy', 'Sell']),
qty: z.string(),
takeProfit: z.string(),
stopLoss: z.string(),
})
.safeParse(body)
const { symbol, id }: { symbol: string; id: string } = body
if (!form.success) return json({}, { status: 400 })
const bybitWebhook = await db.query.bybitWebhook.findFirst({
where: (webhook, { and, eq }) =>
and(eq(webhook.id, id), eq(webhook.userId, locals.user.id)),
})
// const apiKey = 'wrc1w54Zp5JAfXLxY2'
// const apiSecret = 'tY7oYPhwSE1gabFS4PmxtmbDOhkYWvPh0khf'
if (!bybitWebhook) return json({}, { status: 404 })
const { key, secret, side, symbol, qty, stopLoss, takeProfit } =
form.data
const activeOrders = await client.getActiveOrders({
category: 'spot',
symbol: bybitWebhook.symbol,
})
const wallet = await client.getWalletBalance({
accountType: 'SPOT',
coin: bybitWebhook.symbol,
const client = new RestClientV5({
key: key,
secret: secret,
demoTrading: true,
})
if (wallet.result.list.length <= 0) return json({})
const amount = activeOrders.result.list.reduce(
(acc, item) => Number(item.qty) + acc,
0,
)
if (amount > 0) {
client.submitOrder({
category: 'spot',
symbol: bybitWebhook.symbol,
side: bybitWebhook.direction === 'buy' ? 'Buy' : 'Sell',
await client.submitOrder({
category: 'linear',
symbol,
side,
orderType: 'Market',
qty: (
(Number(wallet.result.list[0].totalAvailableBalance) *
bybitWebhook.quantityPercent) /
100
).toString(),
qty,
takeProfit,
stopLoss,
})
} else {
client.submitOrder({
category: 'spot',
symbol: bybitWebhook.symbol,
side: bybitWebhook.direction === 'buy' ? 'Buy' : 'Sell',
orderType: 'Market',
qty: amount.toString(),
})
}
return new Response()
}

Loading…
Cancel
Save