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

Commit

Permalink
general speedups
Browse files Browse the repository at this point in the history
  • Loading branch information
Varunram authored May 7, 2020
2 parents 3441cc9 + 1fe5d38 commit b7fdc24
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 67 deletions.
49 changes: 34 additions & 15 deletions rpc/developer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpc
import (
"log"
"net/http"
"sync"

"github.com/YaleOpenLab/opensolar/messages"

Expand Down Expand Up @@ -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"}
Expand Down
89 changes: 58 additions & 31 deletions rpc/investors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"log"
"net/http"
"sync"

"github.com/YaleOpenLab/opensolar/messages"

Expand Down Expand Up @@ -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
Expand Down
43 changes: 31 additions & 12 deletions rpc/recipient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/http"
"strconv"
"sync"
"time"

"github.com/YaleOpenLab/opensolar/messages"
Expand Down Expand Up @@ -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 {
Expand Down
31 changes: 22 additions & 9 deletions teller/teller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/signal"
"strings"
"sync"
"time"

"github.com/chzyer/readline"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b7fdc24

Please sign in to comment.