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

Loading…
Cancel
Save