@algobright/solana-connector 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,10 @@ interface ConnectButtonProps {
8
8
  connectText?: string;
9
9
  connectingText?: string;
10
10
  showSolBalance?: boolean;
11
+ showDefaultToken?: {
12
+ address: string;
13
+ symbol: string;
14
+ };
11
15
  }
12
16
  declare function ConnectButton(props: ConnectButtonProps): react_jsx_runtime.JSX.Element;
13
17
 
@@ -8,6 +8,10 @@ interface ConnectButtonProps {
8
8
  connectText?: string;
9
9
  connectingText?: string;
10
10
  showSolBalance?: boolean;
11
+ showDefaultToken?: {
12
+ address: string;
13
+ symbol: string;
14
+ };
11
15
  }
12
16
  declare function ConnectButton(props: ConnectButtonProps): react_jsx_runtime.JSX.Element;
13
17
 
@@ -38,7 +38,7 @@ module.exports = __toCommonJS(ConnectButton_exports);
38
38
  // src/components/ConnectButton/ConnectButton.tsx
39
39
  var import_react6 = require("react");
40
40
  var import_ConnectButton = __toESM(require("./ConnectButton.module-O3M32YJK.module.css"));
41
- var import_connector3 = require("@solana/connector");
41
+ var import_connector4 = require("@solana/connector");
42
42
 
43
43
  // src/components/shared/Button/Button.tsx
44
44
  var React = __toESM(require("react"));
@@ -904,12 +904,70 @@ var import_lucide_react5 = require("lucide-react");
904
904
 
905
905
  // src/components/WalletDropdown/WalletDropdown.tsx
906
906
  var import_react4 = require("react");
907
- var import_WalletDropdown = __toESM(require("./WalletDropdown.module-A6KHWKGK.module.css"));
907
+ var import_kit2 = require("@solana/kit");
908
+ var import_WalletDropdown = __toESM(require("./WalletDropdown.module-DOK7CUOQ.module.css"));
908
909
  var import_react5 = require("motion/react");
909
910
  var import_lucide_react4 = require("lucide-react");
911
+ var import_connector3 = require("@solana/connector");
912
+ var import_clsx3 = require("clsx");
913
+
914
+ // src/utils/fetchBalance.tsx
915
+ var import_token = require("@solana-program/token");
910
916
  var import_connector2 = require("@solana/connector");
911
917
  var import_kit = require("@solana/kit");
912
- var import_clsx3 = require("clsx");
918
+ async function getSolBalance(client, pubkey) {
919
+ let balance = 0;
920
+ try {
921
+ const rpcUrl = client.getRpcUrl();
922
+ if (!rpcUrl) {
923
+ console.error("RPC URL is not available from the ConnectorClient.");
924
+ return 0;
925
+ }
926
+ const pubkeyAddress = (0, import_connector2.address)(pubkey);
927
+ const rpc = (0, import_kit.createSolanaRpc)(rpcUrl);
928
+ const balanceResponse = await rpc.getBalance(pubkeyAddress).send();
929
+ balance = (0, import_connector2.lamportsToSol)(balanceResponse.value);
930
+ } catch (error) {
931
+ console.error("Error fetching SOL balance:", error);
932
+ } finally {
933
+ return balance;
934
+ }
935
+ }
936
+ async function getTokenBalance(client, pubkey, mintAddress) {
937
+ var _a;
938
+ let balance = 0;
939
+ try {
940
+ const rpcUrl = client.getRpcUrl();
941
+ if (!rpcUrl) {
942
+ console.error("RPC URL is not available from the ConnectorClient.");
943
+ return 0;
944
+ }
945
+ const pubkeyAddress = (0, import_connector2.address)(pubkey);
946
+ const mintPubkey = (0, import_connector2.address)(mintAddress);
947
+ const rpc = (0, import_kit.createSolanaRpc)(rpcUrl);
948
+ const mintInfo = await rpc.getAccountInfo(mintPubkey).send();
949
+ const ownerProgram = (_a = mintInfo.value) == null ? void 0 : _a.owner;
950
+ if (!ownerProgram) {
951
+ throw new Error("Failed to fetch mint account info");
952
+ }
953
+ const tokenProgram = (0, import_connector2.address)(ownerProgram);
954
+ const [tokenPDA] = await (0, import_token.findAssociatedTokenPda)({
955
+ mint: mintPubkey,
956
+ owner: pubkeyAddress,
957
+ tokenProgram
958
+ });
959
+ const tokenBalance = await rpc.getTokenAccountBalance(tokenPDA).send();
960
+ if (tokenBalance.value) {
961
+ balance = parseFloat(tokenBalance.value.uiAmountString);
962
+ }
963
+ } catch (error) {
964
+ console.error("Error fetching token balance:", error);
965
+ } finally {
966
+ return balance;
967
+ }
968
+ }
969
+
970
+ // src/components/WalletDropdown/WalletDropdown.tsx
913
971
  var import_jsx_runtime9 = require("react/jsx-runtime");
914
972
  var networkColor = {
915
973
  "solana:mainnet": "#00c950",
@@ -918,13 +976,25 @@ var networkColor = {
918
976
  "solana:localnet": "#ff3b3b"
919
977
  };
920
978
  function WalletDropdown(props) {
921
- const client = (0, import_connector2.useConnectorClient)();
922
- const { CN_ConnectButton, selectedAccount, walletIcon, walletName, theme, allowNetworkSwitch, showSolBalance } = props;
979
+ const client = (0, import_connector3.useConnectorClient)();
980
+ const {
981
+ CN_ConnectButton,
982
+ selectedAccount,
983
+ walletIcon,
984
+ walletName,
985
+ theme,
986
+ allowNetworkSwitch,
987
+ showSolBalance,
988
+ showDefaultToken
989
+ } = props;
923
990
  const [view, setView] = (0, import_react4.useState)("wallet");
924
991
  const [copied, setCopied] = (0, import_react4.useState)(false);
925
- const fetching = (0, import_react4.useRef)(false);
992
+ const fetchingSolBalance = (0, import_react4.useRef)(false);
926
993
  const [isFetchingBalance, setIsFetchingBalance] = (0, import_react4.useState)(false);
994
+ const fetchingDefaultToken = (0, import_react4.useRef)(false);
995
+ const [isFetchingDefaultTokenBalance, setIsFetchingDefaultTokenBalance] = (0, import_react4.useState)(false);
927
996
  const [solBalance, setSolBalance] = (0, import_react4.useState)(null);
997
+ const [defaultTokenBalance, setDefaultTokenBalance] = (0, import_react4.useState)(null);
928
998
  const shortAddress = `${selectedAccount.slice(0, 4)}...${selectedAccount.slice(-4)}`;
929
999
  async function handleCopy() {
930
1000
  try {
@@ -937,29 +1007,36 @@ function WalletDropdown(props) {
937
1007
  }
938
1008
  }
939
1009
  async function fetchSolBalance() {
940
- if (!client || fetching.current) return;
1010
+ if (!client || fetchingSolBalance.current) return;
941
1011
  setIsFetchingBalance(true);
942
- fetching.current = true;
943
- try {
944
- const rpcUrl = client.getRpcUrl();
945
- const pubkey = (0, import_connector2.address)(selectedAccount);
946
- if (!rpcUrl) throw new Error("No RPC endpoint configured");
947
- const rpc = (0, import_kit.createSolanaRpc)(rpcUrl);
948
- const solLamports = (await rpc.getBalance(pubkey).send()).value || 0;
949
- const sol = (0, import_connector2.lamportsToSol)(solLamports);
950
- setSolBalance(sol);
951
- } catch (error) {
952
- setSolBalance(0);
953
- } finally {
954
- setIsFetchingBalance(false);
955
- fetching.current = false;
1012
+ fetchingSolBalance.current = true;
1013
+ const solBalance2 = await getSolBalance(client, selectedAccount);
1014
+ setSolBalance(solBalance2);
1015
+ setIsFetchingBalance(false);
1016
+ fetchingSolBalance.current = false;
1017
+ }
1018
+ async function fetchDefaultTokenBalance() {
1019
+ if (!showDefaultToken || !showDefaultToken.address || !client || fetchingDefaultToken.current) return;
1020
+ const isValidAddress = (0, import_kit2.isAddress)(showDefaultToken.address);
1021
+ if (!isValidAddress) {
1022
+ console.error("Invalid default token address:", showDefaultToken);
1023
+ return;
956
1024
  }
1025
+ setIsFetchingDefaultTokenBalance(true);
1026
+ fetchingDefaultToken.current = true;
1027
+ const tokenBalance = await getTokenBalance(client, selectedAccount, showDefaultToken.address);
1028
+ setDefaultTokenBalance(tokenBalance);
1029
+ setIsFetchingDefaultTokenBalance(false);
1030
+ fetchingDefaultToken.current = false;
957
1031
  }
958
1032
  (0, import_react4.useEffect)(() => {
959
1033
  if (showSolBalance && selectedAccount && client) {
960
1034
  fetchSolBalance();
961
1035
  }
962
- }, [selectedAccount, client, showSolBalance]);
1036
+ if (showDefaultToken && selectedAccount && client) {
1037
+ fetchDefaultTokenBalance();
1038
+ }
1039
+ }, [selectedAccount, client, showSolBalance, showDefaultToken]);
963
1040
  if (view === "wallet") {
964
1041
  return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
965
1042
  import_react5.motion.div,
@@ -1001,7 +1078,7 @@ function WalletDropdown(props) {
1001
1078
  }
1002
1079
  ),
1003
1080
  allowNetworkSwitch && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
1004
- import_connector2.ClusterElement,
1081
+ import_connector3.ClusterElement,
1005
1082
  {
1006
1083
  render: ({ cluster }) => /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
1007
1084
  Button_default2,
@@ -1053,10 +1130,38 @@ function WalletDropdown(props) {
1053
1130
  ]
1054
1131
  }
1055
1132
  ),
1056
- /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("div", { className: import_WalletDropdown.default.balanceValue, children: isFetchingBalance ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("div", { className: import_WalletDropdown.default.balanceLoading }) : solBalance !== null ? `${solBalance.toFixed(4)} SOL` : "-- SOL" })
1133
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("div", { className: import_WalletDropdown.default.balanceValue, title: String(solBalance) || "0", children: isFetchingBalance ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("div", { className: import_WalletDropdown.default.balanceLoading }) : solBalance !== null ? `${solBalance.toFixed(4)} SOL` : "-- SOL" })
1134
+ ] }),
1135
+ showDefaultToken && /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("div", { className: import_WalletDropdown.default.balanceSection, children: [
1136
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
1137
+ "div",
1138
+ {
1139
+ className: import_WalletDropdown.default.balanceHeader,
1140
+ children: [
1141
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { className: import_WalletDropdown.default.balanceLabel, children: "Balance" }),
1142
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
1143
+ "button",
1144
+ {
1145
+ onClick: () => fetchDefaultTokenBalance(),
1146
+ disabled: isFetchingDefaultTokenBalance,
1147
+ title: "Refresh balance",
1148
+ className: import_WalletDropdown.default.refreshButton,
1149
+ "data-loading": isFetchingDefaultTokenBalance,
1150
+ children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
1151
+ import_lucide_react4.RefreshCw,
1152
+ {
1153
+ className: import_WalletDropdown.default.refreshIcon
1154
+ }
1155
+ )
1156
+ }
1157
+ )
1158
+ ]
1159
+ }
1160
+ ),
1161
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("div", { className: import_WalletDropdown.default.balanceValue, title: String(defaultTokenBalance) || "0", children: isFetchingDefaultTokenBalance ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("div", { className: import_WalletDropdown.default.balanceLoading }) : defaultTokenBalance !== null ? `${defaultTokenBalance.toFixed(4)} ${(showDefaultToken == null ? void 0 : showDefaultToken.symbol) || ""}` : `-- ${(showDefaultToken == null ? void 0 : showDefaultToken.symbol) || ""}` })
1057
1162
  ] }),
1058
1163
  /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
1059
- import_connector2.DisconnectElement,
1164
+ import_connector3.DisconnectElement,
1060
1165
  {
1061
1166
  render: ({ disconnect, disconnecting }) => /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
1062
1167
  Button_default2,
@@ -1078,7 +1183,6 @@ function WalletDropdown(props) {
1078
1183
  );
1079
1184
  }
1080
1185
  if (view === "network") {
1081
- console.count("Network view rendered");
1082
1186
  return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
1083
1187
  import_react5.motion.div,
1084
1188
  {
@@ -1105,7 +1209,7 @@ function WalletDropdown(props) {
1105
1209
  /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { children: "Network Settings" })
1106
1210
  ] }),
1107
1211
  /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
1108
- import_connector2.ClusterElement,
1212
+ import_connector3.ClusterElement,
1109
1213
  {
1110
1214
  render: ({ cluster, clusters, setCluster }) => {
1111
1215
  const currentClusterId = (cluster == null ? void 0 : cluster.id) || "solana:mainnet";
@@ -1163,10 +1267,11 @@ function ConnectButton(props) {
1163
1267
  CN_ConnectButton,
1164
1268
  connectText = "Connect Wallet",
1165
1269
  connectingText = "Connecting...",
1166
- showSolBalance = false
1270
+ showSolBalance = false,
1271
+ showDefaultToken
1167
1272
  } = props;
1168
1273
  const [isModalOpen, setIsModalOpen] = (0, import_react6.useState)(false);
1169
- const { isConnected, isConnecting, account, connector, walletConnectUri, clearWalletConnectUri } = (0, import_connector3.useConnector)();
1274
+ const { isConnected, isConnecting, account, connector, walletConnectUri, clearWalletConnectUri } = (0, import_connector4.useConnector)();
1170
1275
  if (isConnected && account && connector) {
1171
1276
  const shortAddress = `${account.slice(0, 4)}...${account.slice(-4)}`;
1172
1277
  const walletIcon = connector.icon || void 0;
@@ -1203,7 +1308,8 @@ function ConnectButton(props) {
1203
1308
  walletName: connector.name,
1204
1309
  allowNetworkSwitch: true,
1205
1310
  theme,
1206
- showSolBalance
1311
+ showSolBalance,
1312
+ showDefaultToken
1207
1313
  }
1208
1314
  ) }) }) })
1209
1315
  ] });