Virtual Machines

Creating, Deploying, and Testing a HelloWorld smart contract

Pre-requisites

Deploying smart contracts on the K Ethereum Virtual Machine (KEVM) devnet and testing Mallet requires:

  1. Installation of the Docker Engine.
  2. Installation of Mallet.
  3. Mallet account with enough funds to send a transaction.

Creating the contract

Follow these steps to create a HelloWorld smart contract.

1. Create a Solidity file

Create a myContract.sol file:

$ cat << EOF >myContract.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.1 <0.9.0;
contract HelloWorld {
function helloWorld() external pure returns (string memory) {
return "Hello, World!";
}
}
EOF

2. Compile with solc:

Use the Docker command to compile with solc: (Assumes Docker has been installed.)

$ docker run --rm -v $(pwd):/sources ethereum/solc:0.5.1 -o /sources --bin --abi /sources/myContract.sol

3. Verify that the compiled file exists:

If the file was correctly compiled, there should be a HelloWorld.bin file in your directory.

$ ls *.bin
HelloWorld.bin

Deploying the HelloWorld smart contract using Mallet

Before deploying the contract, import the compiled smart contract into Mallet using the HelloWorld.bin file.

Note: Run all commands below in Mallet.

1. Import the fileSystem module:

fs = require("fs");

2. Read the contents of the binary file:

myContract = "0x" + fs.readFileSync('HelloWorld.bin', 'utf8');

3. Prepare the transaction to deploy the contract:

tx = { gas: 470000, data: myContract}

4. Send a transaction with the smart contract:

deploymentHash = sendTransaction(tx)

This will return the tx hash on which the contract was deployed to.

5. View receipt

Run this command to view transaction details:

getReceipt(deploymentHash)

6. Save your contract address

To save your contract address, create a variable that takes the return value of getReceipt():

myContractAddress = getReceipt(deploymentHash).contractAddress

Testing the smart contract

1. Test the contract

Run this command to test the smart contract.

web3.toAscii(web3.eth.call({to: myContractAddress, data: '0xc605f76c'}))

The expected output should contain "Hello, World!".

© IOHK 2015 - 2021


Cardano is an open-source project.

Cardano is a software platform ONLY and does not conduct any independent diligence on, or substantive review of, any blockchain asset, digital currency, cryptocurrency or associated funds. You are fully and solely responsible for evaluating your investments, for determining whether you will exchange blockchain assets based on your own judgement, and for all your decisions as to whether to exchange blockchain assets with Cardano. In many cases, blockchain assets you exchange on the basis of your research may not increase in value, and may decrease in value. Similarly, blockchain assets you exchange on the basis of your research may fall or rise in value after your exchange.

Past performance is not indicative of future results. Any investment in blockchain assets involves the risk of loss of part or all of your investment. The value of the blockchain assets you exchange is subject to market and other investment risks