Skip to content

Instantly share code, notes, and snippets.

View mattbajorek's full-sized avatar

Matt Bajorek mattbajorek

View GitHub Profile
@mattbajorek
mattbajorek / chaincode_labs_exercise.py
Created November 22, 2022 09:31
Chaincode Labs Bitcoin Protocol Development Seminar Exercise
#!/usr/bin/env python3
"""Chaincode Exercise
Try getting node 1 to mine another block,
send it to node 2, and check that node 2 received it.
"""
from collections import defaultdict
from test_framework.blocktools import (
create_block,
@mattbajorek
mattbajorek / main.go
Last active September 26, 2022 22:34
Example usage of AwaitAll
package main
import (
"errors"
"fmt"
"math/rand"
"strconv"
"sync"
"time"
)
@mattbajorek
mattbajorek / awaitall.go
Last active September 26, 2022 22:23
AwaitAll will take a slice of inputs and will inject the input into the async func to run in parallel with the other inputs. Then function will wait until all of the responses are done and will return them in the same order of the inputs. The async function cannot throw an error and must return the error in the response.
func AwaitAll[Input any, Response any](sliceOfInputs []Input, asyncFunc func(Input) Response) []Response {
responses := make([]Response, len(sliceOfInputs))
var waitGroup sync.WaitGroup
for i, input := range sliceOfInputs {
waitGroup.Add(1)
go func(i int, input Input) {
responses[i] = asyncFunc(input)
waitGroup.Done()
@mattbajorek
mattbajorek / max-requests-per-second-limiter.go
Last active August 16, 2022 18:34
Max requests per second limiter
package main
import (
"fmt"
"sync"
"time"
)
func main() {
// Start request limiter
@mattbajorek
mattbajorek / max-concurrent-requests-limiter.go
Created August 2, 2022 04:46
Max concurrent requests limiter
package main
import (
"fmt"
"sync"
"time"
)
func main() {
// Create request limiter
@mattbajorek
mattbajorek / notification.service.ts
Last active August 23, 2023 06:03
Full notification service
import { Injectable } from '@nestjs/common';
import { mapLimit } from 'async';
import * as firebase from 'firebase-admin';
import { BatchResponse } from 'firebase-admin/lib/messaging/messaging-api';
import { chunk } from 'lodash';
import * as shell from 'shelljs';
export interface ISendFirebaseMessages {
token: string;
title?: string;
@mattbajorek
mattbajorek / notification.service.ts
Last active April 20, 2022 01:19
sendFirebaseMessages method
import { Injectable } from '@nestjs/common';
import { mapLimit } from 'async';
import * as firebase from 'firebase-admin';
import { BatchResponse } from 'firebase-admin/lib/messaging/messaging-api';
import { chunk } from 'lodash';
import * as shell from 'shelljs';
export interface ISendFirebaseMessages {
token: string;
title?: string;
import { Injectable } from '@nestjs/common';
import * as firebase from 'firebase-admin';
import { BatchResponse } from 'firebase-admin/lib/messaging/messaging-api';
import * as shell from 'shelljs';
@Injectable()
export class NotificationsService {
...
public async sendAll(messages: firebase.messaging.TokenMessage[], dryRun?: boolean): Promise<BatchResponse> {
@mattbajorek
mattbajorek / notification.service.ts
Last active October 26, 2022 19:53
Initializing firebase
import { Injectable } from '@nestjs/common';
import * as firebase from 'firebase-admin';
@Injectable()
export class NotificationsService {
constructor() {
// For simplicity these credentials are just stored in the environment
// However these should be stored in a key management system
const firebaseCredentials = JSON.parse(process.env.FIREBASE_CREDENTIAL_JSON);
firebase.initializeApp({
@mattbajorek
mattbajorek / index.js
Created October 9, 2021 01:53
AWS Lambda code receiving API Gateway information for SNS notifications
const AWS = require('aws-sdk');
const sns = new AWS.SNS({ apiVersion: '2010-03-31' });
exports.handler = async (event, context) => {
let body;
let statusCode = '200';
const { origin } = event.headers;
const acceptedOrigins = ['http://localhost:8080', 'https://mywebsite.com']; // PLACE PROPER WEBSITE ORIGIN
const headers = {
'Access-Control-Allow-Headers': 'Content-Type',