Skip to content

Instantly share code, notes, and snippets.

@moodysalem
moodysalem / example.sol
Last active April 1, 2022 22:05
Transient Storage Example
View example.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
/// @notice The canonical ERC20 interface
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function transferFrom(
@moodysalem
moodysalem / TransientStorage.sol
Last active March 18, 2022 03:02
proxy yul code for using transient storage
View TransientStorage.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.12;
type TransientStorageProxy is address;
library TransientStorage {
function init() internal returns (TransientStorageProxy proxy) {
// initCode == bytecode from `yarn compile-tsp`
bytes
memory initCode = hex'602980600d600039806000f3fe366020811460135760408114602057600080fd5b600035b360005260206000f35b602035600035b450';
@moodysalem
moodysalem / prevent-duplicate-async.test.ts
Created December 22, 2018 04:57
TypeScript: Higher order function to prevent duplicate calls to an async function, i.e. memoize
View prevent-duplicate-async.test.ts
import { expect } from 'chai';
import preventDuplicateAsync from '../src/util/prevent-duplicate-async';
function testPromise<T>(): { promise: Promise<T>; resolve: (T) => void; reject: (Error) => void; } {
let rslv, rjct;
const promise = new Promise<T>((resolve, reject) => {
rslv = resolve;
rjct = reject;
@moodysalem
moodysalem / swagger-definitions.ts
Created February 8, 2018 19:51
TypeScript type interfaces for Swagger Models
View swagger-definitions.ts
interface IDataType {
type: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object';
readOnly?: true;
writeOnly?: true;
}
interface IStringType extends IDataType {
type: 'string';
format?: 'date' | 'date-time' | 'password' | 'byte' | 'binary' | 'uuid' | 'email' | 'uri' | 'hostname' | 'ipv4' | 'ipv6';
@moodysalem
moodysalem / SearchBuilder.java
Last active March 15, 2017 20:41
Used for building ElasticSearch query objects
View SearchBuilder.java
package com.fastmodelsports.social.lambda.util.es;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public abstract class SearchBuilder {
public static JsonArray sortOn(final String attribute, final boolean desc) {
final JsonObject sort = new JsonObject(),
attr = new JsonObject();
@moodysalem
moodysalem / app.jsx
Created January 22, 2017 19:48
Help with rendering reddit comment
View app.jsx
import React, { Component } from "react";
const Entry = ({ entry: { name, game } }) => (
<div>
<h3>{name}</h3>
<p>{game}</p>
</div>
);
const CHANNELS = [
@moodysalem
moodysalem / PreventLeaveRoute.jsx
Created January 22, 2017 18:52
React HOC to prevent leaving a page
View PreventLeaveRoute.jsx
import React, { Component, PropTypes } from 'react';
/**
* Only ever render one of these per page
*
* TODO: improve with react-side-effect to better handle multiple instances of this being rendered at once so we can
* nest the component in forms
*/
export default class PreventLeaveRoute extends Component {
static contextTypes = {
@moodysalem
moodysalem / render-component-to-print-html.jsx
Created November 22, 2016 16:09
Render a react component to an html document that shares all the links and styles for printing, and convert it to a data URI for window.open
View render-component-to-print-html.jsx
import React, { PureComponent, PropTypes } from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { Base64 } from 'js-base64';
import Promise from 'bluebird';
const htmlDocTemplate = ({ links, pageStyles, body, title = 'Print' }) => (`
<html>
<head>
<title>${title}</title>
${links}
@moodysalem
moodysalem / render-text.jsx
Created November 11, 2016 19:18
Render React component to text
View render-text.jsx
import { render, unmountComponentAtNode } from 'react-dom';
export function renderText(component) {
const _el = document.createElement('div');
document.body.appendChild(_el);
render(component, _el);
const text = _el.innerText;
unmountComponentAtNode(_el);
document.body.removeChild(_el);
return text;
@moodysalem
moodysalem / react-portal.jsx
Last active November 10, 2016 21:02
Render children to a div tag at the end of the body
View react-portal.jsx
import React, { Children, PureComponent, PropTypes } from 'react';
import { unmountComponentAtNode, unstable_renderSubtreeIntoContainer } from 'react-dom';
// renders children at the end of the body
export default class Portal extends PureComponent {
static propTypes = {
children: PropTypes.node.isRequired
};
componentDidMount() {