-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add index iterator for indexes with large entry count
tests: surpress revert tx test as it requires an archive node to simulate
- Loading branch information
1 parent
83d6776
commit c46f4c1
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ethutils | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/lmittmann/w3/module/eth" | ||
"github.com/lmittmann/w3/w3types" | ||
) | ||
|
||
type BatchIterator struct { | ||
provider *Provider | ||
index common.Address | ||
entryCount *big.Int | ||
currentIndex int64 | ||
batchSize int64 | ||
} | ||
|
||
const defaultBatchSize = 500 | ||
|
||
func (p *Provider) NewBatchIterator(ctx context.Context, index common.Address) (*BatchIterator, error) { | ||
var entryCount big.Int | ||
|
||
if err := p.Client.CallCtx( | ||
ctx, | ||
eth.CallFunc(index, entryCountFunc).Returns(&entryCount), | ||
); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &BatchIterator{ | ||
provider: p, | ||
index: index, | ||
entryCount: &entryCount, | ||
currentIndex: 0, | ||
batchSize: defaultBatchSize, | ||
}, nil | ||
} | ||
|
||
func (iter *BatchIterator) Next(ctx context.Context) ([]common.Address, error) { | ||
if iter.currentIndex >= iter.entryCount.Int64() { | ||
return nil, nil | ||
} | ||
|
||
endIndex := iter.currentIndex + iter.batchSize | ||
if endIndex > iter.entryCount.Int64() { | ||
endIndex = iter.entryCount.Int64() | ||
} | ||
|
||
batchSize := endIndex - iter.currentIndex | ||
calls := make([]w3types.RPCCaller, batchSize) | ||
tokenAddresses := make([]common.Address, batchSize) | ||
|
||
for i := int64(0); i < batchSize; i++ { | ||
index := iter.currentIndex + i | ||
calls[i] = eth.CallFunc(iter.index, entrySig, new(big.Int).SetInt64(index)).Returns(&tokenAddresses[i]) | ||
} | ||
|
||
if err := iter.provider.Client.CallCtx(ctx, calls...); err != nil { | ||
return nil, err | ||
} | ||
|
||
iter.currentIndex = endIndex | ||
return tokenAddresses, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ethutils | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/lmittmann/w3" | ||
) | ||
|
||
func TestIndexIter_Next(t *testing.T) { | ||
p := NewProvider("https://forno.celo.org", CeloMainnet) | ||
|
||
ctx := context.Background() | ||
iter, err := p.NewBatchIterator(ctx, w3.A("0xe2CEf4000d6003958c891D251328850f84654eb9")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
for { | ||
batch, err := iter.Next(ctx) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if batch == nil { | ||
break | ||
} | ||
t.Logf("index batch size %d", len(batch)) | ||
} | ||
} |
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