commit
46b2a57ece
|
@ -647,14 +647,21 @@ async function upsertWallet (
|
|||
}
|
||||
|
||||
const { id, ...walletData } = data
|
||||
const { autoWithdrawThreshold, autoWithdrawMaxFeePercent, enabled, priority } = settings
|
||||
const {
|
||||
autoWithdrawThreshold,
|
||||
autoWithdrawMaxFeePercent,
|
||||
autoWithdrawMaxFeeTotal,
|
||||
enabled,
|
||||
priority
|
||||
} = settings
|
||||
|
||||
const txs = [
|
||||
models.user.update({
|
||||
where: { id: me.id },
|
||||
data: {
|
||||
autoWithdrawMaxFeePercent,
|
||||
autoWithdrawThreshold
|
||||
autoWithdrawThreshold,
|
||||
autoWithdrawMaxFeeTotal
|
||||
}
|
||||
})
|
||||
]
|
||||
|
|
|
@ -182,6 +182,7 @@ export default gql`
|
|||
withdrawMaxFeeDefault: Int!
|
||||
autoWithdrawThreshold: Int
|
||||
autoWithdrawMaxFeePercent: Float
|
||||
autoWithdrawMaxFeeTotal: Int
|
||||
}
|
||||
|
||||
type UserOptional {
|
||||
|
|
|
@ -91,6 +91,7 @@ const typeDefs = `
|
|||
input AutowithdrawSettings {
|
||||
autoWithdrawThreshold: Int!
|
||||
autoWithdrawMaxFeePercent: Float!
|
||||
autoWithdrawMaxFeeTotal: Int!
|
||||
priority: Int
|
||||
enabled: Boolean
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import { useMe } from './me'
|
|||
import { useEffect, useState } from 'react'
|
||||
import { isNumber } from '@/lib/validate'
|
||||
import { useIsClient } from './use-client'
|
||||
import Link from 'next/link'
|
||||
|
||||
function autoWithdrawThreshold ({ me }) {
|
||||
return isNumber(me?.privates?.autoWithdrawThreshold) ? me?.privates?.autoWithdrawThreshold : 10000
|
||||
|
@ -12,7 +13,8 @@ function autoWithdrawThreshold ({ me }) {
|
|||
export function autowithdrawInitial ({ me }) {
|
||||
return {
|
||||
autoWithdrawThreshold: autoWithdrawThreshold({ me }),
|
||||
autoWithdrawMaxFeePercent: isNumber(me?.privates?.autoWithdrawMaxFeePercent) ? me?.privates?.autoWithdrawMaxFeePercent : 1
|
||||
autoWithdrawMaxFeePercent: isNumber(me?.privates?.autoWithdrawMaxFeePercent) ? me?.privates?.autoWithdrawMaxFeePercent : 1,
|
||||
autoWithdrawMaxFeeTotal: isNumber(me?.privates?.autoWithdrawMaxFeeTotal) ? me?.privates?.autoWithdrawMaxFeeTotal : 1
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,13 +53,30 @@ export function AutowithdrawSettings ({ wallet }) {
|
|||
append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>}
|
||||
required
|
||||
/>
|
||||
<h3 className='text-center text-muted pt-3'>network fees</h3>
|
||||
<h6 className='text-center pb-3'>
|
||||
we'll use whichever setting is higher during{' '}
|
||||
<Link
|
||||
target='_blank'
|
||||
href='https://docs.lightning.engineering/the-lightning-network/pathfinding'
|
||||
rel='noreferrer'
|
||||
>pathfinding
|
||||
</Link>
|
||||
</h6>
|
||||
<Input
|
||||
label='max fee'
|
||||
label='max fee rate'
|
||||
name='autoWithdrawMaxFeePercent'
|
||||
hint='max fee as percent of withdrawal amount'
|
||||
append={<InputGroup.Text>%</InputGroup.Text>}
|
||||
required
|
||||
/>
|
||||
<Input
|
||||
label='max fee total'
|
||||
name='autoWithdrawMaxFeeTotal'
|
||||
hint='max fee for any withdrawal amount'
|
||||
append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
|
|
|
@ -27,6 +27,7 @@ ${STREAK_FIELDS}
|
|||
noReferralLinks
|
||||
fiatCurrency
|
||||
autoWithdrawMaxFeePercent
|
||||
autoWithdrawMaxFeeTotal
|
||||
autoWithdrawThreshold
|
||||
withdrawMaxFeeDefault
|
||||
satsFilter
|
||||
|
|
|
@ -366,7 +366,8 @@ export function advSchema (args) {
|
|||
export const autowithdrawSchemaMembers = {
|
||||
enabled: boolean(),
|
||||
autoWithdrawThreshold: intValidator.required('required').min(0, 'must be at least 0').max(msatsToSats(BALANCE_LIMIT_MSATS), `must be at most ${abbrNum(msatsToSats(BALANCE_LIMIT_MSATS))}`),
|
||||
autoWithdrawMaxFeePercent: floatValidator.required('required').min(0, 'must be at least 0').max(50, 'must not exceed 50')
|
||||
autoWithdrawMaxFeePercent: floatValidator.required('required').min(0, 'must be at least 0').max(50, 'must not exceed 50'),
|
||||
autoWithdrawMaxFeeTotal: intValidator.required('required').min(0, 'must be at least 0').max(1_000, 'must not exceed 1000')
|
||||
}
|
||||
|
||||
export const lnAddrAutowithdrawSchema = object({
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
-- AlterTable
|
||||
ALTER TABLE "users" ADD COLUMN "autoWithdrawMaxFeeTotal" INTEGER;
|
||||
|
||||
-- set max total fee for users with autowithdrawals enabled to not interfere with them.
|
||||
-- we set it to 0 instead of 1 because that preserves old behavior.
|
||||
UPDATE "users"
|
||||
SET "autoWithdrawMaxFeeTotal" = 0
|
||||
WHERE "autoWithdrawMaxFeePercent" IS NOT NULL;
|
|
@ -118,6 +118,7 @@ model User {
|
|||
lnAddr String?
|
||||
autoWithdrawMaxFeePercent Float?
|
||||
autoWithdrawThreshold Int?
|
||||
autoWithdrawMaxFeeTotal Int?
|
||||
muters Mute[] @relation("muter")
|
||||
muteds Mute[] @relation("muted")
|
||||
ArcOut Arc[] @relation("fromUser")
|
||||
|
|
|
@ -201,6 +201,7 @@ function useConfig (wallet) {
|
|||
}
|
||||
// these are stored on the server
|
||||
delete newClientConfig.autoWithdrawMaxFeePercent
|
||||
delete newClientConfig.autoWithdrawThreshold
|
||||
delete newClientConfig.autoWithdrawMaxFeeTotal
|
||||
} catch {
|
||||
valid = false
|
||||
|
@ -296,6 +297,7 @@ function useServerConfig (wallet) {
|
|||
const saveConfig = useCallback(async ({
|
||||
autoWithdrawThreshold,
|
||||
autoWithdrawMaxFeePercent,
|
||||
autoWithdrawMaxFeeTotal,
|
||||
priority,
|
||||
enabled,
|
||||
...config
|
||||
|
@ -310,6 +312,7 @@ function useServerConfig (wallet) {
|
|||
settings: {
|
||||
autoWithdrawThreshold: Number(autoWithdrawThreshold),
|
||||
autoWithdrawMaxFeePercent: Number(autoWithdrawMaxFeePercent),
|
||||
autoWithdrawMaxFeeTotal: Number(autoWithdrawMaxFeeTotal),
|
||||
priority,
|
||||
enabled
|
||||
},
|
||||
|
|
|
@ -4,7 +4,10 @@ import { createInvoice } from 'wallets/server'
|
|||
|
||||
export async function autoWithdraw ({ data: { id }, models, lnd }) {
|
||||
const user = await models.user.findUnique({ where: { id } })
|
||||
if (user.autoWithdrawThreshold === null || user.autoWithdrawMaxFeePercent === null) return
|
||||
if (
|
||||
user.autoWithdrawThreshold === null ||
|
||||
user.autoWithdrawMaxFeePercent === null ||
|
||||
user.autoWithdrawMaxFeeTotal === null) return
|
||||
|
||||
const threshold = satsToMsats(user.autoWithdrawThreshold)
|
||||
const excess = Number(user.msats - threshold)
|
||||
|
@ -13,7 +16,10 @@ export async function autoWithdraw ({ data: { id }, models, lnd }) {
|
|||
if (excess < Number(threshold) * 0.1) return
|
||||
|
||||
// floor fee to nearest sat but still denominated in msats
|
||||
const maxFeeMsats = msatsSatsFloor(Math.ceil(excess * (user.autoWithdrawMaxFeePercent / 100.0)))
|
||||
const maxFeeMsats = msatsSatsFloor(Math.max(
|
||||
Math.ceil(excess * (user.autoWithdrawMaxFeePercent / 100.0)),
|
||||
Number(satsToMsats(user.autoWithdrawMaxFeeTotal))
|
||||
))
|
||||
// msats will be floored by createInvoice if it needs to be
|
||||
const msats = BigInt(excess) - maxFeeMsats
|
||||
|
||||
|
|
Loading…
Reference in New Issue