-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: Batchcall to optimize token balance query performances
- Loading branch information
Himitsuko
committed
Aug 26, 2022
1 parent
e31ec0c
commit 1907982
Showing
2 changed files
with
144 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |