Skip to content
This repository was archived by the owner on Nov 24, 2020. It is now read-only.

Commit b7fdc24

Browse files
authored
general speedups
2 parents 3441cc9 + 1fe5d38 commit b7fdc24

File tree

4 files changed

+145
-67
lines changed

4 files changed

+145
-67
lines changed

rpc/developer.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rpc
33
import (
44
"log"
55
"net/http"
6+
"sync"
67

78
"github.com/YaleOpenLab/opensolar/messages"
89

@@ -198,25 +199,43 @@ func developerDashboard() {
198199
x.ProjectWallets.Wallets = make([][]string, 2)
199200

200201
var escrowBalance string
202+
203+
var wg sync.WaitGroup
204+
201205
if consts.Mainnet {
202-
escrowBalance, err = utils.ToString(xlm.GetAssetBalance(project.EscrowPubkey, consts.AnchorUSDCode))
203-
if err != nil {
204-
log.Println(err)
205-
erpc.MarshalSend(w, erpc.StatusInternalServerError)
206-
return
207-
}
208-
ret.YourWallet.ProjectWalletBalance += xlm.GetAssetBalance(project.EscrowPubkey, consts.AnchorUSDCode)
206+
wg.Add(1)
207+
go func(wg *sync.WaitGroup) {
208+
defer wg.Done()
209+
escrowBalance, err = utils.ToString(xlm.GetAssetBalance(project.EscrowPubkey, consts.AnchorUSDCode))
210+
if err != nil {
211+
log.Println(err)
212+
}
213+
}(&wg)
214+
215+
wg.Add(1)
216+
go func(wg *sync.WaitGroup) {
217+
defer wg.Done()
218+
ret.YourWallet.ProjectWalletBalance += xlm.GetAssetBalance(project.EscrowPubkey, consts.AnchorUSDCode)
219+
}(&wg)
209220
} else {
210-
211-
escrowBalance, err = utils.ToString(xlm.GetAssetBalance(project.EscrowPubkey, consts.StablecoinCode))
212-
if err != nil {
213-
log.Println(err)
214-
erpc.MarshalSend(w, erpc.StatusInternalServerError)
215-
return
216-
}
217-
ret.YourWallet.ProjectWalletBalance += xlm.GetAssetBalance(project.EscrowPubkey, consts.StablecoinCode)
221+
wg.Add(1)
222+
go func(wg *sync.WaitGroup) {
223+
defer wg.Done()
224+
escrowBalance, err = utils.ToString(xlm.GetAssetBalance(project.EscrowPubkey, consts.StablecoinCode))
225+
if err != nil {
226+
log.Println(err)
227+
}
228+
}(&wg)
229+
230+
wg.Add(1)
231+
go func(wg *sync.WaitGroup) {
232+
defer wg.Done()
233+
ret.YourWallet.ProjectWalletBalance += xlm.GetAssetBalance(project.EscrowPubkey, consts.StablecoinCode)
234+
}(&wg)
218235
}
219236

237+
wg.Wait()
238+
220239
x.ProjectWallets.Wallets[0] = []string{"Project Escrow Wallet: " + project.EscrowPubkey, escrowBalance}
221240
x.ProjectWallets.Wallets[1] = []string{"Renewable Energy Certificates (****BBDJL)", "10"}
222241
x.PendingPayments = []string{"Your Pending Payment", "$203 due on April 30"}

rpc/investors.go

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"log"
66
"net/http"
7+
"sync"
78

89
"github.com/YaleOpenLab/opensolar/messages"
910

@@ -473,43 +474,69 @@ func invDashboard() {
473474
erpc.ResponseHandler(w, erpc.StatusInternalServerError, messages.TickerError)
474475
}
475476

476-
primNativeBalance := xlm.GetNativeBalance(prepInvestor.U.StellarWallet.PublicKey) * xlmUSD
477-
if primNativeBalance < 0 {
478-
primNativeBalance = 0
479-
}
477+
var primNativeBalance, secNativeBalance, primUsdBalance, secUsdBalance float64
480478

481-
secNativeBalance := xlm.GetNativeBalance(prepInvestor.U.SecondaryWallet.PublicKey) * xlmUSD
482-
if secNativeBalance < 0 {
483-
secNativeBalance = 0
484-
}
479+
var wg sync.WaitGroup
485480

486-
if !consts.Mainnet {
487-
primUsdBalance := xlm.GetAssetBalance(prepInvestor.U.StellarWallet.PublicKey, consts.StablecoinCode)
488-
if primUsdBalance < 0 {
489-
primUsdBalance = 0
481+
wg.Add(1)
482+
go func(wg *sync.WaitGroup) {
483+
defer wg.Done()
484+
primNativeBalance = xlm.GetNativeBalance(prepInvestor.U.StellarWallet.PublicKey) * xlmUSD
485+
if primNativeBalance < 0 {
486+
primNativeBalance = 0
490487
}
491-
492-
secUsdBalance := xlm.GetAssetBalance(prepInvestor.U.SecondaryWallet.PublicKey, consts.StablecoinCode)
493-
if secUsdBalance < 0 {
494-
secUsdBalance = 0
488+
}(&wg)
489+
490+
wg.Add(1)
491+
go func(wg *sync.WaitGroup) {
492+
defer wg.Done()
493+
secNativeBalance = xlm.GetNativeBalance(prepInvestor.U.SecondaryWallet.PublicKey) * xlmUSD
494+
if secNativeBalance < 0 {
495+
secNativeBalance = 0
495496
}
497+
}(&wg)
496498

497-
ret.AccountBalance1 = primNativeBalance + primUsdBalance
498-
ret.AccountBalance2 = secNativeBalance + secUsdBalance
499+
if !consts.Mainnet {
500+
wg.Add(1)
501+
go func(wg *sync.WaitGroup) {
502+
defer wg.Done()
503+
primUsdBalance = xlm.GetAssetBalance(prepInvestor.U.StellarWallet.PublicKey, consts.StablecoinCode)
504+
if primUsdBalance < 0 {
505+
primUsdBalance = 0
506+
}
507+
}(&wg)
508+
509+
wg.Add(1)
510+
go func(wg *sync.WaitGroup) {
511+
defer wg.Done()
512+
secUsdBalance = xlm.GetAssetBalance(prepInvestor.U.SecondaryWallet.PublicKey, consts.StablecoinCode)
513+
if secUsdBalance < 0 {
514+
secUsdBalance = 0
515+
}
516+
}(&wg)
499517
} else {
500-
primUsdBalance := xlm.GetAssetBalance(prepInvestor.U.StellarWallet.PublicKey, consts.AnchorUSDCode)
501-
if primUsdBalance < 0 {
502-
primUsdBalance = 0
503-
}
504-
505-
secUsdBalance := xlm.GetAssetBalance(prepInvestor.U.SecondaryWallet.PublicKey, consts.AnchorUSDCode)
506-
if secUsdBalance < 0 {
507-
secUsdBalance = 0
508-
}
509-
510-
ret.AccountBalance1 = primNativeBalance + primUsdBalance
511-
ret.AccountBalance2 = secNativeBalance + secUsdBalance
512-
}
518+
wg.Add(1)
519+
go func(wg *sync.WaitGroup) {
520+
defer wg.Done()
521+
primUsdBalance = xlm.GetAssetBalance(prepInvestor.U.StellarWallet.PublicKey, consts.AnchorUSDCode)
522+
if primUsdBalance < 0 {
523+
primUsdBalance = 0
524+
}
525+
}(&wg)
526+
wg.Add(1)
527+
go func(wg *sync.WaitGroup) {
528+
defer wg.Done()
529+
secUsdBalance = xlm.GetAssetBalance(prepInvestor.U.SecondaryWallet.PublicKey, consts.AnchorUSDCode)
530+
if secUsdBalance < 0 {
531+
secUsdBalance = 0
532+
}
533+
}(&wg)
534+
}
535+
536+
wg.Wait()
537+
538+
ret.AccountBalance1 = primNativeBalance + primUsdBalance
539+
ret.AccountBalance2 = secNativeBalance + secUsdBalance
513540

514541
if ret.AccountBalance2 < 0 {
515542
ret.AccountBalance2 = 0

rpc/recipient.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"net/http"
1010
"strconv"
11+
"sync"
1112
"time"
1213

1314
"github.com/YaleOpenLab/opensolar/messages"
@@ -870,20 +871,38 @@ func recpDashboard() {
870871
erpc.ResponseHandler(w, erpc.StatusInternalServerError, messages.TickerError)
871872
}
872873

873-
primNativeBalance := xlm.GetNativeBalance(prepRecipient.U.StellarWallet.PublicKey) * xlmUSD
874-
if primNativeBalance < 0 {
875-
primNativeBalance = 0
876-
}
874+
var wg sync.WaitGroup
877875

878-
secNativeBalance := xlm.GetNativeBalance(prepRecipient.U.SecondaryWallet.PublicKey) * xlmUSD
879-
if secNativeBalance < 0 {
880-
secNativeBalance = 0
881-
}
876+
var primNativeBalance, secNativeBalance, primUsdBalance float64
882877

883-
primUsdBalance := xlm.GetAssetBalance(prepRecipient.U.StellarWallet.PublicKey, consts.StablecoinCode)
884-
if primUsdBalance < 0 {
885-
primUsdBalance = 0
886-
}
878+
wg.Add(1)
879+
go func(wg *sync.WaitGroup) {
880+
defer wg.Done()
881+
primNativeBalance = xlm.GetNativeBalance(prepRecipient.U.StellarWallet.PublicKey) * xlmUSD
882+
if primNativeBalance < 0 {
883+
primNativeBalance = 0
884+
}
885+
}(&wg)
886+
887+
wg.Add(1)
888+
go func(wg *sync.WaitGroup) {
889+
defer wg.Done()
890+
secNativeBalance = xlm.GetNativeBalance(prepRecipient.U.SecondaryWallet.PublicKey) * xlmUSD
891+
if secNativeBalance < 0 {
892+
secNativeBalance = 0
893+
}
894+
}(&wg)
895+
896+
wg.Add(1)
897+
go func(wg *sync.WaitGroup) {
898+
defer wg.Done()
899+
primUsdBalance = xlm.GetAssetBalance(prepRecipient.U.StellarWallet.PublicKey, consts.StablecoinCode)
900+
if primUsdBalance < 0 {
901+
primUsdBalance = 0
902+
}
903+
}(&wg)
904+
905+
wg.Wait()
887906

888907
accBal, err := utils.ToString(primUsdBalance + primNativeBalance)
889908
if err != nil {

teller/teller.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/signal"
77
"strings"
8+
"sync"
89
"time"
910

1011
"github.com/chzyer/readline"
@@ -249,37 +250,49 @@ func main() {
249250
var balance float64
250251
var usdBalance float64
251252

252-
go func() {
253+
var wg sync.WaitGroup
254+
255+
wg.Add(1)
256+
go func(wg *sync.WaitGroup) {
257+
defer wg.Done()
253258
StartHash, err = getLatestBlockHash()
254259
if err != nil {
255260
log.Fatal(err)
256261
}
257-
}()
262+
}(&wg)
258263

259-
go func() {
264+
wg.Add(1)
265+
go func(wg *sync.WaitGroup) {
266+
defer wg.Done()
260267
balance, err = getNativeBalance()
261268
if err != nil {
262269
log.Fatal(err)
263270
}
264-
}()
271+
}(&wg)
265272

266273
if consts.Mainnet {
267-
go func() {
274+
wg.Add(1)
275+
go func(wg *sync.WaitGroup) {
276+
defer wg.Done()
268277
usdBalance, err = getAssetBalance("USD")
269278
if err != nil {
270279
log.Fatal(err)
271280
}
272-
}()
281+
}(&wg)
282+
273283
} else {
274-
go func() {
284+
wg.Add(1)
285+
go func(wg *sync.WaitGroup) {
286+
defer wg.Done()
275287
usdBalance, err = getAssetBalance("STABLEUSD")
276288
if err != nil {
277289
log.Fatal(err)
278290
}
279-
}()
291+
}(&wg)
280292
}
281293

282-
time.Sleep(3 * time.Second)
294+
wg.Wait()
295+
283296
colorOutput(MagentaColor, "XLM BALANCE: ", balance)
284297
colorOutput(MagentaColor, "USD BALANCE: ", usdBalance)
285298
colorOutput(MagentaColor, "START HASH: ", StartHash)

0 commit comments

Comments
 (0)