Skip to content

Instantly share code, notes, and snippets.

View trepca's full-sized avatar
🔜

Sebastjan Trepca trepca

🔜
  • Slovenia
  • 20:26 (UTC -12:00)
  • X @trepca
View GitHub Profile
@trepca
trepca / smart_coin.py
Last active March 18, 2024 11:46
Minimal smart coin example on Chia blockchain
import time
from chia.clvm.spend_sim import SimClient, SpendSim
from chia.types.blockchain_format.program import Program
from chia.types.coin_spend import make_spend
from chia.types.condition_opcodes import ConditionOpcode
from chia.types.mempool_inclusion_status import MempoolInclusionStatus
from chia.util.hash import std_hash
from chia.util.ints import uint64
from chia_rs import CoinSpend, G2Element, SpendBundle
@trepca
trepca / debug_assert.cl
Last active December 12, 2023 06:31
Chialisp assert macro for debugging by @prozacchiwawa
(mod (N)
(include *standard-cl-23*)
(defun assert_ (items)
(if (r items)
(qq (if (unquote (f items)) (unquote (assert_ (r items))) (x (unquote (c 1 (f items))))))
(f items)
)
)
@trepca
trepca / chia_nc.sql
Created November 10, 2023 16:27
Calculate Nakamoto Coefficient for Chia
select count(*)
from (select ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS enumerator,
puzzle_hash,
percent,
SUM(percent) OVER (ORDER BY percent desc ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as percent_sum
from (select puzzle_hash, count(*) as blocks, count(*) / bb.block_count::float * 100 as percent
from coin_records,
-- this is to get the total number of blocks in the last 7 days
(select count(*) as block_count
from coin_records
@trepca
trepca / track_coin_id.py
Created October 16, 2023 19:11
Track down spend bundles that spent coin and it's descendants
import requests
import sys
import json
coin_ids = [sys.argv[1]]
transactions = []
cache = {}
while coin_ids:
additions = []
@trepca
trepca / cat_balance.sql
Last active August 20, 2023 18:15
Get CAT balance for a given puzzle hash. This query can be run on https://mojonode.com/explorer
select
-- second parameter of CAT_v2.clsp puzzle is TAIL, so we take it here (indexing starts from 0)
parent.puzzle->'a'->>1 as TAIL,
-- we sum the amount of all unspent coins we fetch
sum(amount)::bigint as amount,
-- we also provide the count of all coins for convenience
count(*) as number_of_coins
from chia.coin_spends parent
-- here we join with coin_records to get unspent coins
-- (we need to also use block name here due to how these tables are distributed in the cluster)

Keybase proof

I hereby claim:

  • I am trepca on github.
  • I am trepca (https://keybase.io/trepca) on keybase.
  • I have a public key ASDfv4y5cduU6DaOTfz4zdWO1aPVfA0F9rWiNoCJdn_ivwo

To claim this, I am signing this object:

[] '60V5Sa_nPMGn5k9701tWR6.5S5KnS3C81LCgPLKiS4xdO.FrBI3m4fXnmQLEeD6teV3LsemEYC9.cuxR5k.a..0cDWyA1.........1fN52SmA.eQXJ2drt0JMt9PM98XRIzzaa6TCYpZsfmmshI7Inumk7.FLVS0OZ....L403.' UNWRAP +! '60V5Sa_nPMGn5k9701tWR6.5S5KnS3C81LCgPLKiS4xdO.RsDI3mBnVt4fXnmQLEeD6teV3Lq9fNsi25khL6.Gg.8V..P3VUXkV.........eq0VrRWkpTBj.uSyFK4yHa_mXP63zySjMb5mumRKtORMdofwMvvmDWDYEZFJfgcN7YvwS.3.XzdatG30...L903.' UNWRAP +! '60V5Sa_nPMGn5k9701tWR6.5S5KnS3C81LCgPLKiS4xdO.BlBYBLiAE8hNycsUae.GQcmSHjitEkq9s05k.a..0cMGyA1.........1fN52Sm62gpoB5Ijq1cjmJoiFHJsbzqrRJgTgb4TLM4uS9z8wyPkwAMC8FsjzyktEOiULAHtD9ZsgmZp8IkBet3.ANSNkPEl25.k2ojyP7rV3..0Nw3F.' UNWRAP +! '60V5Sa_nPMGn5k9701tWR6.5S5KnS3C81LCgPLKiS4xdO.JtD23mBlPsgwapYuYWyPc04iHMpAEHYiXbpF3Q.1N..5W76sg7.........9hVNCr7o6icZ74IEnZYCIZvjvG3sjzm_STOqELIXjAAXpB_znxzwscI7gPXbpGP9F3L53A9.T8OdWbY....4XNG..' UNWRAP +! '60V5Sa_nPMGn5k9701tWR6.5S5KnS3C81LCgPLKiS4xdO.FsBn3m4fXnmQLEeD6teV3LrhaGfhmgYSbm.Gg.8V..P2FUXkV.........eq0VrRXkq6kq.uSyFK4yHa_mXO63zztdWrYVU4BoEhr_.KCrSEDY....4WFG..' UNWRAP +! '60V5Sa_n
@trepca
trepca / first.markdown
Created July 19, 2012 10:26
My first gist entry!

Testing gist.io

Hello world!

@trepca
trepca / counter.py
Created May 21, 2011 20:58
Sharded counters for Django
# -*- coding: utf-8 -*-
"""Efficient django counters that support hundreds updates/sec
Based on Google Appengine sharded counters http://code.google.com/appengine/articles/sharding_counters.html
"""
from django.db import models
from django.db.models import Sum, F
import random
from django.db import transaction, IntegrityError
from django.core.cache import cache
class Todo(models.Model):
title = models.CharField(max_length=128)
owner = models.ForeignKey(User)
body = models.TextField(blank=True, null=True)
due_date = models.DateTimeField(blank=True, null=True)
start_date = models.DateTimeField(blank=True, null=True)
date_created = models.DateTimeField(auto_now_add=True)
location = models.CharField(max_length=128, blank=True, null=True)
all_day = models.BooleanField(default=False)