Skip to content

Instantly share code, notes, and snippets.

Avatar

Mikko Ohtamaa miohtama

View GitHub Profile
View erdpy-failed-transaction.py
from erdpy.accounts import Account, Address
from erdpy.proxy import ElrondProxy
from erdpy.transactions import BunchOfTransactions
from erdpy.transactions import Transaction
from erdpy.wallet import signing
proxy = ElrondProxy("https://devnet-gateway.elrond.com")
sender = Account(pem_file="test-wallet.pem")
sender.sync_nonce(proxy)
View colormapper.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
pragma abicoder v2;
contract ArrayTest {
function colourToString(uint r, uint g, uint b) private pure returns(string memory) {
bytes memory alphabet = "0123456789abcdef";
@miohtama
miohtama / fast-web3-py-event-decoder.py
Created Aug 19, 2021
Fast event fetcher and decoder for web3.py
View fast-web3-py-event-decoder.py
"""Below is an example for faster JSON-RPC event fetcher.
It skips a lot of steps, like converting raw binary values to corresponding numbers (float, ints),
looking up ABI labels and building `AttributedDict` object.
The resulting Event dictitionary is generated faster, but harder to use and you need to know what you are doing.
"""
def _fetch_events_for_all_contracts(
@miohtama
miohtama / write-parquet-larger-than-ram.py
Created Jul 14, 2021
Writing Parquet files incrementally from Python
View write-parquet-larger-than-ram.py
with pq.ParquetWriter(
fname,
Candle.to_pyarrow_schema(small_candles),
compression='snappy',
allow_truncated_timestamps=True,
version='2.0', # Highest available schema
data_page_version='2.0', # Highest available schema
) as writer:
def reset_data():
@miohtama
miohtama / ethereum_address_type_for_sqlalchemy.py
Created Jun 27, 2021
Ethereum address to for SQLAlchemy
View ethereum_address_type_for_sqlalchemy.py
class EthereumAddress(types.TypeDecorator):
"""SQLAlchemy class to store Ethereum addresses as binary in SQL database.
Ethereum address is 160 bits, the last bytes of 256 bit public key of the address's signer.
Ethereum address checksum is encoded in the case of hex letters.
We skip any Ethereum address checksum checks, as they slow down large data processing too much.
Any returned address data will be in lowercase.
"""
@miohtama
miohtama / approximate_row_count_sqlalchemy.py
Created Jun 13, 2021
Fast approximate_row_count with TimescaleDB and SQLAlchemy
View approximate_row_count_sqlalchemy.py
import logging
import time
import timeit
from typing import Type
from sqlalchemy.orm import Session
from sqlalchemy.sql import text
from dex_ohlcv.db import get_real_database
from dex_ohlcv.models.uniswap import Base, UniswapLikeCandle, Pair
@miohtama
miohtama / python-redis-json-dataclass-example.py
Created May 18, 2021
Python simple persistent database with dataclass, JSON and Redis.
View python-redis-json-dataclass-example.py
"""A simple trade execution report database by using Python dataclass, Redis and JSON.
- A very simple database for arbitrage trade execution reports.
- Dataclasses are serialised and deserialised to JSON that is stored in the Redis.
- There is type validation for members and class-types.
- Class-types like Decimal are converted back to their original format upon deserialisation.
- Optional members are supported and the member presence is validated.
- Past trades can be iterated in either order by creation.
- A simple CSV exported is provided.
@miohtama
miohtama / Generating Ethereum addresses from mnemonic seed words.js
Created Apr 14, 2021
Generating Ethereum addresses from mnemonic seed words
View Generating Ethereum addresses from mnemonic seed words.js
const HDWallet = require('ethereum-hdwallet')
const mnemonic = 'your seed prhase goes here'
const hdwallet = HDWallet.fromMnemonic(mnemonic)
for(let i=0; i<1000; i++) {
console.log(`0x${hdwallet.derive(`m/44'/60'/0'/0/${i}`).getAddress().toString('hex')}, 3.33`)
}
View start_ipython_within_your_app.py
def _console(context: ProcessContext):
imported_objects = {}
import datetime
from IPython import embed
from dex_ohlcv.models.base import Base
imported_objects["db_session_scoper"] = context.create_db_session_scoper()
imported_objects["web3"] = context.create_web3()
imported_objects["datetime"] = datetime
@miohtama
miohtama / price_feed_nearest_point_of_time.py
Created Apr 2, 2021
SQLAlchemy crypto price feed class and find the nearest price to a timepoint
View price_feed_nearest_point_of_time.py
import enum
import datetime
import sqlalchemy as sa
from sqlalchemy import case, union_all
from sqlalchemy.orm import Session, aliased
from .utils import TimeStampedBaseModel