mirror of
https://github.com/MetaFam/TheGame.git
synced 2026-04-24 03:00:09 -04:00
Base architecture (#1)
* Hasura and base models * Added role public SELECT permissions * Added role player UPDATE permissions * basic backend api * Added SELECT permissions for player * Update backend to typescript * init app-react * Add apollo * graphql-codegen not working well... * Added web3 to web app * connecting frontend with web3 * Auth webhook verifies eth signature * Update frontend to fetch player_id
This commit is contained in:
71
packages/app-react/src/contexts/Web3.tsx
Normal file
71
packages/app-react/src/contexts/Web3.tsx
Normal file
@@ -0,0 +1,71 @@
|
||||
import React, {createContext, useCallback, useState} from 'react';
|
||||
import WalletConnectProvider from '@walletconnect/web3-provider';
|
||||
import Web3Modal from 'web3modal';
|
||||
import Web3 from 'web3';
|
||||
import { ethers } from 'ethers';
|
||||
import {AsyncSendable} from 'ethers/providers';
|
||||
import {useApolloClient} from '@apollo/react-hooks';
|
||||
|
||||
import config from '../config';
|
||||
import {createToken} from '../lib/did';
|
||||
import {loginLoading, login} from '../apollo/auth';
|
||||
|
||||
export const Web3Context = createContext({
|
||||
ethersProvider: null,
|
||||
connectWeb3: () => {},
|
||||
});
|
||||
|
||||
const providerOptions = {
|
||||
walletconnect: {
|
||||
package: WalletConnectProvider,
|
||||
options: {
|
||||
infuraId: config.infuraId,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const web3Modal = new Web3Modal({
|
||||
network: 'mainnet',
|
||||
cacheProvider: true,
|
||||
providerOptions,
|
||||
});
|
||||
|
||||
|
||||
const Web3ContextProvider = props => {
|
||||
|
||||
const apolloClient = useApolloClient();
|
||||
|
||||
const [ethersProvider, setEthersProvider] = useState(null);
|
||||
|
||||
const connectWeb3 = useCallback(async () => {
|
||||
|
||||
loginLoading(apolloClient);
|
||||
|
||||
try {
|
||||
|
||||
const provider = await web3Modal.connect();
|
||||
|
||||
const web3Provider = new Web3(provider);
|
||||
const ethersProvider = new ethers.providers.Web3Provider(web3Provider.currentProvider as AsyncSendable);
|
||||
const signer = ethersProvider.getSigner();
|
||||
const address = await signer.getAddress();
|
||||
|
||||
const token = await createToken(ethersProvider);
|
||||
console.log(token);
|
||||
|
||||
await login(apolloClient, token, address);
|
||||
setEthersProvider(ethersProvider);
|
||||
|
||||
} catch(error) {
|
||||
console.error('impossible to connect', error);
|
||||
}
|
||||
}, [apolloClient]);
|
||||
|
||||
return (
|
||||
<Web3Context.Provider value={{ ethersProvider, connectWeb3 }}>
|
||||
{props.children}
|
||||
</Web3Context.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default Web3ContextProvider;
|
||||
Reference in New Issue
Block a user