Skip to content

Instantly share code, notes, and snippets.

View akirattii's full-sized avatar

Akira TANAKA akirattii

View GitHub Profile
@akirattii
akirattii / background.js
Created December 2, 2016 03:45
Message passing of Chrome Extension example
/*****************************************************************
* onMessage from the extension or tab (a content script)
*****************************************************************/
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.cmd == "any command") {
sendResponse({ result: "any response from background" });
} else {
sendResponse({ result: "error", message: `Invalid 'cmd'` });
}
@akirattii
akirattii / how-long-your-mining-block-found.js
Created December 4, 2016 11:57
How to know how long your mining block will be found on Ethereum.
// How long it will take for finding your mining block
// will be known from your 'hashrate' and the latest block's 'difficulty':
etm = eth.getBlock("latest").difficulty / miner.hashrate; // estimated time in seconds
Math.floor(etm / 3600.) + "h " + Math.floor((etm % 3600) / 60) + "m " + Math.floor(etm % 60) + "s";
// 1h 3m 30s
// ref. https://ethereum.gitbooks.io/frontier-guide/content/mining_with_geth.html
@akirattii
akirattii / Crowdsale.sol
Last active December 22, 2016 10:29
Ethereum クラウドセール・コントラクト(β)
pragma solidity ^0.4.7;
// =======================================================
// クラウドセール・コントラクトのおおまかな処理
// =======================================================
// + コンストラクタで独自コインの総量などを決定
//
// + 出資者からの資金提供と引き換えに独自コインを渡す
//
@akirattii
akirattii / ImperialDiet.sol
Last active December 23, 2016 23:14
Ethereum帝国議会コントラクトβ
pragma solidity ^0.4.7;
// # テスト用パラメータ
//
// ## テスト・シチュエーション
// - 受益者(臣民4)に利益供与(コインを送金)するかどうかを議会で決めます
//
// ## 登場人物のアドレス
// - 初代皇帝(emperor): accounts[0] 0x4a1c11eaec40197beb6e8607ee61953e7d6d8731
// - 臣民1(subject1): accounts[1] 0x5855a19b64e1068a1edc39bb817647e16a1c57e7
@akirattii
akirattii / ZaifAPI-WebSocketClient-Example.html
Last active January 10, 2017 02:43
Example of Zaif WebSocket Stream API
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Zaif WebSocket Stream API Example</title>
</head>
<body>
<button id="btn-start">start</button>
@akirattii
akirattii / step-with-parallel-example.js
Last active January 19, 2017 09:38
node step module example using `parallel()`
var Step = require("step");
function asyncFn(str, cb) {
setTimeout(function() {
let err = null;
let result = " Hello " + str;
return cb(err, result); // error-first style callback
}, 1000);
}
@akirattii
akirattii / step-without-error-thrown.js
Created January 19, 2017 09:55
node step module example without error throwing to avoid any troubles related to it
var Step = require("step");
function genRandomError() {
let i = Math.floor(Math.random() * 2) + 1;
if (i === 1) {
return Error("Error occured!");
}
return "Success";
}
@akirattii
akirattii / sse-serverside-example.js
Last active April 26, 2024 11:15
Server-Sent Events nodejs example. This shows how to detect the client disconnection.
var express = require('express');
var app = express();
// response header for sever-sent events
const SSE_RESPONSE_HEADER = {
'Connection': 'keep-alive',
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no'
};
@akirattii
akirattii / .tern-project
Last active June 27, 2017 22:24
ternjs project file for sublime text3 which gives availability of auto completion corresponding to export module pattern.
{
"libs": [
"browser",
"jquery"
],
"dontLoad": [
"node_modules/**",
"**/*/bundle.js",
"**/*/extlib/**"
],
@akirattii
akirattii / step-with-generator.js
Created January 30, 2017 04:11
Synchronous iteration for milding cpu overload using node-step with generator
const Step = require("step");
Step(
//
// ** 1st Step:
//
function() {
console.log("### 1st Step:");
let arr = ["aaa", "bbb", "ccc"];
let err, result;