Skip to content

Commit

Permalink
submarine_swaps: implement swaps-over-nostr for qml
Browse files Browse the repository at this point in the history
  • Loading branch information
accumulator committed Nov 15, 2024
1 parent 2d86a61 commit 8a825f2
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 93 deletions.
140 changes: 140 additions & 0 deletions electrum/gui/qml/components/NostrSwapServersDialog.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Controls.Material

import org.electrum 1.0

import "controls"

ElDialog {
id: dialog
title: qsTr("Select Swap Server")

property QtObject swaphelper

property string selectedPubkey

anchors.centerIn: parent

padding: 0

width: parent.width * 4/5
height: parent.height * 4/5

ColumnLayout {
id: rootLayout
width: parent.width
height: parent.height

Frame {
id: accountsFrame
Layout.fillWidth: true
Layout.fillHeight: true
Layout.topMargin: constants.paddingLarge
Layout.bottomMargin: constants.paddingLarge
Layout.leftMargin: constants.paddingMedium
Layout.rightMargin: constants.paddingMedium

verticalPadding: 0
horizontalPadding: 0
background: PaneInsetBackground {}

ColumnLayout {
spacing: 0
anchors.fill: parent

ListView {
id: listview
Layout.preferredWidth: parent.width
Layout.fillHeight: true
clip: true
model: swaphelper.availableSwapServers

delegate: ItemDelegate {
width: ListView.view.width
height: itemLayout.height

onClicked: {
dialog.selectedPubkey = model.npub
dialog.doAccept()
}

GridLayout {
id: itemLayout
columns: 3
rowSpacing: 0

anchors {
left: parent.left
right: parent.right
leftMargin: constants.paddingMedium
rightMargin: constants.paddingMedium
}

Item {
Layout.columnSpan: 3
Layout.preferredHeight: constants.paddingLarge
Layout.preferredWidth: 1
}
Image {
Layout.rowSpan: 3
source: Qt.resolvedUrl('../../icons/network.png')
}
Label {
text: qsTr('npub')
color: Material.accentColor
}
Label {
Layout.fillWidth: true
text: model.npub.substring(0,10)
wrapMode: Text.Wrap
}
Label {
text: qsTr('fee')
color: Material.accentColor
}
Label {
Layout.fillWidth: true
text: model.percentage_fee + '%'
}
Label {
text: qsTr('last seen')
color: Material.accentColor
}
Label {
Layout.fillWidth: true
text: model.timestamp
}
Item {
Layout.columnSpan: 3
Layout.preferredHeight: constants.paddingLarge
Layout.preferredWidth: 1
}
}
}

ScrollIndicator.vertical: ScrollIndicator { }

Label {
visible: swaphelper.availableSwapServers.count == 0
anchors.centerIn: parent
width: listview.width * 4/5
font.pixelSize: constants.fontSizeXXLarge
color: constants.mutedForeground
text: qsTr('No swap servers found')
wrapMode: Text.Wrap
horizontalAlignment: Text.AlignHCenter
}

}
}
}
}

Component.onCompleted: {
if (dialog.selectedPubkey) {
listview.currentIndex = swaphelper.availableSwapServers.indexFor(dialog.selectedPubkey)
}
}
}
25 changes: 25 additions & 0 deletions electrum/gui/qml/components/SwapDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,31 @@ ElDialog {
}
}


Pane {
visible: _swaphelper.isNostr()
background: Rectangle { color: constants.darkerDialogBackground }
padding: 0
anchors.horizontalCenter: parent.horizontalCenter

FlatButton {
text: qsTr('Choose swap provider')
onClicked: {
var dialog = app.nostrSwapServersDialog.createObject(app, {
swaphelper: _swaphelper,
selectedPubkey: Config.swapServerNPub
})
dialog.accepted.connect(function() {
if (true || Config.swapServerNPub != dialog.selectedPubkey) {
Config.swapServerNPub = dialog.selectedPubkey
_swaphelper.init_swap_manager()
}
})
dialog.open()
}
}
}

Item { Layout.fillHeight: true; Layout.preferredWidth: 1 }

ButtonContainer {
Expand Down
20 changes: 20 additions & 0 deletions electrum/gui/qml/components/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,18 @@ ApplicationWindow
}
}

property alias nostrSwapServersDialog: _nostrSwapServersDialog
Component {
id: _nostrSwapServersDialog
NostrSwapServersDialog {
onClosed: destroy()
}
}

Component {
id: swapDialog
SwapDialog {
id: _swapdialog
onClosed: destroy()
swaphelper: SwapHelper {
id: _swaphelper
Expand All @@ -454,6 +463,17 @@ ApplicationWindow
})
dialog.open()
}
onUndefinedNPub: {
var dialog = app.nostrSwapServersDialog.createObject(app, {
swaphelper: _swaphelper,
selectedPubkey: Config.swapServerNPub
})
dialog.accepted.connect(function() {
Config.swapServerNPub = dialog.selectedPubkey
_swaphelper.init_swap_manager()
})
dialog.open()
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions electrum/gui/qml/qeconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ def lightningPaymentFeeMaxMillionths(self, lightningPaymentFeeMaxMillionths):
self.config.LIGHTNING_PAYMENT_FEE_MAX_MILLIONTHS = lightningPaymentFeeMaxMillionths
self.lightningPaymentFeeMaxMillionthsChanged.emit()

swapServerNPubChanged = pyqtSignal()
@pyqtProperty(str, notify=swapServerNPubChanged)
def swapServerNPub(self):
return self.config.SWAPSERVER_NPUB

@swapServerNPub.setter
def swapServerNPub(self, swapserver_npub):
if swapserver_npub != self.config.SWAPSERVER_NPUB:
self.config.SWAPSERVER_NPUB = swapserver_npub
self.swapServerNPubChanged.emit()

@pyqtSlot('qint64', result=str)
@pyqtSlot(QEAmount, result=str)
def formatSatsForEditing(self, satoshis):
Expand Down
Loading

0 comments on commit 8a825f2

Please sign in to comment.