generated from overmind-xyz/pay-me-a-river-remastered
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalletSelector.tsx
206 lines (184 loc) · 7.56 KB
/
walletSelector.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"use client";
import { WalletReadyState, useWallet } from "@aptos-labs/wallet-adapter-react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { useEffect, useState } from "react";
import { FaucetClient, Network } from "aptos";
import { ChevronDownIcon } from "@radix-ui/react-icons";
/*
Component that displays a button to connect a wallet. If the wallet is connected, it displays the
wallet's APT balance, address and a button to disconnect the wallet.
When the connect button is clicked, a dialog is displayed with a list of all supported wallets. If
a supported wallet is installed, the user can click the connect button to connect the wallet. If
the wallet is not installed, the user can click the install button to install the wallet.
*/
export default function WalletSelector(props: { isTxnInProgress?: boolean }) {
// wallet state variables
const { connect, account, connected, disconnect, wallets, isLoading } = useWallet();
// State to hold the current account's APT balance. In string - floating point format.
const [balance, setBalance] = useState<string | undefined>(undefined);
// State to hold whether the faucet is loading or not.
const [isFaucetLoading, setIsFaucetLoading] = useState(false);
/*
Gets the balance of the connected account whenever the connected, account, isFaucetLoading,
and isTxnInProgress variables change.
Also checks if the account exists. If the account does not exist, it initializes the account
by funding it with 1 APT.
*/
useEffect(() => {
if (connected && account) {
ensureAccountExists().then(() => {
getBalance(account.address);
});
}
}, [connected, account, props.isTxnInProgress, isFaucetLoading]);
/*
Checks if the account exists. If the account does not exist, it initializes the account
by funding it with 1 APT.
*/
const ensureAccountExists = async () => {
/*
TODO #5: Make a request to the api endpoint to retrieve the account data. If the request returns
an object that contains `error_code` of `account_not_found`, call the initializeAccount
function to initialize the account.
*/
}
/*
Initializes the account by funding it with 1 APT.
*/
const initializeAccount = async () => {
/*
TODO #6: Return if the wallet is not connected, the account is not defined, a transaction is
in progress, or the faucet is loading.
*/
/*
TODO #7: Set the isFaucetLoading state variable to prevent this function from being called again.
*/
/*
TODO #8: Create a new faucet client with the testnet network and faucet url. Then, call the
fundAccount function to fund the account with 1 APT. Catch any errors that occur.
*/
/*
TODO #9: Set the isFaucetLoading state variable to false.
*/
}
/*
Gets the balance of the given address. In case of an error, the balance is set to 0. The balance
is returned in floating point format.
@param address - The address to get the APT balance of.
*/
const getBalance = async (address: string) => {
/*
TODO #3: Make a call to the 0x1::coin::balance function to get the balance of the given address.
HINT:
- The APT balance is return with a certain number of decimal places. Remember to convert the
balance to floating point format as a string.
- Remember to make the API request in a try/catch block. If there is an error, set the
balance to "0".
*/
};
return (
<div>
{!connected && !isLoading && (
<Dialog>
<DialogTrigger asChild>
<Button className="bg-green-800 hover:bg-green-700 text-white font-matter font-medium px-3 space-x-2">
<p>Connect Wallet</p>
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Connect your wallet</DialogTitle>
{
/*
TODO #1: Return a list of all supported wallets. If the wallet is installed, display
a button to connect the wallet. If the wallet is not installed, display a button
to install the wallet.
HINT:
- Use the two components below to display the wallet name and the connect or
install button. Remember to fill in the `onClick` event handler for the connect
button and the `href` for the install button.
- Use the `wallets` array to get the list of supported wallets.
- Fill in the `Wallet Name` placeholder with the name of the wallet.
-- Connect Wallet Component --
<div
key={wallet.name}
className="flex w-full items-center justify-between rounded-xl p-2"
>
<h1>PLACEHOLDER: Wallet Name</h1>
<Button variant="secondary" onClick={() => console.log("PLACEHOLDER: Connect wallet")}>
Connect
</Button>
</div>
-- Install Wallet Component --
<div
key={wallet.name}
className="flex w-full items-center justify-between rounded-xl p-2"
>
<h1>PLACEHOLDER: Wallet Name</h1>
<a href="PLACEHOLDER.com" target="_blank">
<Button variant="secondary">
Install
</Button>
</a>
</div>
*/
}
</DialogHeader>
</DialogContent>
</Dialog>
)}
{
/*
TODO #4: Display a loading button if the wallet is currently loading
HINT:
- Use the `isLoading` variable to check if the wallet is loading.
- Use the Button component below to display.
-- Loading Button Component --
<Button variant="secondary" disabled>
Loading...
</Button>
*/
}
{
/*
TODO #2: Display the wallet's APT balance and address if the wallet is connected and the
account is defined. Use the component below to display the wallet's APT balance and
address, as well as provide the disconnect button.
HINT:
- Use the `connected` and `account` variables to check if the wallet is connected and the
account is defined.
- Use the `balance` state variable to display the wallet's APT balance.
- Remember to fill in the `onClick` event handler for the disconnect button.
-- Wallet Balance Component --
<div>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button className="font-mono">
PLACEHOLDER APT | {account.address.slice(0, 5)}...{account.address.slice(-4)}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem onClick={() => {console.log("PLACEHOLDER: Disconnect wallet")}}>
Disconnect
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
*/
}
</div>
);
}