Skip to content

Commit

Permalink
Merge pull request #1488 from stackernews/max-base-fee
Browse files Browse the repository at this point in the history
Max total fee
  • Loading branch information
huumn authored Oct 20, 2024
2 parents fd9b087 + d146e50 commit 46b2a57
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 7 deletions.
11 changes: 9 additions & 2 deletions api/resolvers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
]
Expand Down
1 change: 1 addition & 0 deletions api/typeDefs/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export default gql`
withdrawMaxFeeDefault: Int!
autoWithdrawThreshold: Int
autoWithdrawMaxFeePercent: Float
autoWithdrawMaxFeeTotal: Int
}
type UserOptional {
Expand Down
1 change: 1 addition & 0 deletions api/typeDefs/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const typeDefs = `
input AutowithdrawSettings {
autoWithdrawThreshold: Int!
autoWithdrawMaxFeePercent: Float!
autoWithdrawMaxFeeTotal: Int!
priority: Int
enabled: Boolean
}
Expand Down
23 changes: 21 additions & 2 deletions components/autowithdraw-shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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>
</>
Expand Down
1 change: 1 addition & 0 deletions fragments/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ${STREAK_FIELDS}
noReferralLinks
fiatCurrency
autoWithdrawMaxFeePercent
autoWithdrawMaxFeeTotal
autoWithdrawThreshold
withdrawMaxFeeDefault
satsFilter
Expand Down
3 changes: 2 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 8 additions & 0 deletions prisma/migrations/20241018160708_max_fee_total/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions wallets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -296,6 +297,7 @@ function useServerConfig (wallet) {
const saveConfig = useCallback(async ({
autoWithdrawThreshold,
autoWithdrawMaxFeePercent,
autoWithdrawMaxFeeTotal,
priority,
enabled,
...config
Expand All @@ -310,6 +312,7 @@ function useServerConfig (wallet) {
settings: {
autoWithdrawThreshold: Number(autoWithdrawThreshold),
autoWithdrawMaxFeePercent: Number(autoWithdrawMaxFeePercent),
autoWithdrawMaxFeeTotal: Number(autoWithdrawMaxFeeTotal),
priority,
enabled
},
Expand Down
10 changes: 8 additions & 2 deletions worker/autowithdraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 46b2a57

Please sign in to comment.