Skip to content

Instantly share code, notes, and snippets.

Created November 15, 2017 14:38
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 anonymous/2c42d6137295611f3c7db65cc995f8ca to your computer and use it in GitHub Desktop.
Save anonymous/2c42d6137295611f3c7db65cc995f8ca to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.18+commit.9cf6e910.js&optimize=undefined&gist=
pragma solidity ^0.4.0;
contract Greetable {
function Greetable() public {}
function greet() constant public returns (bytes32);
}
pragma solidity ^0.4.0;
import './Greetable.sol';
contract Greeter1 is Greetable {
bytes32 constant greeting = "Hi!";
function Greeter1() public {
}
function greet() constant public returns (bytes32) {
return greeting;
}
}
pragma solidity ^0.4.0;
import './Greetable.sol';
contract Greeter2 is Greetable {
bytes32 constant greeting = "Hello!";
function Greeter2() public {
}
function greet() constant public returns (bytes32) {
return greeting;
}
}
pragma solidity ^0.4.0;
import './Greetable.sol';
contract GreeterContainer is Greetable {
Greetable greeter;
address owner;
function GreeterContainer(address greeter_address) public {
greeter = Greetable(greeter_address);
owner = msg.sender;
}
function setGreeter(address _greeter_address) public {
require(owner == msg.sender);
greeter = Greetable(_greeter_address);
}
function greet() constant public returns (bytes32) {
return greeter.greet();
}
function greetString() constant public returns (string) {
return bytes32ToString(greeter.greet());
}
// bytes32 to string converter
function bytes32ToString(bytes32 x) constant internal returns (string) {
bytes memory bytesString = new bytes(32);
uint charCount = 0;
for (uint j = 0; j < 32; j++) {
byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
if (char != 0) {
bytesString[charCount] = char;
charCount++;
}
}
bytes memory bytesStringTrimmed = new bytes(charCount);
for (j = 0; j < charCount; j++) {
bytesStringTrimmed[j] = bytesString[j];
}
return string(bytesStringTrimmed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment