Skip to content

Instantly share code, notes, and snippets.

@nakajo2011
Created January 9, 2025 16:49
Show Gist options
  • Save nakajo2011/654a254769a5d68fb9ef45febe28135d to your computer and use it in GitHub Desktop.
Save nakajo2011/654a254769a5d68fb9ef45febe28135d to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.26+commit.8a97fa7a.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
struct Hashtable {
mapping(address => uint) valueMap;
address[] keys;
}
function put(Hashtable storage self, address key, uint value) returns(bool) {
if(self.valueMap[key] != 0) {
return false;
}
self.valueMap[key] = value;
self.keys.push(key);
return true;
}
using {put} for Hashtable;
contract HashtableExample {
Hashtable tokenValues;
function mint(address receiver, uint amount) public returns(bool) {
return tokenValues.put(receiver, amount);
}
function totalSupply() public view returns(uint) {
uint total = 0;
for(uint i=0; i<tokenValues.keys.length; i++) {
address user = tokenValues.keys[i];
total += tokenValues.valueMap[user];
}
return total;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment