Skip to content

Instantly share code, notes, and snippets.

@laalaguer
laalaguer / find-account-has-code.py
Last active February 28, 2022 09:23
Find block that one account has code
''' Python script to find the earliest block that an account has code.
python3 find.py [node_url] [address]
'''
import sys
from typing import Union
from thor_requests.connect import Connect
from math import sqrt
class Uniswap:
def __init__(self, xs):
self.xs = xs
self.lastK = sqrt(xs[0] * xs[1])
def swap(self, i, dx):
d = self.xs[0] * self.xs[1]
# Speed up.
cache = {}
def f(n):
# Speed up.
if cache.get(n) != None:
return cache[n]
if n == 1:
print('n==1, directly return 1')
# No speed up.
def f(n):
if n == 1:
print('n==1, directly return 1')
return 1
p = [] # Storage for temp results.
for i in range(1, n): # i in [1, n)
temp = 1 + max(i-1, f(n-i))
p.append(temp)
@laalaguer
laalaguer / path-finder.js
Created December 6, 2021 11:32
Uniswap V2 route (path) finder for two tokens, if given a set of pools
class PathFinder {
constructor(pairs) {
this.graph = new Map()
pairs.forEach((item) => {
if (!this.graph.has(item[0])) {
this.graph.set(item[0], new Array())
}
if (!this.graph.has(item[1])) {
this.graph.set(item[1], new Array())
}
@laalaguer
laalaguer / path-finder-recursive.js
Last active December 6, 2021 11:33
Uniswap v2 path (route) finder between two tokens, only yields one route
class DotPath {
constructor(a, b) {
this.storage = [a, b]
}
hasDot(dot) {
switch (dot) {
case this.storage[0]:
return true
case this.storage[1]:
@laalaguer
laalaguer / test_abstract.py
Last active October 12, 2021 02:43
Python Abstract Override can be done with different func params but same name, no error is raised.
import abc
class A(abc.ABC):
@abc.abstractmethod
def foo(self):
print("abstract foo()")
class B(A):
def foo(self, who:str): # Note: function signature is different
print("hello", who)
@laalaguer
laalaguer / abstract-python.py
Created September 28, 2021 05:39
Python3 Interface and Abstract class
import abc
class MyInterface(abc.ABC): # Same as class MyInterface(metaclass=abc.ABCMeta)
def __init__(self, age: int):
print('MyInterface: init')
self.age = age
@abc.abstractmethod
def load_data(self, name: str):
# CPU temperature
read temperature < /sys/class/thermal/thermal_zone0/temp
printf "CPU:\t%s °C\n" $(($temperature/1000))
# Memory pressure
memtotal=0
memfree=0
while read -r name value unit; do
if [ "${name}" == "MemTotal:" ]; then
memtotal=${value}
@laalaguer
laalaguer / over_transfer_vtho.py
Created August 24, 2021 03:40
Over Transfer VTHO using transfer() method
from thor_requests.connect import Connect
from thor_requests.wallet import Wallet
connector = Connect("https://sync-testnet.vechain.org")
# wallet address: 0x7567d83b7b8d80addcb281a71d54fc7b3364ffed
_wallet = Wallet.fromPrivateKey(bytes.fromhex("dce1443bd2ef0c2631adc1c67e5c93f13dc23a41c18b536effbbdcbcdb96fb65"))
# View the vtho this wallet has (in Wei):
vtho_balance = connector.get_vtho_balance(_wallet.getAddress())