show the ln to btc swap table and btc to ln swap table

This commit is contained in:
Uthpala Heenatigala 2023-01-05 19:33:13 +01:00
parent 93d6d1279e
commit 57d345c149
4 changed files with 188 additions and 54 deletions

View File

@ -4,11 +4,27 @@ from typing import List
from . import db
from .models import (
Token,
LnToBtcSwap,
BtcToLnSwap
)
"""
Get Deezy Token
"""
async def get_ln_to_btc() -> List[LnToBtcSwap]:
rows = await db.fetchall(
f"SELECT * FROM deezy.ln_to_btc_swap ORDER BY created_at DESC",
)
return [LnToBtcSwap(**row) for row in rows]
async def get_btc_to_ln() -> List[BtcToLnSwap]:
rows = await db.fetchall(
f"SELECT * FROM deezy.btc_to_ln_swap ORDER BY created_at DESC",
)
return [BtcToLnSwap(**row) for row in rows]
async def get_token() -> Token:
@ -36,3 +52,57 @@ async def save_token(
),
)
return data
async def save_ln_to_btc(
data: LnToBtcSwap,
) -> LnToBtcSwap:
await db.execute(
"""
INSERT INTO deezy.ln_to_btc_swap (
amount_sats,
on_chain_address,
on_chain_sats_per_vbyte,
bolt11_invoice,
fee_sats,
txid,
tx_hex
)
VALUES (?,?,?,?,?,?,?)
""",
(
data.amount_sats,
data.on_chain_address,
data.on_chain_sats_per_vbyte,
data.bolt11_invoice,
data.fee_sats,
data.txid,
data.tx_hex,
),
)
async def save_btc_to_ln(
data: BtcToLnSwap,
) -> BtcToLnSwap:
await db.execute(
"""
INSERT INTO deezy.btc_to_ln_swap (
ln_address,
on_chain_address,
secret_access_key,
commitment,
signature
)
VALUES (?,?,?,?,?)
""",
(
data.ln_address,
data.on_chain_address,
data.secret_access_key,
data.commitment,
data.signature,
),
)

View File

@ -1,6 +1,27 @@
from typing import Optional
from pydantic.main import BaseModel
from sqlalchemy.engine import base # type: ignore
class Token(BaseModel):
deezy_token: str
class LnToBtcSwap(BaseModel):
amount_sats: int
on_chain_address: str
on_chain_sats_per_vbyte: int
bolt11_invoice: str
fee_sats: int
txid: str = ""
tx_hex: str = ""
created_at: str = ""
class BtcToLnSwap(BaseModel):
ln_address: str
on_chain_address: str
secret_access_key: str
commitment: str
signature: str
created_at: str = ""

View File

@ -213,7 +213,7 @@
</q-card-section>
</q-card>
</div>
<div class="q-pa-md">
<div class="q-pa-md full-width">
<q-table
title="Swaps Lightning -> BTC"
:data="rowsLnToBtc"
@ -221,6 +221,14 @@
row-key="name"
/>
</div>
<div class="q-pa-md full-width">
<q-table
title="Swaps BTC -> Lightning"
:data="rowsBtcToLn"
:columns="columnsBtcToLn"
row-key="name"
/>
</div>
</div>
{% endblock %} {% block scripts %} {{ window_vars(user) }}
<script>
@ -231,39 +239,24 @@
data: function () {
return {
columnsLnToBtc: [
{
name: 'Amount Sats',
required: true,
label: 'Amount Sats',
align: 'left',
field: row => row.amount_sats,
format: val => `${val}`,
sortable: true
},
{ name: 'on_chain_address', align: 'center', label: 'On chain address', field: 'on_chain_address' },
{ name: 'on_chain_sats_per_vbyte', align: 'center', label: 'Onchin sats per vbyte', field: 'on_chain_sats_per_vbyte', sortable: true },
{ name: 'txid', label: 'Tx id)', field: 'txid' },
{ name: 'fee_sats', label: 'Fee sats', field: 'fee_sats' },
{ name: 'created_at', label: 'Created at', field: 'created_at', sortable: true, sort: true }
{ name: 'amount_sats',label: 'Amount Sats', align: 'left', field: 'amount_sats', sortable: true},
{ name: 'on_chain_address', align: 'left', label: 'On chain address', field: 'on_chain_address' },
{ name: 'on_chain_sats_per_vbyte', align: 'left', label: 'Onchin sats per vbyte', field: 'on_chain_sats_per_vbyte', sortable: true },
{ name: 'fee_sats', label: 'Fee sats', align: 'left',field: 'fee_sats' },
{ name: 'txid', label: 'Tx Id',align: 'left', field: 'txid' },
{ name: 'tx_hex', label: 'Tx Hex',align: 'left', field: 'tx_hex' },
{ name: 'created_at', label: 'Created at',align: 'left', field: 'created_at', sortable: true, sort: true }
],
rowsLnToBtc: [
{
amount_sats: 1231232,
on_chain_address: 'btc address goes here',
on_chain_sats_per_vbyte: 6,
txid: 'transaction id',
fee_sats: 123,
created_at: '2002-02-01'
},
{
amount_sats: 1231232,
on_chain_address: 'btc address goes here',
on_chain_sats_per_vbyte: 6,
txid: 'transaction id',
fee_sats: 123,
created_at: '2002-02-01'
},
rowsLnToBtc: [],
columnsBtcToLn: [
{ name: 'ln_address', align: 'left', label: 'Ln Address or Invoice', field: 'ln_address' },
{ name: 'on_chain_address', align: 'left', label: 'Onchain Address', field: 'on_chain_address' },
{ name: 'secret_access_key', align: 'left', label: 'Onchain Address', field: 'secret_access_key' },
{ name: 'commitment', align: 'left', label: 'Commitment', field: 'commitment' },
{ name: 'signature', align: 'left', label: 'Signature', field: 'signature' },
{ name: 'created_at', label: 'Created at', field: 'created_at', align: 'left', sortable: true, sort: true }
],
rowsBtcToLn: [],
showDeezyTokenForm: false,
storedDeezyToken: null,
deezyToken: null,
@ -288,19 +281,40 @@
}
}
},
created: function () {
var self = this
axios({
method: 'GET',
url: '/deezy/api/v1/token',
}).then(function (response) {
self.storedDeezyToken = response.data.deezy_token
if (!self.storeDeezyToken) {
showDeezyTokenForm = true
}
})
created: async function () {
this.getToken()
this.getLnToBtc()
this.getBtcToLn()
},
methods: {
getToken() {
var self = this
axios({
method: 'GET',
url: '/deezy/api/v1/token',
}).then(function (response) {
self.storedDeezyToken = response.data.deezy_token
if (!self.storeDeezyToken) {
showDeezyTokenForm = true
}
})
},
getLnToBtc() {
var self = this
axios.get('/deezy/api/v1/ln-to-btc').then(function (response) {
if (response.data.length) {
self.rowsLnToBtc = response.data
}
})
},
getBtcToLn() {
var self = this
axios.get('/deezy/api/v1/btc-to-ln').then(function (response) {
if (response.data.length) {
self.rowsBtcToLn = response.data
}
})
},
showLnToBtcForm() {
if (!this.swapLnToBtc.show) {
this.getSuggestedOnChainFees()
@ -360,13 +374,11 @@
response: response.data.bolt11_invoice
}
const payload = {
amount_sats: self.swapLnToBtc.data.amount_sats,
amount_sats: parseInt(self.swapLnToBtc.data.amount),
on_chain_address: self.swapLnToBtc.data.on_chain_address,
on_chain_sats_per_vbyte: self.swapLnToBtc.data.on_chain_sats_per_vbyte,
bolt11_invoice: response.data.bolt11_invoice,
fee_sats: response.data.fee_sats,
txid: response.data.txid,
tx_hex: response.data.tx_hex
}
self.storeLnToBtc(payload)
self.checkIfInvoiceIsPaid()
@ -392,7 +404,7 @@
showDetails: true
}
const payload = {
ln_address: selt.swapBtcToLn.data.lnurl_or_lnaddress,
ln_address: self.swapBtcToLn.data.lnurl_or_lnaddress,
on_chain_address: response.data.address,
secret_access_key: response.data.secret_access_key,
commitment:response.data.commitment,
@ -408,7 +420,7 @@
var self = this
axios
.post('/deezy/api/v1/store-btc-to-ln', {
data: payload
...payload
})
.then(function (response) {
console.log('btc to ln is stored', response)
@ -421,7 +433,7 @@
var self = this
axios
.post('/deezy/api/v1/store-ln-to-btc', {
data: payload
...payload
})
.then(function (response) {
console.log('ln to btc is stored', response)

View File

@ -10,22 +10,53 @@ from . import deezy_ext
from . import db
from .models import (
Token,
LnToBtcSwap,
BtcToLnSwap
)
from .crud import (
get_token,
save_token
get_ln_to_btc,
get_btc_to_ln,
save_token,
save_btc_to_ln,
save_ln_to_btc
)
@deezy_ext.get("/api/v1/token")
async def api_deezy():
"""Get token from table."""
rows = await get_token()
return rows
@deezy_ext.get("/api/v1/ln-to-btc")
async def api_deezy():
rows = await get_ln_to_btc()
return rows
@deezy_ext.get("/api/v1/btc-to-ln")
async def api_deezy():
rows = await get_btc_to_ln()
return rows
@deezy_ext.post("/api/v1/store-token")
async def api_deezy(data: Token):
await save_token(data)
return data.deezy_token
@deezy_ext.post("/api/v1/store-ln-to-btc")
async def api_deezy(data: LnToBtcSwap):
response = await save_ln_to_btc(data)
return response
@deezy_ext.post("/api/v1/store-btc-to-ln")
async def api_deezy(data: BtcToLnSwap):
response = await save_btc_to_ln(data)
return response