Learning exercise project to deploy and interact with a smartcontract using ethers.js
.
- Simple Storage Solidity
- Smartcontract
- Requirements
- Compiling
- Blockchain
- Deploy (and interact)
- Security improvement
- Typescript
The smartcontract that is used is a simple implementation of a contract where you can change the value of a number saved on the blockchain. You can also save the number associated to a person.
To use the following repository you need to have installed this packages:
- git
- You'll know you did it right if you can run
git --version
and you see a response likegit version x.x.x
- You'll know you did it right if you can run
- Nodejs
- You'll know you've installed nodejs right if you can run:
node --version
and get an ouput like:vx.x.x
- You'll know you've installed nodejs right if you can run:
- Yarn instead of
npm
- You'll know you've installed yarn right if you can run:
yarn --version
and get an output like:x.x.x
- You might need to install it with npm
- You'll know you've installed yarn right if you can run:
- ganache
- You can alternatively use ganache-cli or hardhat
To compile the solidity code solc
is used
yarn add solc
if you are using in your contract a solidity version older than the latest you can type
yarn add [email protected]
Finally, to compile the solidity code run the following command
yarn solcjs --bin --abi --include-path node_modules/ --base-path <base-path> -o <output path> <solidity-file-name>
it is easier to add this line in the package.json
file.
{
"scripts": {
"compile": "<compile-command>"
}
}
This repo can work with any EVM compatible blockchain. To connect to a blockchain you need an RPC_URL
.
For developement purpouses we'll use a local enviroment as ganache
.
To deploy the contract is enough to run the command
node deploy.js
Make sure to have set the RPC_URL
and the PRIVATE_KEY
in the .env
file.
IMPORTANT -> remember never to push the .env
file to github or never make it public. Add a .gitignore
file!
If you are worried you might leak in some way your .env
you can run the script as this without the need of any file
RPC_URL=<rpc url> PRIVATE_KEY=<private key> node deploy.js
If you don't wont to insert your enviromental variables every time but you still don't want to show the private key to everyone who has access to your project the solution would be to encrypt the key.
The steps to follow are:
- Insert the
PRIVATE_KEY
and aPASSWORD
in.env
or at execution time - Run the
encrypt script
node encryptKey.js
- The output will be saved in
.encryptedKey.json
- Delete PRIVATE_KEY and PASSWORD from
.env
- Now you can run the deploy script as this
PASSWORD=<your password> node deploy-encrypted.js
after the execution is suggest to run
history -c
to delete the terminal history so it is not possible to find out what the password used is.
There is a typescript compatible version of the code on the typescript branch
It works exatly the same, you should execute commands replacing node
with ts-node
ts-node <file name>