Skip to content

Instantly share code, notes, and snippets.

View mattlockyer's full-sized avatar
💭
🥳

Matt Lockyer mattlockyer

💭
🥳
View GitHub Profile
@mattlockyer
mattlockyer / app.js
Last active April 25, 2020 15:30
A waaay simpler redux, redux-thunk friendly setup for reducer, state and actions
import { getReducer, getState } from '../util/redux-util'
//default state
const defaultState = {
toggle: false
}
//reducer
const type = 'appReducer'
export const appReducer = getReducer(type, defaultState)
export const appState = getState(type)
//actions
@mattlockyer
mattlockyer / hmy_estimateGas_tests.txt
Created February 10, 2020 03:52
Harmony RPC endpoint hmy_estimateGas tests and results
curl -X GET http://localhost:9500 -H 'Content-Type: application/json' -d '{
"jsonrpc":"2.0",
"method":"hmy_estimateGas",
"params":[{
"from": "0x7c41e0668b551f4f902cfaec05b5bdca68b124ce",
"to": "0x7c41e0668b551f4f902cfaec05b5bdca68b124ce",
"value": "0x1"
}],
"id": 1
}'
@mattlockyer
mattlockyer / get-fields.js
Last active January 23, 2020 16:20
Turn JSON / POJO into Firestore Fields for REST API
//only handles integerValue, doubleValue, stringValue and booleanValue
const getType = (val) => {
if (val === null) return 'nullValue'
const t = typeof val
switch (t) {
case 'number': return n % 1 === 0 ? 'integerValue' : 'doubleValue'
case 'string':
case 'boolean':
return t + 'Value'
}
@mattlockyer
mattlockyer / tx-curl.sh
Created December 6, 2019 19:20
Harmony Transaction Log Curl Script (testnet)
SHARD0=https://api.s0.b.hmny.io
SHARD1=https://api.s1.b.hmny.io
SHARD2=https://api.s2.b.hmny.io
#your params
SHARD=SHARD1
#example is HRC20 mint and transfer
TXID=0x039d2f87e6bdb81220e5a7490dc783ea835443f57f4e12d16d90dd0b3aa1f5af
#curl
curl -X POST $SHARD1 -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate' -H 'Cache-Control: no-cache' -H 'Connection: keep-alive' -H 'Content-Length: 162' -H 'Content-Type: application/json' -H 'Host: api.s1.b.hmny.io' -H 'Postman-Token: d5415117-657a-49f9-9100-a5b7ebc70daf,cc2f3cb9-2d10-408c-a003-d6e0822ec985' -H 'User-Agent: PostmanRuntime/7.19.0' -H 'cache-control: no-cache' -d '{
"jsonrpc":"2.0",
@mattlockyer
mattlockyer / jwt.js
Created November 2, 2019 04:01
JWT Token Module for Cloudflare Workers / Browser
/********************************
* Module for generating and verifying JWT tokens
* Designed to work on Cloudflare Workers
* Should work in the browser or any env that supports Web Crypto API
********************************/
/********************************
* Key secret is a random Uint8Array of length 64
* See below for instructions on generating random values / keys
********************************/
@mattlockyer
mattlockyer / ga_cfw.js
Last active September 30, 2020 20:31
Google Analytics for Cloudflare Workers
/********************************
* GA data
********************************/
let data = {
v: 1,
}
/********************************
* Initializes GA data
* @param {string} tid your tracking id for GA
* @param {object} req the request object from event.request
@mattlockyer
mattlockyer / base64.js
Created July 10, 2019 03:22
base 64 conversions using file reader and fetch
//FROM HERE: https://stackoverflow.com/a/54123275/1060487
// base64 to buffer
function base64ToBufferAsync(base64) {
var dataUrl = "data:application/octet-binary;base64," + base64;
fetch(dataUrl)
.then(res => res.arrayBuffer())
.then(buffer => {
console.log("base64 to buffer: " + new Uint8Array(buffer));
@mattlockyer
mattlockyer / genfunc.js
Created June 30, 2019 15:21
Generate functions that are based on a similar structure by name - e.g. set redux state of UI components
//example functions to set redux state of UI components
const {setDrawerState, setDialogState, setPopoverState} = ['Drawer', 'Dialog', 'Popover'].map((name) => ({
['set' + name + 'State']: (state) => async (dispatch, getState) => {
const stateName = name.toLowerCase() + 'State'
const currentState = getState().appReducer[stateName]
dispatch({ type: 'UPDATE_UI_STATE', [stateName]: { ...currentState, ...state } })
}
})).reduce((a, c) => ({...a, ...c}))
export { setDrawerState, setDialogState, setPopoverState }
@mattlockyer
mattlockyer / conditional-json.js
Created June 19, 2019 15:25
Using spread syntax to conditionally add JSON key, value to object. Fetch example using POST and Authorization.
export const POST = (auth) => ({
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, cors, *same-origin
headers: {
"Content-Type": "application/json",
...(auth ? { "Authorization": auth } : {})
},
})
@mattlockyer
mattlockyer / index.html
Created June 19, 2019 00:05
YouTube Search with Go / GoLang Server. Client sends url query param "q" to server. Server searches YouTube API for videos matching q. Server decodes, does "anything", then responds to client with JSON.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>YouTube Search Example</title>
<meta name="description" content="YouTube Search Example">
<meta name="author" content="Matt Lockyer">
</head>