-
Notifications
You must be signed in to change notification settings - Fork 14
Mapping issue #19 #26
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
Mapping issue #19 #26
Conversation
issue chainstacklabs#19 Co-Authored-By: Priyank Gupta <priyankgupta0308@gmail.com>
|
|
||
| goerli: { | ||
|
|
||
| url: `https://eth-goerli.alchemyapi.io/v2/${Chainstack_API_KEY}`, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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];
}
There was a problem hiding this comment.
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{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| contract MappingArrays{ | |
| contract MappingArrays{ | |
| ```suggestion | |
| contract MappingArrays{ | |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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

This is the submission of issue #19. Please, let me know if you have any edits.
I look forward to hearing from you.