diff --git a/README.md b/README.md index d681534..89ba982 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ This is a collection of solidity examples that I practiced while I was learning Almost all smart contracts have tests with them, so feel free to play around with them to understand more about Solidity programming language or business logic in the contract itself. + +STEP 1: Installation and environmental setup. ## How to Run ```sh @@ -13,6 +15,7 @@ $ npm install $ npx hardhat test ``` +STEP 2: solidity bases. ## Getting Started with Solidity 1. [Data types](contracts/getting_started_with_solidity/01_data_types/) @@ -40,6 +43,7 @@ $ npx hardhat test 23. [Libraries](contracts/getting_started_with_solidity/23_libraries/) 24. [Interface](contracts/getting_started_with_solidity/24_interface/) +STEP3: Solidity smart contract projects to build yourself. ## Sample Apps 1. [Sample Contract](contracts/sample_apps/01_sample_contract/) @@ -55,6 +59,7 @@ $ npx hardhat test 11. [Flash Loan](contracts/sample_apps/11_flash_loan/). (See more [resources](https://github.com/samnang/flash-loan-examples)) 12. [Uniswap - Simple Swap](contracts/sample_apps/12_uniswap_simple_swap/) +STEP4: Advance frontend and backend(smart contract) projects. ## Road to Web3 1. [NFT Smart Contract ECR-721](contracts/road_to_web3/week_01/) diff --git a/contracts/sample_apps/02_address_book/AddressBook.sol b/contracts/sample_apps/02_address_book/AddressBook.sol index 6489c11..bc90c39 100644 --- a/contracts/sample_apps/02_address_book/AddressBook.sol +++ b/contracts/sample_apps/02_address_book/AddressBook.sol @@ -4,35 +4,51 @@ pragma solidity ^0.8.0; +// This is a smart contract allows one to store a list of contact addressess under their own address +//each contact address stored under the owner's address con be given an alias name, this is a nested mapping in solidity. + contract AddressBook { + // stores a list/array of contact address under the owner's address mapping(address => address[]) private contacts; + + //Each contact address under the owner's address can be given an alias Name mapping(address => mapping(address => string)) private aliases; + // Returns a list/array of addressess stored under my address function getContacts() public view returns (address[] memory) { return contacts[msg.sender]; } + // You can add contactAddress to your array and give it an alias name function addContact(address contactAddress, string memory aliasName) public { contacts[msg.sender].push(contactAddress); aliases[msg.sender][contactAddress] = aliasName; } + // Allows you see the alias name that a particular contactAddress has. function getAlias(address contactAddress) public view returns (string memory) { return aliases[msg.sender][contactAddress]; } + // remove the alias name of a particular contactAddress from my array. function removeContact(address contactAddress) public { uint256 length = contacts[msg.sender].length; for (uint256 i = 0; i < length; i++) { - if (contactAddress == contacts[msg.sender][i]) { + // Rather compare each contact address in the current user array to the specified contactAddress from input function. + if (contacts[msg.sender][i] == contactAddress) { + + // Check for overflow and underflow even though we are using solidity 0.8 if (contacts[msg.sender].length > 1 && i < length - 1) { + + // Shift the item to be removed to the last element in my list. contacts[msg.sender][i] = contacts[msg.sender][length - 1]; } - + // Remove the last item in my list. contacts[msg.sender].pop(); + // Remove alias name of the deleted contactAddress. delete aliases[msg.sender][contactAddress]; - break; + break; // Stop the loop. } } }