Skip to content

Instantly share code, notes, and snippets.

@goodjoon
Created April 16, 2017 16:30
Show Gist options
  • Select an option

  • Save goodjoon/0d81980a807341ebc059ad015de644a9 to your computer and use it in GitHub Desktop.

Select an option

Save goodjoon/0d81980a807341ebc059ad015de644a9 to your computer and use it in GitHub Desktop.
[HYUNDAI] IOT INSURANCE
pragma solidity ^0.4.2;
contract tokenReceipient {
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData);
}
contract HyundaiToken {
function transferFrom(address _from, address _to, uint256 _value) returns (bool success_);
}
contract IoTInsurance is tokenReceipient {
// 등록된 Device
struct Device {
address addr; // Device 의 Address
string name; // Device Name
string deviceType; // Device Type
}
// 보험 가입 계약
struct Insurance {
Device device;
address owner; // Device 의 Owner
HyundaiToken ownerToken;
uint approvedToken;
uint spentToken;
bytes extraData;
}
mapping (address => Insurance) signedContracts; //device address to Insurance
mapping (address => address) ownedDevice;
event ContractSigned (address owner, address device, string deviceName, string deviceType);
event Approved (address device, string deviceName, uint token);
event PaidFor (address opponent, uint amount, uint date);
// owner 가 device 를 보험에 가입시킨다
function signContract(address _owner, address _device, string _deviceName, string _deviceType) {
signedContracts[_device].device = Device({
addr: _device
,name: _deviceName
,deviceType: _deviceType
});
signedContracts[_device].owner = _owner;
ownedDevice[_owner] = _device;
ContractSigned(_owner, _device, _deviceName, _deviceType);
}
// Token 의 approval 을 받음
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) {
Insurance ins = signedContracts[ownedDevice[_from]];
if (ins.owner != _from)
throw;
ins.ownerToken = HyundaiToken(_token);
ins.approvedToken = _value;
ins.extraData = _extraData;
Approved (ins.device.addr, ins.device.name, ins.approvedToken);
}
// 사고가 발생한 경우 내 damage 와 상대방 damage 의 비율 중 내 비율의 금액을 상대방으로 이체 요청
function reportAccident (address _opponent, uint _opponentDamage) {
uint payAmount = _opponentDamage * 1000;
Insurance ins = signedContracts[msg.sender];
if (payAmount > (ins.approvedToken - ins.spentToken))
throw;
ins.ownerToken.transferFrom(ins.owner, _opponent, payAmount);
PaidFor(_opponent, payAmount, now);
}
function () {
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment