Skip to content

Instantly share code, notes, and snippets.

View KiranM27's full-sized avatar

Kiran Mohan KiranM27

View GitHub Profile
@KiranM27
KiranM27 / SimpleWalletFactory.sol
Created July 14, 2023 03:20
Factory Contract for creating Simple Wallets
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Factory {
// Returns the address of the newly deployed contract
function deploy(
uint _salt
) public payable returns (address) {
// This syntax is a newer way to invoke create2 without assembly, you just need to pass salt
// https://docs.soliditylang.org/en/latest/control-structures.html#salted-contract-creations-create2
@KiranM27
KiranM27 / SimpleWallet.sol
Created July 14, 2023 02:47
Simple Wallet Solidity Code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract SimpleWallet {
address public owner;
// Only owners can call transactions marked with this modifier
modifier onlyOwner() {
require(owner == msg.sender, "Caller is not the owner");
_;
}
@KiranM27
KiranM27 / ReactQueryExample.tsx
Created July 9, 2023 15:34
example to use react query
import React from 'react';
import { useGithubUser } from '@/api/github.api';
const ReactQueryExample = () => {
const { isLoading, error, data } = useGithubUser();
if (isLoading) return 'Loading...';
if (error) console.log('An error occurred while fetching the user data ', error);
return (
@KiranM27
KiranM27 / github.api.ts
Created July 9, 2023 15:29
github.api.ts - contains code to fetch and cache the data for react query
// api/github.api.ts
import { useQuery } from '@tanstack/react-query';
export type TGithubUser = {
name: string;
bio: string;
};
export const fetchGithubUser = async () => {
const res = await fetch('https://api.github.com/users/kiranm27');
@KiranM27
KiranM27 / _app.tsx
Created July 9, 2023 15:14
_app.tsx for react query
// _app.tsx or index.tsx
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
const queryClient = new QueryClient();
function MyApp({ Component, pageProps }: AppProps) {
return (
<QueryClientProvider client={queryClient}>
<Component {...pageProps} />
</QueryClientProvider>
@KiranM27
KiranM27 / ReduxSetExample.tsx
Last active July 2, 2023 10:30
Using useDispatch to alter the global state
import { setUserUid } from '@/redux/slices/userSlice';
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
const ReduxSetExample = () => {
// create an instance of the dispatch function
const dispatch = useDispatch();
useEffect(() => {
// dispatch the action as such
@KiranM27
KiranM27 / ReduxFetchExample.tsx
Last active July 2, 2023 10:23
Using useSelector to access the global stae
import { RootState } from '@/redux/store';
import { useEffect } from 'react';
import { useSelector } from 'react-redux';
const ReduxFetchExample = () => {
// access the redux state as such
const { uid } = useSelector((state: RootState) => state.user);
useEffect(() => {
if (!uid) return;
import { store } from '@/redux/store';
import '@/styles/globals.css';
import { AppPropsWithLayout } from '@/types/page';
import { ThemeProvider } from 'next-themes';
import { Provider as ReduxProvider } from 'react-redux';
export default function App({ Component, pageProps }: AppPropsWithLayout) {
const getLayout = Component.getLayout ?? ((page) => page);
return (
@KiranM27
KiranM27 / userSlice.ts
Last active July 2, 2023 09:33
User Slice for Redux
// redux/slices/userSlice.ts
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { IUser } from '../redux.types';
const initialState: IUser = {
uid: '',
email: '',
};
export const userSlice = createSlice({
@KiranM27
KiranM27 / redux.types.ts
Last active July 2, 2023 09:34
User Interface for the redux store
// redux/redux.types.ts
export interface IUser {
uid: string;
email: string;
}