In order to keep the documentation up to date, It was necessary to have “testing” of the documentation. That’s why, this tutorial was developed using the literate programming style. So from this document is possible to generate 3 files
test_installation.sh
- Executes prerequisites installation instructions.
test_smartcontract_deploy.js
- which follows all the instructions in Mallet.
Of course it also generates the tutorial, which was the original purpose.
mallet_end_to_end_tutorial.md
- this will live in testnets.cardano.org
To generate the files, you only need to:
/bin/rm -rf ./mallet /usr/bin/emacs ./mallet_end_to_end_tutorial.org --batch --eval "(require 'org)" \ --eval '(org-md-export-to-markdown)' \ --eval '(org-babel-tangle-file "mallet_end_to_end_tutorial.org")'
In order to execute the installation instructions
bash ./test_installation.sh
The script comes with Unofficial Bash Strict Mode enabled:
#!/bin/bash
#set +u
set -xeo pipefail
IFS=$'\n\t'
Is designed to be executed within Mallet. So one needs to execute as:
cd ./mallet/ mallet kevm -d ./my_data/ mallet> .load ../test_smartcontract_deploy.js
The export command leaves the article in the current directory but it should live in testnets repo.
mv 2020-05-04_11-00-00_deploying-smart-contracts-en.md ~/testnets-cardano-org/resources/content/articles/en/
On Linux and Mac, you will require Node.js 10.16.3, or the latest version, and the Git tools. For Windows, you will also need the Windows Subsystem for Linux (WSL).
For reference this is the official nodejs documentation.
curl -sL https://deb.nodesource.com/setup_15.x | sudo -E bash -
sudo apt-get -q install -y nodejs
node --version
v13.14.0
curl -s -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
nvm --version
0.37.2
With nvm
working you can install Mallet.
To get the latest version of Mallet, you first need to clone the repository at https://github.com/input-output-hk/mallet, and then install with npm.
Open a terminal window and type in:
git clone https://github.com/input-output-hk/mallet
After cloning the repository, execute:
cd mallet
cat .nvmrc
$ $ 10.16.3
Then install the required node version
nvm install 10.16.3
nvm use --silent
npm install --silent
Finally, To check your installation, execute Mallet’s version
:
./mallet --version
2.1.0
If the version number displays correctly. It means you have successfully installed mallet.
The solcjs
compiler, takes your source code, written in solidity, and creates a binary that file
that you can later, deploy to the devnet using Mallet.
sudo npm install -g solc
solcjs --version
0.8.0+commit.c7dfd78e.Emscripten.clang
Once we get the right version on solcjs
we can go forward to deploy smart contracts.
If you have problems installing any of the prerequisites (node, Mallet or solcjs), contact the community in Slack: Join IOHK | Devnets on Slack
To deploy your smart contracts on the KEVM devnet and to test Mallet you will need to compile to KEVM (K - Ethereum virtual machine) bytecode. You can compile the bytecode directly with using solcjs.
Create a myContract.sol
file
cat << EOF >myContract.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;
contract HelloWorld {
function helloWorld() external pure returns (string memory) {
return "Hello, World!";
}
}
EOF
solcjs --bin --abi --base-path . ./myContract.sol
In your directory now there should be an .bin
file
ls *.bin
_myContract_sol_HelloWorld.bin
Mallet, the minimal wallet, is the command line interface used to send transactions, deploy smart contracts, and interact with the IELE and KEVM devnets.
./mallet kevm -d ./data
Mallet 2.1.0 - IELE/KEVM devnet utility
Type 'help()' to view the online documentation or 'listCommands()' to view available commands
This will open a session in the read-eval-print-loop (Repl) environment for Node.js. Mallet commands are imported automatically.
Everything typed in the Repl has to be valid JavaScript. Technically speaking, the commands are simply functions and properties of Mallet object. However, we tend to refer to them as commands because that reflects how they are used.
//execute inthe Mallet Repl
//mallet kevm -d ./my_data/
//mallet> .load ../test_smartcontract_deploy.js
myAccount = newAccount()
The newAccount
command asks your password, and returns your new account
address.
Note that we are assigning the return value of newAccount to a variable named myAccount so that we can refer to it later.
Now we will make the account just created active
selectAccount(myAccount)
'0x45402404f51909b640d03f361c742c38d34bb3e7'
Since the account has just been created its balance should be 0.
getBalance()
If you don’t give any argument, this will return the balance of the selected account.
requestFunds()
It may take a few minutes for the funds to be transferred.
Now that you have created and funded your account, you can compile and deploy smart contracts.
getBalance()
Using the _myContract_HelloWorld.bin
created in the step:
Create a HelloWorld smart contract
you can now import it to mallet
fs = require("fs");
myContract = fs.readFileSync('_myContract_sol_HelloWorld.bin', 'utf8');
Now that you have the bytecode from solcjs
, the next step is simply to deploy it:
tx = { gas: 470000, data: myContract}
deploymentHash = sendTransaction(tx)
you will get back the tx hash on which it was deployed.
You can view details with the following command:
getReceipt(deploymentHash)
To save your contract address, create a variable that takes the return value of getReceipt().
myContractAddress = getReceipt(deploymentHash).contractAddress
sendTransaction({to: myContractAddress,gas:10000,arguments: []})
When running Mallet in the command-line interface, the help command can be useful. This opens the Readme file in your default web browser:
help()
Or you can Join IOHK | Devnets on Slack, where the community helps each other