Skip to content

Commit

Permalink
Implement token --by-acct
Browse files Browse the repository at this point in the history
Fixes Issue TrueBlocks#3964 where `chifra token --by-acct` was not implemented.

Signed-off-by: Vincent Mele <[email protected]>
  • Loading branch information
vincentmele authored Feb 17, 2025
1 parent 621578e commit c899677
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/apps/chifra/internal/tokens/handle_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ import (

func (opts *TokensOptions) HandleShow(rCtx *output.RenderCtx) error {
chain := opts.Globals.Chain
tokenAddr := base.HexToAddress(opts.Addrs[0])

var singleAddr base.Address
var addrRange []string

if opts.ByAcct {
singleAddr = base.HexToAddress(opts.Addrs[len(opts.Addrs)-1]) // by_acct case. Last address is for balance, all others are token addresses.
addrRange = opts.Addrs[:len(opts.Addrs)-1]
} else {
singleAddr = base.HexToAddress(opts.Addrs[0]) // normal case. First address is token, the rest are addresses for balances.
addrRange = opts.Addrs[1:]
}

fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
for _, address := range opts.Addrs[1:] {
addr := base.HexToAddress(address)
for _, address := range addrRange {
addr := base.HexToAddress(address) // if by_acct, this is token not address
currentBn := base.Blknum(0)
currentTs := base.Timestamp(0)
for _, br := range opts.BlockIds {
Expand All @@ -32,6 +42,14 @@ func (opts *TokensOptions) HandleShow(rCtx *output.RenderCtx) error {
}

for _, bn := range blockNums {
var tokenAddr base.Address
if opts.ByAcct {
tokenAddr = addr
addr = singleAddr
} else {
tokenAddr = singleAddr
}

if balance, err := opts.Conn.GetBalanceAtToken(tokenAddr, addr, fmt.Sprintf("0x%x", bn)); balance == nil {
errorChan <- err
} else {
Expand Down

0 comments on commit c899677

Please sign in to comment.