Skip to content

Instantly share code, notes, and snippets.

View ryanwaits's full-sized avatar

Ryan ryanwaits

View GitHub Profile
(define-constant MAINNET_VERSION_BYTES 0x16)
(define-constant ADDRESS 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM)
(define-read-only (get-hash-bytes)
(get hash-bytes (unwrap-err-panic (principal-destruct? ADDRESS)))
)
(principal-construct? MAINNET_VERSION_BYTES (get-hash-bytes))
;; (some 'SP1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRCBGD7R)
@ryanwaits
ryanwaits / rollback.ts
Last active April 10, 2024 15:20
Psuedocode for handling rollbacks in Chainhook
import type { ChainhookStructure } from '@/types/chainhooks-type';
import { db } from '@/utils/db'; // Assuming db is a module for database operations
export async function handleChainhookEvent(chainhookData: ChainhookStructure) {
const { apply, rollback } = chainhookData;
// Check if there's a rollback action required
if (rollback.length) {
console.log(`Handling rollback for ${rollback.length} blocks`);
for (const rollbackEvent of rollback) {
@ryanwaits
ryanwaits / chainhook.ts
Created April 10, 2024 15:15
Pseudocode for indexing Chainhook payload
import { db } from '@/utils/db';
import { isDAOProposalTx } from '@/helpers';
import type { ChainhookStructure } from '@/types/chainhooks-type';
export async function processChainhookTransactions(chainhookData: ChainhookStructure) {
const { apply } = chainhookData;
const transactions = apply[0].transactions;
// Loop through each transaction in the Chainhook payload
for (const transaction of transactions) {
@ryanwaits
ryanwaits / no-loss-lottery.clar
Last active March 25, 2024 19:13
An implementation of a no-loss lottery pool using the CityCoins protocol
(define-constant OWNER tx-sender)
(define-constant ERR_UNAUTHORIZED (err u101000))
;; Participants map stores each participant's stacked amount, cycle and their ticket ID.
(define-map Participants
{ participant: principal }
{ ticketId: uint, amount: uint, cycle: uint, ticketExpirationAtCycle: uint, isWinner: bool }
)
@ryanwaits
ryanwaits / swapper.clar
Created March 7, 2024 14:32
basic token swapper
(use-trait sip10-token .sip-10.sip-10-trait)
(define-public (swap (from-token <sip10-token>) (to-token <sip10-token>) (amount uint))
(begin
(try! (contract-call? from-token transfer amount tx-sender (as-contract tx-sender) none))
(try! (contract-call? to-token transfer amount (as-contract tx-sender) tx-sender none))
(ok true)
)
)
@ryanwaits
ryanwaits / token.clar
Last active March 7, 2024 14:42
sip10 token
(impl-trait .sip-10.sip-10-trait)
(define-fungible-token clarity-token)
(define-constant ERR_OWNER_ONLY (err u100))
(define-constant ERR_NOT_TOKEN_OWNER (err u101))
(define-constant CONTRACT_OWNER tx-sender)
(define-constant TOKEN_URI u"https://clarity-lang.org")
(define-constant TOKEN_NAME "Clarity Token")
@ryanwaits
ryanwaits / sip10.clar
Last active March 7, 2024 14:34
sip10 token trait
(define-trait sip-10-trait
(
(transfer (uint principal principal (optional (buff 34))) (response bool uint))
(get-name () (response (string-ascii 32) uint))
(get-symbol () (response (string-ascii 32) uint))
(get-decimals () (response uint uint))
(get-balance (principal) (response uint uint))
(get-total-supply () (response uint uint))
(get-token-uri () (response (optional (string-utf8 256)) uint))
)
@ryanwaits
ryanwaits / use-auth.ts
Last active February 3, 2024 06:19
Stacks.js Hooks - useAuth
/**
* React Hooks for Stacks.js: useAuth
*
* Description:
* This custom React hook integrates with Stacks blockchain authentication,
* simplifying the sign-in and sign-out process within React applications.
*
* Features:
* - Manages `UserSession` lifecycle from the `@stacks/auth` package.
* - Exposes `signIn` method to initiate the authentication request.