diff --git a/workshop/abstraction.sol b/workshop/abstraction.sol new file mode 100644 index 0000000..80d04d3 --- /dev/null +++ b/workshop/abstraction.sol @@ -0,0 +1,8 @@ +pragma solidity >=0.4.0 <0.6.0; +contract vehicle { + function noOfWheels() public view returns (uint); +} + +contract Car is vehicle { + function noOfWheels() public view returns (uint){ return 4; } +} \ No newline at end of file diff --git a/workshop/arraySample.sol b/workshop/arraySample.sol new file mode 100644 index 0000000..08f96a1 --- /dev/null +++ b/workshop/arraySample.sol @@ -0,0 +1,122 @@ +pragma solidity ^0.5.0; +pragma experimental "ABIEncoderV2"; + +contract Arrays { + + uint[3] fixedSimpleArr; // fixed sized array + uint[] simpleDynamicArr; // dynamic array + uint[3][3] fixedSized; // fixed sized 2D array + uint[][3] arrayDynamic; // dynamic array with each element an array with 3 + uint[3][] dynamicArray; // fixed sized array with each element a dynamic + uint[][] dynamicArr; // dynamic array with dynamic array elemnts. + uint[3][3][3] threeDArr; + constructor () public { + fixedSimpleArr = [1,2,3]; + uint j; + for (uint i=0;i<3;i++){ + j = i+1; + fixedSized[i] = [j*1, j*2, j*3]; + } + uint[3] memory temp = [uint(1),2,3]; + arrayDynamic[0] = new uint[](4); + arrayDynamic[1] = [1,2,3,4,5,6,7,8]; + arrayDynamic[2] = temp; + dynamicArray = new uint[2][](3); + dynamicArray = [[1,2], [6,7], [9,0]]; + threeDArr = [ + [[1,2,3], [4,5,6], [7,8,9]], + [[10,11,12], [13,14,15], [16,17,18]], + [[19,20,21], [22,23,24], [25,26,27]] + ]; + } + + function setFixedSimpleArr(uint[3] memory simpleArr) public { + fixedSimpleArr = simpleArr; + } + + // This function is illegal + function setSimpleDynamicArr(uint[] memory simpleArr) public { + simpleDynamicArr = simpleArr; + } + + function setFixedSized(uint[3][3] memory inputArr) public { + fixedSized = inputArr; + } + + function setDynamicArray(uint[3][] memory inputArr) public { + dynamicArray = inputArr; + } + + // This function is illegal + function setArrayDynamic(uint[][3] memory inputArr) public { + arrayDynamic = inputArr; + } + + // This function is illegal + function setDynamicArr(uint[][] memory inputArr) public { + dynamicArr = inputArr; + } + + function setThreeDArr(uint[3][3][3] memory inputArr) public { + threeDArr = inputArr; + } + + function getFixedSimpleArr() public view returns (uint[3] memory) { + return fixedSimpleArr; + } + + function getSimpleDynamicArr() public view returns (uint[] memory) { + return simpleDynamicArr; + } + + function getFixedSized() public view returns (uint[3][3] memory) { + return fixedSized; + } + + function getDynamicArray() public view returns (uint[3][] memory) { + return dynamicArray; + } + + // This function is illegal + function getArrayDynamic() public view returns (uint[][3] memory) { + return arrayDynamic; + } + + // This function is illegal + function getDynamicArr() public view returns (uint[][] memory) { + return dynamicArr; + } + + function getThreeDArr() public view returns (uint[3][3][3] memory) { + return threeDArr; + } + + function getElementFixedSimpleArr(uint i) public view returns (uint) { + return fixedSimpleArr[i]; + } + + // This function is illegal + function getElementSimpleDynamicArr(uint i) public view returns (uint) { + return simpleDynamicArr[i]; + } + + function getElementFixedSized(uint i, uint j) public view returns (uint) { + return fixedSized[i][j]; + } + + function getElementDynamicArray(uint i, uint j) public view returns (uint) { + return dynamicArray[i][j]; + } + + function getElementArrayDynamic(uint i, uint j) public view returns (uint) { + return arrayDynamic[i][j]; + } + + function getElementDynamicArr(uint i, uint j) public view returns (uint) { + return dynamicArr[i][j]; + } + + function getElementThreeDArr(uint i, uint j, uint k) public view returns (uint) { + return threeDArr[i][j][k]; + } +} diff --git a/workshop/assgnmnt.sol b/workshop/assgnmnt.sol new file mode 100644 index 0000000..8c94db8 --- /dev/null +++ b/workshop/assgnmnt.sol @@ -0,0 +1,16 @@ +pragma solidity ^0.5.9; + contract C { + uint index; + function f() public pure returns (uint, bool, uint) { + return (7, true, 2); + } + function g() public returns(uint) { + (uint x, , uint y) = f(); + (x, y) = (y, x); + (index, , ) = f(); // Sets the index to 7 + return index; + } + function get() public view returns(uint) { + return index; + } + } diff --git a/workshop/complex.sol b/workshop/complex.sol new file mode 100644 index 0000000..36d6188 --- /dev/null +++ b/workshop/complex.sol @@ -0,0 +1,15 @@ +pragma solidity ^0.5.9; +pragma experimental "ABIEncoderV2"; +contract complexSol { + struct cx { + int256 x; + int256 y; + } + cx result; + function complexAddition(cx memory a, cx memory b) public view returns(cx memory){ + cx memory res; + res.x = a.x + b.x; + res.y = a.y + b.y; + return res; + } +} diff --git a/workshop/inherit.sol b/workshop/inherit.sol new file mode 100644 index 0000000..dc6683a --- /dev/null +++ b/workshop/inherit.sol @@ -0,0 +1,49 @@ +// pragma solidity >=0.4.0 <0.6.0; + +// contract C { +// uint private data; + +// function f(uint a) public pure returns(uint b) { return a + 1; } +// function setData(uint a) public { data = a; } +// function getData() public view returns(uint) { return data; } +// function compute(uint a, uint b) external pure returns (uint) { return a + b; } +// } + +// // This will not compile +// contract D { +// function readData() public { +// C c = new C(); +// uint local = c.f(7); // error: member `f` is not visible +// c.setData(3); +// local = c.getData(); +// local = c.compute(3, 5); // error: member `compute` is not visible +// } +// } + + + +// pragma solidity >=0.4.0 <0.6.0; + +// contract C { +// uint public data = 42; +// } + +// contract Caller { +// C c = new C(); +// function f() public view returns (uint) { +// return c.data(); +// } +// } + + + + +// contract E is C { +// function g() public { +// C c = new C(); +// uint val = compute(3, 5); // access to internal member (from derived to parent contract) +// } +// } + + + diff --git a/workshop/reciept.sol b/workshop/reciept.sol new file mode 100644 index 0000000..dd0e0cc --- /dev/null +++ b/workshop/reciept.sol @@ -0,0 +1,18 @@ +pragma solidity >=0.4.21 <0.7.0; + +contract ClientReceipt { + event Deposit( + address indexed _from, + bytes32 indexed _id, + uint _value + ); + + function deposit(bytes32 _id) public payable { + // Events are emitted using `emit`, followed by + // the name of the event and the arguments + // (if any) in parentheses. Any such invocation + // (even deeply nested) can be detected from + // the JavaScript API by filtering for `Deposit`. + emit Deposit(msg.sender, _id, msg.value); + } +} \ No newline at end of file diff --git a/workshop/string.sol b/workshop/string.sol new file mode 100644 index 0000000..c5a18ea --- /dev/null +++ b/workshop/string.sol @@ -0,0 +1,29 @@ +pragma solidity ^0.5.0; +import "/Users/soumyanil jana/solidity-examples/workshop/stringUtils.sol"; + +contract strTest { + using stringUtils for string; + + function stringToUintt() public { + string memory unum = "1485517484864826648847788999984865484747564568484"; + uint newNum = 1485517484864826648847788999984865484747564568484; + Assert.equal(unum.stringToUint(), newNum, "working"); + } + + function stringCompare() public { + string memory str_1 = "hello World"; + string memory str_2 = "hello World"; + Assert.ok(str_1.stringCompare(str_2), "working"); + } + + function stringLen() public { + string memory str = "hello World"; + Assert.equal(str.stringLen(), 11, "working"); + } + + function stringConcat() public { + string memory str_1 = "hello "; + string memory str_2 = "World"; + Assert.equal(str_1.stringConcat(str_2), "hello World", "working"); + } +} \ No newline at end of file diff --git a/workshop/stringUtils.sol b/workshop/stringUtils.sol new file mode 100644 index 0000000..d9581cf --- /dev/null +++ b/workshop/stringUtils.sol @@ -0,0 +1,63 @@ +// pragma solidity 0.5.9; +// contract C { +// string s; +// function append(byte c) public { +// bytes(s).push(c); +// } + +// function set(uint i, byte c) public { +// bytes(s)[i] = c; +// } +// function get() public view returns(string memory) { +// return s; +// } +// } + + +pragma solidity ^0.5.0; + +contract stringUtils { + + function stringToUint(string memory str) public view returns(uint) { + bytes memory strBytes = bytes(str); + uint res = 0; + uint pow = 10; + bytes4 temp; + + for(uint i = 0; i < strBytes.length; i++) { + res *= pow; + temp = bytes4(strBytes[i]) >> 24; + res += uint32(temp)-48; + } + return res; + } + + function stringCompare (string memory str1, string memory str2) public view returns (bool isSame) { + if(keccak256(bytes(str1)) == keccak256(bytes(str2))) { + isSame = true; + } + } + + function stringLen(string memory str) public view returns (uint) { + return bytes(str).length; + } + + function stringConcat(string memory str1, string memory str2) public view returns (string memory) { + bytes memory str_1 = bytes(str1); + bytes memory str_2 = bytes(str2); + bytes memory str_b = new bytes(str_1.length + str_2.length); + uint i = 0; + uint j = 0; + + while (i < str_1.length) { + str_b[j++] = str_1[i++]; + } + + i=0; + + while (i < str_2.length) { + str_b[j++] = str_2[i++]; + } + return string(str_b); + } +}