Skip to content

Commit

Permalink
Inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
l3wi committed Feb 24, 2021
1 parent 6dfe095 commit d2085b0
Show file tree
Hide file tree
Showing 19 changed files with 5,976 additions and 252 deletions.
1 change: 1 addition & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INFURA=XXXXXXXX
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ yarn-error.log*

# vercel
.vercel

# DotEnv
.env
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
# Web3 Starter

## Getting Started


## Get started:

First, run the development server:

Expand All @@ -18,9 +20,13 @@ You can start editing the page by modifying `pages/index.js`. The page auto-upda

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.

## Layout:



## Learn More

To learn more about Next.js, take a look at the following resources:
To learn more about the underlying framework: Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
Expand Down
24 changes: 24 additions & 0 deletions components/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import styled from 'styled-components'

export const Column = styled.div`
display: flex;
flex-direction: column;
width: ${({ w }) => w};
height: ${({ h }) => h};
padding: ${({ p }) => p};
margin: ${({ m }) => m};
align-items: ${({ ai }) => ai};
justify-content: ${({ jc }) => jc};
flex-grow: ${({ fg }) => fg};
`
export const Row = styled.div`
width: ${({ w }) => (w ? w : '100%')};
height: ${({ h }) => h};
display: flex;
flex-direction: row;
padding: ${({ p }) => p};
margin: ${({ m }) => m};
justify-content: ${({ jc }) => jc};
align-items: ${({ ai }) => ai};
flex-grow: ${({ fg }) => fg};
`
95 changes: 95 additions & 0 deletions contexts/useWeb3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import React, {
createContext,
useState,
useContext,
useMemo,
useEffect,
useCallback,
} from 'react'

import { useWallet } from 'use-wallet'
import { web3, registerProvider } from '../utils/ethers'

import useLocalStorage from '../hooks/useLocalStorage'
const atob = (a) => Buffer.from(a, 'base64').toString('binary')

// Create Context
export const UseWeb3Context = createContext()

export const Web3Provider = (props) => {
const { account, connect, status, ethereum, reset } = useWallet()

// Remember provider preference
const [provider, setProvider] = useLocalStorage('provider', false)

// Connect/Disconnect Wallet
const connectWallet = async (key) => {
await connect(key)
setProvider(key)
registerProvider(ethereum)
}
const disconnectWallet = () => {
reset()
setProvider(false)
}

// Check to see if we've set a provider in local Storage and connect
const initProvider = () => {
if (provider) {
console.log('Provider Found:', provider)
connect(provider)
registerProvider(ethereum)
}
}

// Once we've connected a wallet, switch to wallet provider
useEffect(() => {
if (status === 'connected') {
console.log('Connected!')
registerProvider(ethereum)
}
}, [status])

// Once loaded, initalise the provider
useEffect(() => {
initProvider()
}, [provider])

const tools = useMemo(
() => ({
provider,
setProvider,
connectWallet,
disconnectWallet,
account,
status,
}),
[provider, account, status]
)

// pass the value in provider and return
return (
<UseWeb3Context.Provider
value={{
tools,
}}
>
{props.children}
</UseWeb3Context.Provider>
)
}

export function useWeb3() {
const web3Context = useContext(UseWeb3Context)
if (web3Context === null) {
throw new Error(
'useWeb3() can only be used inside of <UseToolsProvider />, ' +
'please declare it at a higher level.'
)
}
const { tools } = web3Context

return useMemo(() => ({ web3, ...tools }), [tools])
}

export default useWeb3
3 changes: 3 additions & 0 deletions contracts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import usdc from './usdc'

export default { usdc }
Loading

0 comments on commit d2085b0

Please sign in to comment.