-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setting up hooks for marketplace contract
- Loading branch information
Showing
9 changed files
with
466 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NEXT_PUBLIC_CONTRACT_NAME=mycontract.lucassouza.testnet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { Contract, WalletConnection } from 'near-api-js' | ||
|
||
declare global { | ||
interface Window { | ||
walletConnection: WalletConnection | ||
accountId: any | ||
contract: Contract | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useWallet } from './useWallet' | ||
import { useCallback, useMemo } from 'react' | ||
import { Contract } from 'near-api-js' | ||
import { v4 as uuid4 } from 'uuid' | ||
import { parseNearAmount } from 'near-api-js/lib/utils/format' | ||
|
||
interface UseMarketplaceProps { | ||
env: 'mainnet' | 'testnet' | ||
gasPrice?: number | ||
} | ||
|
||
export interface Product { | ||
id: string | ||
name: string | ||
description: string | ||
image: string | ||
location: string | ||
price: number | ||
owner: string | ||
sold: number | ||
} | ||
|
||
type MarketplaceContract = Contract & { | ||
getProduct: (id: { id: string }) => Promise<Product | null> | ||
getProducts: () => Promise<Product[]> | ||
|
||
buyProduct: (productId: { productId: string }, gas?: number, price?: number) => Promise<void> | ||
setProduct: (product: { product: Product }) => Promise<void> | ||
} | ||
|
||
const GAS = 100000000000000 | ||
|
||
export const useMarketplace = ({ env, gasPrice }: UseMarketplaceProps) => { | ||
const { account } = useWallet({ env }) | ||
|
||
const contract = useMemo(() => { | ||
if (!account?.accountId) return null | ||
|
||
return new Contract(account, process.env.NEXT_PUBLIC_CONTRACT_NAME as string, { | ||
viewMethods: ['getProduct', 'getProducts'], | ||
changeMethods: ['buyProduct', 'setProduct'], | ||
}) as MarketplaceContract | ||
}, [account]) | ||
|
||
const createProduct = useCallback( | ||
(newProduct: Product) => { | ||
const product = Object.assign(newProduct, { | ||
id: uuid4(), | ||
price: parseNearAmount(newProduct.price.toString()), | ||
}) | ||
|
||
return contract?.setProduct({ product }) | ||
}, | ||
[contract] | ||
) | ||
|
||
const getProducts = useCallback(async () => { | ||
return contract?.getProducts() | ||
}, [contract]) | ||
|
||
const buyProduct = useCallback( | ||
(id: string, price: number) => { | ||
return contract?.buyProduct({ productId: id }, gasPrice || GAS, price) | ||
}, | ||
[contract, gasPrice] | ||
) | ||
|
||
return { | ||
createProduct, | ||
getProducts, | ||
buyProduct, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { connect, keyStores, Near, WalletConnection } from 'near-api-js' | ||
import { useCallback, useEffect, useMemo, useState } from 'react' | ||
import environment from '../utils/config' | ||
import { formatNearAmount } from 'near-api-js/lib/utils/format' | ||
|
||
interface UseWalletProps { | ||
env: 'mainnet' | 'testnet' | ||
} | ||
|
||
export const useWallet = ({ env }: UseWalletProps) => { | ||
const [near, setNear] = useState<Near | null>(null) | ||
const [balance, setBalance] = useState<string | null>(null) | ||
|
||
const nearEnv = environment(env) | ||
|
||
useEffect(() => { | ||
if (near) return | ||
|
||
connect( | ||
Object.assign(nearEnv, { | ||
keyStore: new keyStores.BrowserLocalStorageKeyStore(), | ||
}) | ||
).then((data) => setNear(data)) | ||
}, [setNear, nearEnv, near]) | ||
|
||
const walletConnection = useCallback(() => { | ||
return near && new WalletConnection(near, 'my-app') | ||
}, [near]) | ||
|
||
const account = useMemo(() => { | ||
return walletConnection()?.account() | ||
}, [walletConnection]) | ||
|
||
useEffect(() => { | ||
;(async () => { | ||
if (!account?.accountId) return | ||
|
||
const response = await walletConnection()?.account().getAccountBalance() | ||
|
||
if (!response) return | ||
|
||
return setBalance(formatNearAmount(response.total, 2)) | ||
})() | ||
}, [walletConnection, account?.accountId, setBalance]) | ||
|
||
const login = () => | ||
walletConnection()?.requestSignIn({ contractId: process.env.NEXT_PUBLIC_CONTRACT_NAME }) | ||
|
||
const signOut = () => { | ||
walletConnection()?.signOut() | ||
window.location.reload() | ||
} | ||
|
||
return { | ||
account, | ||
balance, | ||
login, | ||
signOut, | ||
nearEnv, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,66 @@ | ||
import type { NextPage } from 'next' | ||
import { Product, useMarketplace } from '../hooks/useMarketplace' | ||
import { useState } from 'react' | ||
import { Badge, Button, Card, Group, Image, SimpleGrid, Text } from '@mantine/core' | ||
import { formatNearAmount } from 'near-api-js/lib/utils/format' | ||
import { IconMapPin } from '@tabler/icons' | ||
|
||
const Home: NextPage = () => { | ||
return <h1 title="sasas">Hello world</h1> | ||
const [products, setProducts] = useState<Product[]>([]) | ||
const { getProducts } = useMarketplace({ env: 'testnet' }) | ||
|
||
getProducts().then((data) => setProducts(data ?? [])) | ||
|
||
console.log(products) | ||
|
||
return products.length ? ( | ||
<SimpleGrid | ||
breakpoints={[ | ||
{ minWidth: 980, cols: 4, spacing: 'md' }, | ||
{ maxWidth: 755, cols: 2, spacing: 'sm' }, | ||
{ maxWidth: 600, cols: 1, spacing: 'sm' }, | ||
]} | ||
> | ||
{products.map((product) => ( | ||
<Card key={product.id} shadow="sm" p="lg" radius="md" withBorder> | ||
<Card.Section> | ||
<Image src={product.image} height={160} alt="Norway" /> | ||
</Card.Section> | ||
|
||
<Group position="apart" mt="md" mb="xs"> | ||
<Text weight={500}>{product.name}</Text> | ||
<Badge color="red" variant="light"> | ||
{formatNearAmount(product.sold.toString(), 2)} SOLD | ||
</Badge> | ||
</Group> | ||
|
||
<Text size="sm" color="dimmed"> | ||
{product.description} | ||
</Text> | ||
|
||
<Group position="apart" mt="md" mb="xs"> | ||
<Text size="sm" weight="bold"> | ||
by {product.owner} | ||
</Text> | ||
<Group spacing={4}> | ||
<IconMapPin size={15} /> | ||
<Text align="end" size="xs"> | ||
{product.location} | ||
</Text> | ||
</Group> | ||
</Group> | ||
|
||
<Button variant="gradient" color="blue" fullWidth mt="md" radius="md"> | ||
Buy for {formatNearAmount(product.price.toString(), 2)} NEAR | ||
</Button> | ||
</Card> | ||
))} | ||
</SimpleGrid> | ||
) : ( | ||
<Text size={36} align="center"> | ||
Connect your wallet | ||
</Text> | ||
) | ||
} | ||
|
||
export default Home |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { ConnectConfig } from 'near-api-js' | ||
|
||
export const CONTRACT_NAME = process.env.NEXT_PUBLIC_CONTRACT_NAME as string | ||
|
||
function environment(env: 'mainnet' | 'testnet'): ConnectConfig { | ||
switch (env) { | ||
case 'mainnet': | ||
return { | ||
networkId: 'mainnet', | ||
nodeUrl: 'https://rpc.mainnet.near.org', | ||
walletUrl: 'https://wallet.near.org', | ||
helperUrl: 'https://helper.mainnet.near.org', | ||
headers: {}, | ||
} | ||
case 'testnet': | ||
return { | ||
networkId: 'testnet', | ||
nodeUrl: 'https://rpc.testnet.near.org', | ||
walletUrl: 'https://wallet.testnet.near.org', | ||
helperUrl: 'https://helper.testnet.near.org', | ||
headers: {}, | ||
} | ||
default: | ||
throw Error(`Unknown environment '${env}'.`) | ||
} | ||
} | ||
|
||
export default environment |
Oops, something went wrong.
2ae57cd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
near-marketplace – ./
near-marketplace.vercel.app
near-marketplace-git-main-lucascodr.vercel.app
near-marketplace-lucascodr.vercel.app