feat: add payment for stoeage
This commit is contained in:
parent
2233521a43
commit
dfda2367a2
5 changed files with 130 additions and 35 deletions
|
|
@ -10,3 +10,11 @@ UI for diagnostics and management (key alow/ban lists, rate limiting) coming soo
|
|||
|
||||
1. Enable extension
|
||||
2. Enable relay
|
||||
|
||||
|
||||
### Development
|
||||
|
||||
Create Symbolic Link:
|
||||
```
|
||||
ln -s /Users/my-user/git-repos/nostr-relay-extension/ /Users/my-user/git-repos/lnbits/lnbits/extensions/nostrrelay
|
||||
```
|
||||
|
|
@ -307,10 +307,14 @@ class NostrFilter(BaseModel):
|
|||
return inner_joins, where, values
|
||||
|
||||
|
||||
class RelayJoin(BaseModel):
|
||||
class BuyOrder(BaseModel):
|
||||
action: str
|
||||
relay_id: str
|
||||
pubkey: str
|
||||
units_to_buy = 0
|
||||
|
||||
def is_valid_action(self):
|
||||
return self.action in ['join', 'storage']
|
||||
|
||||
class NostrAccount(BaseModel):
|
||||
pubkey: str
|
||||
|
|
|
|||
22
tasks.py
22
tasks.py
|
|
@ -31,6 +31,11 @@ async def on_invoice_paid(payment: Payment):
|
|||
await invoice_paid_to_join(relay_id, pubkey)
|
||||
return
|
||||
|
||||
if payment.extra.get("action") == "storage":
|
||||
storage_to_buy = payment.extra.get("storage_to_buy")
|
||||
await invoice_paid_for_storage(relay_id, pubkey, storage_to_buy)
|
||||
return
|
||||
|
||||
async def invoice_paid_to_join(relay_id: str, pubkey: str):
|
||||
try:
|
||||
account = await get_account(relay_id, pubkey)
|
||||
|
|
@ -44,5 +49,22 @@ async def invoice_paid_to_join(relay_id: str, pubkey: str):
|
|||
account.paid_to_join = True
|
||||
await update_account(relay_id, account)
|
||||
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
|
||||
|
||||
async def invoice_paid_for_storage(relay_id: str, pubkey: str, storage_to_buy: int):
|
||||
try:
|
||||
account = await get_account(relay_id, pubkey)
|
||||
if not account:
|
||||
await create_account(relay_id, NostrAccount(pubkey=pubkey, storage=storage_to_buy))
|
||||
return
|
||||
|
||||
if account.blocked:
|
||||
return
|
||||
|
||||
account.storage = storage_to_buy
|
||||
await update_account(relay_id, account)
|
||||
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
|
|
@ -12,7 +12,7 @@
|
|||
<h4 v-text="relay.name" class="q-my-none"></h4>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
<q-card>
|
||||
<q-card class="q-pb-xl">
|
||||
<q-card-section>
|
||||
<span class="text-bold">Public Key:</span>
|
||||
<q-input
|
||||
|
|
@ -24,38 +24,78 @@
|
|||
></q-input>
|
||||
</q-card-section>
|
||||
<q-card-section v-if="relay.config.isPaidRelay">
|
||||
<q-btn
|
||||
@click="createJoinInvoice"
|
||||
unelevated
|
||||
color="primary float-right"
|
||||
>Show Invoice</q-btn
|
||||
>
|
||||
<span class="text-bold">Cost to join: </span>
|
||||
|
||||
<span v-text="relay.config.costToJoin"></span>
|
||||
<q-badge color="orange">
|
||||
<span>sats</span>
|
||||
</q-badge>
|
||||
<div class="row q-mb-md">
|
||||
<div class="col-2">
|
||||
<span class="text-bold">Cost to join: </span>
|
||||
</div>
|
||||
<div class="col-6">
|
||||
<span v-text="relay.config.costToJoin"></span>
|
||||
<span class="text-bold q-ml-sm">sats</span>
|
||||
</div>
|
||||
<div class="col-4">
|
||||
<q-btn
|
||||
@click="createInvoice('join')"
|
||||
unelevated
|
||||
color="primary"
|
||||
class="float-right"
|
||||
>Pay to Join</q-btn
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
<q-separator></q-separator>
|
||||
<div class="row q-mt-md q-mb-md">
|
||||
<div class="col-2">
|
||||
<span class="text-bold">Storage cost: </span>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
<span v-text="relay.config.storageCostValue"></span>
|
||||
<span class="text-bold q-ml-sm"> sats per</span>
|
||||
<q-badge color="orange">
|
||||
<span v-text="relay.config.storageCostUnit"></span>
|
||||
</q-badge>
|
||||
</div>
|
||||
<div class="col-2">
|
||||
<q-input
|
||||
filled
|
||||
dense
|
||||
v-model="unitsToBuy"
|
||||
type="number"
|
||||
min="0"
|
||||
:label="relay.config.storageCostUnit"
|
||||
></q-input>
|
||||
</div>
|
||||
<div class="col-2">
|
||||
<span class="text-bold q-ml-md" v-text="storageCost"></span>
|
||||
<span>sats</span>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
<q-btn
|
||||
@click="createInvoice('storage')"
|
||||
unelevated
|
||||
color="primary"
|
||||
class="float-right"
|
||||
>Buy storage space</q-btn
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
<q-separator></q-separator>
|
||||
</q-card-section>
|
||||
<q-card-section v-else>
|
||||
<q-badge color="yellow" text-color="black">
|
||||
This is a free relay
|
||||
</q-badge>
|
||||
</q-card-section>
|
||||
<q-card-section v-if="joinInvoice">
|
||||
<q-card-section v-if="invoice">
|
||||
<q-expansion-item
|
||||
group="join-invoice"
|
||||
label="Pay invoice to join relay"
|
||||
label="Invoice"
|
||||
:content-inset-level="0.5"
|
||||
default-opened
|
||||
>
|
||||
<div class="row q-ma-md">
|
||||
<div class="col-3"></div>
|
||||
<div class="col-6 text-center">
|
||||
<q-btn
|
||||
outline
|
||||
color="grey"
|
||||
@click="copyText(joinInvoice)"
|
||||
<q-btn outline color="grey" @click="copyText(invoice)"
|
||||
>Copy invoice</q-btn
|
||||
>
|
||||
</div>
|
||||
|
|
@ -66,7 +106,7 @@
|
|||
<div class="col-6">
|
||||
<q-responsive :ratio="1">
|
||||
<qrcode
|
||||
:value="'lightning:'+joinInvoice"
|
||||
:value="'lightning:'+invoice"
|
||||
:options="{width: 340}"
|
||||
class="rounded-borders"
|
||||
></qrcode>
|
||||
|
|
@ -163,11 +203,19 @@
|
|||
return {
|
||||
relay: JSON.parse('{{relay | tojson | safe}}'),
|
||||
pubkey: '',
|
||||
joinInvoice: ''
|
||||
invoice: '',
|
||||
unitsToBuy: 0
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
storageCost: function () {
|
||||
if (!this.relay || !this.relay.config.storageCostValue) return 0
|
||||
return this.unitsToBuy * this.relay.config.storageCostValue
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
createJoinInvoice: async function () {
|
||||
createInvoice: async function (action) {
|
||||
if (!action) return
|
||||
if (!this.pubkey) {
|
||||
this.$q.notify({
|
||||
timeout: 5000,
|
||||
|
|
@ -177,17 +225,20 @@
|
|||
return
|
||||
}
|
||||
try {
|
||||
const reqData = {
|
||||
action,
|
||||
relay_id: this.relay.id,
|
||||
pubkey: this.pubkey,
|
||||
units_to_buy: this.unitsToBuy
|
||||
}
|
||||
const {data} = await LNbits.api.request(
|
||||
'PUT',
|
||||
'/nostrrelay/api/v1/join',
|
||||
'/nostrrelay/api/v1/pay',
|
||||
'',
|
||||
{
|
||||
relay_id: this.relay.id,
|
||||
pubkey: this.pubkey
|
||||
}
|
||||
reqData
|
||||
)
|
||||
console.log('### data.invoice', data.invoice)
|
||||
this.joinInvoice = data.invoice
|
||||
this.invoice = data.invoice
|
||||
} catch (error) {
|
||||
LNbits.utils.notifyApiError(error)
|
||||
}
|
||||
|
|
|
|||
22
views_api.py
22
views_api.py
|
|
@ -27,7 +27,7 @@ from .crud import (
|
|||
update_relay,
|
||||
)
|
||||
from .helpers import normalize_public_key
|
||||
from .models import NostrRelay, RelayJoin
|
||||
from .models import BuyOrder, NostrRelay
|
||||
|
||||
client_manager = NostrClientManager()
|
||||
|
||||
|
|
@ -154,9 +154,8 @@ async def api_delete_relay(
|
|||
)
|
||||
|
||||
|
||||
@nostrrelay_ext.put("/api/v1/join")
|
||||
async def api_pay_to_join(data: RelayJoin):
|
||||
|
||||
@nostrrelay_ext.put("/api/v1/pay")
|
||||
async def api_pay_to_join(data: BuyOrder):
|
||||
try:
|
||||
pubkey = normalize_public_key(data.pubkey)
|
||||
relay = await get_relay_by_id(data.relay_id)
|
||||
|
|
@ -166,18 +165,29 @@ async def api_pay_to_join(data: RelayJoin):
|
|||
detail="Relay not found",
|
||||
)
|
||||
|
||||
if relay.is_free_to_join:
|
||||
if data.action == 'join' and relay.is_free_to_join:
|
||||
raise ValueError("Relay is free to join")
|
||||
|
||||
storage_to_buy = 0
|
||||
if data.action == 'storage':
|
||||
if relay.config.storage_cost_value == 0:
|
||||
raise ValueError("Relay storage cost is zero. Cannot buy!")
|
||||
if data.units_to_buy == 0:
|
||||
raise ValueError("Must specify how much storage to buy!")
|
||||
storage_to_buy = data.units_to_buy * relay.config.storage_cost_value * 1024
|
||||
if relay.config.storage_cost_unit == "MB":
|
||||
storage_to_buy *= 1024
|
||||
|
||||
_, payment_request = await create_invoice(
|
||||
wallet_id=relay.config.wallet,
|
||||
amount=int(relay.config.cost_to_join),
|
||||
memo=f"Pubkey '{data.pubkey}' wants to join {relay.id}",
|
||||
extra={
|
||||
"tag": "nostrrely",
|
||||
"action": "join",
|
||||
"action": data.action,
|
||||
"relay_id": relay.id,
|
||||
"pubkey": pubkey,
|
||||
"storage_to_buy": storage_to_buy
|
||||
},
|
||||
)
|
||||
print("### payment_request", payment_request)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue