Contract storage is a key of 32 bytes and a value of 32 bytes. If I understand it correctly, it can support storing 2^32 bytes which is 4GB?
I have a contract code like below. It uses a map mapping(address => uint256) balances;
to store users' balance. My concern is:
If there are millions of users in the system, will it exceed the maximum storage limit? Because the map will have millions of keys, one for each user's address.
Store a large data in the contract is very expensive. It probably costs a very high gas fee when sending a transaction.
what is the best way to solve this issue?
// SPDX-License-Identifier: MIT
// Tells the Solidity compiler to compile only from v0.8.13 to v0.9.0
pragma solidity ^0.8.13;
import "./ConvertLib.sol";
// This is just a simple example of a coin-like contract.
// It is not ERC20 compatible and cannot be expected to talk to other
// coin/token contracts.
contract MetaCoin {
mapping(address => uint256) balances;
uint256 transactionCount = 0;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
constructor() {
balances[tx.origin] = 10000;
}
function sendCoin(address receiver, uint256 amount)
public
returns (bool sufficient)
{
if (balances[msg.sender] < amount) return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
transactionCount++;
emit Transfer(msg.sender, receiver, amount);
return true;
}
function getBalanceInEth(address addr) public view returns (uint256) {
return ConvertLib.convert(getBalance(addr), 2);
}
function getBalance(address addr) public view returns (uint256) {
return balances[addr];
}
}