1.0.3 • Published 2 years ago

goodmapping v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

GoodMapping.sol

A simple, easy, out-of-the-box mapping with keys in Solidity programming language.

Import it effortlessly into your projects whenever the need arises.

Please note that certain operations may have a high time complexity and results in a high gas cost.

Installation

If you are using Truffle or Hardhat, you can install it via npm.

npm install goodmapping

Usage

All the map contracts are named as Map_[KeyType][ValueType], in which U represents uint256, A represents address, S represents string, B represents bytes. Here is a list of the available types in below table.

(Since solidity does not support generic, we only provide some possible combinations of key-value pairs)

Contract NameKey TypeValue TypeStatus
Map_UAuint256address✅ Ready to use
Map_UUuint256uint256🚧 In progress
Map_AUaddressuint256✅ Ready to use
Map_AAaddressaddress🚧 In progress
Map_USuint256string🚧 In progress
Map_SUstringuint256✅ Ready to use
Map_USuint256string✅ Ready to use
Map_SSstringstring🚧 In progress
Map_ASaddressstring🚧 In progress
Map_SAstringaddress🚧 In progress
Map_UBuint256bytes🚧 In progress
Map_ABaddressbytes🚧 In progress
Map_SBstringbytes🚧 In progress
Map_BBbytesbytes🚧 In progress

Here are some key functions you can use (for example, Map_UA).

Import

Import the correct contract you need into your Solidity project, and initialize the contract Map_UA (uint256 => address) in your contract.

import "goodmapping/contracts/Map_UA.sol";

Contract MyContract {
  Map_UA private map;
}

Set value with key-value pair

map.set(
  0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65,
  100
)

Get value with a key

bool bExist = false;
uint256 value = 0;

(bExist, value) = map.get(0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65);

Remove a key-value pair

bool bIsSuccess = map.remove(0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65);

Get all keys

uint256[] memory keys = map.keys();

Determine if a key exists

bool bExist = map.contains(0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65);

Get the size of the mapping

uint256 size = map.size();

Test

Run the following command to compile and test the contracts via Hardhat.

npx hardhat test
1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago