Skip to content

Commit

Permalink
chore: update dapp with starknet-react v3
Browse files Browse the repository at this point in the history
  • Loading branch information
bluecco committed Jul 3, 2024
1 parent ababcbc commit 31f0aa1
Show file tree
Hide file tree
Showing 15 changed files with 1,434 additions and 561 deletions.
9 changes: 9 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ const starknetkitLatestVersion = Object.entries(p.dependencies)
.replace("^", "")
.replace("npm:", "")
.split("@")[1]

const starknetReactVersion = Object.entries(p.dependencies)
.find((dep) => dep[0] === "@starknet-react/core")[1]
.replace("^", "")
.replace("npm:", "")
.split("@")[0]

const starknetReactNextVersion = Object.entries(p.dependencies)
.find((dep) => dep[0] === "starknet-react-core-next")[1]
.replace("^", "")
.replace("npm:", "")
.split("@")[2]

const nextConfig = {
productionBrowserSourceMaps: true,
env: {
starknetkitNextVersion,
starknetkitLatestVersion,
starknetReactVersion,
starknetReactNextVersion,
},
}

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@chakra-ui/react": "^2.8.2",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@starknet-io/types-js": "^0.7.7",
"@starknet-react/chains": "^0.1.7",
"@starknet-react/core": "^2.8.2",
"colord": "^2.9.3",
Expand All @@ -25,9 +26,11 @@
"popmotion": "^11.0.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"starknet": "^6.9.0",
"starknet": "^6.11.0",
"starknet-react-chains-next": "npm:@starknet-react/[email protected]",
"starknet-react-core-next": "npm:@starknet-react/[email protected]",
"starknetkit-latest": "npm:starknetkit@^1.1.9",
"starknetkit-next": "npm:starknetkit@^2.2.11"
"starknetkit-next": "npm:starknetkit@^2.2.13"
},
"devDependencies": {
"@types/lodash-es": "^4.17.12",
Expand All @@ -45,7 +48,6 @@
"lint-staged": "^15.2.5",
"postcss": "^8.4.38",
"prettier": "^3.3.0",
"starknet-types": "^0.7.0",
"typescript": "^5.4.5"
},
"lint-staged": {
Expand Down
1,339 changes: 809 additions & 530 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,20 @@ export default function Home() {
<Flex flexDirection="column">
<Button
as="a"
href="/with"
href="/withStarknetReactNext"
p="4"
rounded="lg"
colorScheme="primary"
h="20"
isDisabled
>
<Flex flexDirection="column" alignItems="center">
<span>starknetkit@next ({process.env.starknetkitNextVersion})</span>
<span>
starknetkit@next ({process.env.starknetkitLatestVersion})
</span>
<span>+</span>
<span>starknet-react ({process.env.starknetReactVersion})</span>
<span>starknet-react ({process.env.starknetReactNextVersion})</span>
</Flex>
</Button>
<Flex justifyContent="center">
<strong>TODO (wait for starknet-react v3)</strong>
</Flex>
</Flex>
</Flex>
)
Expand Down
25 changes: 18 additions & 7 deletions src/app/starknetkitNext/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export default function StarknetkitLatest() {
}
}, [wallet])

const handleDashboard = () => {
window
.open(process.env.NEXT_PUBLIC_ARGENT_WEBWALLET_URL, "_blank", "noopener")
?.focus()
}

return (
<Flex as="main" flexDirection="column" p="10" gap="4" w="dvw" h="100dvh">
{wallet && (
Expand All @@ -82,13 +88,18 @@ export default function StarknetkitLatest() {
/>

{wallet.id === "argentWebWallet" && (
<a
target="_blank"
href={`${process.env.NEXT_PUBLIC_ARGENT_WEBWALLET_URL}`}
rel="noopener noreferrer"
>
Webwallet dashboard
</a>
<Flex gap="10">
<a
href={process.env.NEXT_PUBLIC_ARGENT_WEBWALLET_URL}
target="_blank"
rel="noopener noreferrer"
>
Webwallet dashboard href
</a>
<div onClick={handleDashboard}>
Webwallet dashboard window.open
</div>
</Flex>
)}

<AccountSection
Expand Down
104 changes: 102 additions & 2 deletions src/app/withStarknetReactNext/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,104 @@
"use client"

import { AccountSection } from "@/components/AccountSection"
import { SignMessageWithStarknetReactNext } from "@/components/Actions/SignMessageWithStarknetReactNext"
import { TransferWithStarknetReactNext } from "@/components/Actions/TransferWithStarknetReactNext"
import { DisconnectButton } from "@/components/DisconnectButton"
import { Section } from "@/components/Section"
import { ConnectStarknetReactNext } from "@/components/connect/ConnectStarknetReactNext"
import { CHAIN_ID } from "@/constants"
import { availableConnectors } from "@/helpers/connectorsNext"
import { useWaitForTx } from "@/hooks/useWaitForTx"
import { Flex } from "@chakra-ui/react"
import { useEffect, useState } from "react"
import { constants } from "starknet"
import { mainnet, sepolia } from "starknet-react-chains-next"
import {
StarknetConfig,
publicProvider,
useAccount,
useWalletRequest,
} from "starknet-react-core-next"
import { Connector, StarknetkitConnector, disconnect } from "starknetkit-next"

const StarknetReactDappContent = () => {
const { account, status, isConnected } = useAccount()
const [chainId, setChainId] = useState<constants.StarknetChainId | undefined>(
undefined,
)

console.log({ account, isConnected })

useWaitForTx()

useEffect(() => {
const getChainId = async () => {
setChainId(await account?.getChainId())
}

if (account) {
getChainId()
}
}, [account])

return (
<>
{!isConnected && <ConnectStarknetReactNext />}
{isConnected && (
<Flex
as="main"
flexDirection="column"
p="10"
gap="4"
w="dvw"
h="100dvh"
>
<>
<DisconnectButton disconnectFn={disconnect} />
<AccountSection address={account?.address} chainId={chainId} />
{/* <Section>
<MintWithStarknetReact />
</Section> */}
<Section>
<TransferWithStarknetReactNext />
</Section>
<Section>
<SignMessageWithStarknetReactNext chainId={chainId} />
</Section>
{/*
TODO: wait for the next version of starknetkit and starknet-react with rpc methods
<Section>
<SessionKeysSign />
<SessionKeysExecute />
<Flex alignItems="center" gap="100">
<SessionKeysExecuteOutside />
<SessionKeysTypedDataOutside />
</Flex>
</Section> */}
</>
</Flex>
)}
</>
)
}

export default function StarknetReactNext() {
//useWaitForTx()
return <>TODO: wait for starknet-react v3</>
const chains = [
CHAIN_ID === constants.NetworkName.SN_MAIN ? mainnet : sepolia,
]
const providers = publicProvider()

return (
<>
<StarknetConfig
chains={chains}
provider={providers}
connectors={
availableConnectors as any
} /* remove when starknet-react update types */
>
<StarknetReactDappContent />
</StarknetConfig>
</>
)
}
17 changes: 14 additions & 3 deletions src/components/AccountSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
import { Box, Flex, Heading } from "@chakra-ui/react"
import { useAtomValue } from "jotai"
import { FC } from "react"
import { constants } from "starknet"
import { constants, num } from "starknet"
import { Section } from "./Section"
import { CHAIN_ID } from "@/constants"
import { ChainId } from "@starknet-io/types-js"

interface AccountSectionProps {
address?: string
chainId?: constants.StarknetChainId | string
chainId?: bigint | string
}

const AccountSection: FC<AccountSectionProps> = ({ address, chainId }) => {
Expand All @@ -26,6 +27,9 @@ const AccountSection: FC<AccountSectionProps> = ({ address, chainId }) => {
const starknetkitVersion = useAtomValue(starknetkitVersionAtom)
const starknetReactVersion = useAtomValue(starknetReactVersionAtom)

const hexChainId =
typeof chainId === "bigint" ? num.toHex(chainId ?? 0) : null

return (
<>
<Flex alignItems="center" justifyContent="space-between">
Expand All @@ -34,7 +38,14 @@ const AccountSection: FC<AccountSectionProps> = ({ address, chainId }) => {
</Flex>
<Section>
<Box>Account: {address}</Box>
<Box>Chain: {chainId}</Box>
<Box>
Chain:{" "}
{!hexChainId
? chainId
: hexChainId === constants.StarknetChainId.SN_SEPOLIA
? constants.NetworkName.SN_SEPOLIA
: constants.NetworkName.SN_MAIN}
</Box>
<Box
cursor={lastTxHash ? "pointer" : "default"}
_hover={{ textDecoration: lastTxHash ? "underline" : "none" }}
Expand Down
7 changes: 2 additions & 5 deletions src/components/Actions/MintWithStarknetReact.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { ETHTokenAddress } from "@/constants"
import { lastTxHashAtom, lastTxStatusAtom } from "@/state/transactionState"
import { Status } from "@/types/status"
import { bigDecimal } from "@argent/x-shared"
import { Heading, Input } from "@chakra-ui/react"
import { Flex } from "@chakra-ui/react"
import { Flex, Heading, Input } from "@chakra-ui/react"
import { useAccount, useContractWrite } from "@starknet-react/core"
import { useAtom, useSetAtom } from "jotai"
import { FC, useMemo, useState } from "react"
import { AccountInterface } from "starknet"
import { useMemo, useState } from "react"

const MintWithStarknetReact = () => {
const { account } = useAccount()
Expand Down
86 changes: 86 additions & 0 deletions src/components/Actions/MintWithStarknetReactNext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { ETHTokenAddress, provider } from "@/constants"
import { lastTxHashAtom, lastTxStatusAtom } from "@/state/transactionState"
import { Flex, Heading, Input } from "@chakra-ui/react"
import { useAtom, useSetAtom } from "jotai"
import { useState } from "react"
import { Abi, useContract } from "starknet-react-core-next"

const abi = [
{
type: "function",
name: "permissionedMint",
state_mutability: "external",
inputs: [
{
name: "recipient",
type: "core::starknet::contract_address::ContractAddress",
},
{
name: "amount",
type: "core::integer::u256",
},
],
outputs: [],
},
] as const satisfies Abi

const MintWithStarknetReact = () => {
const [mintAmount, setMintAmount] = useState("10")

const [transactionStatus, setTransactionStatus] = useAtom(lastTxStatusAtom)
const setLastTransactionHash = useSetAtom(lastTxHashAtom)

const buttonsDisabled = ["approve", "pending"].includes(transactionStatus)

const { contract } = useContract({
abi,
address: ETHTokenAddress,
provider,
})

const handleMintSubmit = async (e: React.FormEvent) => {
e.preventDefault()
try {
setTransactionStatus("approve")
const { transaction_hash } = await contract
setLastTransactionHash(transaction_hash)
setTransactionStatus("pending")
} catch (e) {
console.error(e)
setTransactionStatus("idle")
}
}

return (
<Flex flex={1} width="full" gap={10}>
<Flex
as="form"
onSubmit={handleMintSubmit}
direction="column"
flex={1}
p="4"
gap="3"
borderRadius="lg"
>
<Heading as="h2">Mint token</Heading>
<Input
disabled
placeholder="Amount"
type="text"
id="mint-amount"
name="fname"
value={mintAmount}
onChange={(e) => setMintAmount(e.target.value)}
/>

<Input
type="submit"
disabled={true || buttonsDisabled}
value="Not possible with ETH!"
/>
</Flex>
</Flex>
)
}

export { MintWithStarknetReact }
Loading

0 comments on commit 31f0aa1

Please sign in to comment.