Skip to content

Commit

Permalink
Cleanup ticket purchase modal
Browse files Browse the repository at this point in the history
  • Loading branch information
beansgum committed Sep 13, 2021
1 parent 3e44c01 commit 633497b
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 132 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ require (

replace (
github.com/decred/dcrdata/txhelpers/v4 => github.com/decred/dcrdata/txhelpers/v4 v4.0.0-20200108145420-f82113e7e212
github.com/planetdecred/dcrlibwallet => github.com/C-ollins/mobilewallet v1.0.0-rc1.0.20210910121332-e44a98c834c2
github.com/planetdecred/dcrlibwallet => github.com/C-ollins/mobilewallet v1.0.0-rc1.0.20210912175524-041481a23c8b
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ github.com/C-ollins/mobilewallet v1.0.0-rc1.0.20210825093648-ec344982588e h1:x6A
github.com/C-ollins/mobilewallet v1.0.0-rc1.0.20210825093648-ec344982588e/go.mod h1:sRwfsPrOEnpGBNL54KS83Dpxx2kp2AzZ0Je5vKRRE4o=
github.com/C-ollins/mobilewallet v1.0.0-rc1.0.20210910121332-e44a98c834c2 h1:EHZLz82ivykxAkm6BI15FBrPL/PdUsUD2gtrb6pnFpY=
github.com/C-ollins/mobilewallet v1.0.0-rc1.0.20210910121332-e44a98c834c2/go.mod h1:sRwfsPrOEnpGBNL54KS83Dpxx2kp2AzZ0Je5vKRRE4o=
github.com/C-ollins/mobilewallet v1.0.0-rc1.0.20210912175524-041481a23c8b h1:JvcNx/P2fSfgXpySx/HVbnRyYo6ZPgnpyVmnar6SuE4=
github.com/C-ollins/mobilewallet v1.0.0-rc1.0.20210912175524-041481a23c8b/go.mod h1:sRwfsPrOEnpGBNL54KS83Dpxx2kp2AzZ0Je5vKRRE4o=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/DataDog/zstd v1.4.8 h1:Rpmta4xZ/MgZnriKNd24iZMhGpP5dvUcs/uqfBapKZY=
github.com/DataDog/zstd v1.4.8/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
Expand Down
45 changes: 5 additions & 40 deletions ui/load/vsp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package load

import (
"context"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -95,12 +94,12 @@ func (wl *WalletLoad) GetVSPList() {
}

wl.MultiWallet.ReadUserConfigValue(dcrlibwallet.VSPHostConfigKey, &valueOut)
var loadedVSP []wallet.VSPInfo
var loadedVSP []*wallet.VSPInfo

for _, host := range valueOut.List {
v, err := getVSPInfo(host)
if err == nil {
loadedVSP = append(loadedVSP, wallet.VSPInfo{
loadedVSP = append(loadedVSP, &wallet.VSPInfo{
Host: host,
Info: v,
})
Expand All @@ -110,14 +109,14 @@ func (wl *WalletLoad) GetVSPList() {
l, _ := getInitVSPInfo("https://api.decred.org/?c=vsp")
for h, v := range l {
if strings.Contains(wl.Wallet.Net, v.Network) {
loadedVSP = append(loadedVSP, wallet.VSPInfo{
loadedVSP = append(loadedVSP, &wallet.VSPInfo{
Host: fmt.Sprintf("https://%s", h),
Info: v,
})
}
}

(*wl.VspInfo).List = loadedVSP
wl.VspInfo.List = loadedVSP
}

// TicketPrice get ticket price
Expand All @@ -130,40 +129,6 @@ func (wl *WalletLoad) TicketPrice() int64 {
return pr.TicketPrice
}

func (wl *WalletLoad) NewVSPD(host string, walletID int, accountID int32) (*dcrlibwallet.VSP, error) {
if host == "" {
return nil, fmt.Errorf("Host is required")
}
wall := wl.MultiWallet.WalletWithID(walletID)
if wall == nil {
return nil, ErrIDNotExist
}
vspd, err := wl.MultiWallet.NewVSPClient(host, walletID, uint32(accountID))
if err != nil {
return nil, fmt.Errorf("Something wrong when creating new VSPD: %v", err)
}
return vspd, nil
}

func (wl *WalletLoad) PurchaseTicket(walletID int, tickets uint32, passphrase []byte, vspd *dcrlibwallet.VSP) (err error) {
wall := wl.MultiWallet.WalletWithID(walletID)
if wall == nil {
return fmt.Errorf("wallet ID does not exist")
}

_, err = vspd.GetInfo(context.Background())
if err != nil {
return err
}

err = vspd.PurchaseTickets(int32(tickets), wl.MultiWallet.GetBestBlock().Height+256, passphrase)
if err != nil {
return
}

return
}

func (wl *WalletLoad) AddVSP(host string) (err error) {
var valueOut struct {
Remember string
Expand All @@ -190,7 +155,7 @@ func (wl *WalletLoad) AddVSP(host string) (err error) {

valueOut.List = append(valueOut.List, host)
wl.MultiWallet.SaveUserConfigValue(dcrlibwallet.VSPHostConfigKey, valueOut)
(*wl.VspInfo).List = append((*wl.VspInfo).List, wallet.VSPInfo{
(*wl.VspInfo).List = append((*wl.VspInfo).List, &wallet.VSPInfo{
Host: host,
Info: info,
})
Expand Down
7 changes: 0 additions & 7 deletions ui/page/overview_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,6 @@ func (pg *OverviewPage) loadTransactions() {
// Layout lays out the entire content for overview pg.
func (pg *OverviewPage) Layout(gtx layout.Context) layout.Dimensions {
pg.queue = gtx
if pg.WL.Info.LoadedWallets == 0 {
return components.UniformPadding(gtx, func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
return pg.Theme.H3(values.String(values.StrNoWalletLoaded)).Layout(gtx)
})
})
}

pageContent := []func(gtx C) D{
func(gtx C) D {
Expand Down
125 changes: 63 additions & 62 deletions ui/page/tickets/purchase_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"image/color"
"strconv"

"gioui.org/gesture"
"gioui.org/layout"
"gioui.org/widget"

"github.com/decred/dcrd/dcrutil"
"github.com/decred/dcrd/dcrutil/v3"
"github.com/planetdecred/dcrlibwallet"
"github.com/planetdecred/godcr/ui/decredmaterial"
"github.com/planetdecred/godcr/ui/load"
Expand All @@ -22,23 +21,19 @@ const purchaseModalID = "ticket_purchase_modal"
type ticketPurchaseModal struct {
*load.Load

ticketPrice string
totalCost int64
balanceLessCost int64
vspIsFetched bool
isPurchaseLoading bool
ticketPrice dcrutil.Amount
totalCost int64
balanceLessCost int64
vspIsFetched bool

modal decredmaterial.Modal
tickets decredmaterial.Editor
rememberVSP decredmaterial.CheckBoxStyle
selectVSP []*gesture.Click
cancelPurchase decredmaterial.Button
reviewPurchase decredmaterial.Button

accountSelector *components.AccountSelector
vspSelector *vspSelector

vsp *dcrlibwallet.VSP
}

func newTicketPurchaseModal(l *load.Load) *ticketPurchaseModal {
Expand All @@ -60,6 +55,22 @@ func newTicketPurchaseModal(l *load.Load) *ticketPurchaseModal {
return tp
}

func (tp *ticketPurchaseModal) OnResume() {
tp.initializeAccountSelector()
err := tp.accountSelector.SelectFirstWalletValidAccount()
if err != nil {
tp.Toast.NotifyError(err.Error())
}

tp.vspSelector = newVSPSelector(tp.Load).title("Select a vsp")
tp.ticketPrice = dcrutil.Amount(tp.WL.TicketPrice())

if tp.vspIsFetched && tp.WL.GetRememberVSP() != "" {
tp.vspSelector.selectVSP(tp.WL.GetRememberVSP())
tp.rememberVSP.CheckBox.Value = true
}
}

func (tp *ticketPurchaseModal) Layout(gtx layout.Context) layout.Dimensions {
l := []layout.Widget{
func(gtx C) D {
Expand All @@ -73,28 +84,30 @@ func (tp *ticketPurchaseModal) Layout(gtx layout.Context) layout.Dimensions {
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{Top: values.MarginPadding8}.Layout(gtx, func(gtx C) D {
return components.LayoutBalance(gtx, tp.Load, tp.ticketPrice)
return components.LayoutBalanceSize(gtx, tp.Load, tp.ticketPrice.String(), values.Size28)
})
}),
)
})
}),
layout.Rigid(func(gtx C) D {
return layout.Flex{}.Layout(gtx,
layout.Flexed(.5, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
tit := tp.Theme.Label(values.TextSize14, "Total")
tit.Color = tp.Theme.Color.Gray2
return tit.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
return tp.Theme.Label(values.TextSize16, tp.ticketPrice).Layout(gtx)
}),
)
}),
layout.Flexed(.5, tp.tickets.Layout),
)
return layout.Inset{Top: values.MarginPadding8}.Layout(gtx, func(gtx C) D {
return layout.Flex{}.Layout(gtx,
layout.Flexed(.5, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
tit := tp.Theme.Label(values.TextSize14, "Total")
tit.Color = tp.Theme.Color.Gray3
return tit.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
return tp.Theme.Label(values.TextSize16, dcrutil.Amount(int64(tp.ticketPrice)*tp.ticketCount()).String()).Layout(gtx)
}),
)
}),
layout.Flexed(.5, tp.tickets.Layout),
)
})
}),
)
},
Expand Down Expand Up @@ -145,7 +158,7 @@ func (tp *ticketPurchaseModal) ticketCount() int64 {
}

func (tp *ticketPurchaseModal) canPurchase() bool {
if tp.vspSelector.selectedVSP.Info == nil {
if tp.vspSelector.selectedVSP == nil {
return false
}

Expand All @@ -155,10 +168,6 @@ func (tp *ticketPurchaseModal) canPurchase() bool {
return false
}

if tp.vspSelector.selectedVSP.Host == "" {
return false
}

if tp.ticketCount() < 1 {
return false
}
Expand All @@ -178,22 +187,6 @@ func (tp *ticketPurchaseModal) Dismiss() {
tp.DismissModal(tp)
}

func (tp *ticketPurchaseModal) OnResume() {
tp.initializeAccountSelector()
err := tp.accountSelector.SelectFirstWalletValidAccount()
if err != nil {
tp.Toast.NotifyError(err.Error())
}

tp.vspSelector = newVSPSelector(tp.Load).title("Select a vsp")
tp.ticketPrice = dcrutil.Amount(tp.WL.TicketPrice()).String()

if tp.vspIsFetched && tp.WL.GetRememberVSP() != "" {
tp.vspSelector.selectVSP(tp.WL.GetRememberVSP())
tp.rememberVSP.CheckBox.Value = true
}
}

func (tp *ticketPurchaseModal) initializeAccountSelector() {
tp.accountSelector = components.NewAccountSelector(tp.Load).
Title("Purchasing account").
Expand All @@ -216,35 +209,44 @@ func (tp *ticketPurchaseModal) initializeAccountSelector() {
func (tp *ticketPurchaseModal) OnDismiss() {}

func (tp *ticketPurchaseModal) calculateTotals() {
accountBalance := tp.accountSelector.SelectedAccount().Balance.Spendable
feePercentage := tp.vspSelector.selectedVSP.Info.FeePercentage
total := tp.WL.TicketPrice() * tp.ticketCount()
fee := int64((float64(total) / 100) * feePercentage)
tp.totalCost = total + fee
tp.balanceLessCost = accountBalance - tp.totalCost
}
account := tp.accountSelector.SelectedAccount()
wal := tp.WL.MultiWallet.WalletWithID(account.WalletID)

func (tp *ticketPurchaseModal) createNewVSPD() {
selectedAccount := tp.accountSelector.SelectedAccount()
selectedVSP := tp.vspSelector.SelectedVSP()
vspd, err := tp.WL.NewVSPD(selectedVSP.Host, selectedAccount.WalletID, selectedAccount.Number)
ticketPrice, err := wal.TicketPrice()
if err != nil {
tp.Toast.NotifyError(err.Error())
return
}
tp.vsp = vspd

feePercentage := tp.vspSelector.selectedVSP.Info.FeePercentage
total := ticketPrice.TicketPrice * tp.ticketCount()
fee := int64((float64(total) / 100) * feePercentage)

tp.totalCost = total + fee
tp.balanceLessCost = account.Balance.Spendable - tp.totalCost
}

func (tp *ticketPurchaseModal) purchaseTickets(password []byte) {
tp.Dismiss()
tp.Toast.Notify(fmt.Sprintf("attempting to purchase %v ticket(s)", tp.ticketCount()))
tp.Toast.Notify(fmt.Sprintf("Attempting to purchase %v ticket(s)", tp.ticketCount()))

go func() {
selectedVSP := tp.vspSelector.SelectedVSP()
account := tp.accountSelector.SelectedAccount()
err := tp.WL.PurchaseTicket(account.WalletID, uint32(tp.ticketCount()), password, tp.vsp)
wal := tp.WL.MultiWallet.WalletWithID(account.WalletID)

vsp, err := tp.WL.MultiWallet.NewVSPClient(selectedVSP.Host, account.WalletID, uint32(account.Number))
if err != nil {
tp.Toast.NotifyError(err.Error())
return
}

err = vsp.PurchaseTickets(int32(tp.ticketCount()), wal.GetBestBlock()+256, password)
if err != nil {
tp.Toast.NotifyError(err.Error())
return
}

tp.Toast.Notify(fmt.Sprintf("%v ticket(s) purchased successfully", tp.ticketCount()))
}()
}
Expand All @@ -263,7 +265,6 @@ func (tp *ticketPurchaseModal) Handle() {
}

if tp.reviewPurchase.Button.Clicked() && tp.canPurchase() {
go tp.createNewVSPD()

if tp.vspSelector.Changed() && tp.rememberVSP.CheckBox.Value {
tp.WL.RememberVSP(tp.vspSelector.selectedVSP.Host)
Expand Down
2 changes: 1 addition & 1 deletion ui/page/tickets/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func ticketCard(gtx layout.Context, l *load.Load, tx *transactionItem, showWalle

durationLayout := layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Inset{Right: values.MarginPadding4}.Layout(gtx, l.Icons.TimerIcon.Layout)
return layout.Inset{Right: values.MarginPadding4}.Layout(gtx, l.Icons.TimerIcon.Layout12dp)
}),
layout.Rigid(txt.Layout),
)
Expand Down
Loading

0 comments on commit 633497b

Please sign in to comment.