Last active
August 15, 2022 20:11
-
-
Save SHarpreetSingh/56b548a53956ba4618becf202b1df005 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.16+commit.07a7930e.js&optimize=false&runs=200&gist= Mapping explain in simple
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
import "hardhat/console.sol"; | |
contract MappingCont { | |
// Declare a mapiing | |
mapping(address => uint) balances; | |
// nested mapping | |
mapping(address => mapping(address => bool)) approved; | |
mapping(address => uint[]) scores; | |
function foo(address sender) external { | |
// Add value | |
balances[msg.sender] = 100; | |
// update map | |
balances[msg.sender]=20; | |
// get mapp | |
balances[msg.sender]; | |
// Delete | |
delete balances[msg.sender]; | |
// Default value | |
// balances[someAddressDoNotExists]=0; | |
// Exotic mapping a | |
approved[msg.sender][sender] = true; | |
approved[msg.sender][sender]; | |
console.log(approved[msg.sender][sender]); | |
approved[msg.sender][sender]=false; | |
delete approved[msg.sender][sender]; | |
// Exotic mapping b | |
scores[msg.sender].push(1); | |
scores[msg.sender].push(2); | |
scores[msg.sender]; | |
scores[msg.sender][0]=10; | |
delete scores[msg.sender][0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment