Skip to content

PreciousA20's contribution to this project #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ 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
$ npm install
$ npx hardhat test
```

STEP 2: solidity bases.
## Getting Started with Solidity

1. [Data types](contracts/getting_started_with_solidity/01_data_types/)
Expand Down Expand Up @@ -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/)
Expand All @@ -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/)
Expand Down
22 changes: 19 additions & 3 deletions contracts/sample_apps/02_address_book/AddressBook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
}
Expand Down