Skip to content

Instantly share code, notes, and snippets.

@goodjoon
Created April 18, 2017 15:19
Show Gist options
  • Select an option

  • Save goodjoon/7421cf5f55061e6beb33d8995351b554 to your computer and use it in GitHub Desktop.

Select an option

Save goodjoon/7421cf5f55061e6beb33d8995351b554 to your computer and use it in GitHub Desktop.
[HYUNDAI] IoTInsurance.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Iot Insurance</title>
<script type="text/javascript" src="../bower_components/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="../bower_components/web3/dist/web3.js"></script>
</head>
<body>
<h2>IoT Insurance</h2>
<div>
<h3>Contract</h3>
<button id="btnCreate">Create</button>
<input type="text" id="_contractAddress" size="20"/>
<button id="btnAt">At</button>
<h4>Enroll to Insurance</h4>
Device Address : <input type="text" id="_deviceAddress" size="50"/><br/>
Device Name : <input type="text" id="_deviceName" size="30"/><br/>
Device Type : <input type="text" id="_deviceType" size="20"/><br/>
Owner's Address : <input type="text" id="_ownerAddress" size="50"/><br/>
<button id="btnSignContract">Enroll to Insurance</button>
<h4>Report Accident</h4>
My Device Address : <input type="text" id="_accidentDeviceAddress" size="50"/><br/>
Opponent Address : <input type="text" id="_opponentAddress" size="50"/><br/>
Opponent Damage : <input type="text" id="_opponentDamage" size="10"/> %<br/>
<button type="text" id="btnReport">Report</button>
</div>
<h3>Log</h3>
<div>
<textarea id="_log" rows="7" cols="80"></textarea>
</div>
<script type="text/javascript">
function _log(content) {
var logger = $('#_log').append(content).append("\n");
logger.scrollTop(logger[0].scrollHeight);
}
// web3 Initialization (Set web3 Provider)
var Web3 = require('web3');
//var web3 = new Web3(new Web3.providers.HttpProvider("http://192.168.0.169:8546"));
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
//var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8546"));
// Filter latest block
var latestFilter = web3.eth.filter('latest');
latestFilter.watch(function(error, result) {
var newBlock = web3.eth.getBlock(result);
var log = "[NEW BLOCK] " + newBlock.number + "\n";
log += "Txs : " + newBlock.transactions.length + "\n";
_log(log);
});
function addHandlers(insurance) {
insurance.ContractSigned(function(error, result) {
var log = "\n## Contract Signed ##\n";
log += "\n Owner : " + result.args.owner;
log += "\n Device Name : " + result.args.deviceName;
log += "\n Device Type : " + result.args.deviceType;
log += "\n Device Address : " + result.args.device;
_log(log);
});
insurance.Approved(function(error, result) {
var log = "\n## Transfer Approval ##\n";
log += "\nDevice Name : " + result.args.deviceName;
log += "\nDevice Address : " + result.args.device;
log += "\nApproved Token Amount : " + result.args.token;
_log(log);
});
insurance.PaidFor(function (error, result) {
var log = "\n## Token Paid\n";
log += "\nPaid to : " + result.args.opponent;
log += "\nPaid Token Amount : " + result.args.amount;
log += "\nDate : " + new Date(result.args.date * 1000);
_log(log);
});
}
var code = "6060604052341561000c57fe5b5b610bb08061001c6000396000f30060606040523615610055576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630f34b73f1461006b5780638f4ffcb1146100aa578063938113021461014b575b341561005d57fe5b6100695b60006000fd5b565b005b341561007357fe5b6100a8600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610226565b005b34156100b257fe5b610149600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091905050610440565b005b341561015357fe5b610224600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506106d4565b005b600060006103e883029150600060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000209050806006015481600501540382111561028a5760006000fd5b8060040160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166323b872dd8260030160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1686856000604051602001526040518463ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019350505050602060405180830381600087803b15156103ae57fe5b60325a03f115156103bb57fe5b50505060405180519050507f2f481f70de5c15faddf21faa7dacb05892f4255c8fc8b2f0aead35cd5ec4eb1e848342604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001838152602001828152602001935050505060405180910390a15b50505050565b600060006000600160008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002090508473ffffffffffffffffffffffffffffffffffffffff168160030160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415156105415760006000fd5b828160040160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550838160050181905550818160070190805190602001906105a5929190610a5f565b507f9465e045c75591d39dfb7963fbe54404b94b51001ecc26cd60a6dee5283e67608160000160000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16826000016001018360050154604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001806020018381526020018281038252848181546001816001161561010002031660029004815260200191508054600181600116156101000203166002900480156106bc5780601f10610691576101008083540402835291602001916106bc565b820191906000526020600020905b81548152906001019060200180831161069f57829003601f168201915b505094505050505060405180910390a15b5050505050565b6060604051908101604052808473ffffffffffffffffffffffffffffffffffffffff16815260200183815260200182815250600060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060000160008201518160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060208201518160010190805190602001906107ab929190610adf565b5060408201518160020190805190602001906107c8929190610adf565b5090505083600060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060030160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555082600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055507f297bf16facc1fb7b5eaa7398fa825a9100ff06d67c14e1e6f78ed4be90b78b3084848484604051808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200180602001806020018381038352858181518152602001915080519060200190808383600083146109a7575b8051825260208311156109a757602082019150602081019050602083039250610983565b505050905090810190601f1680156109d35780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360008314610a1b575b805182526020831115610a1b576020820191506020810190506020830392506109f7565b505050905090810190601f168015610a475780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a15b50505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610aa057805160ff1916838001178555610ace565b82800160010185558215610ace579182015b82811115610acd578251825591602001919060010190610ab2565b5b509050610adb9190610b5f565b5090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610b2057805160ff1916838001178555610b4e565b82800160010185558215610b4e579182015b82811115610b4d578251825591602001919060010190610b32565b5b509050610b5b9190610b5f565b5090565b610b8191905b80821115610b7d576000816000905550600101610b65565b5090565b905600a165627a7a72305820c1a85ce1751ef658f0f145fd144cd77be4b711222cd37fe5ce350f73a1d278f00029";
var abi = [{"constant":false,"inputs":[{"name":"_opponent","type":"address"},{"name":"_opponentDamage","type":"uint256"}],"name":"reportAccident","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_value","type":"uint256"},{"name":"_token","type":"address"},{"name":"_extraData","type":"bytes"}],"name":"receiveApproval","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_owner","type":"address"},{"name":"_device","type":"address"},{"name":"_deviceName","type":"string"},{"name":"_deviceType","type":"string"}],"name":"signContract","outputs":[],"payable":false,"type":"function"},{"payable":false,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"owner","type":"address"},{"indexed":false,"name":"device","type":"address"},{"indexed":false,"name":"deviceName","type":"string"},{"indexed":false,"name":"deviceType","type":"string"}],"name":"ContractSigned","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"device","type":"address"},{"indexed":false,"name":"deviceName","type":"string"},{"indexed":false,"name":"token","type":"uint256"}],"name":"Approved","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"opponent","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"date","type":"uint256"}],"name":"PaidFor","type":"event"}];
var Contract = web3.eth.contract(abi);
var insurance = null;
$(document).ready(function() {
// Unlock First Account
//web3.personal.unlockAccount(web3.eth.accounts[0],'0000');
console.log("UNLOCKED");
$('#btnCreate').click(function(){
insurance = Contract.new(
{
data:code,
gas:2000000,
from:web3.eth.accounts[0]
}, function (error, contract) {
if (!error) {
if (!contract.address) {
_log("Creating Contract : " + contract.transactionHash);
} else {
address = contract.address;
_log("Contract Deployed : " + contract.address);
addHandlers(contract);
}
} else
console.error(error);
});
});
$('#btnAt').click(function(){
var address = $('#_contractAddress').val();
insurance = Contract.at(address);
addHandlers(insurance);
});
$('#btnSignContract').click(function() {
var tx = insurance.signContract.sendTransaction($('#_ownerAddress').val(), $('#_deviceAddress').val(),
$('#_deviceName').val(), $('#_deviceType').val(), {
from: web3.eth.accounts[0]
,gas: 2000000
});
_log("signContract : " + tx);
});
$('#btnReport').click(function() {
var tx = insurance.reportAccident.sendTransaction($('#_opponentAddress').val(), $('#_opponentDamage').val(),
{
from: $('#_accidentDeviceAddress').val()
,gas: 2000000
});
_log("reportAccident : " + tx);
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment