Skip to content

Commit

Permalink
Merge pull request #76 from getamis/develop
Browse files Browse the repository at this point in the history
Release v0.3.0
  • Loading branch information
changwu-tw committed Aug 8, 2018
2 parents 925672c + f7a59a4 commit 14d4e9d
Show file tree
Hide file tree
Showing 197 changed files with 518 additions and 173,388 deletions.
100 changes: 3 additions & 97 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
branch = "feature/indexer_diff_1.8.10"

[[constraint]]
branch = "develop"
name = "github.com/getamis/sirius"
version = "=1.0.0"

[[constraint]]
name = "github.com/jinzhu/gorm"
Expand All @@ -49,6 +49,10 @@
name = "github.com/stretchr/testify"
version = "1.2.1"

[[constraint]]
name = "github.com/golang/protobuf"
version = "=1.1.0"

[[override]]
branch = "master"
name = "github.com/docker/libnetwork"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ first time to run indexer you need to create the database schema
```shell
$ mkdir -p ~/indexer-data/mysql ~/indexer-data/geth
# Create database sechema
MYSQL_DATA_PATH="$HOME/indexer-data/mysql" docker-compose up ws-database ws-migration
# press Ctrl + C when see `eth-indexer_ws-migration_1 exited with code 0`
MYSQL_DATA_PATH="$HOME/indexer-data/mysql" docker-compose up idx-database idx-migration
# press Ctrl + C when see `eth-indexer_idx-migration_1 exited with code 0`
```

then use `docker-compose up` with environment variables to start indexer:
Expand Down
38 changes: 11 additions & 27 deletions client/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ const (
// Balancer is a wrapper interface to batch get balances
type Balancer interface {
// BalanceOf returns the balances of ETH and multiple erc20 tokens for multiple accounts
BalanceOf(context.Context, *big.Int, map[ethCommon.Address]map[ethCommon.Address]struct{}) (map[ethCommon.Address]map[ethCommon.Address]*big.Int, error)
BalanceOf(context.Context, *big.Int, map[ethCommon.Address]map[ethCommon.Address]*big.Int) error
}

// BalanceOf returns the balances of ETH and multiple erc20 tokens for multiple accounts
func (c *client) BalanceOf(ctx context.Context, blockNumber *big.Int, addrs map[ethCommon.Address]map[ethCommon.Address]struct{}) (balances map[ethCommon.Address]map[ethCommon.Address]*big.Int, err error) {
func (c *client) BalanceOf(ctx context.Context, blockNumber *big.Int, balances map[ethCommon.Address]map[ethCommon.Address]*big.Int) (err error) {
logger := log.New("number", blockNumber.Int64())

var msgs []*ethereum.CallMsg
var owners []ethCommon.Address
// Only handle non-ETH balances
for erc20Addr, list := range addrs {
for erc20Addr, list := range balances {
if erc20Addr == model.ETHAddress {
continue
}
Expand All @@ -56,8 +56,6 @@ func (c *client) BalanceOf(ctx context.Context, blockNumber *big.Int, addrs map[
}
}

balances = make(map[ethCommon.Address]map[ethCommon.Address]*big.Int)

// Get batch results
lens := len(msgs)
for begin := 0; begin < lens; begin += chunkSize {
Expand All @@ -70,46 +68,32 @@ func (c *client) BalanceOf(ctx context.Context, blockNumber *big.Int, addrs map[
logger.Info("processing ERC20 balance chunk", "total", lens, "begin", begin, "end", end)
outputs, err := c.BatchCallContract(ctx, chunk, blockNumber)
if err != nil {
return nil, err
return err
}

for i := 0; i < len(chunk); i++ {
balance, err := contracts.DecodeBalanceOf(outputs[i])
if err != nil {
return nil, err
return err
}

contractAddr := *chunk[i].To
if balances[contractAddr] == nil {
balances[contractAddr] = make(map[ethCommon.Address]*big.Int)
}
balances[contractAddr][owners[begin+i]] = balance
}
}

// Handle ETH balances
if _, ok := addrs[model.ETHAddress]; ok {
balances[model.ETHAddress], err = c.ethBalanceOf(ctx, blockNumber, addrs[model.ETHAddress])
if err != nil {
return nil, err
}
if _, ok := balances[model.ETHAddress]; !ok {
return
}
return
}

// ethBalanceOf returns the ether balances
func (c *client) ethBalanceOf(ctx context.Context, blockNumber *big.Int, addrs map[ethCommon.Address]struct{}) (etherBalances map[ethCommon.Address]*big.Int, err error) {
logger := log.New("number", blockNumber.Int64())
lens := len(addrs)
var addrList []ethCommon.Address
for addr := range addrs {
for addr := range balances[model.ETHAddress] {
addrList = append(addrList, addr)
}

// Construct ether balances
etherBalances = make(map[ethCommon.Address]*big.Int, lens)

// Get ethers
lens = len(addrList)
for begin := 0; begin < lens; begin += chunkSize {
end := begin + chunkSize
if end > lens {
Expand All @@ -120,11 +104,11 @@ func (c *client) ethBalanceOf(ctx context.Context, blockNumber *big.Int, addrs m
logger.Info("processing ETH balance chunk", "total", lens, "begin", begin, "end", end)
ethers, err := c.BatchBalanceAt(ctx, chunk, blockNumber)
if err != nil {
return nil, err
return err
}

for i, e := range ethers {
etherBalances[addrList[begin+i]] = e
balances[model.ETHAddress][addrList[begin+i]] = e
}
}
return
Expand Down
41 changes: 23 additions & 18 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,32 @@ func (c *client) GetERC20(ctx context.Context, addr common.Address) (*model.ERC2
}
caller, err := contracts.NewERC20TokenCaller(addr, c)
if err != nil {
logger.Warn("Failed to initiate contract caller", "err", err)
logger.Error("Failed to initiate contract caller", "err", err)
return nil, err
}

// Set total supply
supply, err := caller.TotalSupply(&bind.CallOpts{})
if err != nil {
logger.Error("Failed to get total supply", "err", err)
return nil, err
}
erc20.TotalSupply = supply.String()

// Get optional methods
// Set decimals
decimal, err := caller.Decimals(&bind.CallOpts{})
if err != nil {
logger.Warn("Failed to get decimals", "err", err)
} else {
// Set decimals
decimal, err := caller.Decimals(&bind.CallOpts{})
if err != nil {
logger.Warn("Failed to get decimals", "err", err)
}
erc20.Decimals = int(decimal)
}

// Set total supply
supply, err := caller.TotalSupply(&bind.CallOpts{})
if err != nil {
logger.Warn("Failed to get total supply", "err", err)
}
erc20.TotalSupply = supply.String()

// Set name
name, err := caller.Name(&bind.CallOpts{})
if err != nil {
logger.Warn("Failed to get name", "err", err)
}
// Set name
name, err := caller.Name(&bind.CallOpts{})
if err != nil {
logger.Warn("Failed to get name", "err", err)
} else {
erc20.Name = name
}
return erc20, nil
Expand Down
Loading

0 comments on commit 14d4e9d

Please sign in to comment.