Skip to content

Conversation

@johnfawole
Copy link
Contributor

This is the submission of issue #19. Please, let me know if you have any edits.

I look forward to hearing from you.


goerli: {

url: `https://eth-goerli.alchemyapi.io/v2/${Chainstack_API_KEY}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey John,
The Goerli URL needs to change in terms of format. I appreciate using the Chainstack_API_KEY const in the URL,
but the overall URL needs to look like this-
https://nd-123-456-789.p2pify.com/$${Chainstack_API_KEY}

We can't use a URL with 'alchemy' in it on our blog.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

mapping(address => mapping(address => uint)) public allowance;
```
The mapping above is a nested one that involves two sets of addresses where one allows or approves the other to take a specific action.
3. ### Identifying owners
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line doesn't render correctly while viewing the file. Attaching an SS.
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted.

## How Mappings Are Stored in Solidity
Mapping in itself is not capable of storing data. The only extent to which they are helpful in helping us tie some key types to value types. This is why you will discover that you cannot get the “.length” of mappings because they do not store actual data in real-time.

But while it does not store data, we will need the hash of the key to trace its value pair. The hashes are stored in the storage slots of the contract. As a result, mappings can only declared at the state level and should be stored in storage; not calldata or memory.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice explanation.

theBalanceOf[msg.sender] = 3 ether;
}
```
### Getting Mapping
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice little section. I feel one more thing could be added.
We don't have to always assign mapping value to a new variable to get a mapping. We can also simply return a mapping value from a function without using another variable.

Something like this-

function gettingMapping2() external view returns(uint){
return theBalanceOf[msg.sender];
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

## How to Work with Arrays in Mapping
There is a slight variation when you have strings of arrays as the value type of your mapping, especially when it is a dynamic array. In such instances, be mindful of your parameters.
```
contract MappingArrays{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code snippet has a missing closing parenthesis } in the end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
contract MappingArrays{
contract MappingArrays{
```suggestion
contract MappingArrays{

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added


The next stage is to compile and deploy this contract using Hardhat. Perhaps you have not installed Hardhat in your terminal; paste this on your terminal:
```
nvm install latest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @johnfawole,

This command is used to install node through nvm, this is NOT the command to install hardhat from the command line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to - npm install — save-dev hardhat

```
For the compilation, run this command:
```
npx hardhat compile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The readers should be told where they should paste their code. Hardhat has an src folder in their root directory where contracts are written.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

```
npx hardhat compile
```
Bear in mind that you should not have put on the auto-compile option on Remix. Or else, the CLI will return that there is nothing to compile.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this one. How is remix related here? Are you somehow working with remix through the CLI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but for convenience, I have deleted it and suggested doing this on Vs Code terminal instead.

```
Bear in mind that you should not have put on the auto-compile option on Remix. Or else, the CLI will return that there is nothing to compile.

Once that is successful, deploy it to your favorite testnet using the Chainstack Ethereum URL. First, create a file in deploy.js and run this script:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a deploy script. This is the configuration that goes inside the hardhat.config file. This isn't how a deploy script goes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.


You will need Hardhat for the compiling and deployment. Run this on your terminal
```
nvm install latest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a command to install the latest version of node, this doesn't install hardhat.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.


The next stage is to compile and deploy this contract using Hardhat. Go to the Hardhat folder on your device, locate the src folder and write your contract there. Perhaps you have not installed Hardhat in your terminal, paste this on your Vs Code terminal:
```
npm install — save-dev hardhat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm install --save-dev hardhat is the correct command to install hardhat. I think you made a spelling mistake here. The section to install hardhat needs to include the process to create a new hardhat project using their in-terminal UI.
We can install ethers and other required libraries without running an extra command, by using npx hardhat command to set up a basic javascript project.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was a typo. Corrected it now.


This contract is a simple wallet contract where users can deposit, transfer, and know the remaining Ethers in the contract. Here, we used mapping to track both balances and allowance.

The next stage is to compile and deploy this contract using Hardhat. Go to the Hardhat folder on your device, locate the src folder and write your contract there. Perhaps you have not installed Hardhat in your terminal, paste this on your Vs Code terminal:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is an src folder in hardhat anymore. The smart contract needs to go under the 'contracts' folder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, yes, discovered that too.


};
```
The final step is to write a deployment script. Create a deploy.js file where you will write your deployment script - you can choose either Javascript or Typescript. When you are done, start a node:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to start a node to deploy a contract, a mock blockchain is used to test contracts, not to deploy them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted it since it is a test project. Thanks.

```
npx hardhat node
```
Then you can deploy to a testnet:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First a deploy script specific to the contract needs to be written to deploy a contract before calling the script.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indicated that.

This is the final draft, I have added the deployment script as instructed. Do consider merging the PR. Thanks
@Genesis3800 Genesis3800 merged commit aa29b0d into chainstacklabs:master Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants