Skip to content

Commit

Permalink
Update Ethereum balance updating via Ethplorer
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Jan 17, 2018
1 parent 5dc6df7 commit d571773
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 53 deletions.
20 changes: 8 additions & 12 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,18 +474,14 @@ func TestSendHTTPRequest(t *testing.T) {

func TestSendHTTPGetRequest(t *testing.T) {
type test struct {
Status int `json:"status"`
Data []struct {
Address string `json:"address"`
Balance float64 `json:"balance"`
Nonce interface{} `json:"nonce"`
Code string `json:"code"`
Name interface{} `json:"name"`
Storage interface{} `json:"storage"`
FirstSeen interface{} `json:"firstSeen"`
} `json:"data"`
}
url := `https://etherchain.org/api/account/multiple/0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe`
Address string `json:"address"`
ETH struct {
Balance int `json:"balance"`
TotalIn int `json:"totalIn"`
TotalOut int `json:"totalOut"`
} `json:"ETH"`
}
url := `https://api.ethplorer.io/getAddressInfo/0xff71cb760666ab06aa73f34995b42dd4b85ea07b?apiKey=freekey`
result := test{}

err := SendHTTPGetRequest(url, true, false, &result)
Expand Down
91 changes: 66 additions & 25 deletions portfolio/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const (

etherchainAPIURL = "https://etherchain.org/api"
etherchainAccountMultiple = "account/multiple"

ethplorerAPIURL = "https://api.ethplorer.io"
ethplorerAddressInfo = "getAddressInfo"

// PortfolioAddressExchange is a label for an exchange address
PortfolioAddressExchange = "Exchange"
// PortfolioAddressPersonal is a label for a personal/offline address
Expand Down Expand Up @@ -51,6 +55,47 @@ type EtherchainBalanceResponse struct {
} `json:"data"`
}

// EthplorerResponse holds JSON address data for Ethplorer
type EthplorerResponse struct {
Address string `json:"address"`
ETH struct {
Balance float64 `json:"balance"`
TotalIn float64 `json:"totalIn"`
TotalOut float64 `json:"totalOut"`
} `json:"ETH"`
CountTxs int `json:"countTxs"`
ContractInfo struct {
CreatorAddress string `json:"creatorAddress"`
TransactionHash string `json:"transactionHash"`
Timestamp int `json:"timestamp"`
} `json:"contractInfo"`
TokenInfo struct {
Address string `json:"address"`
Name string `json:"name"`
Decimals int `json:"decimals"`
Symbol string `json:"symbol"`
TotalSupply string `json:"totalSupply"`
Owner string `json:"owner"`
LastUpdated int `json:"lastUpdated"`
TotalIn int64 `json:"totalIn"`
TotalOut int64 `json:"totalOut"`
IssuancesCount int `json:"issuancesCount"`
HoldersCount int `json:"holdersCount"`
Image string `json:"image"`
Description string `json:"description"`
Price struct {
Rate int `json:"rate"`
Diff int `json:"diff"`
Ts int `json:"ts"`
Currency string `json:"currency"`
} `json:"price"`
} `json:"tokenInfo"`
Error struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error"`
}

// ExchangeAccountInfo : Generic type to hold each exchange's holdings in all
// enabled currencies
type ExchangeAccountInfo struct {
Expand All @@ -67,26 +112,20 @@ type ExchangeAccountCurrencyInfo struct {

// GetEthereumBalance single or multiple address information as
// EtherchainBalanceResponse
func GetEthereumBalance(address []string) (EtherchainBalanceResponse, error) {
for _, add := range address {
valid, _ := common.IsValidCryptoAddress(add, "eth")
if !valid {
return EtherchainBalanceResponse{}, errors.New("Not an ethereum address")
}
func GetEthereumBalance(address string) (EthplorerResponse, error) {
valid, _ := common.IsValidCryptoAddress(address, "eth")
if !valid {
return EthplorerResponse{}, errors.New("Not an ethereum address")
}

addresses := common.JoinStrings(address, ",")
url := fmt.Sprintf(
"%s/%s/%s", etherchainAPIURL, etherchainAccountMultiple, addresses,
"%s/%s/%s?apiKey=freekey", ethplorerAPIURL, ethplorerAddressInfo, address,
)
result := EtherchainBalanceResponse{}
result := EthplorerResponse{}
err := common.SendHTTPGetRequest(url, true, false, &result)
if err != nil {
return result, err
}
if result.Status != 1 {
return result, errors.New("Status was not 1")
}
return result, nil
}

Expand Down Expand Up @@ -230,14 +269,23 @@ func (p *Base) UpdatePortfolio(addresses []string, coinType string) bool {
return true
}

errors := 0
if coinType == "ETH" {
result, err := GetEthereumBalance(addresses)
if err != nil {
return false
}
for x := range addresses {
result, err := GetEthereumBalance(addresses[x])
if err != nil {
errors++
continue
}

for _, x := range result.Data {
p.AddAddress(x.Address, coinType, PortfolioAddressPersonal, x.Balance)
if result.Error.Message != "" {
errors++
continue
}
p.AddAddress(addresses[x], coinType, PortfolioAddressPersonal, result.ETH.Balance)
}
if errors > 0 {
return false
}
return true
}
Expand Down Expand Up @@ -354,10 +402,6 @@ func (p *Base) GetPortfolioSummary() Summary {
totalCoins := make(map[string]float64)

for x, y := range personalHoldings {
if x == "ETH" {
y = y / common.WeiPerEther
personalHoldings[x] = y
}
totalCoins[x] = y
}

Expand Down Expand Up @@ -424,9 +468,6 @@ func (p *Base) GetPortfolioSummary() Summary {
offlineSummary := make(map[string][]OfflineCoinSummary)
for _, x := range p.Addresses {
if x.Description != PortfolioAddressExchange {
if x.CoinType == "ETH" {
x.Balance = x.Balance / common.WeiPerEther
}
coinSummary := OfflineCoinSummary{
Address: x.Address,
Balance: x.Balance,
Expand Down
25 changes: 9 additions & 16 deletions portfolio/portfolio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,22 @@ import (
)

func TestGetEthereumBalance(t *testing.T) {
addresses := []string{"0xb794f5ea0ba39494ce839613fffba74279579268",
"0xe853c56864a2ebe4576a807d26fdc4a0ada51919"}
nonsenseAddress := []string{
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"0xe853c56864a2ebe4576a807d26fdc4a0ada51919",
}
address := "0xb794f5ea0ba39494ce839613fffba74279579268"
nonsenseAddress := "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

response, err := GetEthereumBalance(addresses)
response, err := GetEthereumBalance(address)
if err != nil {
t.Errorf("Test Failed - Portfolio GetEthereumBalance() Error: %s", err)
}
if len(response.Data) != 2 {
t.Error(
"Test Failed - Portfolio GetEthereumBalance() Error: Incorrect address",
)

if response.Address != "0xb794f5ea0ba39494ce839613fffba74279579268" {
t.Error("Test Failed - Portfolio GetEthereumBalance() address invalid")
}

response, err = GetEthereumBalance(nonsenseAddress)
if err == nil {
t.Error("Test Failed - Portfolio GetEthereumBalance()")
}
if len(response.Data) != 0 {
t.Error("Test Failed - Portfolio GetEthereumBalance() error")
if response.Error.Message != "" {
t.Errorf("Test Failed - Portfolio GetEthereumBalance() Error: %s",
response.Error.Message)
}
}

Expand Down

0 comments on commit d571773

Please sign in to comment.