adding deezy token

This commit is contained in:
Uthpala Heenatigala 2023-01-05 11:46:01 +01:00
parent cd05eba183
commit 93d6d1279e
6 changed files with 233 additions and 5 deletions

View file

@ -22,3 +22,4 @@ def deezy_renderer():
from .views import * # noqa from .views import * # noqa
from .views_api import * # noqa

View file

@ -0,0 +1,38 @@
from http import HTTPStatus
from typing import List
from . import db
from .models import (
Token,
)
"""
Get Deezy Token
"""
async def get_token() -> Token:
row = await db.fetchone(
f"SELECT * FROM deezy.token ORDER BY created_at DESC",
)
return Token(**row) if row else None
async def save_token(
data: Token,
) -> Token:
await db.execute(
"""
INSERT INTO deezy.token (
deezy_token
)
VALUES (?)
""",
(
data.deezy_token,
),
)
return data

View file

@ -27,3 +27,11 @@ async def m001_initial(db):
); );
""" """
) )
await db.execute(
f"""
CREATE TABLE deezy.token (
deezy_token TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
"""
)

View file

@ -1,5 +1,6 @@
# from pydantic import BaseModel from pydantic.main import BaseModel
from sqlalchemy.engine import base # type: ignore
# class Example(BaseModel):
# id: str class Token(BaseModel):
# wallet: str deezy_token: str

View file

@ -5,6 +5,30 @@
<q-card> <q-card>
<q-card-section> <q-card-section>
<h5 class="text-subtitle1 q-mt-none q-mb-md">Deezy</h5> <h5 class="text-subtitle1 q-mt-none q-mb-md">Deezy</h5>
<p class="text-subtitle2 q-mt-none q-mb-md">Due to regulatory reasons you need to get a access token from deezy. Contact - support@deezy.io or @dannydeezy on telegram </p>
<div>
<div class="flex justify-between items-center">
<span>Deezy token </span>
<q-btn type="button" @click="showDeezyTokenForm = !showDeezyTokenForm">Add or Update token</q-btn>
</div>
<p v-if="storedDeezyToken" v-text="storedDeezyToken"></p>
</div>
<q-form v-if="showDeezyTokenForm" @submit="storeDeezyToken" class="q-gutter-md q-mt-lg">
<q-input
filled
dense
emit-value
:placeholder="storedDeezyToken"
v-model.trim="deezyToken"
label="Deezy Token"
type="text"
></q-input>
<q-btn
color="grey"
type="submit"
label="Store Deezy Token"
></q-btn>
</q-form>
<q-separator class="q-my-lg"></q-separator> <q-separator class="q-my-lg"></q-separator>
<q-card> <q-card>
<q-card-section> <q-card-section>
@ -189,6 +213,14 @@
</q-card-section> </q-card-section>
</q-card> </q-card>
</div> </div>
<div class="q-pa-md">
<q-table
title="Swaps Lightning -> BTC"
:data="rowsLnToBtc"
:columns="columnsLnToBtc"
row-key="name"
/>
</div>
</div> </div>
{% endblock %} {% block scripts %} {{ window_vars(user) }} {% endblock %} {% block scripts %} {{ window_vars(user) }}
<script> <script>
@ -198,6 +230,43 @@
mixins: [windowMixin], mixins: [windowMixin],
data: function () { data: function () {
return { 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 }
],
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'
},
],
showDeezyTokenForm: false,
storedDeezyToken: null,
deezyToken: null,
lightning_btc: '', lightning_btc: '',
tools: [], tools: [],
swapLnToBtc: { swapLnToBtc: {
@ -219,9 +288,23 @@
} }
} }
}, },
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
}
})
},
methods: { methods: {
showLnToBtcForm() { showLnToBtcForm() {
if (!this.swapLnToBtc.show) {
this.getSuggestedOnChainFees() this.getSuggestedOnChainFees()
}
this.swapLnToBtc.show = true this.swapLnToBtc.show = true
this.swapBtcToLn.show = false this.swapBtcToLn.show = false
}, },
@ -265,6 +348,10 @@
on_chain_sats_per_vbyte: parseInt( on_chain_sats_per_vbyte: parseInt(
self.swapLnToBtc.data.on_chain_sats_per_vbyte self.swapLnToBtc.data.on_chain_sats_per_vbyte
) )
}, {
headers: {
"x-api-token": self.storedDeezyToken,
}
}) })
.then(function (response) { .then(function (response) {
self.swapLnToBtc = { self.swapLnToBtc = {
@ -272,6 +359,16 @@
showInvoice: true, showInvoice: true,
response: response.data.bolt11_invoice response: response.data.bolt11_invoice
} }
const payload = {
amount_sats: self.swapLnToBtc.data.amount_sats,
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() self.checkIfInvoiceIsPaid()
}) })
.catch(function (error) { .catch(function (error) {
@ -283,6 +380,10 @@
axios axios
.post('https://api.deezy.io/v1/source', { .post('https://api.deezy.io/v1/source', {
lnurl_or_lnaddress: self.swapBtcToLn.data.lnurl_or_lnaddress lnurl_or_lnaddress: self.swapBtcToLn.data.lnurl_or_lnaddress
}, {
headers: {
"x-api-token": self.storedDeezyToken,
}
}) })
.then(function (response) { .then(function (response) {
self.swapBtcToLn = { self.swapBtcToLn = {
@ -290,6 +391,54 @@
response: response.data, response: response.data,
showDetails: true showDetails: true
} }
const payload = {
ln_address: selt.swapBtcToLn.data.lnurl_or_lnaddress,
on_chain_address: response.data.address,
secret_access_key: response.data.secret_access_key,
commitment:response.data.commitment,
signature: response.data.signature
}
self.storeBtcToLn(payload)
})
.catch(function (error) {
console.log(error)
})
},
storeBtcToLn(payload) {
var self = this
axios
.post('/deezy/api/v1/store-btc-to-ln', {
data: payload
})
.then(function (response) {
console.log('btc to ln is stored', response)
})
.catch(function (error) {
console.log(error)
})
},
storeLnToBtc(payload) {
var self = this
axios
.post('/deezy/api/v1/store-ln-to-btc', {
data: payload
})
.then(function (response) {
console.log('ln to btc is stored', response)
})
.catch(function (error) {
console.log(error)
})
},
storeDeezyToken() {
var self = this
axios
.post('/deezy/api/v1/store-token', {
deezy_token: self.deezyToken
})
.then(function (response) {
self.storedDeezyToken = response.data
self.showDeezyTokenForm = false
}) })
.catch(function (error) { .catch(function (error) {
console.log(error) console.log(error)

View file

@ -0,0 +1,31 @@
# views_api.py is for you API endpoints that could be hit by another service
# add your dependencies here
# import httpx
# (use httpx just like requests, except instead of response.ok there's only the
# response.is_error that is its inverse)
from . import deezy_ext
from . import db
from .models import (
Token,
)
from .crud import (
get_token,
save_token
)
@deezy_ext.get("/api/v1/token")
async def api_deezy():
"""Get token from table."""
rows = await get_token()
return rows
@deezy_ext.post("/api/v1/store-token")
async def api_deezy(data: Token):
await save_token(data)
return data.deezy_token