Skip to content

Instantly share code, notes, and snippets.

@kenjirai
Created July 22, 2018 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenjirai/d1b9c135117eb39d7891d658cbd6154c to your computer and use it in GitHub Desktop.
Save kenjirai/d1b9c135117eb39d7891d658cbd6154c 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.4.24+commit.e67f0147.js&optimize=false&gist=
pragma solidity ^0.4.23;
contract Preservation {
// public library contracts
address public timeZone1Library;
address public timeZone2Library;
address public owner;
uint public storedTime;
// Sets the function signature for delegatecall
bytes4 constant setTimeSignature = bytes4(keccak256("setTime(uint256)"));
constructor(address _timeZone1LibraryAddress, address _timeZone2LibraryAddress) public {
timeZone1Library = _timeZone1LibraryAddress;
timeZone2Library = _timeZone2LibraryAddress;
owner = msg.sender;
}
// set the time for timezone 1
function setFirstTime(uint _timeStamp) public {
timeZone1Library.delegatecall(setTimeSignature, _timeStamp);
}
// set the time for timezone 2
function setSecondTime(uint _timeStamp) public {
timeZone2Library.delegatecall(setTimeSignature, _timeStamp);
}
}
// Simple library contract to set the time
contract LibraryContract {
// stores a timestamp
uint storedTime;
function setTime(uint _time) public {
storedTime = _time;
}
}
contract SecondLibraryContract {
address public timeZone1Library;
address public timeZone2Library;
address public owner;
uint public storedTime;
function setTime(uint _time) public {
bytes32 b;
assembly { mstore(add(b, 32), _time) }
}
}
pragma solidity ^0.4.23;
contract Casting {
function uint8toBytes32(uint8 value) public returns(bytes32) {
/*
input: 255
output: 0x00000000000000000000000000000000000000000000000000000000000000ff
*/
return bytes32(value);
}
function uinttoBytes32(uint value) public returns(bytes32) {
/*
input: 255
output: 0x00000000000000000000000000000000000000000000000000000000000000ff
*/
return bytes32(value);
}
function bytes32ToAddress(bytes32 provAdd) public returns(address) {
/*
input: 0x000000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733c
output: 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c
*/
return address(provAdd);
}
function bytes32ToBytes16(bytes32 convBytes32) public returns(bytes16) {
//Input bytes32
//0x000000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733c
//Output bytes16
//0x000000000000000000000000ca35b7d9
//Important Notice
//16 bytes with length size of 32 from left is being included rest are excluded.
return bytes16(convBytes32);
}
function bytes8ToUint32(bytes8 value8) public returns(uint32) {
//Study comment section below of two functions.
return uint32(value8);
}
function bytes8ToUint16(bytes8 value8) public returns(uint16) {
/*
Input bytes8 value: 0x73616e6465736872('sandeshr')
Output uint16: 26738
Since uint16 only hold 2**16 - 1 integers, EVM proceed the conversion from right
side, converting hexadecimal 0x6872 is equal to 114 any thing greater than than
will result into being greater than integer 65536.
*/
return uint16(value8);
}
function bytes8ToUint8(bytes8 value8) public returns(uint8) {
/*
Input bytes8 value: 0x73616e6465736872('sandeshr')
Output uint8: 114
Since uint8 only hold 2**8 - 1 integers, EVM proceed the conversion from right
side, converting hexadecimal 0x72 is equal to 114 any thing greater than than
will result into being greater than integer 255.
*/
return uint8(value8);
}
function retBytes8(bytes8 value8) public returns(bytes8) {
return value8;
}
function byte8ToByte16(bytes8 value8) public returns(bytes16) {
/*
bytes8 input: 0x73616e6465736872
bytes16 output: 0x73616e64657368720000000000000000
*/
return bytes16(value8);
}
function uintToAddress(uint value256) public returns(address) {
address(value256);
}
}
contract LibraryContract {
// stores a timestamp
address public timeZone1Library;
address public timeZone2Library;
address public owner;
uint storedTime;
function setTime(uint _time) public {
owner = address(bytes32(_time));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment