diff --git a/rpc/developer.go b/rpc/developer.go index 6c2f7a7..c0d581b 100644 --- a/rpc/developer.go +++ b/rpc/developer.go @@ -3,6 +3,7 @@ package rpc import ( "log" "net/http" + "sync" "github.com/YaleOpenLab/opensolar/messages" @@ -198,25 +199,43 @@ func developerDashboard() { x.ProjectWallets.Wallets = make([][]string, 2) var escrowBalance string + + var wg sync.WaitGroup + if consts.Mainnet { - escrowBalance, err = utils.ToString(xlm.GetAssetBalance(project.EscrowPubkey, consts.AnchorUSDCode)) - if err != nil { - log.Println(err) - erpc.MarshalSend(w, erpc.StatusInternalServerError) - return - } - ret.YourWallet.ProjectWalletBalance += xlm.GetAssetBalance(project.EscrowPubkey, consts.AnchorUSDCode) + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + escrowBalance, err = utils.ToString(xlm.GetAssetBalance(project.EscrowPubkey, consts.AnchorUSDCode)) + if err != nil { + log.Println(err) + } + }(&wg) + + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + ret.YourWallet.ProjectWalletBalance += xlm.GetAssetBalance(project.EscrowPubkey, consts.AnchorUSDCode) + }(&wg) } else { - - escrowBalance, err = utils.ToString(xlm.GetAssetBalance(project.EscrowPubkey, consts.StablecoinCode)) - if err != nil { - log.Println(err) - erpc.MarshalSend(w, erpc.StatusInternalServerError) - return - } - ret.YourWallet.ProjectWalletBalance += xlm.GetAssetBalance(project.EscrowPubkey, consts.StablecoinCode) + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + escrowBalance, err = utils.ToString(xlm.GetAssetBalance(project.EscrowPubkey, consts.StablecoinCode)) + if err != nil { + log.Println(err) + } + }(&wg) + + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + ret.YourWallet.ProjectWalletBalance += xlm.GetAssetBalance(project.EscrowPubkey, consts.StablecoinCode) + }(&wg) } + wg.Wait() + x.ProjectWallets.Wallets[0] = []string{"Project Escrow Wallet: " + project.EscrowPubkey, escrowBalance} x.ProjectWallets.Wallets[1] = []string{"Renewable Energy Certificates (****BBDJL)", "10"} x.PendingPayments = []string{"Your Pending Payment", "$203 due on April 30"} diff --git a/rpc/investors.go b/rpc/investors.go index df97fe6..e64a3c2 100644 --- a/rpc/investors.go +++ b/rpc/investors.go @@ -4,6 +4,7 @@ import ( "errors" "log" "net/http" + "sync" "github.com/YaleOpenLab/opensolar/messages" @@ -473,43 +474,69 @@ func invDashboard() { erpc.ResponseHandler(w, erpc.StatusInternalServerError, messages.TickerError) } - primNativeBalance := xlm.GetNativeBalance(prepInvestor.U.StellarWallet.PublicKey) * xlmUSD - if primNativeBalance < 0 { - primNativeBalance = 0 - } + var primNativeBalance, secNativeBalance, primUsdBalance, secUsdBalance float64 - secNativeBalance := xlm.GetNativeBalance(prepInvestor.U.SecondaryWallet.PublicKey) * xlmUSD - if secNativeBalance < 0 { - secNativeBalance = 0 - } + var wg sync.WaitGroup - if !consts.Mainnet { - primUsdBalance := xlm.GetAssetBalance(prepInvestor.U.StellarWallet.PublicKey, consts.StablecoinCode) - if primUsdBalance < 0 { - primUsdBalance = 0 + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + primNativeBalance = xlm.GetNativeBalance(prepInvestor.U.StellarWallet.PublicKey) * xlmUSD + if primNativeBalance < 0 { + primNativeBalance = 0 } - - secUsdBalance := xlm.GetAssetBalance(prepInvestor.U.SecondaryWallet.PublicKey, consts.StablecoinCode) - if secUsdBalance < 0 { - secUsdBalance = 0 + }(&wg) + + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + secNativeBalance = xlm.GetNativeBalance(prepInvestor.U.SecondaryWallet.PublicKey) * xlmUSD + if secNativeBalance < 0 { + secNativeBalance = 0 } + }(&wg) - ret.AccountBalance1 = primNativeBalance + primUsdBalance - ret.AccountBalance2 = secNativeBalance + secUsdBalance + if !consts.Mainnet { + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + primUsdBalance = xlm.GetAssetBalance(prepInvestor.U.StellarWallet.PublicKey, consts.StablecoinCode) + if primUsdBalance < 0 { + primUsdBalance = 0 + } + }(&wg) + + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + secUsdBalance = xlm.GetAssetBalance(prepInvestor.U.SecondaryWallet.PublicKey, consts.StablecoinCode) + if secUsdBalance < 0 { + secUsdBalance = 0 + } + }(&wg) } else { - primUsdBalance := xlm.GetAssetBalance(prepInvestor.U.StellarWallet.PublicKey, consts.AnchorUSDCode) - if primUsdBalance < 0 { - primUsdBalance = 0 - } - - secUsdBalance := xlm.GetAssetBalance(prepInvestor.U.SecondaryWallet.PublicKey, consts.AnchorUSDCode) - if secUsdBalance < 0 { - secUsdBalance = 0 - } - - ret.AccountBalance1 = primNativeBalance + primUsdBalance - ret.AccountBalance2 = secNativeBalance + secUsdBalance - } + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + primUsdBalance = xlm.GetAssetBalance(prepInvestor.U.StellarWallet.PublicKey, consts.AnchorUSDCode) + if primUsdBalance < 0 { + primUsdBalance = 0 + } + }(&wg) + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + secUsdBalance = xlm.GetAssetBalance(prepInvestor.U.SecondaryWallet.PublicKey, consts.AnchorUSDCode) + if secUsdBalance < 0 { + secUsdBalance = 0 + } + }(&wg) + } + + wg.Wait() + + ret.AccountBalance1 = primNativeBalance + primUsdBalance + ret.AccountBalance2 = secNativeBalance + secUsdBalance if ret.AccountBalance2 < 0 { ret.AccountBalance2 = 0 diff --git a/rpc/recipient.go b/rpc/recipient.go index c904f97..b360996 100644 --- a/rpc/recipient.go +++ b/rpc/recipient.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "strconv" + "sync" "time" "github.com/YaleOpenLab/opensolar/messages" @@ -870,20 +871,38 @@ func recpDashboard() { erpc.ResponseHandler(w, erpc.StatusInternalServerError, messages.TickerError) } - primNativeBalance := xlm.GetNativeBalance(prepRecipient.U.StellarWallet.PublicKey) * xlmUSD - if primNativeBalance < 0 { - primNativeBalance = 0 - } + var wg sync.WaitGroup - secNativeBalance := xlm.GetNativeBalance(prepRecipient.U.SecondaryWallet.PublicKey) * xlmUSD - if secNativeBalance < 0 { - secNativeBalance = 0 - } + var primNativeBalance, secNativeBalance, primUsdBalance float64 - primUsdBalance := xlm.GetAssetBalance(prepRecipient.U.StellarWallet.PublicKey, consts.StablecoinCode) - if primUsdBalance < 0 { - primUsdBalance = 0 - } + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + primNativeBalance = xlm.GetNativeBalance(prepRecipient.U.StellarWallet.PublicKey) * xlmUSD + if primNativeBalance < 0 { + primNativeBalance = 0 + } + }(&wg) + + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + secNativeBalance = xlm.GetNativeBalance(prepRecipient.U.SecondaryWallet.PublicKey) * xlmUSD + if secNativeBalance < 0 { + secNativeBalance = 0 + } + }(&wg) + + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + primUsdBalance = xlm.GetAssetBalance(prepRecipient.U.StellarWallet.PublicKey, consts.StablecoinCode) + if primUsdBalance < 0 { + primUsdBalance = 0 + } + }(&wg) + + wg.Wait() accBal, err := utils.ToString(primUsdBalance + primNativeBalance) if err != nil { diff --git a/teller/teller.go b/teller/teller.go index 5a0afc3..f09ac20 100644 --- a/teller/teller.go +++ b/teller/teller.go @@ -5,6 +5,7 @@ import ( "os" "os/signal" "strings" + "sync" "time" "github.com/chzyer/readline" @@ -249,37 +250,49 @@ func main() { var balance float64 var usdBalance float64 - go func() { + var wg sync.WaitGroup + + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() StartHash, err = getLatestBlockHash() if err != nil { log.Fatal(err) } - }() + }(&wg) - go func() { + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() balance, err = getNativeBalance() if err != nil { log.Fatal(err) } - }() + }(&wg) if consts.Mainnet { - go func() { + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() usdBalance, err = getAssetBalance("USD") if err != nil { log.Fatal(err) } - }() + }(&wg) + } else { - go func() { + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() usdBalance, err = getAssetBalance("STABLEUSD") if err != nil { log.Fatal(err) } - }() + }(&wg) } - time.Sleep(3 * time.Second) + wg.Wait() + colorOutput(MagentaColor, "XLM BALANCE: ", balance) colorOutput(MagentaColor, "USD BALANCE: ", usdBalance) colorOutput(MagentaColor, "START HASH: ", StartHash)