Skip to content

Instantly share code, notes, and snippets.

View traderbagel's full-sized avatar

traderbagel traderbagel

View GitHub Profile
@traderbagel
traderbagel / csdn_pdf.js
Last active October 10, 2020 05:34
CSDN Blog Article to PDF
// run in chrome console
(function(){
$("#side").remove();
$("#comment_title, #comment_list, #comment_bar, #comment_form, .announce, #ad_cen, #ad_bot").remove();
$(".nav_top_2011, #header, #navigator").remove();
$(".p4course_target, .comment-box, .recommend-box, #csdn-toolbar, #tool-box").remove();
$("aside").remove();
$(".tool-box").remove();
$("#toolBarBox").remove();
$("main").css('display','content');
@traderbagel
traderbagel / datetime_timestamp.py
Created August 20, 2020 09:01
python datatime to timestamp and vice-versa
from datetime import datetime
ts = 1597809600
dt = datetime.fromtimestamp(ts)
# current date and time
now = datetime.now()
ts = datetime.timestamp(now)
@traderbagel
traderbagel / bitfinex-lending-bot.py
Created May 21, 2020 06:56
bitfinex-lending-bot
import time
import base64
import json
import hmac
import hashlib
import requests
URL = 'https://api.bitfinex.com'
API_VERSION = '/v1'
API_KEY = ''
@traderbagel
traderbagel / construct_a_tuple_with_1_elements.py
Created June 30, 2019 15:13
Contruct a tuple with only one elements
# tuple
single_tuple = (1,)
# integer
# Python thinks it's part of a mathematical operation and has precedence
not_tuple = (1)
@traderbagel
traderbagel / delete_dict_key.py
Created June 28, 2019 07:35
python delete dictionary key #python
# 1. get deleted value
my_dict.pop('key', None)
# 2. simple but not atomic
if 'key' in my_dict: del my_dict['key']
# 3. atomic
try:
del my_dict['key']
except KeyError:
@traderbagel
traderbagel / bnb_transfer_solidity.sol
Created September 4, 2018 16:04
bnb_transfer_solidity
/* event定義中的 keyword `indexed` 會被放在 topics當成參數做查詢, topics最多有四個, 第一個保留為 event的 hash值(識別用) */
event Transfer(address indexed from, address indexed to, uint256 value);
/* Send coins */
function transfer(address _to, uint256 _value) {
if (_to == 0x0) throw; // Prevent transfer to 0x0 address. Use burn() instead
if (_value <= 0) throw;
if (balanceOf[msg.sender] < _value) throw; // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
balanceOf[msg.sender] = SafeMath.safeSub(balanceOf[msg.sender], _value); // Subtract from the sender
@traderbagel
traderbagel / crawl_bnb_transaction.py
Created September 4, 2018 11:04
crawl_bnb_transaction.py
import datetime
import concurrent.futures
from collections import namedtuple
BNB_transaction = namedtuple('BNB_transactions', ['hash', 'created_at', 'block', 'from_address', 'to_address', 'amount'])
def get_receipt(tx):
if isinstance(tx, AttributeDict):
tx = tx.hash
return web3.eth.getTransactionReceipt(tx)
@traderbagel
traderbagel / web3_query_transaction_with_logs.py
Created September 4, 2018 10:54
web3_query_transaction_with_logs.py
>>> web3.eth.getTransactionReceipt('0xba48fc0e658bfd927370ae0dda6fd37793e504e90289cd8216e986ebf714699b')
AttributeDict({
'blockHash': HexBytes('0xf16ea2ee219d8ae91ce7dae021cfd9a5a9e959bd85d78b74be1ca4444e994a81'),
'blockNumber': 6259346,
'contractAddress': None,
'cumulativeGasUsed': 7095293,
'from': '0x98702707fe04c38ce752afad9c392f4a46289274',
'gasUsed': 22514,
'logs': [AttributeDict({
'address': '0xB8c77482e45F1F44dE1745F52C74426C631bDD52',
@traderbagel
traderbagel / visualize_txs_network.py
Last active July 21, 2018 12:25
visualize txs network
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
figure(num=None, figsize=(20, 16), dpi=80, facecolor='w', edgecolor='k')
import networkx as nx
import math
pos = nx.spring_layout(G)
# Draw Nodes
@traderbagel
traderbagel / build_graph_from_transactions.py
Last active July 21, 2018 12:25
build graph from transactions
from collections import defaultdict
from decimal import Decimal
import networkx as nx
edges = defaultdict(lambda :defaultdict(Decimal))
for tx in eth_transactions:
edges[tx.from_address][tx.to_address] += tx.amount
G = nx.DiGraph()
G.add_weighted_edges_from([(f, t, edges[f][t]) for f