Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebView implementation #51

Merged
merged 1 commit into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions app/dcrdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
walletrpc "decred.org/dcrwallet/rpc/walletrpc"
"github.com/go-resty/resty/v2"
proto "github.com/golang/protobuf/proto"
gui "github.com/peterzen/kohola/walletgui"
"github.com/zserge/lorca"
"github.com/peterzen/kohola/walletgui"
)

const baseURL = "https://explorer.dcrdata.org/api"
Expand All @@ -28,15 +27,15 @@ func queryDcrdata(path string) ([]byte, error) {
return resp.Body(), nil
}

func fetchStakeDiffHistory(startBlock uint32, endBlock uint32) (history *gui.StakeDiffHistory, err error) {
func fetchStakeDiffHistory(startBlock uint32, endBlock uint32) (history *walletgui.StakeDiffHistory, err error) {

apiPath := fmt.Sprintf("/stake/diff/r/%d/%d", startBlock, endBlock)
jsonResponse, err := queryDcrdata(apiPath)
if err != nil {
return nil, err
}

history = &gui.StakeDiffHistory{
history = &walletgui.StakeDiffHistory{
StartBlock: startBlock,
EndBlock: endBlock,
}
Expand All @@ -51,8 +50,8 @@ func fetchStakeDiffHistory(startBlock uint32, endBlock uint32) (history *gui.Sta
}

// ExportDcrdataAPI exports functions to the UI
func ExportDcrdataAPI(ui lorca.UI) {
ui.Bind("walletgui__FetchStakeDiffHistory", func(startTimestamp int64, endTimestamp int64) (r gui.LorcaMessage) {
func ExportDcrdataAPI(ui walletgui.WebViewInterface) {
ui.Bind("walletgui__FetchStakeDiffHistory", func(startTimestamp int64, endTimestamp int64) (r walletgui.LorcaMessage) {

request := &walletrpc.BestBlockRequest{}
response, err := walletServiceClient.BestBlock(ctx, request)
Expand Down
36 changes: 17 additions & 19 deletions app/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ import (

walletrpc "decred.org/dcrwallet/rpc/walletrpc"
proto "github.com/golang/protobuf/proto"
gui "github.com/peterzen/kohola/walletgui"

"github.com/zserge/lorca"
"github.com/peterzen/kohola/walletgui"
)

var (
gRPCConnection *grpc.ClientConn = nil
currentEndpoint *gui.GRPCEndpoint = nil
gRPCConnection *grpc.ClientConn = nil
currentEndpoint *walletgui.GRPCEndpoint = nil

walletServiceClient walletrpc.WalletServiceClient
votingServiceClient walletrpc.VotingServiceClient
Expand All @@ -42,7 +40,7 @@ func WalletAPIInit() {
}

// connectWallet creates a gRPC client connection
func connectWallet(endpointCfg *gui.GRPCEndpoint) error {
func connectWallet(endpointCfg *walletgui.GRPCEndpoint) error {

var err error = nil
gRPCConnection, err = newGRPCClient(endpointCfg)
Expand Down Expand Up @@ -75,7 +73,7 @@ func connectWallet(endpointCfg *gui.GRPCEndpoint) error {
}

// NewGRPCClient connects to the wallet gRPC server
func newGRPCClient(endpointCfg *gui.GRPCEndpoint) (*grpc.ClientConn, error) {
func newGRPCClient(endpointCfg *walletgui.GRPCEndpoint) (*grpc.ClientConn, error) {
// fmt.Printf("%#v", endpointCfg)
var creds credentials.TransportCredentials = nil
var err error = nil
Expand All @@ -95,7 +93,7 @@ func newGRPCClient(endpointCfg *gui.GRPCEndpoint) (*grpc.ClientConn, error) {
return conn, nil
}

func subscribeTxNotifications(ui lorca.UI) {
func subscribeTxNotifications(ui walletgui.WebViewInterface) {
request := &walletrpc.TransactionNotificationsRequest{}
ntfnStream, err := walletServiceClient.TransactionNotifications(ctx, request)
if err != nil {
Expand Down Expand Up @@ -125,11 +123,11 @@ func subscribeTxNotifications(ui lorca.UI) {
}
encodedMsg := hex.EncodeToString(b)
js := fmt.Sprintf("window.lorcareceiver__OnTxNotification('%s')", encodedMsg)
ui.Eval(js)
walletgui.ExecuteJS(js)
}
}

// func subscribeConfirmNotifications(ui lorca.UI) {
// func subscribeConfirmNotifications(ui walletgui.WebViewInterface) {
// request := &walletrpc.ConfirmationNotificationsRequest{}
// ntfnStream, err := walletServiceClient.ConfirmationNotifications(ctx, request)
// if err != nil {
Expand All @@ -152,7 +150,7 @@ func subscribeTxNotifications(ui lorca.UI) {
// }
// }

func subscribeAccountNotifications(ui lorca.UI) {
func subscribeAccountNotifications(ui walletgui.WebViewInterface) {

request := &walletrpc.AccountNotificationsRequest{}
ntfnStream, err := walletServiceClient.AccountNotifications(ctx, request)
Expand All @@ -173,12 +171,12 @@ func subscribeAccountNotifications(ui lorca.UI) {
}
encodedMsg := hex.EncodeToString(b)
js := fmt.Sprintf("window.lorcareceiver__OnAccountNotification('%s')", encodedMsg)
ui.Eval(js)
walletgui.ExecuteJS(js)
}
}

// SetupNotifications creates subscriptions
func subscribeNotifications(ui lorca.UI) {
func subscribeNotifications(ui walletgui.WebViewInterface) {
go subscribeTxNotifications(ui)
go subscribeAccountNotifications(ui)
// go subscribeConfirmNotifications(ui)
Expand All @@ -195,13 +193,13 @@ func disconnectEndpoint() {
gRPCConnection = nil
}

func connectEndpoint(endpointID string, ui lorca.UI) (endpoint *gui.GRPCEndpoint, err error) {
func connectEndpoint(endpointID string, ui walletgui.WebViewInterface) (endpoint *walletgui.GRPCEndpoint, err error) {

if isEndpointConnected() {
disconnectEndpoint()
}

endpoints := gui.GetConfig().GetWalletEndpoints()
endpoints := walletgui.GetConfig().GetWalletEndpoints()
endpoint = getEndpointByID(endpoints, endpointID)

if endpoint == nil {
Expand Down Expand Up @@ -254,7 +252,7 @@ func getNetwork() (*walletrpc.NetworkResponse, error) {
return response, nil
}

func checkGRPCConnection(endpointCfg *gui.GRPCEndpoint) (bool, error) {
func checkGRPCConnection(endpointCfg *walletgui.GRPCEndpoint) (bool, error) {
gRPCConnection, err := newGRPCClient(endpointCfg)
if err != nil {
return false, err
Expand Down Expand Up @@ -312,7 +310,7 @@ func monitorEndpoint(ctx context.Context, cc walletrpc.WalletServiceClient) {
}
}

func getEndpointByID(endpoints []*gui.GRPCEndpoint, id string) *gui.GRPCEndpoint {
func getEndpointByID(endpoints []*walletgui.GRPCEndpoint, id string) *walletgui.GRPCEndpoint {
for _, endpoint := range endpoints {
if endpoint.Id == id {
return endpoint
Expand All @@ -327,8 +325,8 @@ func getEndpointByID(endpoints []*gui.GRPCEndpoint, id string) *gui.GRPCEndpoint

type apiInterface func(context.Context, *struct{}, ...grpc.CallOption) (proto.Message, error)

func makeAPIEndpoint(requestClass *struct{}, method apiInterface) func() gui.LorcaMessage {
return func() (r gui.LorcaMessage) {
func makeAPIEndpoint(requestClass *struct{}, method apiInterface) func() walletgui.LorcaMessage {
return func() (r walletgui.LorcaMessage) {
request := requestClass
response, err := method(ctx, request)
if err != nil {
Expand Down
66 changes: 13 additions & 53 deletions app/exchangeratebot/coingecko.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"time"

proto "github.com/golang/protobuf/proto"
gui "github.com/peterzen/kohola/walletgui"
coingecko "github.com/superoo7/go-gecko/v3"
"github.com/zserge/lorca"
"github.com/peterzen/kohola/walletgui"
)

var cg *coingecko.Client
Expand All @@ -38,7 +37,7 @@ func initializeCgClient() *coingecko.Client {
return coingecko.NewClient(httpClient)
}

func fetchCurrentRates(altCurrencies []string) (rates *gui.AltCurrencyRates, err error) {
func fetchCurrentRates(altCurrencies []string) (rates *walletgui.AltCurrencyRates, err error) {

ids := []string{"decred"}
sp, err := cg.SimplePrice(ids, altCurrencies)
Expand All @@ -48,11 +47,11 @@ func fetchCurrentRates(altCurrencies []string) (rates *gui.AltCurrencyRates, err
}
decred := (*sp)["decred"]

rates = &gui.AltCurrencyRates{
Rates: make([]*gui.AltCurrencyRates_AltCurrencyRate, len(altCurrencies)),
rates = &walletgui.AltCurrencyRates{
Rates: make([]*walletgui.AltCurrencyRates_AltCurrencyRate, len(altCurrencies)),
}
for i, currency := range altCurrencies {
r := &gui.AltCurrencyRates_AltCurrencyRate{
r := &walletgui.AltCurrencyRates_AltCurrencyRate{
CurrencyCode: currency,
CurrentRate: decred[currency],
}
Expand All @@ -61,25 +60,24 @@ func fetchCurrentRates(altCurrencies []string) (rates *gui.AltCurrencyRates, err
return rates, nil
}

func fetchMarketChart(request *gui.GetMarketChartRequest) (marketChartData *gui.GetMarketChartResponse, err error) {
func fetchMarketChart(request *walletgui.GetMarketChartRequest) (marketChartData *walletgui.GetMarketChartResponse, err error) {

if cg == nil {
return nil, errors.New("fetchMarketChart: not initialized yet")
}

response, err := cg.CoinsIDMarketChart("decred", request.CurrencyCode, fmt.Sprint(request.Days))

if err != nil {
fmt.Printf("fetchMarketChart: %s", err)
return nil, err
}

marketChartData = &gui.GetMarketChartResponse{
Datapoints: make([]*gui.MarketChartDataPoint, len(*response.Prices)),
marketChartData = &walletgui.GetMarketChartResponse{
Datapoints: make([]*walletgui.MarketChartDataPoint, len(*response.Prices)),
}

for i, v := range *response.Prices {
marketChartData.Datapoints[i] = &gui.MarketChartDataPoint{
marketChartData.Datapoints[i] = &walletgui.MarketChartDataPoint{
Timestamp: int64(v[0]) / 1000,
ExchangeRate: v[1],
}
Expand All @@ -89,10 +87,10 @@ func fetchMarketChart(request *gui.GetMarketChartRequest) (marketChartData *gui.
}

// ExportExchangeRateAPI exports the exchangerateBot API functions to the UI
func ExportExchangeRateAPI(ui lorca.UI) {
ui.Bind("exchangerate__GetMarketChart", func(currencyCode string, days uint32) (r gui.LorcaMessage) {
func ExportExchangeRateAPI(ui walletgui.WebViewInterface) {
ui.Bind("exchangerate__GetMarketChart", func(currencyCode string, days uint32) (r walletgui.LorcaMessage) {

request := &gui.GetMarketChartRequest{
request := &walletgui.GetMarketChartRequest{
Days: days,
CurrencyCode: currencyCode,
}
Expand All @@ -108,7 +106,7 @@ func ExportExchangeRateAPI(ui lorca.UI) {
}

// Start initializes a Coingecko client and starts a periodic fetcher process
func Start(altCurrencies []string, onUpdate func(rates *gui.AltCurrencyRates)) {
func Start(altCurrencies []string, onUpdate func(rates *walletgui.AltCurrencyRates)) {

cg = initializeCgClient()

Expand All @@ -121,41 +119,3 @@ func Start(altCurrencies []string, onUpdate func(rates *gui.AltCurrencyRates)) {
time.Sleep(60 * time.Second)
}
}

/*
func main() {
cg := gecko.NewClient(nil)

// simpleprice
fmt.Println("Simpleprice-----")
ids := []string{"decred"}
vc := []string{"btc", "usd", "eur"}
sp, err := cg.SimplePrice(ids, vc)
if err != nil {
log.Fatal(err)
}
decred := (*sp)["decred"]
fmt.Println(fmt.Sprintf("Decred is worth %f (btc) %f usd (eur %f)", decred["btc"], decred["usd"], decred["eur"]))

m, err := cg.CoinsIDMarketChart("decred", "usd", "7")
fmt.Println("Marketchart")

fmt.Printf("Prices\n")
for _, v := range *m.Prices {
fmt.Printf("%s -> %.04f\n", time.Unix(int64(v[0])/1000, int64(v[0])%1000).UTC().Format(time.RFC3339), v[1])
}

fmt.Printf("MarketCaps\n")
for _, v := range *m.MarketCaps {
fmt.Printf("%s:%.04f\n", time.Unix(int64(v[0])/1000, int64(v[0])%1000).UTC().Format(time.RFC3339), v[1])
}

fmt.Printf("TotalVolumes\n")
for _, v := range *m.TotalVolumes {
fmt.Printf("%s -> %.04f\n", time.Unix(int64(v[0])/1000, int64(v[0])%1000).UTC().Format(time.RFC3339), v[1])
}

os.Exit(0)

}
*/
2 changes: 1 addition & 1 deletion app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/markbates/pkger v0.15.0
github.com/sqweek/dialog v0.0.0-20190728103509-6254ed5b0d3c
github.com/superoo7/go-gecko v1.0.0
github.com/zserge/lorca v0.1.9
github.com/zserge/webview v0.0.0-20200410160118-50e4bcc420ab
google.golang.org/grpc v1.28.0
)

Expand Down
4 changes: 2 additions & 2 deletions app/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/superoo7/go-gecko v1.0.0 h1:Xa1hZu2AYSA20eVMEd4etY0fcJoEI5deja1mdRmqlpI=
github.com/superoo7/go-gecko v1.0.0/go.mod h1:6AMYHL2wP2EN8AB9msPM76Lbo8L/MQOknYjvak5coaY=
github.com/zserge/lorca v0.1.9 h1:vbDdkqdp2/rmeg8GlyCewY2X8Z+b0s7BqWyIQL/gakc=
github.com/zserge/lorca v0.1.9/go.mod h1:bVmnIbIRlOcoV285KIRSe4bUABKi7R7384Ycuum6e4A=
github.com/zserge/webview v0.0.0-20200410160118-50e4bcc420ab h1:oN9z1ICUvaapBWxfrsjnVE2P594yMWwi6vgH7Umzq3s=
github.com/zserge/webview v0.0.0-20200410160118-50e4bcc420ab/go.mod h1:qxc/5N3SOFrs3q+EAVHqaJ1oLbm+hHlDhfhRYg8x7wQ=
go.etcd.io/bbolt v1.3.4 h1:hi1bXHMVrlQh6WwxAy+qZCV/SYIlqo+Ushwdpa4tAKg=
go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
Expand Down
Loading