Skip to content

Instantly share code, notes, and snippets.

View drichar's full-sized avatar

Doug Richar drichar

View GitHub Profile
@drichar
drichar / web3auth.ts
Created July 6, 2025 03:03
TypeScript utility module that integrates Web3Auth Single Factor Auth with Algorand blockchain for React Native applications, enabling Firebase JWT-based authentication and Algorand transaction signing through Web3Auth's managed private keys
import { CHAIN_NAMESPACES, IBaseProvider } from '@web3auth/base'
import { CommonPrivateKeyProvider } from '@web3auth/base-provider'
import { Web3Auth, SDK_MODE } from '@web3auth/single-factor-auth'
import algosdk from 'algosdk'
// Create an in-memory storage that doesn't persist between app sessions
// This prevents automatic session restoration while satisfying Web3Auth's storage requirement
class InMemoryStorage {
private storage: Map<string, string> = new Map()
@drichar
drichar / deflex.ts
Created June 25, 2025 18:09
TypeScript client library for Deflex DEX aggregator API. Provides type-safe functions to fetch swap quotes and transaction data, plus strongly-typed classes for API responses including quotes, routes, transactions, and signatures.
// Deflex API responses
interface QuoteResponse {
quote: string | number
profit: {
amount: number
asa: {
id: number
}
}
priceBaseline: number
@drichar
drichar / useSwap.ts
Created June 25, 2025 18:01
React hook for executing swaps using Deflex order router. Handles both composable and non-composable protocols with automatic opt-ins/opt-outs, atomic transaction grouping, Web3Auth signing, and abort functionality.
import { AccountInformation } from '@algorandfoundation/algokit-utils/types/account'
import { useMutation } from '@tanstack/react-query'
import algosdk from 'algosdk'
import { ALGORAND_ASSET_ID } from '@/constants/assets'
import { useAccountInfo } from '@/hooks/useAccountInfo'
import { algorand } from '@/lib/algorand'
import { useAuth } from '@/lib/auth'
import {
DeflexQuote,
DeflexTransaction,
@drichar
drichar / useDeflexQuote.ts
Created June 25, 2025 17:57
React hook for fetching swap quotes from Deflex order router using TanStack Query. Includes smart caching, debounced input, tiered fee calculation, and loading/error states.
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { useSingleAssetMarketData } from '@/hooks/useAssetMarketData'
import { useDebounce } from '@/hooks/useDebounce'
import { fetchDeflexQuote } from '@/lib/deflex'
import { addBreadcrumb } from '@/lib/sentry/config'
import { calculateAssetInAmount } from '@/utils/amount'
import { AssetAmount } from '@/utils/asset-amount'
type Asset = {
id: bigint // ASA ID
@drichar
drichar / ManageContract.tsx
Last active August 13, 2024 07:12
Factory for creating custom useMutation hooks to sign transactions from the NFDomains API
import * as React from 'react'
import { NfdRecord } from '@/api/api-client'
import { useNfdCacheUpdate } from '@/api/hooks/useNfd'
import { usePostContractLock } from '@/api/hooks/usePostContractLock'
interface ManageContractProps {
nfd: NfdRecord
}
export function ManageContract({ nfd }: ManageContractProps) {
@drichar
drichar / NFD-vaults-example.md
Created August 5, 2023 16:53
An example of how to use the NFDomains API to send assets to and from an NFD's vault

NFDomains Vault Example

In this guide we will provide an example of how to use the NFDomains API to send assets to and from an NFD's vault, as a reference for adding Vault support in your own application.

Overview

A vault is an Algorand account controlled by an NFD's smart contract that can automatically opt-in to assets it receives.

Vaults are locked by default, meaning only the NFD's owner can send assets to it. When the owner unlocks the vault, anyone can send assets to it. The owner can lock or unlock the vault at any time.