Skip to content

Commit 0eba11d

Browse files
committed
feat: wallet selector
1 parent 697c416 commit 0eba11d

File tree

24 files changed

+294
-117
lines changed

24 files changed

+294
-117
lines changed

Diff for: src/actions/borrow.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ import {
3232
initObligationInstruction,
3333
} from "../models";
3434
import { toLamports } from "../utils/utils";
35+
import { WalletAdapter } from "../contexts/wallet";
3536

3637
export const borrow = async (
3738
connection: Connection,
38-
wallet: any,
39+
wallet: WalletAdapter,
3940

4041
from: TokenAccount,
4142
amount: number,
@@ -49,6 +50,10 @@ export const borrow = async (
4950

5051
obligationAccount?: PublicKey
5152
) => {
53+
if (!wallet.publicKey) {
54+
throw new Error('Wallet is not connected');
55+
}
56+
5257
notify({
5358
message: "Borrowing funds...",
5459
description: "Please review transactions to approve.",

Diff for: src/actions/deposit.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ import {
2020
findOrCreateAccountByMint,
2121
} from "./account";
2222
import { approve, TokenAccount } from "../models";
23+
import { WalletAdapter } from "../contexts/wallet";
2324

2425
export const deposit = async (
2526
from: TokenAccount,
2627
amountLamports: number,
2728
reserve: LendingReserve,
2829
reserveAddress: PublicKey,
2930
connection: Connection,
30-
wallet: any
31+
wallet: WalletAdapter
3132
) => {
33+
if (!wallet.publicKey) {
34+
throw new Error('Wallet is not connected');
35+
}
36+
3237
notify({
3338
message: "Depositing funds...",
3439
description: "Please review transactions to approve.",

Diff for: src/actions/liquidate.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import {
2525
TokenAccount,
2626
} from "../models";
2727
import { cache, ParsedAccount } from "../contexts/accounts";
28+
import { WalletAdapter } from "../contexts/wallet";
2829

2930
export const liquidate = async (
3031
connection: Connection,
31-
wallet: any,
32+
wallet: WalletAdapter,
3233
from: TokenAccount, // liquidity account
3334
amountLamports: number, // in liquidty token (lamports)
3435

@@ -39,6 +40,10 @@ export const liquidate = async (
3940

4041
withdrawReserve: ParsedAccount<LendingReserve>
4142
) => {
43+
if (!wallet.publicKey) {
44+
throw new Error('Wallet is not connected');
45+
}
46+
4247
notify({
4348
message: "Repaying funds...",
4449
description: "Please review transactions to approve.",

Diff for: src/actions/repay.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../utils/ids";
1616
import { createTokenAccount, findOrCreateAccountByMint } from "./account";
1717
import { approve, LendingObligation, TokenAccount } from "../models";
1818
import { ParsedAccount } from "../contexts/accounts";
19+
import { WalletAdapter } from "../contexts/wallet";
1920

2021
export const repay = async (
2122
from: TokenAccount,
@@ -31,8 +32,12 @@ export const repay = async (
3132
withdrawReserve: ParsedAccount<LendingReserve>,
3233

3334
connection: Connection,
34-
wallet: any
35+
wallet: WalletAdapter
3536
) => {
37+
if (!wallet.publicKey) {
38+
throw new Error('Wallet is not connected');
39+
}
40+
3641
notify({
3742
message: "Repaying funds...",
3843
description: "Please review transactions to approve.",

Diff for: src/actions/withdraw.tsx

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import {
44
PublicKey,
55
TransactionInstruction,
66
} from "@solana/web3.js";
7+
import { AccountLayout } from "@solana/spl-token";
78
import { sendTransaction } from "../contexts/connection";
8-
import { notify } from "../utils/notifications";
9+
import {WalletAdapter} from "../contexts/wallet";
910
import {
1011
accrueInterestInstruction,
1112
LendingReserve,
1213
withdrawInstruction,
13-
} from "./../models/lending";
14-
import { AccountLayout } from "@solana/spl-token";
14+
} from "../models/lending";
1515
import { LENDING_PROGRAM_ID } from "../utils/ids";
16+
import { notify } from "../utils/notifications";
1617
import { findOrCreateAccountByMint } from "./account";
1718
import { approve, TokenAccount } from "../models";
1819

@@ -22,8 +23,12 @@ export const withdraw = async (
2223
reserve: LendingReserve,
2324
reserveAddress: PublicKey,
2425
connection: Connection,
25-
wallet: any
26+
wallet: WalletAdapter
2627
) => {
28+
if (!wallet.publicKey) {
29+
throw new Error('Wallet is not connected');
30+
}
31+
2732
notify({
2833
message: "Withdrawing funds...",
2934
description: "Please review transactions to approve.",

Diff for: src/components/AppBar/index.tsx

+8-19
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,21 @@ import { CurrentUserBadge } from "../CurrentUserBadge";
55
import { SettingOutlined } from "@ant-design/icons";
66
import { Settings } from "../Settings";
77
import { LABELS } from "../../constants";
8+
import {ConnectButton} from "../ConnectButton";
89

910
export const AppBar = (props: { left?: JSX.Element; right?: JSX.Element }) => {
10-
const { connected, connect, disconnect } = useWallet();
11+
const { connected } = useWallet();
1112

1213
const TopBar = (
1314
<div className="App-Bar-right">
14-
<CurrentUserBadge />
15-
<div>
16-
{!connected && (
17-
<Button
15+
{connected ? (
16+
<CurrentUserBadge />
17+
) : (
18+
<ConnectButton
1819
type="text"
1920
size="large"
20-
onClick={connected ? disconnect : connect}
21-
style={{ color: "#2abdd2" }}
22-
>
23-
{LABELS.CONNECT_BUTTON}
24-
</Button>
25-
)}
26-
{connected && (
27-
<Popover
28-
placement="bottomRight"
29-
title={LABELS.WALLET_TOOLTIP}
30-
trigger="click"
31-
></Popover>
32-
)}
33-
</div>
21+
style={{ color: "#2abdd2" }}/>
22+
)}
3423
<Popover
3524
placement="topRight"
3625
title={LABELS.SETTINGS_TOOLTIP}

Diff for: src/components/BorrowInput/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export const BorrowInput = (props: {
131131
collateralReserve?.info.collateralMint
132132
);
133133
const onBorrow = useCallback(() => {
134-
if (!collateralReserve) {
134+
if (!collateralReserve || !wallet?.publicKey) {
135135
return;
136136
}
137137

Diff for: src/components/CurrentUserBadge/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const CurrentUserBadge = (props: {}) => {
99
const { wallet } = useWallet();
1010
const { account } = useNativeAccount();
1111

12-
if (!wallet || !wallet.publicKey) {
12+
if (!wallet?.publicKey) {
1313
return null;
1414
}
1515

@@ -23,7 +23,7 @@ export const CurrentUserBadge = (props: {}) => {
2323
<div className="wallet-key">
2424
{shortenAddress(`${wallet.publicKey}`)}
2525
<Identicon
26-
address={wallet.publicKey?.toBase58()}
26+
address={wallet.publicKey.toBase58()}
2727
style={{ marginLeft: "0.5rem", display: "flex" }}
2828
/>
2929
</div>

Diff for: src/components/DepositInput/index.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export const DepositInput = (props: {
4343
const { value, setValue, pct, setPct, type } = useSliderInput(convert);
4444

4545
const onDeposit = useCallback(() => {
46+
if (!wallet?.publicKey) {
47+
return;
48+
}
49+
4650
setPendingTx(true);
4751

4852
(async () => {

Diff for: src/components/LiquidateInput/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const LiquidateInput = (props: {
6262
const { value, setValue, pct, setPct, type } = useSliderInput(convert);
6363

6464
const onLiquidate = useCallback(() => {
65-
if (!withdrawReserve) {
65+
if (!withdrawReserve || !wallet?.publicKey) {
6666
return;
6767
}
6868

Diff for: src/components/RepayInput/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export const RepayInput = (props: {
7474
!collateralReserve ||
7575
!obligation ||
7676
!repayReserve ||
77-
!obligationAccount
77+
!obligationAccount ||
78+
!wallet?.publicKey
7879
) {
7980
return;
8081
}

Diff for: src/components/Settings/index.tsx

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from "react";
2-
import { Select } from "antd";
2+
import { Button, Select } from 'antd';
33
import { ENDPOINTS, useConnectionConfig } from "../../contexts/connection";
4-
import { useWallet, WALLET_PROVIDERS } from "../../contexts/wallet";
4+
import { useWallet } from "../../contexts/wallet";
55

66
export const Settings = () => {
7-
const { providerUrl, setProvider } = useWallet();
7+
const { select } = useWallet();
88
const { endpoint, setEndpoint } = useConnectionConfig();
99

1010
return (
@@ -14,24 +14,15 @@ export const Settings = () => {
1414
<Select
1515
onSelect={setEndpoint}
1616
value={endpoint}
17-
style={{ marginRight: 8 }}
17+
style={{ marginBottom: 20 }}
1818
>
1919
{ENDPOINTS.map(({ name, endpoint }) => (
2020
<Select.Option value={endpoint} key={endpoint}>
2121
{name}
2222
</Select.Option>
2323
))}
2424
</Select>
25-
</div>
26-
<div style={{ display: "grid" }}>
27-
Wallet:{" "}
28-
<Select onSelect={setProvider} value={providerUrl}>
29-
{WALLET_PROVIDERS.map(({ name, url }) => (
30-
<Select.Option value={url} key={url}>
31-
{name}
32-
</Select.Option>
33-
))}
34-
</Select>
25+
<Button type="primary" onClick={select}>Select Wallet</Button>
3526
</div>
3627
</>
3728
);

Diff for: src/components/WithdrawInput/index.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export const WithdrawInput = (props: {
5252
const { value, setValue, pct, setPct, type } = useSliderInput(convert);
5353

5454
const onWithdraw = useCallback(() => {
55+
if (!wallet?.publicKey) {
56+
return;
57+
}
58+
5559
setPendingTx(true);
5660

5761
(async () => {
@@ -94,6 +98,7 @@ export const WithdrawInput = (props: {
9498
type,
9599
value,
96100
wallet,
101+
wallet?.publicKey
97102
]);
98103

99104
const bodyStyle: React.CSSProperties = {

Diff for: src/contexts/accounts.tsx

+16-6
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,19 @@ const UseNativeAccount = () => {
327327

328328
const updateCache = useCallback(
329329
(account) => {
330+
if (!connection || !wallet?.publicKey) {
331+
return;
332+
}
333+
330334
const wrapped = wrapNativeAccount(wallet.publicKey, account);
331-
if (wrapped !== undefined && wallet) {
332-
const id = wallet.publicKey?.toBase58();
335+
if (wrapped !== undefined) {
336+
const id = wallet.publicKey.toBase58();
333337
cache.registerParser(id, TokenAccountParser);
334338
genericCache.set(id, wrapped as TokenAccount);
335339
cache.emitter.raiseCacheUpdated(id, false, TokenAccountParser);
336340
}
337341
},
338-
[wallet]
342+
[wallet, wallet?.publicKey, connection]
339343
);
340344

341345
useEffect(() => {
@@ -355,7 +359,7 @@ const UseNativeAccount = () => {
355359
setNativeAccount(acc);
356360
}
357361
});
358-
}, [setNativeAccount, wallet, wallet.publicKey, connection, updateCache]);
362+
}, [setNativeAccount, wallet, wallet?.publicKey, connection, updateCache]);
359363

360364
return { nativeAccount };
361365
};
@@ -389,14 +393,20 @@ export function AccountsProvider({ children = null as any }) {
389393
const { nativeAccount } = UseNativeAccount();
390394

391395
const selectUserAccounts = useCallback(() => {
396+
if (!wallet?.publicKey) {
397+
return [];
398+
}
399+
400+
const publicKey = wallet.publicKey.toBase58();
401+
392402
return cache
393403
.byParser(TokenAccountParser)
394404
.map((id) => cache.get(id))
395405
.filter(
396-
(a) => a && a.info.owner.toBase58() === wallet.publicKey?.toBase58()
406+
(a) => a && a.info.owner.toBase58() === publicKey
397407
)
398408
.map((a) => a as TokenAccount);
399-
}, [wallet]);
409+
}, [wallet, wallet?.publicKey]);
400410

401411
useEffect(() => {
402412
const accounts = selectUserAccounts().filter(

Diff for: src/contexts/connection.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { notify } from "./../utils/notifications";
1111
import { ExplorerLink } from "../components/ExplorerLink";
1212
import LocalTokens from "../config/tokens.json";
1313
import { setProgramIds } from "../utils/ids";
14+
import { WalletAdapter } from "./wallet";
1415

1516
export type ENV =
1617
| "mainnet-beta"
@@ -217,11 +218,15 @@ const getErrorForTransaction = async (connection: Connection, txid: string) => {
217218

218219
export const sendTransaction = async (
219220
connection: Connection,
220-
wallet: any,
221+
wallet: WalletAdapter,
221222
instructions: TransactionInstruction[],
222223
signers: Account[],
223224
awaitConfirmation = true
224225
) => {
226+
if (!wallet?.publicKey) {
227+
throw new Error('Wallet is not connected');
228+
}
229+
225230
let transaction = new Transaction();
226231
instructions.forEach((instruction) => transaction.add(instruction));
227232
transaction.recentBlockhash = (

0 commit comments

Comments
 (0)