Skip to content

Instantly share code, notes, and snippets.

View aalmada's full-sized avatar
🏠
Working from home

Antão Almada aalmada

🏠
Working from home
View GitHub Profile
@aalmada
aalmada / JazziconAvatar.tsx
Last active December 14, 2022 14:42
Customizing RainbowKit to use jazzicon avatars. Uses the Jazzicon component shared on previous gist.
import { AvatarComponent } from "@rainbow-me/rainbowkit";
import Jazzicon from "./Jazzicon";
import { addressToSeed } from "./utils";
const JazziconAvatar: AvatarComponent = ({ address, ensImage, size }) =>
ensImage ? (
<img
src={ensImage}
width={size}
height={size}
@aalmada
aalmada / ErrorUtils.ts
Created December 14, 2022 12:20
TypeScript methods to get type of error
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const isAbortError = (error: any) =>
typeof error === "object" && error !== null && error.name === "AbortError";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const isRpcError = (
error: any
): error is { data: { message: string } } =>
typeof error === "object" &&
error !== null &&
@aalmada
aalmada / useChainId.tsx
Last active December 15, 2022 09:28
Example of using typechain and react query to call paused() on a contract that implements Pausable. It uses event listening to invalidate cache.
import { BaseContract } from "@ethersproject/contracts";
import { useEffect, useState } from "react";
const useChainId = (contract: BaseContract | undefined) => {
const [chainId, setChainId] = useState<number>();
useEffect(() => {
const updateChainId = async (contract: BaseContract | undefined) =>
setChainId(await contract?.signer.getChainId());
@aalmada
aalmada / example.ts
Last active November 30, 2022 18:39
An extension to React Query's useMutation hook to execute ethers transactions. Examples using it and typechain.
// example of using the usePause hook to log the transaction hash and the gas used
const { pause, isLoading: isPausing } = usePause({
onTransaction: response =>
console.log(
"transaction: ",
response.hash
),
onSuccess: receipt =>
console.log(
"gas used: ",
@aalmada
aalmada / Jazzicon.tsx
Last active November 30, 2022 11:34
A react component that generates a jazzicon. It exports pure SVG making it easy to integrate it on other components. Use the `addressToSeed` method to pass in an address as the seed.
// based on: https://github.com/MetaMask/jazzicon
import React, { useState, useEffect } from "react";
import MersenneTwister from "mersenne-twister";
type RGB = {
r: number;
g: number;
b: number;
};
public int Sum(List<int> source)
=> Sum(CollectionsMarshal.AsSpan(source));
static int Sum(List<int> source)
{
var sum = 0;
for (var index = 0; index < source.Count; index++)
sum += source[index];
return sum;
}
@aalmada
aalmada / Deck.cs
Last active June 18, 2021 13:21
Cards deck implemented in C#
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace NetFabric
{
public class Deck<T>
@aalmada
aalmada / Shuffle.cs
Created June 18, 2021 07:55
Fisher-Yates Shuffle implemented in C#
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace NetFabric
{
public static class ShuffleExtensions
{
public static void Shuffle<T>(this List<T> source)
=> Shuffle(source, new Random());
public static async ValueTask<int> CountAsync<TEnumerable, TEnumerator, TSource>(this TEnumerable source, CancellationToken cancellationToken = default)
where TEnumerable : IAsyncValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IAsyncEnumerator<TSource>
{
var counter = 0;
var enumerator = source.GetAsyncEnumerator(cancellationToken);
try
{
checked
{