Skip to content

Commit

Permalink
add: Batchcall to optimize token balance query performances
Browse files Browse the repository at this point in the history
  • Loading branch information
Himitsuko committed Aug 26, 2022
1 parent e31ec0c commit 1907982
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 24 deletions.
121 changes: 100 additions & 21 deletions core/balance_scanner.go

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

47 changes: 44 additions & 3 deletions core/balance_scanner_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,61 @@
package core

import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)

func TestBalanceScanner(t *testing.T) {
client, _ := ethclient.Dial("https://bsc-dataseed1.ninicoin.io/")
bep20, _ := NewBalanceScanner(BinanceChain, client)
bep20, _ := NewBalanceScanner(BinanceChain, client, nil)
balances, _ := bep20.GetBalances(
&bind.CallOpts{},
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"), []common.Address{
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
})
assert.Equal(t, balances[0].String(), "34000000000000000000")
}

func TestBalanceScanner_TokenMapBalances(t *testing.T) {
client, _ := ethclient.Dial("https://bsc-dataseed1.ninicoin.io/")
bep20, _ := NewBalanceScanner(BinanceChain, client, nil)
balances, _ := bep20.TokenMapBalances(
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"), []common.Address{
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
})
assert.Equal(t, balances[strings.ToLower("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74")].String(), "34000000000000000000")
}

func TestBalanceScanner_BatchCall(t *testing.T) {
client, _ := ethclient.Dial("https://bsc-dataseed.binance.org/")
r, _ := rpc.Dial("https://bsc-dataseed.binance.org/")
bep20, _ := NewBalanceScanner(BinanceChain, client, r)
resultCount := 0
ret, _ := bep20.BatchCallBalances(common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"), [][]common.Address{
{
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
},
{
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
},
{
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
common.HexToAddress("0x7d99eda556388Ad7743A1B658b9C4FC67D7A9d74"),
},
})
for j := 0; j < len(ret); j++ {
resultCount++
assert.Equal(t, ret[j].String(), "34000000000000000000")
}
fmt.Println("Total result", resultCount)
}

0 comments on commit 1907982

Please sign in to comment.