Skip to content

Instantly share code, notes, and snippets.

@dseg
dseg / estimategas.sh
Created Mar 28, 2018
Estimate Gas (of Ethereum)
View estimategas.sh
#!/bin/sh
# Requirements: geth
fee=$(geth attach ipc:/home/ubuntu/main_net/geth.ipc --exec 'web3.fromWei(web3.eth.gasPrice*web3.eth.estimateGas({from:"0xc1912fee45d61c87cc5ea59dae31190fffff232d"}))')
date
echo -n $fee | tr -d \"
echo \ ETH.
View A Simple TCP Server by Rust
use std::io::Write;
use std::net::{TcpListener, TcpStream};
use std::thread;
fn handle_client(mut stream: TcpStream) {
thread::spawn(move || {
stream.write(b"Hello World\r\n").unwrap();
});
}
View Satoshi to BTC
# Usage: btc bitcoin_amount_in_satoshi
# $ btc 1550000000
# 15.50000 BTC
#
function btc {
if [[ -n $1 ]]; then
btc=$(bc -l <<< "$1 / 100000000.0")
printf '%.5f BTC\n' $btc
fi
}
@dseg
dseg / jst.sh
Created Mar 30, 2017
Output now in BusyBox (for example, Alpine Linux) as Japan Standard Time
View jst.sh
TZ=JST-9 date -Iseconds|tr T ' '|tr - /|head -c 19
@dseg
dseg / makeQueryString.ts
Created Feb 23, 2017
Crafting a query string from an array using ES6's template literal feature
View makeQueryString.ts
/**
* Template String Tag (For crafting query-string for HTTP/Get from a array)
*
* Usage: qsTag`${['a','b','c','d']}`(); // a=b&c=d
*/
export const qsTag = (strings, ...keys) => (...values) => {
let result = [];
const input = keys[0];
input.forEach((key: any, i: number, ary: any[]) => {
const postfix = (ary.length-1 === i ? '' : '&');
@dseg
dseg / select.tsx
Created Jan 27, 2017
A React Select Snippet
View select.tsx
<select>
{
// options が undefined or nullなら、何も出力しない。配列なら選択肢に整形する。
options && Array.isArray(options) && options.map((item : any, idx : number) =>
<option
key={`inq_opt_${idx}`}
value={item.value}>{item.label}</option>)
}
</select>
@dseg
dseg / base64_encode_decode.sh
Created Jan 5, 2017
Base64 Encode & Decode
View base64_encode_decode.sh
#!/bin/bash
function b64enc {
# エンコード。-e は省略可。
if [[ -n $1 ]] && [[ -f $1 ]]; then
openssl enc -e -base64 "$1"
fi
}
function b64dec {
@dseg
dseg / tscc.sh
Created Dec 22, 2016
Compile and run a typescript file
View tscc.sh
function tscc {
local target
if [[ -n $2 ]]; then
target="$2"
else
target=es6
fi
tsc -t "$target" "$1" -outFile /dev/stdout | node
}
@dseg
dseg / template-literal-1.ts
Created Dec 17, 2016
An ES6 Template Literal experiment
View template-literal-1.ts
let tag = (strings, ...keys) => (...values) => {
//console.log(strings); // [ '', 'は', 'です。' ]
//console.log(keys); // [ 0, 1 ]
let dict = values[values.length - 1] || {};
let result = [strings[0]];
keys.forEach((key: any, i: number) => {
let value = Number.isInteger(key) ? values[key] : dict[key];
result.push(value, strings[i + 1]);
});
return result.join('');
@dseg
dseg / confirm-dialog.tsx
Created Dec 15, 2016
stateless-function-component-in-typescript
View confirm-dialog.tsx
import * as React from 'react';
interface IInquiryConfirmDialogProps {
message: string;
onClick?: (evt: any) => void;
}
const InquiryConfirmDialog: React.StatelessComponent<IInquiryConfirmDialogProps> = (props: IInquiryConfirmDialogProps) =>
<div className="modal-box">
<form>
You can’t perform that action at this time.