-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
246 additions
and
1 deletion.
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
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,52 @@ | ||
import { addNetworkLatest, addNetworkNext } from "@/services/addNetwork.ts" | ||
import { walletStarknetkitLatestAtom } from "@/state/connectedWalletStarknetkitLatest" | ||
import { walletStarknetkitNextAtom } from "@/state/connectedWalletStarknetkitNext" | ||
import { Flex, Heading } from "@chakra-ui/react" | ||
import { useAtomValue } from "jotai" | ||
import { FC, useState } from "react" | ||
|
||
const AddNetworkNext = () => { | ||
const wallet = useAtomValue(walletStarknetkitNextAtom) | ||
return <AddNetwork addNetworkFn={async () => await addNetworkNext(wallet)} /> | ||
} | ||
|
||
const AddNetworkLatest = () => { | ||
const wallet = useAtomValue(walletStarknetkitLatestAtom) | ||
return ( | ||
<AddNetwork addNetworkFn={async () => await addNetworkLatest(wallet)} /> | ||
) | ||
} | ||
|
||
interface AddNetworkProps { | ||
addNetworkFn: () => Promise<void> | ||
} | ||
|
||
const AddNetwork: FC<AddNetworkProps> = ({ addNetworkFn }) => { | ||
const [addNetworkError, setAddNetworkError] = useState("") | ||
|
||
const handleAddNetwork = async () => { | ||
try { | ||
await addNetworkFn() | ||
setAddNetworkError("") | ||
} catch (error) { | ||
setAddNetworkError((error as any).message) | ||
} | ||
} | ||
|
||
return ( | ||
<Flex direction="column" gap="3" flex="1"> | ||
<Heading as="h2">Network</Heading> | ||
<Flex | ||
as="button" | ||
color="#0097fc" | ||
fontWeight="bold" | ||
onClick={handleAddNetwork} | ||
> | ||
Add network to wallet | ||
</Flex> | ||
<span className="error-message">{addNetworkError}</span> | ||
</Flex> | ||
) | ||
} | ||
|
||
export { AddNetworkLatest, AddNetworkNext } |
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,99 @@ | ||
import { FC, useState } from "react" | ||
import { Code, Flex, Heading } from "@chakra-ui/react" | ||
import { formatTruncatedAddress } from "@/utils/formatAddress" | ||
import { DAITokenAddress, ETHTokenAddress } from "@/constants" | ||
import { addTokenLatest, addTokenNext } from "@/services/addToken" | ||
import { walletStarknetkitLatestAtom } from "@/state/connectedWalletStarknetkitLatest" | ||
import { useAtomValue } from "jotai" | ||
import { walletStarknetkitNextAtom } from "@/state/connectedWalletStarknetkitNext" | ||
|
||
const AddTokenLatest = () => { | ||
const wallet = useAtomValue(walletStarknetkitLatestAtom) | ||
return ( | ||
<AddToken | ||
addEthFn={async () => await addTokenLatest(wallet, ETHTokenAddress)} | ||
addDaiFn={async () => await addTokenLatest(wallet, DAITokenAddress)} | ||
/> | ||
) | ||
} | ||
|
||
const AddTokenNext = () => { | ||
const wallet = useAtomValue(walletStarknetkitNextAtom) | ||
return ( | ||
<AddToken | ||
addEthFn={async () => await addTokenNext(wallet, ETHTokenAddress)} | ||
addDaiFn={async () => await addTokenNext(wallet, DAITokenAddress)} | ||
/> | ||
) | ||
} | ||
|
||
interface AddTokenProps { | ||
addEthFn: () => Promise<void> | ||
addDaiFn: () => Promise<void> | ||
} | ||
|
||
const AddToken: FC<AddTokenProps> = ({ addDaiFn, addEthFn }) => { | ||
const [addTokenError, setAddTokenError] = useState("") | ||
|
||
const handleAddEth = async () => { | ||
try { | ||
await addEthFn() | ||
setAddTokenError("") | ||
} catch (error) { | ||
setAddTokenError((error as any).message) | ||
} | ||
} | ||
|
||
const handleAddDai = async () => { | ||
try { | ||
await addDaiFn() | ||
setAddTokenError("") | ||
} catch (error) { | ||
setAddTokenError((error as any).message) | ||
} | ||
} | ||
|
||
return ( | ||
<Flex direction="column" gap="3" flex="1"> | ||
<Heading as="h2">ERC20</Heading> | ||
ETH token address | ||
<Code | ||
backgroundColor="#0097fc4f" | ||
borderRadius="8px" | ||
p="0 0.5rem" | ||
width="fit-content" | ||
> | ||
<a | ||
target="_blank" | ||
rel="noreferrer" | ||
style={{ | ||
color: "#0097fc", | ||
display: "inline-block", | ||
textDecoration: "none", | ||
}} | ||
> | ||
{formatTruncatedAddress(ETHTokenAddress)} | ||
</a> | ||
</Code> | ||
<Flex | ||
as="button" | ||
color="#0097fc" | ||
fontWeight="bold" | ||
onClick={handleAddEth} | ||
> | ||
Add ETH token to wallet | ||
</Flex> | ||
<Flex | ||
as="button" | ||
color="#0097fc" | ||
fontWeight="bold" | ||
onClick={handleAddDai} | ||
> | ||
Add DAI token to wallet | ||
</Flex> | ||
<span className="error-message">{addTokenError}</span> | ||
</Flex> | ||
) | ||
} | ||
|
||
export { AddTokenNext, AddTokenLatest } |
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,39 @@ | ||
import { StarknetWindowObject as StarknetWindowObjectNext } from "starknetkit-next" | ||
import { StarknetWindowObject as StarknetWindowObjectLatest } from "starknetkit-latest" | ||
|
||
export const addNetworkNext = async ( | ||
wallet: StarknetWindowObjectNext | undefined | null, | ||
) => { | ||
if (!wallet) { | ||
throw Error("starknet wallet not connected") | ||
} | ||
|
||
await wallet.request({ | ||
type: "wallet_addStarknetChain", | ||
params: { | ||
id: "dapp-test", | ||
chain_id: "SN_DAPP_TEST", | ||
chain_name: "Test chain name", | ||
rpc_urls: ["http://localhost:5050/rpc"], | ||
}, | ||
}) | ||
} | ||
|
||
export const addNetworkLatest = async ( | ||
wallet: StarknetWindowObjectLatest | undefined | null, | ||
) => { | ||
if (!wallet) { | ||
throw Error("starknet wallet not connected") | ||
} | ||
|
||
await wallet.request({ | ||
type: "wallet_addStarknetChain", | ||
params: { | ||
id: "dapp-test", | ||
chainId: "SN_DAPP_TEST", | ||
chainName: "Test chain name", | ||
baseUrl: "http://localhost:5050", | ||
rpcUrls: ["http://localhost:5050/rpc"], | ||
}, | ||
}) | ||
} |
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,40 @@ | ||
import { StarknetWindowObject as StarknetWindowObjectNext } from "starknetkit-next" | ||
import { StarknetWindowObject as StarknetWindowObjectLatest } from "starknetkit-latest" | ||
|
||
export const addTokenLatest = async ( | ||
wallet: StarknetWindowObjectLatest | undefined | null, | ||
address: string, | ||
): Promise<void> => { | ||
if (!wallet) { | ||
throw Error("starknet wallet not connected") | ||
} | ||
|
||
await wallet.request({ | ||
type: "wallet_watchAsset", | ||
params: { | ||
type: "ERC20", | ||
options: { | ||
address, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
export const addTokenNext = async ( | ||
wallet: StarknetWindowObjectNext | undefined | null, | ||
address: string, | ||
): Promise<void> => { | ||
if (!wallet) { | ||
throw Error("starknet wallet not connected") | ||
} | ||
|
||
await wallet.request({ | ||
type: "wallet_watchAsset", | ||
params: { | ||
type: "ERC20", | ||
options: { | ||
address, | ||
}, | ||
}, | ||
}) | ||
} |