Skip to content

Commit

Permalink
feat : added error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
surajhub255 committed Aug 13, 2024
1 parent 2e8ba05 commit 81d677c
Show file tree
Hide file tree
Showing 10 changed files with 516 additions and 785 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"@react-three/postprocessing": "^2.16.2",
"@readyplayerme/react-avatar-creator": "^0.5.0",
"@readyplayerme/visage": "^5.2.1",
"@tanstack/react-query": "^5.45.1",
"@tanstack/react-query": "^5.51.23",
"@uploadthing/react": "^6.6.0",
"@web3modal/wagmi": "^5.0.3",
"@web3modal/wagmi": "^5.0.11",
"axios": "^1.7.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
Expand All @@ -44,8 +44,8 @@
"three": "^0.165.0",
"uploadthing": "^6.12.0",
"uuid": "^10.0.0",
"viem": "^2.16.1",
"wagmi": "^2.10.4",
"viem": "^2.19.4",
"wagmi": "^2.12.5",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
737 changes: 211 additions & 526 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

247 changes: 128 additions & 119 deletions src/app/create-brand/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { useRouter } from 'next/navigation'
import { useAccount, useChainId, useWalletClient } from 'wagmi'
import { NFTStorage } from 'nft.storage'
import { Hex, createPublicClient, http } from 'viem'
import { baseSepolia } from 'viem/chains'
import { base } from 'viem/chains'
import axios from 'axios'

import Simplestore from "@/lib/Simplestore.json"
Expand Down Expand Up @@ -81,7 +81,7 @@ export default function CreateBrand() {
const chainId = useChainId()
const { data: walletClient } = useWalletClient({ chainId })
const publicClient = createPublicClient({
chain: baseSepolia,
chain: base,
transport: http(),
})

Expand Down Expand Up @@ -159,8 +159,11 @@ export default function CreateBrand() {

return txn.contractAddress
} catch (error) {
console.error('Deployment error:', error)
setError('Error deploying contract: ' + error)
console.error('Deployment error:', error);
toast.error('Error deploying AccessMaster contract: ' + error, {
position: 'top-left',
});
throw error;
}
};

Expand Down Expand Up @@ -191,8 +194,11 @@ export default function CreateBrand() {

return txn.contractAddress
} catch (error) {
console.error('Deployment error:', error)
setError('Error deploying contract: ' + error)
console.error('Deployment error:', error);
toast.error('Error deploying TradeHub contract: ' + error, {
position: 'top-left',
});
throw error;
}
};
const handleDeploy = async (): Promise<boolean> => {
Expand All @@ -202,21 +208,25 @@ export default function CreateBrand() {
console.log('Contract deployed at:', address);
return address !== null;
} catch (error) {
console.error('Error deploying contract:', error);
setError('Error deploying contract: ' + error);
console.error('Error deploying AccessMaster contract:', error);
toast.error('Failed to deploy AccessMaster contract', {
position: 'top-left',
});
return false;
}
};

const TradehubDeploy = async (): Promise<boolean> => {
try {
const address = await deployTradehubContract(30, 'NFT BAZAAR');
localStorage.setItem("TradehubAddress", address as `0x${string}`)
console.log('Contract deployed at:', address);
localStorage.setItem('TradehubAddress', address as `0x${string}`);
console.log('TradeHub Contract deployed at:', address);
return address !== null;
} catch (error) {
console.error('Error deploying contract:', error);
setError('Error deploying contract: ' + error);
console.error('Error deploying TradeHub contract:', error);
toast.error('Failed to deploy TradeHub contract', {
position: 'top-left',
});
return false;
}
};
Expand Down Expand Up @@ -253,117 +263,116 @@ export default function CreateBrand() {
if (!account.addresses) {
toast.warning('Connect your wallet', {
position: 'top-left',
})
} else {
if (!cid) {
setImageError(true)
}

try {
values.logo_image = "ipfs://" + cid
values.cover_image = "ipfs://" + cidCover
values.manager_id = account.address!
localStorage.setItem('brand_name', values.name)
console.log(values)

if (cid !== '') {
setLoading(true)
// const res = await fetch(`${apiUrl}/users/all`)

// if (!res.ok) {
// throw new Error('Network response was not ok');
// }

// const result = await res.json();
// console.log(result);

// const addressExists = result.some((user: { wallet_address: string | undefined }) => user.wallet_address === account.address);

// if (!addressExists) {
toast.warning('Now we are deploying AccessMaster to manage your brand', {
position: 'top-left',
})
const deploySuccess = await handleDeploy();
if (deploySuccess) {
const AccessMasterAddress = localStorage.getItem("AccessMasterAddress");
console.log('Contract deployed at:', AccessMasterAddress);
toast.warning('Now we will deploy TradeHub ', {
position: 'top-left',
})
const deployTradeHub = await TradehubDeploy();
if (deployTradeHub) {
const TradehubAddress = localStorage.getItem("TradehubAddress");
console.log('Contract deployed at:', TradehubAddress)
toast.success('Deploy Successful', {
position: 'top-left',
})
const brandId = uuidv4()
const chaintype = localStorage.getItem("BaseSepoliaChain");
const response = await fetch(`${apiUrl}/brands`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id: brandId,
name: values.name,
description: values.description,
logo_image: values.logo_image,
cover_image: values.cover_image,
representative: values.representative,
contact_email: values.contact_email,
contact_phone: values.contact_phone,
shipping_address: values.shipping_address,
additional_info: values.additional_info,
manager_id: values.manager_id,
access_master: AccessMasterAddress,
trade_hub: TradehubAddress,
payout_address: account.address,
chain_id: "84532",
chaintype_id: chaintype
}),
})
const brand = await response.json();
console.log(brand)
localStorage.setItem("BrandId", brand.id);
if (response.status === 200) {
const users = await fetch(`${apiUrl}/users`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
}, body: JSON.stringify({
id: brandId,
wallet_address: account.address,
chaintype_id: chaintype,
}),
})
console.log(users);
toast.success('Your Brand has been created', {
position: 'top-left',
})
router.push(`/congratulations?brand_name=${values.name}`);
}
}
}
// }else{
// toast.warning('With one address only one Brand can be created')
// }

} else if (!imageError && cid === '') {
toast.warning('Wait for your image to finish upload', {
position: 'top-left',
})
}
} catch (error) {
console.log(error)
toast.warning('Failed to create Brand', {
});
return;
}

if (!cid) {
setImageError(true);
return;
}

try {
values.logo_image = 'ipfs://' + cid;
values.cover_image = 'ipfs://' + cidCover;
values.manager_id = account.address!;
localStorage.setItem('brand_name', values.name);
console.log(values);

if (cid !== '') {
setLoading(true);

toast.warning('Deploying AccessMaster to manage your brand', {
position: 'top-left',
});

const deploySuccess = await handleDeploy();
if (!deploySuccess) throw new Error('AccessMaster deployment failed');

const AccessMasterAddress = localStorage.getItem('AccessMasterAddress');
console.log('Contract deployed at:', AccessMasterAddress);

toast.warning('Deploying TradeHub', {
position: 'top-left',
});

const deployTradeHub = await TradehubDeploy();
if (!deployTradeHub) throw new Error('TradeHub deployment failed');

const TradehubAddress = localStorage.getItem('TradehubAddress');
console.log('Contract deployed at:', TradehubAddress);

toast.success('Deployment Successful', {
position: 'top-left',
})
setLoading(false)
});

const brandId = uuidv4();
const chaintype = localStorage.getItem('BaseSepoliaChain');

const response = await fetch(`${apiUrl}/brands`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id: brandId,
name: values.name,
description: values.description,
logo_image: values.logo_image,
cover_image: values.cover_image,
representative: values.representative,
contact_email: values.contact_email,
contact_phone: values.contact_phone,
shipping_address: values.shipping_address,
additional_info: values.additional_info,
manager_id: values.manager_id,
access_master: AccessMasterAddress,
trade_hub: TradehubAddress,
payout_address: account.address,
chain_id: '84532',
chaintype_id: chaintype,
}),
});

if (!response.ok) throw new Error('Failed to create brand');

const brand = await response.json();
localStorage.setItem('BrandId', brand.id);
console.log(brand);

const users = await fetch(`${apiUrl}/users`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id: brandId,
wallet_address: account.address,
chaintype_id: chaintype,
}),
});

if (!users.ok) throw new Error('Failed to add user');

console.log(users);
toast.success('Your Brand has been created', {
position: 'top-left',
});
router.push(`/congratulations?brand_name=${values.name}`);
} else if (!imageError && cid === '') {
toast.warning('Wait for your image to finish upload', {
position: 'top-left',
});
}
} catch (error) {
console.error(error);
toast.error('Failed to create Brand: ' + error, {
position: 'top-left',
});
setLoading(false);
}
}


const uploadImage = (e: ChangeEvent<HTMLInputElement>) => {
const files = e.target.files;
Expand Down
4 changes: 2 additions & 2 deletions src/app/create-phygital-detail/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { v4 as uuidv4 } from 'uuid'
import { useAccount, useChainId, useWalletClient } from 'wagmi'
import { NFTStorage } from 'nft.storage'
import { Hex, createPublicClient, http } from 'viem'
import { baseSepolia } from 'viem/chains'
import { base } from 'viem/chains'
import axios from 'axios'
import phygital from "@/lib/phygital.json"

Expand Down Expand Up @@ -63,7 +63,7 @@ export default function CreatePhygitalDetail() {
const chainId = useChainId()
const { data: walletClient } = useWalletClient({ chainId })
const publicClient = createPublicClient({
chain: baseSepolia,
chain: base,
transport: http(),
})

Expand Down
4 changes: 2 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { headers } from 'next/headers'

import { cookieToInitialState } from 'wagmi'
import Providers from '@/lib/providers'
import Web3ModalProvider from '@/lib/providers'
import { config } from '@/lib/wagmi'
import AppKitProvider from '@/lib/providers'


const fontSans = FontSans({
Expand Down Expand Up @@ -66,7 +66,7 @@ export default function RootLayout({
fontSans.variable
)}
>
<Web3ModalProvider initialState={initialState}>{children}</Web3ModalProvider>
<AppKitProvider initialState={initialState}>{children}</AppKitProvider >
</body>
</Providers>
</html>
Expand Down
Loading

0 comments on commit 81d677c

Please sign in to comment.