From e4ebb02b77190a1499168cd040f5bdd43c913927 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 19 Aug 2024 13:36:17 +0300 Subject: [PATCH] Added main template and check balance --- cmd/main.go | 49 ++++++++++++++++++++++++++++++++++ go-vise | 2 +- services/registration/main | 3 +-- services/registration/main.vis | 12 ++++++--- services/registration/main_swa | 2 +- 5 files changed, 61 insertions(+), 7 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 5ae7928..d408f9f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -29,6 +29,7 @@ const ( const ( createAccountURL = "https://custodial.sarafu.africa/api/account/create" trackStatusURL = "https://custodial.sarafu.africa/api/track/" + checkBalanceURL = "https://custodial.sarafu.africa/api/account/status/" ) type accountResponse struct { @@ -53,6 +54,14 @@ type trackStatusResponse struct { } `json:"result"` } +type balanceResponse struct { + Ok bool `json:"ok"` + Result struct { + Balance string `json:"balance"` + Nonce json.Number `json:"nonce"` + } `json:"result"` +} + type fsData struct { path string st *state.State @@ -262,6 +271,45 @@ func (fsd *fsData) quit(ctx context.Context, sym string, input []byte) (resourc return res, nil } +func (fsd *fsData) checkBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { + res := resource.Result{} + + fp := fsd.path + "_data" + + jsonData, err := os.ReadFile(fp) + if err != nil { + return res, err + } + + var accountData map[string]string + err = json.Unmarshal(jsonData, &accountData) + if err != nil { + return res, err + } + + resp, err := http.Get(checkBalanceURL + accountData["PublicKey"]) + if err != nil { + return res, nil + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return res, nil + } + + var balanceResp balanceResponse + err = json.Unmarshal(body, &balanceResp) + if err != nil { + return res, nil + } + + balance := balanceResp.Result.Balance + + res.Content = balance + + return res, nil +} var ( scriptDir = path.Join("services", "registration") @@ -327,6 +375,7 @@ func main() { rfs.AddLocalFunc("check_account_status", fs.check_account_status) rfs.AddLocalFunc("unlock_account", fs.unlock) rfs.AddLocalFunc("quit",fs.quit) + rfs.AddLocalFunc("check_balance", fs.checkBalance) cont, err := en.Init(ctx) if err != nil { diff --git a/go-vise b/go-vise index ef9a4c3..1f47a67 160000 --- a/go-vise +++ b/go-vise @@ -1 +1 @@ -Subproject commit ef9a4c3073e7e878846693d4aa7fd0cf1cc977a7 +Subproject commit 1f47a674d95380be8c387f410f0342eb72357df5 diff --git a/services/registration/main b/services/registration/main index 5c1d929..bf15ea5 100644 --- a/services/registration/main +++ b/services/registration/main @@ -1,2 +1 @@ -Balance: 0.00 SRF - +Balance: {{.check_balance}} diff --git a/services/registration/main.vis b/services/registration/main.vis index 477e80a..ae46d5e 100644 --- a/services/registration/main.vis +++ b/services/registration/main.vis @@ -1,9 +1,15 @@ -LOAD check_identifier 64 -RELOAD check_identifier -MAP check_identifier +LOAD check_balance 64 +RELOAD check_balance +MAP check_balance MOUT send 1 MOUT vouchers 2 MOUT account 3 MOUT help 4 +MOUT quit 9 HALT +INCMP send 1 +INCMP vouchers 2 INCMP my_account 3 +INCMP help 4 +INCMP quit 9 +INCMP . * diff --git a/services/registration/main_swa b/services/registration/main_swa index 19eb750..b72abf0 100644 --- a/services/registration/main_swa +++ b/services/registration/main_swa @@ -1 +1 @@ -Salio: 0.00 SRF +Salio: {{.check_balance}}