Skip to content

Instantly share code, notes, and snippets.

@shalahuddinn
Created December 10, 2019 06:16
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 shalahuddinn/cce34db33097981312759acd80d9eae8 to your computer and use it in GitHub Desktop.
Save shalahuddinn/cce34db33097981312759acd80d9eae8 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.13+commit.5b0b510c.js&optimize=false&gist=
pragma solidity ^0.5.0;
contract ChargingStationDeviceDemo {
uint constant IMPOSSIBLE_INDEX = 99999999999;
address owner;
struct ChargingStationDeviceDemoStruct {
string status;
}
ChargingStationDeviceDemoStruct[] items;
event ItemCreated(uint id, address createdBy);
event ItemDeleted(uint id, address deletedBy);
event ItemUpdated(uint id, address updatedBy);
event ItemSynced(uint id, address updatedBy);
event ReceivePayment(address sender, uint amount);
event FinishCharging(uint id);
constructor() public {
owner = msg.sender;
}
modifier ownerOnly {
require (msg.sender == owner);
_;
}
function getBalance() public view returns(uint){
return address(this).balance;
}
function withdraw() public ownerOnly{
msg.sender.transfer(getBalance());
}
function pay() payable public {
require(msg.value == 1 ether);
emit ReceivePayment(msg.sender, msg.value);
}
//source: https://ethereum.stackexchange.com/a/45819/57020
function compareStringsbyBytes(string memory s1, string memory s2) internal pure returns(bool){
return keccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2));
}
function getLength() public view returns(uint count) {
return items.length;
}
function addDevice(string memory _status) public {
uint id = items.length++;
items[id] = ChargingStationDeviceDemoStruct({
status: _status
});
emit ItemCreated(id, msg.sender);
}
function updateDevice(uint _id, string memory _status) public {
require(_id < items.length, "Invalid ChargingStationDeviceDemoStruct id");
items[_id].status = _status;
emit ItemUpdated(_id, msg.sender);
}
function syncDevice(uint _id, string memory _status) public {
require(_id < items.length, "Invalid ChargingStationDeviceDemoStruct id");
bool old_status = compareStringsbyBytes(items[_id].status, "on");
bool new_status = compareStringsbyBytes(_status, "off");
if (old_status == true && new_status == true) {
emit FinishCharging(_id);
}
items[_id].status = _status;
emit ItemSynced(_id, msg.sender);
}
function removeDevice(uint _id) public {
require(_id < items.length, "Invalid ChargingStationDeviceDemoStruct id");
delete items[_id];
emit ItemDeleted(_id, msg.sender);
}
function getDevice(uint _id) public view returns (string memory) {
require(_id < items.length, "Invalid ChargingStationDeviceDemoStruct id");
return (items[_id].status);
}
function indexOf(uint[] storage values, uint value) private view returns(uint) {
for (uint i = 0; i < values.length; i++) {
if (values[i] == value) {
return i;
}
}
return IMPOSSIBLE_INDEX;
}
}
pragma solidity ^0.5.0;
contract JetbotDeviceDemo {
uint constant IMPOSSIBLE_INDEX = 99999999999;
struct JetbotDeviceDemoStruct {
string battery;
string detect;
string move;
string payment;
string ram;
string speed_gain;
string steering_gain;
}
JetbotDeviceDemoStruct[] items;
event ItemCreated(uint id, address createdBy);
event ItemDeleted(uint id, address deletedBy);
event ItemUpdated(uint id, address updatedBy);
event ItemSynced(uint id, address updatedBy);
event DoPayment(uint id);
constructor() public {
}
function getLength() public view returns(uint count) {
return items.length;
}
function addDevice(string memory _battery, string memory _detect, string memory _move, string memory _payment, string memory _ram, string memory _speed_gain, string memory _steering_gain) public {
uint id = items.length++;
items[id] = JetbotDeviceDemoStruct({
battery: _battery,
detect: _detect,
move: _move,
payment: _payment,
ram: _ram,
speed_gain: _speed_gain,
steering_gain: _steering_gain
});
emit ItemCreated(id, msg.sender);
}
function updateDevice(uint _id, string memory _battery, string memory _detect, string memory _move, string memory _payment, string memory _ram, string memory _speed_gain, string memory _steering_gain) public {
require(_id < items.length, "Invalid JetbotDeviceDemoStruct id");
items[_id].battery = _battery;
items[_id].detect = _detect;
items[_id].move = _move;
items[_id].payment = _payment;
items[_id].ram = _ram;
items[_id].speed_gain = _speed_gain;
items[_id].steering_gain = _steering_gain;
emit ItemUpdated(_id, msg.sender);
}
function syncDevice(uint _id, string memory _battery, string memory _detect, string memory _move, string memory _payment, string memory _ram, string memory _speed_gain, string memory _steering_gain) public {
require(_id < items.length, "Invalid JetbotDeviceDemoStruct id");
items[_id].battery = _battery;
items[_id].detect = _detect;
items[_id].move = _move;
bool old_status = compareStringsbyBytes(items[_id].payment, "false");
bool new_status = compareStringsbyBytes(_payment, "true");
items[_id].payment = _payment;
items[_id].ram = _ram;
items[_id].speed_gain = _speed_gain;
items[_id].steering_gain = _steering_gain;
if (old_status == true && new_status == true) {
emit DoPayment(_id);
}
emit ItemSynced(_id, msg.sender);
}
//source: https://ethereum.stackexchange.com/a/45819/57020
function compareStringsbyBytes(string memory s1, string memory s2) internal pure returns(bool){
return keccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2));
}
function removeDevice(uint _id) public {
require(_id < items.length, "Invalid JetbotDeviceDemoStruct id");
delete items[_id];
emit ItemDeleted(_id, msg.sender);
}
function getDevice(uint _id) public view returns (string memory, string memory, string memory, string memory, string memory, string memory, string memory) {
require(_id < items.length, "Invalid JetbotDeviceDemoStruct id");
return (items[_id].battery,items[_id].detect,items[_id].move,items[_id].payment,items[_id].ram,items[_id].speed_gain,items[_id].steering_gain);
}
function indexOf(uint[] storage values, uint value) private view returns(uint) {
for (uint i = 0; i < values.length; i++) {
if (values[i] == value) {
return i;
}
}
return IMPOSSIBLE_INDEX;
}
}
pragma solidity ^0.5.0;
contract ChargerDeviceDemo {
address owner;
string status;
//The logs that will be emitted in every step of the contract's life cycle
event ReceivePayment(address sender, uint amount);
event betul(string message);
constructor() public{
owner = msg.sender;
status = "on";
}
modifier ownerOnly {
require (msg.sender == owner);
_;
}
function getBalance() public view returns(uint){
return address(this).balance;
}
function test(string memory pesan) public {
bool yogs = compareStringsbyBytes(status, "on");
bool yogs2 = compareStringsbyBytes(pesan, "off");
if (yogs == yogs2) {
emit betul("hahaha");
}
}
//source: https://ethereum.stackexchange.com/a/45819/57020
function compareStringsbyBytes(string memory s1, string memory s2) internal pure returns(bool){
return keccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2));
}
function withdraw() public ownerOnly{
msg.sender.transfer(getBalance());
}
function pay() payable public {
require(msg.value == 1 ether);
emit ReceivePayment(msg.sender, msg.value);
}
}
// contract JetbotDeviceDemo {
// uint constant IMPOSSIBLE_INDEX = 99999999999;
// struct JetbotDeviceDemoStruct {
// string battery;
// string detect;
// string move;
// string ram;
// string speed_gain;
// string steering_gain;
// }
// JetbotDeviceDemoStruct[] items;
// event ItemCreated(uint id, address createdBy);
// event ItemDeleted(uint id, address deletedBy);
// event ItemUpdated(uint id, address updatedBy);
// event ItemSynced(uint id, address updatedBy);
// constructor() public {
// }
// function getLength() public view returns(uint count) {
// return items.length;
// }
// function addDevice(string memory _battery, string memory _detect, string memory _move, string memory _ram, string memory _speed_gain, string memory _steering_gain) public {
// uint id = items.length++;
// items[id] = JetbotDeviceDemoStruct({
// battery: _battery,
// detect: _detect,
// move: _move,
// ram: _ram,
// speed_gain: _speed_gain,
// steering_gain: _steering_gain
// });
// emit ItemCreated(id, msg.sender);
// }
// function updateDevice(uint _id, string memory _battery, string memory _detect, string memory _move, string memory _ram, string memory _speed_gain, string memory _steering_gain) public {
// require(_id < items.length, "Invalid JetbotDeviceDemoStruct id");
// items[_id].battery = _battery;
// items[_id].detect = _detect;
// items[_id].move = _move;
// items[_id].ram = _ram;
// items[_id].speed_gain = _speed_gain;
// items[_id].steering_gain = _steering_gain;
// emit ItemUpdated(_id, msg.sender);
// }
// function syncDevice(uint _id, string memory _battery, string memory _detect, string memory _move, string memory _ram, string memory _speed_gain, string memory _steering_gain) public {
// require(_id < items.length, "Invalid JetbotDeviceDemoStruct id");
// items[_id].battery = _battery;
// items[_id].detect = _detect;
// items[_id].move = _move;
// items[_id].ram = _ram;
// items[_id].speed_gain = _speed_gain;
// items[_id].steering_gain = _steering_gain;
// emit ItemSynced(_id, msg.sender);
// }
// function removeDevice(uint _id) public {
// require(_id < items.length, "Invalid JetbotDeviceDemoStruct id");
// delete items[_id];
// emit ItemDeleted(_id, msg.sender);
// }
// function getDevice(uint _id) public view returns (string memory, string memory, string memory, string memory, string memory, string memory) {
// require(_id < items.length, "Invalid JetbotDeviceDemoStruct id");
// return (items[_id].battery,items[_id].detect,items[_id].move,items[_id].ram,items[_id].speed_gain,items[_id].steering_gain);
// }
// function indexOf(uint[] storage values, uint value) private view returns(uint) {
// for (uint i = 0; i < values.length; i++) {
// if (values[i] == value) {
// return i;
// }
// }
// return IMPOSSIBLE_INDEX;
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment