Skip to content

Instantly share code, notes, and snippets.

@lharland
Last active December 8, 2021 04:15
Show Gist options
  • Save lharland/25e49777d209b818a4ae7a9ff40a56de to your computer and use it in GitHub Desktop.
Save lharland/25e49777d209b818a4ae7a9ff40a56de to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.0+commit.1d4f565a.js&optimize=false&runs=200&gist=
pragma solidity 0.8.7;
contract HelloWorld {
string private stateVariable = "Hello world";
uint public var2 = 1;
function GetHelloWorld() public view returns(string memory)
{
return stateVariable;
}
}
pragma solidity ^0.5.0;
contract Types {
uint[6] data;
function array_example() public payable returns (uint6[6] memory)
{
data = [uint(10), 20, 30, 40, 50, 60];
return data;
}
function array_element() public payable returns (uint) {
uint x = data[2];
return x;
}
}
pragma solidity 0.5.0;
contract Types {
// Declaring dynamic array
uint[] data;
//Assign values to the array
function array_example() public returns (uint[] memory)
{
data = [1, 2, 3, 4, 5];
return data;
}
function array_lengt() public returns (uint)
{
uint x = data.length;
return x;
}
function array_push() public returns (uint[] memory)
{
data.push(70);
data.push(80);
data.push(90);
return data;
}
function array_pop() public returns (uint[] memory)
{
data.pop();
return data;
}
}
pragma solidity ^0.5.0;
contract Test {
uint16 public a = 20;
uint16 public b = 10;
uint16 public and = a & b;
uint16 public or = a | b;
uint16 public xor = a ^ b;
uint16 public not = ~a;
uint16 public leftshift = a << b;
uint16 public rightshift = a >> b;
}
pragma solidity ^0.5.0;
contract LogicalOperators {
function Logic(bool a, bool b) public view returns(bool, bool, bool, bool)
{
bool and = a && b;
bool or = a || b;
bool nota = !a;
bool notb = !b;
return (and, or, nota, notb);
}
}
pragma solidity ^0.5.0;
contract RelOps {
//Declaring variable
uint16 public a = 20;
uint16 public b = 10;
bool public eq = a == b;
bool public not_eq = a != b;
bool public gtr = a > b;
bool public les = a < b;
bool public geq = a >= b;
bool public leq = a <= b;
}
pragma solidity ^0.5.0;
contract Types {
uint[] data;
uint8 j = 0;
function loop() public returns(uint[] memory) {
while (j < 5) {
data.push(++j);
}
return data;
}
}
pragma solidity ^0.5.0;
contract ForLoop {
uint[] data;
string[] data1;
function for_loop() public returns(uint[] memory)
{
for(uint i = 0; i < 10;i++)
{
if (i == 3)
{
continue;
}
if (i == 5)
{
break;
}
data.push(i+1);
}
return data;
}
}
pragma solidity ^0.5.0;
contract Structs {
// Declaring a structure
struct Book {
string title;
string writer;
uint id;
bool available;
}
Book book1;
// Assigning values to the field for the structure book
Book book2 = Book("Building Smart Contracts", "Artur",2 , false);
function set_book_detail() public
{
book1 = Book("Solidity Programming", "Leslie", 1, true);
}
function book2_info() public view returns(string memory, string memory, uint, bool)
{
return (book2.title, book2.writer, book2.id, book2.available);
}
function book1_info() public view returns(string memory, string memory, uint, bool)
{
return (book1.title, book1.writer, book1.id, book1.available);
}
}
pragma solidity ^0.5.0;
contract hash_functions {
function callhash256() public pure returns(bytes32 result) {
return sha256("Hello world");
}
function callKeccak256() public pure returns(bytes32 result) {
return keccak256("Hello world");
}
}
pragma solidity 0.8.7;
contract Test{
function return_example() public view returns(uint, uint, uint, string memory)
{
uint num1 = 10;
uint num2 = 20;
uint sum = num1 + num2;
uint diff = num2 - num1;
uint prod = num1 * num2;
string memory msg = "Multiple return values";
return (sum, diff, prod, msg);
}
}
pragma solidity ^0.5.0;
contract SolidityTest {
// This is a comment
/*
Comments are ignored by the compiler
This is a block comment
*/
function getResult() public pure returns (uint)
{
uint a = 1;
uint b = 2;
uint result = a + b;
return result;
}
}
pragma solidity ^0.5.0;
contract SolidityVarTest {
//Declare a state variable
uint8 public state_var;
constructor() public {
//108404
state_var = 16;
}
}
pragma solidity ^0.5.0;
contract SolidityVarTest {
//Defining a function to show concept and scope of local variables
function getResult() public view return(uint)
{
//Initialise some local variables
uint local_var1 = 1;
uint local_var2 = 2;
uint result = local_var1 * local_var2;
return result;
}
function getResult2() public view return(uint)
{
uint result = local_var1 + local_var2;
return result;
}
}
pragma solidity ^0.5.0;
contract Test {
address public admin;
//Creating a constructor to use Global variable
constructor() public {
admin = msg.sender;
}
}
pragma solidity ^0.5.0;
contract SolidityVarTest {
uint16 public a = 20;
uint16 public b = 10;
uint public sum = a + b;
uint public diff = a - b;
uint public mul = a * b;
uint public div = a / b;
uint public mod = a % b;
uint public inc = ++a;
uint public dec = b--;
}
pragma solidity ^0.5.0;
contract Types {
uint[6] data;
// Defining function to add value to an array
function array_example() public returns (int[5] memory, uint[6] memory)
{
int[5] memory data = [int(50), -60, 77, -28, 90];
data1 = [uint(10), 20, 30, 40, 50, 60];
return (data, data1);
}
}
pragma solidity ^0.5.0;
contract DynamicArrays {
uint[] data = [10, 20, 30, 40, 50];
int[] data1;
function dynamic_array() public returns(uint[] memory, int[] memory)
{
data1 = [int(-60), 70, -80, 90, -100, -120, 140];
return (data, data1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment