1- // import { useState, useEffect, useCallback } from "react";
2- // import { ArgentWebWallet} from "@argent/webwallet-sdk";
3- // import { RpcProvider } from "starknet";
4- // // import { toast } from "sonner";
1+ import { useState , useEffect , useCallback } from "react" ;
2+ import { ArgentWebWallet } from "@argent/webwallet-sdk" ;
3+ import { RpcProvider } from "starknet" ;
4+ // import { toast } from "sonner";
55
6- // const ARGENT_DUMMY_CONTRACT_ADDRESS = "0x07557a2fbe051e6327ab603c6d1713a91d2cfba5382ac6ca7de884d3278636d7";
7- // const ARGENT_DUMMY_CONTRACT_ENTRYPOINT = "increase_number";
6+ const ARGENT_DUMMY_CONTRACT_ADDRESS = "0x07557a2fbe051e6327ab603c6d1713a91d2cfba5382ac6ca7de884d3278636d7" ;
7+ const ARGENT_DUMMY_CONTRACT_ENTRYPOINT = "increase_number" ;
88
9- // const provider = new RpcProvider({});
9+ const provider = new RpcProvider ( { } ) ;
1010
11- // const argentWebWallet = ArgentWebWallet.init({
12- // appName: "hackbot",
13- // environment: "dev",
14- // sessionParams: {
15- // allowedMethods: [
16- // {
17- // contract: ARGENT_DUMMY_CONTRACT_ADDRESS,
18- // selector: ARGENT_DUMMY_CONTRACT_ENTRYPOINT,
19- // },
20- // ],
21- // },
22- // });
11+ const argentWebWallet = ArgentWebWallet . init ( {
12+ appName : "hackbot" ,
13+ environment : "dev" ,
14+ sessionParams : {
15+ allowedMethods : [
16+ {
17+ contract : ARGENT_DUMMY_CONTRACT_ADDRESS ,
18+ selector : ARGENT_DUMMY_CONTRACT_ENTRYPOINT ,
19+ } ,
20+ ] ,
21+ } ,
22+ } ) ;
2323
24- // export const useArgentWallet = () => {
25- // const [account, setAccount] = useState<undefined> (undefined);
26- // const [isLoading, setIsLoading] = useState(false);
27- // const [txHash, setTxHash] = useState<string | undefined> (undefined);
24+ export const useArgentWallet = ( ) => {
25+ const [ account , setAccount ] = useState ( undefined ) ;
26+ const [ isLoading , setIsLoading ] = useState ( false ) ;
27+ const [ txHash , setTxHash ] = useState ( undefined ) ;
2828
29- // const connect = useCallback(async () => {
30- // try {
31- // const response = await argentWebWallet.requestConnection({
32- // callbackData: "custom_callback_data",
33- // approvalRequests: [
34- // {
35- // tokenAddress: "0x049D36570D4e46f48e99674bd3fcc84644DdD6b96F7C741B1562B82f9e004dC7",
36- // amount: BigInt("100000000000000000").toString(),
37- // spender: "0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a",
38- // },
39- // ],
40- // });
41- // setAccount(response.account);
42- // } catch (err) {
43- // console.error("Failed to connect to Argent Web Wallet", err);
44- // }
45- // }, []);
29+ useEffect ( ( ) => {
30+ argentWebWallet
31+ . connect ( )
32+ . then ( ( res ) => {
33+
34+ if ( ! res ) {
35+ console . log ( "Not connected" ) ;
36+ return ;
37+ }
4638
47- // const submitTransaction = useCallback(async () => {
48- // if (!account) {
49- // console.error("Account not connected");
50- // return;
51- // }
52- // setIsLoading(true);
53- // try {
54- // const call = {
55- // contractAddress: ARGENT_DUMMY_CONTRACT_ADDRESS,
56- // entrypoint: ARGENT_DUMMY_CONTRACT_ENTRYPOINT,
57- // calldata: ["0x1"],
58- // };
59- // const { transaction_hash } = await account.execute(call, {
60- // version: "0x3",
61- // // resourceBounds: { ... },
62- // });
63- // setTxHash(transaction_hash);
64- // await account.waitForTransaction(transaction_hash);
65- // } catch (error) {
66- // console.error("Transaction failed", error);
67- // } finally {
68- // setIsLoading(false);
69- // }
70- // }, [account]);
39+ console . log ( "Connected to Argent Web Wallet" , res ) ;
40+ const { account, callbackData, approvalTransactionHash } = res ;
7141
72- // return { account, isLoading, txHash, connect, submitTransaction };
73- // };
42+ if ( account . getSessionStatus ( ) !== "VALID" ) {
43+ console . log ( "Session is not valid" ) ;
44+ return ;
45+ }
46+
47+ setAccount ( account ) ;
48+ console . log ( "Callback data" , callbackData ) ; // -- custom_callback_string
49+ console . log ( "Approval transaction hash" , approvalTransactionHash ) ; // -- custom_callback_string
50+ } )
51+ . catch ( ( err ) => {
52+ console . error ( "Failed to connect to Argent Web Wallet" , err ) ;
53+ } ) ;
54+ } , [ ] ) ;
55+
56+ const submitTransaction = useCallback ( async ( ) => {
57+ if ( ! account ) {
58+ console . error ( "Account not connected" ) ;
59+ return ;
60+ }
61+ setIsLoading ( true ) ;
62+ try {
63+ const call = {
64+ contractAddress : ARGENT_DUMMY_CONTRACT_ADDRESS ,
65+ entrypoint : ARGENT_DUMMY_CONTRACT_ENTRYPOINT ,
66+ calldata : [ "0x1" ] ,
67+ } ;
68+ const { transaction_hash } = await account . execute ( call , {
69+ version : "0x3" ,
70+ // resourceBounds: { ... },
71+ } ) ;
72+ setTxHash ( transaction_hash ) ;
73+ await account . waitForTransaction ( transaction_hash ) ;
74+ } catch ( error ) {
75+ console . error ( "Transaction failed" , error ) ;
76+ } finally {
77+ setIsLoading ( false ) ;
78+ }
79+ } , [ account ] ) ;
80+
81+
82+ const handleConnect = async ( ) => {
83+ try {
84+ const response = await argentWebWallet . requestConnection ( {
85+ callbackData : "custom_callback_data" ,
86+ approvalRequests : [
87+ {
88+ tokenAddress : "0x049D36570D4e46f48e99674bd3fcc84644DdD6b96F7C741B1562B82f9e004dC7" ,
89+ amount : BigInt ( "100000000000000000" ) . toString ( ) ,
90+ // Your dapp contract
91+ spender : "0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a" ,
92+ } ,
93+ ] ,
94+ } ) ;
95+
96+ const { account : sessionAccount } = response
97+ console . log ( sessionAccount ) ;
98+ setAccount ( sessionAccount ) ;
99+ } catch ( err ) {
100+ console . error ( err ) ;
101+ }
102+ } ;
103+
104+ return { account, isLoading, txHash, submitTransaction, handleConnect } ;
105+ } ;
0 commit comments