0% found this document useful (0 votes)
61 views

Solidity Programs1 6

The document contains code for 5 smart contracts (Mycontract1-5) that demonstrate different ways to initialize and set string and number variables in Solidity. Mycontract1 defines a public string variable without initializing it. Mycontract2 initializes the string variable to a value. Mycontract3 initializes both a string and number variable. Mycontract4 uses a constructor to initialize its variables. Mycontract5 allows initializing its variables through a function call.

Uploaded by

Kumara S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Solidity Programs1 6

The document contains code for 5 smart contracts (Mycontract1-5) that demonstrate different ways to initialize and set string and number variables in Solidity. Mycontract1 defines a public string variable without initializing it. Mycontract2 initializes the string variable to a value. Mycontract3 initializes both a string and number variable. Mycontract4 uses a constructor to initialize its variables. Mycontract5 allows initializing its variables through a function call.

Uploaded by

Kumara S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Mycontract1.

sol

pragma solidity ^0.8.13;

contract Mycontract1{

string public hey;

Mycontract2.sol

pragma solidity ^0.8.13;

contract Mycontract2 {

string public hey=”Hello Ethereum”;

Mycontract3.sol

pragma solidity ^0.8.13;

contract Mycontract3 {

string public hey=”Hello Ethereum”;

uint256 public no=4;

Mycontract4.sol

pragma solidity ^0.8.13;

contract Mycontract4 {

string public hey;

uint256 public no;


constructor (string memory _hey, uint256 _no)

hey=_hey;

no=_no;

Mycontract5.sol

pragma solidity ^0.8.13;

contract Mycontract5 {

string public hey;

uint256 public no;

function addInfo(string memory _hey,uint256 _no) public

hey=_hey;

no=_no;

}
List of Experiments:

Experiment 1 :
How to navigate to the Remix IDE web URL on the browser
and exploring the various tabs and features of the IDE.

How to open a Remix IDE

Step 1: Open any web browser


Step 2: Web browser type  https://remix.ethereum.org/
Step 3: To View Dashboard or Main screen in Remix IDE
We're using Remix IDE to Compile and Run our Solidity Code base.

Step 1 − Copy the given code in Remix IDE Code Section.


Step 2 − Under Compile Tab, click Start to Compile button.
Step 3 − Under Run Tab, click Deploy button.
Step 4 − Under Run Tab, Select SolidityTest at 0x... in drop-down.
Step 5 − Click getResult Button to display the result.
Remix IDE Workspace and features
Experiment 2 :
Dhanalakshmi is a student in Government Engineering College city school
in Mayiladuthurai, His teacher gave him homework to check whether a
number is Incrementing or Decrementing. Help her to solve the problem

NFT Counter Program

pragma solidity ^0.8.13;


contract NFTCount{
uint256 public numberofNFT;

//This function will increment the NFT Number


function addNFT() public
{
numberofNFT+=1;
}

//This function will delete the NFT by 1


function deleteNFT() public
{
numberofNFT-=1;
}
//check total number of NFT
function checkTotalNFT() public view returns(uint256)
{
return numberofNFT;
}
}
Experiment 3 :

Write a Program to understand the solidity variables and its properties.

pragma solidity ^0.8.13;


contract Datatypes{
//bool public hey;
bool public no=true;

/* uint8; //range 0-2 **8-1 //255


unit16; //ranges 0 -2 **16-1 // 65535
unit256; // ranges 0 - 2 ** 256-1 //1.1579209e+77
*/

uint public u8=1;


uint u256=456;
uint public u=123; // uint is an alias from uint256;

int8 public i8=-1;


int public i256=456;
int public i=-1234;

//Now you can add min and Max int

int public minInt=type(int).min;


int public maxInt=type(int).max;

bytes1 public a=0xb5;


bytes1 public b=0x56;
//address types
/*address public hey;
address public addr=
*/

//default values

bool public defaultBool; //false


uint public number; //0
int public defaultInt; // 0
address public hey; // 0x000000000
}

Experiment 4:

Write a Program to understand the arrays in the Solidity with


regards to fixed length array and dynamic array.

// Solidity program to demonstrate creating a fixed-size array


pragma solidity ^0.8.18;

// Creating a contract
contract Types {

// Declaring state variables of type array


uint[6] data1;

// Defining function to add values to an array


function array_example() public returns (int[5] memory, uint[6] memory)
{

int[5] memory data= [int(50), -63, 77, -28, 90];


data1= [uint(10), 20, 30, 40, 50, 60];
return (data, data1);
}
}

// Solidity program to demonstrate the DynamicArray approach


pragma solidity ^0.8.18;
contract DynamicArray
{

// Declaring state variable


int[] private arr;

// Function to add data in dynamic array


function addData(int num) public
{
arr.push(num);
}

// Function to get data of dynamic array


function getData() public view returns(int[] memory)
{
return arr;
}

// Function to return length of dynamic array


function getLength() public view returns (uint)
{
return arr.length;
}
// Function to return sum of elements of dynamic array
function getSum() public view returns(int)
{
uint i;
int sum = 0;

for(i = 0; i < arr.length; i++)


sum = sum + arr[i];
return sum;
}

// Function to search an element in dynamic array


function search(int num) public view returns(bool)
{
uint i;

for(i = 0; i < arr.length; i++)


{
if(arr[i] == num)
{
return true;
}
}

if(i >= arr.length)


return false;
}
}
Experiment 5:
Saravanan started learning solidity language, He completed his theory
classes now it is time for the practical session. He was given a problem
printing the string “Welcome to Blockchain”. Help him to print the String

// Solidity program to set string value using function with


parameters
pragma solidity ^0.5.0;

contract LearningStrings
{
string public text;

// Assigning the text directly


function setText() public
{
text = 'hello';
}

// Assigning the text by passing the value in the function


function setTextByPassing(string memory message) public
{
text = message;
}

// Function to get the text


function getText() view public returns (string memory)
{
return text;
}
}
Experiment 6:
Write a Program to generate the ABI(Contract Application
Binary Interface ) and Bytecode of a Smart Contract by compiling
the solidity file.

JSON JavaScript Object Notation

Generate the ABI and Byte code of a Smart Contract

Steps to be followed:

1. Creating a Solidity smart contract using the text editor

2. Compiling the smart contract to generate ABI and Byte code

Step 1: Creating a Solidity Smart Contract using the Text


Editor

1. Open your Remix IDE and click the Create new file button to
create a student.sol file

2. Once the file is created, type the following code in


the student.sol file

pragma solidity ^0.8.18;

contract structSample {
struct student
{

string name;

uint256 age;

uint256 id;
}

//create instance

student _studata;

function setStudentValues( string memory _sname,uint256


_sage,uint256 _sid) public

_studata.name = _sname;

_studata.age = _sage;

_studata.id = _sid;

function getStudentValues() public view returns (string memory,

uint256,uint256)

return (_studata.name, _studata.age, _studata.id);

3. Once typing the code is done, save the file. Once


the student.sol file is saved, observe that JSON metadata will be
created for the file
Step 2: Compiling the Smart Contract to generate the ABI and
Byte code

2. Click the Solidity Compiler section to start the compilation of


the student.sol file

2. Remix detects the version of the compiler to be used but it may be


changed according to user preference. Click the Compile
student.sol button to compile the file

2.3 Observe that ABI and Byte code buttons are displayed on the screen.
This indicates that the Solidity smart contract has been compiled
without an error

2. Clicking the Compilation Details button will reveal the ABI and the
Byte code of the student.sol file

3. Observe the Byte code. ABI of the student.sol file is generated


and displayed

You might also like