Added main template and check balance

This commit is contained in:
Alfred Kamanda 2024-08-19 13:36:17 +03:00
parent d76063261a
commit e4ebb02b77
5 changed files with 61 additions and 7 deletions

View File

@ -29,6 +29,7 @@ const (
const ( const (
createAccountURL = "https://custodial.sarafu.africa/api/account/create" createAccountURL = "https://custodial.sarafu.africa/api/account/create"
trackStatusURL = "https://custodial.sarafu.africa/api/track/" trackStatusURL = "https://custodial.sarafu.africa/api/track/"
checkBalanceURL = "https://custodial.sarafu.africa/api/account/status/"
) )
type accountResponse struct { type accountResponse struct {
@ -53,6 +54,14 @@ type trackStatusResponse struct {
} `json:"result"` } `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 { type fsData struct {
path string path string
st *state.State st *state.State
@ -262,6 +271,45 @@ func (fsd *fsData) quit(ctx context.Context, sym string, input []byte) (resourc
return res, nil 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 ( var (
scriptDir = path.Join("services", "registration") scriptDir = path.Join("services", "registration")
@ -327,6 +375,7 @@ func main() {
rfs.AddLocalFunc("check_account_status", fs.check_account_status) rfs.AddLocalFunc("check_account_status", fs.check_account_status)
rfs.AddLocalFunc("unlock_account", fs.unlock) rfs.AddLocalFunc("unlock_account", fs.unlock)
rfs.AddLocalFunc("quit",fs.quit) rfs.AddLocalFunc("quit",fs.quit)
rfs.AddLocalFunc("check_balance", fs.checkBalance)
cont, err := en.Init(ctx) cont, err := en.Init(ctx)
if err != nil { if err != nil {

@ -1 +1 @@
Subproject commit ef9a4c3073e7e878846693d4aa7fd0cf1cc977a7 Subproject commit 1f47a674d95380be8c387f410f0342eb72357df5

View File

@ -1,2 +1 @@
Balance: 0.00 SRF Balance: {{.check_balance}}

View File

@ -1,9 +1,15 @@
LOAD check_identifier 64 LOAD check_balance 64
RELOAD check_identifier RELOAD check_balance
MAP check_identifier MAP check_balance
MOUT send 1 MOUT send 1
MOUT vouchers 2 MOUT vouchers 2
MOUT account 3 MOUT account 3
MOUT help 4 MOUT help 4
MOUT quit 9
HALT HALT
INCMP send 1
INCMP vouchers 2
INCMP my_account 3 INCMP my_account 3
INCMP help 4
INCMP quit 9
INCMP . *

View File

@ -1 +1 @@
Salio: 0.00 SRF Salio: {{.check_balance}}