store fiat_base_multiplier in db

This commit is contained in:
callebtc 2022-06-10 16:05:33 +02:00
parent b7008495b8
commit 6617450c21
3 changed files with 22 additions and 9 deletions

View file

@ -10,10 +10,10 @@ async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink:
returning = "" if db.type == SQLITE else "RETURNING ID"
method = db.execute if db.type == SQLITE else db.fetchone
# database only allows int4 entries for min and max. For fiat currencies,
# we multiply by 100 to save the value in cents.
if data.currency:
data.min *= 100
data.max *= 100
# we multiply by data.fiat_base_multiplier (usually 100) to save the value in cents.
if data.currency and data.fiat_base_multiplier:
data.min *= data.fiat_base_multiplier
data.max *= data.fiat_base_multiplier
result = await (method)(
f"""
@ -28,9 +28,10 @@ async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink:
success_text,
success_url,
comment_chars,
currency
currency,
fiat_base_multiplier
)
VALUES (?, ?, ?, ?, 0, 0, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, 0, 0, ?, ?, ?, ?, ?, ?)
{returning}
""",
(
@ -43,6 +44,7 @@ async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink:
data.success_url,
data.comment_chars,
data.currency,
data.fiat_base_multiplier,
),
)
if db.type == SQLITE:

View file

@ -50,3 +50,13 @@ async def m003_min_max_comment_fiat(db):
await db.execute("ALTER TABLE lnurlp.pay_links ADD COLUMN max INTEGER;")
await db.execute("UPDATE lnurlp.pay_links SET max = min;")
await db.execute("DROP TABLE lnurlp.invoices")
async def m004_fiat_base_multiplier(db):
"""
Store the multiplier for fiat prices. We store the price in cents and
remember to multiply by 100 when we use it to convert to Dollars.
"""
await db.execute(
"ALTER TABLE lnurlp.pay_links ADD COLUMN fiat_base_multiplier INTEGER DEFAULT 1;"
)

View file

@ -18,6 +18,7 @@ class CreatePayLinkData(BaseModel):
webhook_url: str = Query(None)
success_text: str = Query(None)
success_url: str = Query(None)
fiat_base_multiplier: int = Query(100, ge=1)
class PayLink(BaseModel):
@ -37,9 +38,9 @@ class PayLink(BaseModel):
@classmethod
def from_row(cls, row: Row) -> "PayLink":
data = dict(row)
if data["currency"]:
data["min"] /= 100
data["max"] /= 100
if data["currency"] and data["fiat_base_multiplier"]:
data["min"] /= data["fiat_base_multiplier"]
data["max"] /= data["fiat_base_multiplier"]
return cls(**data)
def lnurl(self, req: Request) -> str: