Skip to content

Commit

Permalink
forgot to pass SC_Code and SC_ID :S goroutine to open transfer fixes …
Browse files Browse the repository at this point in the history
…res channel
  • Loading branch information
g45t345rt committed Mar 14, 2024
1 parent bba98be commit 74fc6ab
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 33 deletions.
41 changes: 25 additions & 16 deletions containers/build_tx_modal/build_tx_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ type TransferResponse struct {
Err error
}

type LoadStatus string

var (
Default LoadStatus = ""
FetchAddr LoadStatus = "fetch_addr"
LoadFees LoadStatus = "load_fees"
Building LoadStatus = "building"
Sending LoadStatus = "sending"
)

type TxPayload struct {
Transfer rpc.Transfer_Params
Description string
Expand Down Expand Up @@ -90,7 +100,7 @@ type BuildTxModal struct {
animationLoading *animation.Animation
buttonClose *components.Button

loadStatus string
loadStatus LoadStatus
txFees uint64
gasFees uint64

Expand Down Expand Up @@ -205,7 +215,7 @@ func (b *BuildTxModal) OpenWithRandomAddr(scId crypto.Hash, onLoad func(addr str
wallet := wallet_manager.OpenedWallet
b.modal.SetVisible(true)

b.SetLoadStatus("fetch_addr")
b.SetLoadStatus(FetchAddr)
randomAddr, err := wallet.GetRandomAddress(scId)
time.Sleep(1 * time.Second)
if err != nil {
Expand All @@ -223,6 +233,7 @@ func (b *BuildTxModal) OpenWithRandomAddr(scId crypto.Hash, onLoad func(addr str
}

func (b *BuildTxModal) Open(txPayload TxPayload) {
b.txPayload = txPayload
wallet := wallet_manager.OpenedWallet
if !b.modal.Visible {
b.modal.SetVisible(true)
Expand All @@ -249,28 +260,25 @@ func (b *BuildTxModal) Open(txPayload TxPayload) {
}

b.txFees = wallet.Memory.EstimateTxFees(len(txPayload.Transfer.Transfers), int(txPayload.Transfer.Ringsize), txPayload.Transfer.SC_RPC, txType)
b.txPayload = txPayload
return nil
}

b.SetLoadStatus("load_fees")
b.SetLoadStatus(LoadFees)
err := load()
time.Sleep(1 * time.Second)
time.Sleep(500 * time.Millisecond) // tiny sleep so we can see the estimated fees status
if err != nil {
b.Close(err)
notification_modal.Open(notification_modal.Params{
Type: notification_modal.ERROR,
Title: lang.Translate("Error"),
Text: err.Error(),
})

return
}

b.SetLoadStatus("")
b.SetLoadStatus(Default)
}

func (b *BuildTxModal) SetLoadStatus(status string) {
func (b *BuildTxModal) SetLoadStatus(status LoadStatus) {
if status == "" {
b.animationLoading.Reset()
} else {
Expand All @@ -292,6 +300,7 @@ func (b *BuildTxModal) Complete(response TransferResponse) {
func (b *BuildTxModal) Close(err error) {
if err != nil {
b.Complete(TransferResponse{Err: err})
return
}

b.Complete(TransferResponse{Err: fmt.Errorf("cancel transfer")})
Expand All @@ -302,13 +311,13 @@ func (b *BuildTxModal) buildAndSendTx() {
wallet := wallet_manager.OpenedWallet

buildAndSend := func() (tx *transaction.Transaction, err error) {
b.SetLoadStatus("building")
b.SetLoadStatus(Building)
tx, err = wallet.Memory.TransferFeesPrecomputed(b.txPayload.Transfer.Transfers, b.txPayload.Transfer.Ringsize, false, b.txPayload.Transfer.SC_RPC, b.gasFees, b.txFees, false)
if err != nil {
return
}

b.SetLoadStatus("sending")
b.SetLoadStatus(Sending)
err = wallet.Memory.SendTransaction(tx)
if err != nil {
return
Expand Down Expand Up @@ -384,7 +393,7 @@ func (b *BuildTxModal) layout(gtx layout.Context, th *material.Theme) {
}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
var childs []layout.FlexChild

if b.loadStatus != "" {
if b.loadStatus != Default {
childs = append(childs,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Flex{
Expand All @@ -394,13 +403,13 @@ func (b *BuildTxModal) layout(gtx layout.Context, th *material.Theme) {
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
txt := ""
switch b.loadStatus {
case "building":
case Building:
txt = lang.Translate("Building transaction...")
case "sending":
case Sending:
txt = lang.Translate("Sending transaction...")
case "fetch_addr":
case FetchAddr:
txt = lang.Translate("Fetching addr...")
case "load_fees":
case LoadFees:
txt = lang.Translate("Estimating fees...")
}

Expand Down
21 changes: 11 additions & 10 deletions containers/xswd_perm_modal/xswd_perm_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,32 +138,33 @@ func (c *XSWDPermModal) Open(app *xswd.ApplicationData, req *jrpc2.Request) chan
return c.permChan
}

func (c *XSWDPermModal) set(perm xswd.Permission) {
c.permChan <- perm
c.Modal.SetVisible(false)
close(c.permChan)
app_instance.Window.Invalidate()
func (c *XSWDPermModal) Close(perm xswd.Permission) {
if c.Modal.Visible {
c.permChan <- perm
c.Modal.SetVisible(false)
close(c.permChan)
}
}

func (c *XSWDPermModal) Layout(gtx layout.Context, th *material.Theme) layout.Dimensions {
if c.buttonAllow.Clicked(gtx) {
go c.set(xswd.Allow)
c.Close(xswd.Allow)
}

if c.buttonDeny.Clicked(gtx) {
go c.set(xswd.Deny)
c.Close(xswd.Deny)
}

if c.buttonAlwaysAllow.Clicked(gtx) {
go c.set(xswd.AlwaysAllow)
c.Close(xswd.AlwaysAllow)
}

if c.buttonAlwaysDeny.Clicked(gtx) {
go c.set(xswd.AlwaysDeny)
c.Close(xswd.AlwaysDeny)
}

if c.buttonClose.Clicked(gtx) {
go c.set(xswd.Deny)
c.Close(xswd.Deny)
}

c.Modal.Style.Colors = theme.Current.ModalColors
Expand Down
15 changes: 8 additions & 7 deletions pages/wallet/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,13 @@ func (p *Page) OpenXSWD() error {
}

reqHandler := func(appData *xswd.ApplicationData, req *jrpc2.Request) (xswd.Permission, interface{}, error) {
// fmt.Println(req.Method(), req.ParamString())

txt := lang.Translate("An app is requesting access for {}.")
txt = strings.Replace(txt, "{}", req.Method(), -1)

switch req.Method() {
method := strings.ToLower(req.Method())
switch method {
case "transfer":
notify.Push("XSWD", txt) // notify only for transfer or sc_invoke
notify.Push("XSWD", txt) // notify only for transfer or scinvoke

var params rpc.Transfer_Params
err := req.UnmarshalParams(&params)
Expand All @@ -354,19 +353,21 @@ func (p *Page) OpenXSWD() error {
description = strings.Replace(description, "{}", appData.Url, -1)

transferResponse := make(chan build_tx_modal.TransferResponse)
build_tx_modal.Instance.Open(build_tx_modal.TxPayload{
go build_tx_modal.Instance.Open(build_tx_modal.TxPayload{
Transfer: rpc.Transfer_Params{
Transfers: params.Transfers,
Ringsize: params.Ringsize,
SC_RPC: params.SC_RPC,
SC_Code: params.SC_Code,
SC_ID: params.SC_ID,
},
Description: description,
TransferResponse: transferResponse,
})

res := <-transferResponse
return xswd.Allow, res.Result, res.Err
case "sc_invoke":
case "scinvoke":
notify.Push("XSWD", txt)

var params rpc.SC_Invoke_Params
Expand All @@ -379,7 +380,7 @@ func (p *Page) OpenXSWD() error {
description = strings.Replace(description, "{}", appData.Url, -1)

transferResponse := make(chan build_tx_modal.TransferResponse)
build_tx_modal.Instance.OpenWithRandomAddr(crypto.ZEROHASH, func(addr string) build_tx_modal.TxPayload {
go build_tx_modal.Instance.OpenWithRandomAddr(crypto.ZEROHASH, func(addr string) build_tx_modal.TxPayload {
transferParams := build_tx_modal.FormatSCInvoke(params, addr)
return build_tx_modal.TxPayload{
Transfer: transferParams,
Expand Down

0 comments on commit 74fc6ab

Please sign in to comment.