-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle argentX wallet + trying to get_balance from deployed con…
…tract
- Loading branch information
hieu.ha
committed
Jan 9, 2025
1 parent
446e97e
commit 6461507
Showing
15 changed files
with
1,298 additions
and
28 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
87 changes: 87 additions & 0 deletions
87
packages/components/connectWallet/ConnectArgentXButton.tsx
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,87 @@ | ||
import React from "react"; | ||
import { Linking } from "react-native"; | ||
import { ConnectWalletButton } from "./components/ConnectWalletButton"; | ||
import { useFeedbacks } from "../../context/FeedbacksProvider"; | ||
|
||
import argentSVG from "@/assets/icons/argent-x.svg"; | ||
import { | ||
setIsArgentXConnected, | ||
setSelectedNetworkId, | ||
setSelectedWalletId, | ||
} from "@/store/slices/settings"; | ||
import { useAppDispatch } from "@/store/store"; | ||
import { StarknetWindowObject, connect } from "starknetkit"; | ||
import { | ||
useAccount, | ||
InjectedConnector, | ||
useConnect, | ||
} from "@starknet-react/core"; | ||
import { getStarknetNetworkByChainId } from "@/networks"; | ||
|
||
export const ConnectArgentXButton: React.FC<{ | ||
onDone?: (err?: unknown) => void; | ||
}> = ({ onDone }) => { | ||
const { setToast } = useFeedbacks(); | ||
const dispatch = useAppDispatch(); | ||
|
||
const { address, status } = useAccount(); | ||
const { connectAsync: connectViaReact } = useConnect(); | ||
|
||
const handlePress = async () => { | ||
// FIXME: only work with argentX for now, later we can allow to select all available wallets | ||
const starknet = window.starknet as StarknetWindowObject; | ||
if (!starknet) { | ||
Linking.openURL( | ||
"https://chromewebstore.google.com/detail/argent-x-starknet-wallet/dlcobpjiigpikoobohmabehhmhfoodbb", | ||
); | ||
return; | ||
} | ||
|
||
const connector = new InjectedConnector({ | ||
options: { | ||
id: starknet.id, | ||
name: starknet.name, | ||
icon: starknet.icon, | ||
}, | ||
}); | ||
|
||
try { | ||
const { connectorData } = await connect({ | ||
connectors: [connector], | ||
}); | ||
const chainId = connectorData?.chainId?.toString(16); | ||
let network = getStarknetNetworkByChainId(chainId); | ||
if (!network) throw Error("failed to get starknet network"); | ||
|
||
// FIXME: force to connect via react, check later to link react with normal connection | ||
await connectViaReact({ connector }); | ||
|
||
dispatch(setIsArgentXConnected(true)); | ||
dispatch(setSelectedNetworkId(network.id)); | ||
if (connectorData?.account) { | ||
dispatch(setSelectedWalletId("argentX-" + connectorData?.account)); | ||
} | ||
|
||
onDone?.(); | ||
} catch (err) { | ||
console.error(err); | ||
if (err instanceof Error) { | ||
setToast({ | ||
type: "error", | ||
message: err.message, | ||
mode: "normal", | ||
title: "Failed to connect to ArgentX (1)", | ||
}); | ||
} | ||
onDone?.(err); | ||
} | ||
}; | ||
return ( | ||
<ConnectWalletButton | ||
text="Argent X Wallet" | ||
icon={argentSVG} | ||
iconSize={20} | ||
onPress={handlePress} | ||
/> | ||
); | ||
}; |
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,58 @@ | ||
import { useMemo, useEffect } from "react"; | ||
|
||
import { Wallet } from "./wallet"; | ||
import { useSelectedNetworkInfo } from "../../hooks/useSelectedNetwork"; | ||
import { NetworkKind, getUserId } from "../../networks"; | ||
import { setSelectedWalletId } from "../../store/slices/settings"; | ||
import { useAppDispatch } from "../../store/store"; | ||
import { WalletProvider } from "../../utils/walletProvider"; | ||
import { useAccount } from "@starknet-react/core"; | ||
import { starknetNetwork } from "@/networks/starknet"; | ||
|
||
type UseArgentXResult = [true, boolean, Wallet[]] | [false, boolean, undefined]; | ||
|
||
export const useArgentX: () => UseArgentXResult = () => { | ||
const selectedNetworkInfo = useSelectedNetworkInfo(); | ||
const { address, status } = useAccount(); | ||
const dispatch = useAppDispatch(); | ||
|
||
const isConnected = status === "connected"; | ||
|
||
const wallet: Wallet | undefined = useMemo(() => { | ||
if (!address || !isConnected) return; | ||
let targetNetworkId = starknetNetwork.id; | ||
|
||
if (selectedNetworkInfo?.kind === NetworkKind.Starknet) { | ||
targetNetworkId = selectedNetworkInfo.id; | ||
} | ||
const userId = getUserId(targetNetworkId, address); | ||
const walletId = `argentX-${address}`; | ||
const wallet: Wallet = { | ||
id: walletId, | ||
address, | ||
provider: WalletProvider.ArgentX, | ||
networkKind: NetworkKind.Starknet, | ||
networkId: targetNetworkId, | ||
userId, | ||
connected: isConnected, | ||
}; | ||
return wallet; | ||
}, [address, isConnected, selectedNetworkInfo]); | ||
|
||
useEffect(() => { | ||
if ( | ||
wallet?.connected && | ||
selectedNetworkInfo?.kind === NetworkKind.Starknet | ||
) { | ||
dispatch(setSelectedWalletId(wallet.id)); | ||
} | ||
}, [dispatch, selectedNetworkInfo, wallet]); | ||
|
||
const hasArgentX = useMemo(() => { | ||
return typeof (window as any).ethereum !== "undefined"; | ||
}, []); | ||
|
||
return hasArgentX | ||
? [true, true, wallet ? [wallet] : []] | ||
: [false, true, undefined]; | ||
}; |
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
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,33 @@ | ||
[ | ||
{ | ||
"type": "impl", | ||
"name": "HelloStarknetImpl", | ||
"interface_name": "todo_list::IHelloStarknet" | ||
}, | ||
{ | ||
"type": "interface", | ||
"name": "todo_list::IHelloStarknet", | ||
"items": [ | ||
{ | ||
"type": "function", | ||
"name": "increase_balance", | ||
"inputs": [{ "name": "amount", "type": "core::felt252" }], | ||
"outputs": [], | ||
"state_mutability": "external" | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "get_balance", | ||
"inputs": [], | ||
"outputs": [{ "type": "core::felt252" }], | ||
"state_mutability": "view" | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "event", | ||
"name": "todo_list::HelloStarknet::Event", | ||
"kind": "enum", | ||
"variants": [] | ||
} | ||
] |
Oops, something went wrong.