Smart contract pagination for list

Nhan Cao
2 min readJun 28, 2021
  • A contract has an infinite internal storage
  • There are 2 limits to practicality: gas costs and the block gas limit.

Example

Create 5k5 players, read one time

https://testnet.bscscan.com/address/0x721ff9e58831b698efd26b9ac744cf8f0aaecbc2#readContract

Max Gas Limit is 28,500,000 at May 12, 2021 = 0.22BNB ~ $145

  • [WRITE] Can add 500 player in 1 tx
  • [READ] Can not get all 5600 players (1600 is ok)
call to TestPlayerRead.getPlayers
call to TestPlayerRead.getPlayers errored: Error: Internal JSON-RPC error. { "code": -32000, "message": "out of gas" }
  • [READ] To read all data, should use paging approach

TestPlayerRead.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.3;

contract TestPlayerRead {

struct Player {
uint id;
}

Player[] public playerList;
mapping(uint => Player) public playerMap;

constructor () {}

function addPlayers(uint more) public {
uint currentSize = getPlayerSize();
for (uint i = currentSize; i <…

--

--