-
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: backport index iter from ethutils
- Loading branch information
1 parent
2bd6077
commit 8e834f9
Showing
1 changed file
with
66 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 celoutils | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/celo-org/celo-blockchain/common" | ||
"github.com/grassrootseconomics/w3-celo/module/eth" | ||
"github.com/grassrootseconomics/w3-celo/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 | ||
} |