Skip to content

Commit

Permalink
Add a threshold for onchain payments (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
talvasconcelos authored Aug 30, 2024
1 parent 73fa178 commit 57e107f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 25 deletions.
7 changes: 4 additions & 3 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ async def create_tipjar(data: CreateTipJar) -> TipJar:
name,
wallet,
webhook,
onchain
onchain,
onchain_limit
)
VALUES (?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?)
{returning}
""",
(data.name, data.wallet, data.webhook, data.onchain),
(data.name, data.wallet, data.webhook, data.onchain, data.onchain_limit),
)
if db.type == SQLITE:
tipjar_id = result._result_proxy.lastrowid
Expand Down
9 changes: 8 additions & 1 deletion migrations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
async def m001_initial(db):

await db.execute(
f"""
CREATE TABLE IF NOT EXISTS tipjar.TipJars (
Expand All @@ -25,3 +24,11 @@ async def m001_initial(db):
);
"""
)


async def m002_add_onchain_limit(db):
await db.execute(
"""
ALTER TABLE tipjar.TipJars ADD COLUMN onchain_limit INTEGER;
"""
)
2 changes: 2 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CreateTipJar(BaseModel):
wallet: str
webhook: Optional[str]
onchain: Optional[str]
onchain_limit: Optional[int]


class CreateTips(BaseModel):
Expand All @@ -50,6 +51,7 @@ class TipJar(BaseModel):
wallet: str # Lightning wallet
onchain: Optional[str] # Watchonly wallet
webhook: Optional[str] # URL to POST tips to
onchain_limit: Optional[int] # Bellow this amount, tips will be offchain only

@classmethod
def from_row(cls, row: Row) -> "TipJar":
Expand Down
30 changes: 18 additions & 12 deletions templates/tipjar/display.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,24 @@ <h5 class="q-my-none">Tip {{ donatee }} some sats!</h5>
}
},
methods: {
invoice() {
axios
.post('/tipjar/api/v1/tips', {
tipjar: '{{ tipjar_id }}',
name: this.tipDialog.data.name,
sats: this.tipDialog.data.sats,
message: this.tipDialog.data.message
})
.then(response => {
this.redirect_url = response.data.redirect_url
window.location.href = this.redirect_url
})
async invoice() {
try {
const {data} = await LNbits.api.request(
'POST',
'/tipjar/api/v1/tips',
null,
{
name: this.tipDialog.data.name,
sats: this.tipDialog.data.sats,
tipjar: '{{ tipjar_id }}',
message: this.tipDialog.data.message
}
)
this.redirectUrl = data.redirect_url
window.location.href = this.redirectUrl
} catch (error) {
LNbits.utils.notifyApiError(error)
}
}
}
})
Expand Down
40 changes: 31 additions & 9 deletions templates/tipjar/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ <h6 class="text-subtitle1 q-my-none">
<div class="row">
<div class="col">
<div v-if="walletLinks.length > 0">
<q-checkbox v-model="tipjarDialog.data.chain" label="Chain" />
<q-checkbox v-model="tipjarDialog.chain" label="Chain" />
</div>
<div v-else>
<q-checkbox :value="false" label="Chain" disabled>
Expand All @@ -163,15 +163,24 @@ <h6 class="text-subtitle1 q-my-none">
</div>
</div>
</div>
<div v-if="tipjarDialog.data.chain">
<div v-if="tipjarDialog.chain">
<q-select
filled
dense
emit-value
v-model="tipjarDialog.data.onchain"
:options="walletLinks"
label="Chain Wallet"
/>
></q-select>
<q-input
filled
dense
v-model.trim="tipjarDialog.data.onchain_limit"
type="number"
label="Onchain Limit (sats)"
hint="The minimum amount of sats to be paid onchain"
class="q-mt-sm"
></q-input>
</div>
<q-input
filled
Expand Down Expand Up @@ -258,6 +267,12 @@ <h6 class="text-subtitle1 q-my-none">
label: 'Onchain Address',
field: 'onchain'
},
{
name: 'onchain limit',
align: 'left',
label: 'Limit (Dust)',
field: 'onchain_limit'
},
{
name: 'webhook',
align: 'left',
Expand Down Expand Up @@ -309,6 +324,7 @@ <h6 class="text-subtitle1 q-my-none">
this.g.user.wallets[0].inkey
)
.then(function (response) {
console.log(response.data)
for (i = 0; i < response.data.length; i++) {
self.walletLinks.push(response.data[i].id)
}
Expand All @@ -322,7 +338,11 @@ <h6 class="text-subtitle1 q-my-none">
var self = this

LNbits.api
.request('GET', '/tipjar/api/v1/tips', this.g.user.wallets[0].inkey)
.request(
'GET',
'/tipjar/api/v1/tips',
this.g.user.wallets[0].adminkey
)
.then(function (response) {
self.tips = response.data.map(function (obj) {
return mapTipJar(obj)
Expand All @@ -340,7 +360,7 @@ <h6 class="text-subtitle1 q-my-none">
.request(
'DELETE',
'/tipjar/api/v1/tips/' + tipId,
_.findWhere(self.g.user.wallets, {id: tips.wallet}).inkey
_.findWhere(self.g.user.wallets, {id: tips.wallet}).adminkey
)
.then(function (response) {
self.tips = _.reject(self.tips, function (obj) {
Expand All @@ -363,7 +383,7 @@ <h6 class="text-subtitle1 q-my-none">
.request(
'GET',
'/tipjar/api/v1/tipjars',
this.g.user.wallets[0].inkey
this.g.user.wallets[0].adminkey
)
.then(function (response) {
self.tipjars = response.data.map(function (obj) {
Expand All @@ -383,7 +403,7 @@ <h6 class="text-subtitle1 q-my-none">
createTipJar: function (wallet, data) {
var self = this
LNbits.api
.request('POST', '/tipjar/api/v1/tipjars', wallet.inkey, data)
.request('POST', '/tipjar/api/v1/tipjars', wallet.adminkey, data)
.then(function (response) {
self.tipjars.push(mapTipJar(response.data))
self.tipjarDialog.show = false
Expand All @@ -395,11 +415,13 @@ <h6 class="text-subtitle1 q-my-none">
},
updatetipjarDialog: function (tipjarId) {
var link = _.findWhere(this.tipjars, {id: tipjarId})
console.log(link.id)

this.tipjarDialog.data.id = link.id
this.tipjarDialog.data.wallet = link.wallet
this.tipjarDialog.data.name = link.name
this.tipjarDialog.data.webhook = link.webhook
this.tipjarDialog.data.onchain_limit = link.onchain_limit
this.tipjarDialog.chain = link.onchain != null
this.tipjarDialog.show = true
},
deleteTipJar: function (tipjarsId) {
Expand All @@ -413,7 +435,7 @@ <h6 class="text-subtitle1 q-my-none">
.request(
'DELETE',
'/tipjar/api/v1/tipjars/' + tipjarsId,
_.findWhere(self.g.user.wallets, {id: tipjars.wallet}).inkey
_.findWhere(self.g.user.wallets, {id: tipjars.wallet}).adminkey
)
.then(function (response) {
self.tipjars = _.reject(self.tipjars, function (obj) {
Expand Down
3 changes: 3 additions & 0 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ async def api_create_tip(data: CreateTips):
status_code=HTTPStatus.NOT_FOUND, detail="Tipjar wallet does not exist."
)

if tipjar.onchain_limit and (sats <= tipjar.onchain_limit):
tipjar.onchain = None

name = data.name or "Anonymous"
try:
charge_id = await create_charge(
Expand Down

0 comments on commit 57e107f

Please sign in to comment.