Skip to content

Instantly share code, notes, and snippets.

View adrianmcli's full-sized avatar
:shipit:
What's happening?

Adrian Li adrianmcli

:shipit:
What's happening?
View GitHub Profile
@adrianmcli
adrianmcli / getWeb3.js
Created November 27, 2017 18:39
Javascript to get Web3 from the browser environment
import Web3 from 'web3'
const resolveWeb3 = (resolve) => {
let { web3 } = window
const alreadyInjected = typeof web3 !== 'undefined' // i.e. Mist/Metamask
const localProvider = `http://localhost:9545`
if (alreadyInjected) {
console.log(`Injected web3 detected.`)
web3 = new Web3(web3.currentProvider)
@adrianmcli
adrianmcli / withWeb3.js
Last active February 15, 2018 07:46
React.js HOC for Web3
import React from 'react'
import getWeb3 from './getWeb3'
const withWeb3 = PassedComponent => class extends React.Component {
state = { web3: null }
async componentDidMount () {
try {
const web3 = await getWeb3()
this.setState({ web3 })
@adrianmcli
adrianmcli / getWeb3.js
Created November 27, 2017 10:29
An HOC approach to getting web3 (and a truffle contract) for your React-based dapp
import Web3 from 'web3'
const resolveWeb3 = (resolve) => {
let { web3 } = window
const alreadyInjected = typeof web3 !== 'undefined' // i.e. Mist/Metamask
if (alreadyInjected) {
web3 = new Web3(web3.currentProvider)
console.log(`Injected web3 detected.`)
resolve(web3)
@adrianmcli
adrianmcli / dapp.js
Last active November 22, 2017 10:10
import React from 'react'
import withWeb3 from '../lib/withWeb3'
// Demonstration of a basic dapp with the withWeb3 higher-order component
class Dapp extends React.Component {
state = { balance: null }
storeValue = async () => {
const { accounts, contract } = this.props
const response = await contract.set(5, { from: accounts[0] })
import React from 'react'
import withWeb3 from '../lib/withWeb3'
// Our `withWeb3` HOC actually injects web3, accounts, and contract into
// the props, but we are only using the accounts prop.
const Accounts = ({ accounts }) =>
<div>
<h1>My Accounts</h1>
<pre>{JSON.stringify(accounts, null, 4)}</pre>
</div>
@adrianmcli
adrianmcli / SOYLENT_OBJECTIONS.md
Last active June 24, 2017 11:20
Common objections to Soylent
@adrianmcli
adrianmcli / tierTransform.js
Last active April 17, 2017 20:53
When the data that the client wants to consume is different from the database model, transform it on the backend before sending it down.
export default (show, venueType) => {
const tiers = show.tierPrices.map((price, index) => ({
title: venueType[`tier${index + 1}Title`],
description: venueType[`tier${index + 1}Description`],
price,
}));
return {
name: show.name,
venueType: venueType.name,
tiers,
@adrianmcli
adrianmcli / Softmax function
Last active April 6, 2017 09:35
Storing stuff I'm learning from Udacity's deep learning course.
"""Softmax."""
scores = [3.0, 1.0, 0.2]
import numpy as np
from math import e
def softmax(scores):
denominator = sum([e ** x for x in scores])
result = [e ** x / denominator for x in scores]