@cordystackx/cordy_minikit 2.0.10 → 3.0.0
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.
- package/README.md +53 -753
- package/dist/client/jwt.d.ts +32 -0
- package/dist/client/jwt.d.ts.map +1 -0
- package/dist/client/jwt.js +84 -0
- package/dist/client/transfer.d.ts +104 -0
- package/dist/client/transfer.d.ts.map +1 -0
- package/dist/client/transfer.js +613 -0
- package/dist/client.d.ts +5 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +2 -0
- package/dist/client__provider.d.ts +3 -1
- package/dist/client__provider.d.ts.map +1 -1
- package/dist/client__provider.js +29 -2
- package/dist/components/Connect_wallet_bt.d.ts +9 -3
- package/dist/components/Connect_wallet_bt.d.ts.map +1 -1
- package/dist/components/Connect_wallet_bt.js +63 -47
- package/dist/components/UI_Comp.d.ts.map +1 -1
- package/dist/components/UI_Comp.js +51 -60
- package/dist/config/Image.json +2 -2
- package/dist/config/walletConfig.d.ts +3 -0
- package/dist/config/walletConfig.d.ts.map +1 -1
- package/dist/config/walletConfig.js +43 -2
- package/dist/controllers/Get_Account_Info.d.ts.map +1 -1
- package/dist/controllers/Get_Balance.d.ts +22 -0
- package/dist/controllers/Get_Balance.d.ts.map +1 -1
- package/dist/controllers/Get_Balance.js +90 -28
- package/dist/controllers/StellarButton.d.ts.map +1 -1
- package/dist/controllers/StellarButton.js +4 -0
- package/dist/controllers/WagmiButton.d.ts +2 -1
- package/dist/controllers/WagmiButton.d.ts.map +1 -1
- package/dist/controllers/WagmiButton.js +33 -33
- package/dist/controllers/loadStellarBalance.d.ts +23 -1
- package/dist/controllers/loadStellarBalance.d.ts.map +1 -1
- package/dist/controllers/loadStellarBalance.js +47 -14
- package/dist/css/UI_Comp/styles.module.css +121 -314
- package/dist/demo/next.config.d.ts +4 -0
- package/dist/demo/next.config.d.ts.map +1 -0
- package/dist/demo/next.config.js +12 -0
- package/dist/demo/src/app/layout.d.ts +5 -0
- package/dist/demo/src/app/layout.d.ts.map +1 -0
- package/dist/demo/src/app/layout.js +9 -0
- package/dist/demo/src/app/page.d.ts +2 -0
- package/dist/demo/src/app/page.d.ts.map +1 -0
- package/dist/demo/src/app/page.js +9 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/package.json +29 -14
- package/dist/server/jwt.d.ts +18 -0
- package/dist/server/jwt.d.ts.map +1 -0
- package/dist/server/jwt.js +35 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/wagmi__providers.d.ts +7 -2
- package/dist/wagmi__providers.d.ts.map +1 -1
- package/dist/wagmi__providers.js +24 -5
- package/package.json +29 -14
|
@@ -1,40 +1,102 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Contract, formatUnits, isAddress, JsonRpcProvider } from "ethers";
|
|
2
2
|
import ERC20_ABI from "../config/ERC20_ABI.json";
|
|
3
|
-
|
|
3
|
+
/** Shared ERC-20 balance reader used by wallet status and the Cordy API. */
|
|
4
|
+
export async function readTokenBalance(address, options = {}) {
|
|
5
|
+
const tokenAddress = options.tokenAddress || process.env.NEXT_PUBLIC_TOKENADDRESS;
|
|
6
|
+
const rpcUrl = options.rpcUrl || process.env.NEXT_PUBLIC_RPC_ENDPOINT;
|
|
7
|
+
const provider = options.provider || (rpcUrl ? new JsonRpcProvider(rpcUrl) : undefined);
|
|
8
|
+
if (!tokenAddress)
|
|
9
|
+
throw new Error("No EVM token address is configured.");
|
|
10
|
+
if (!isAddress(tokenAddress))
|
|
11
|
+
throw new Error("The configured EVM token address is invalid.");
|
|
12
|
+
if (!provider)
|
|
13
|
+
throw new Error("No EVM RPC endpoint or connected wallet provider is available.");
|
|
14
|
+
let network;
|
|
4
15
|
try {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
16
|
+
network = await provider.getNetwork();
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
throw new Error("Could not connect to the configured EVM RPC endpoint.");
|
|
20
|
+
}
|
|
21
|
+
const chainId = Number(network.chainId);
|
|
22
|
+
if (options.expectedChainId !== undefined && chainId !== options.expectedChainId) {
|
|
23
|
+
throw new Error(`The EVM RPC is on chain ID ${chainId}, but the wallet is on ${options.networkName || "another network"} (chain ID ${options.expectedChainId}).`);
|
|
24
|
+
}
|
|
25
|
+
let readProvider = provider;
|
|
26
|
+
let code;
|
|
27
|
+
try {
|
|
28
|
+
code = await readProvider.getCode(tokenAddress);
|
|
29
|
+
}
|
|
30
|
+
catch (primaryError) {
|
|
31
|
+
const fallbackProvider = options.fallbackProvider;
|
|
32
|
+
if (!fallbackProvider || fallbackProvider === provider) {
|
|
33
|
+
throw new Error(`The EVM RPC could not read the token contract on ${options.networkName || network.name}. Check the RPC endpoint and try again.`);
|
|
10
34
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
35
|
+
try {
|
|
36
|
+
const fallbackNetwork = await fallbackProvider.getNetwork();
|
|
37
|
+
if (Number(fallbackNetwork.chainId) !== chainId) {
|
|
38
|
+
throw new Error(`The connected wallet is on chain ID ${fallbackNetwork.chainId}, not ${chainId}.`);
|
|
39
|
+
}
|
|
40
|
+
readProvider = fallbackProvider;
|
|
41
|
+
code = await readProvider.getCode(tokenAddress);
|
|
14
42
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
console.error(`No contract bytecode found at ${tokenAddress} on ${rpcEndpoint}`);
|
|
19
|
-
return { balance: "0", symbol: "" };
|
|
43
|
+
catch {
|
|
44
|
+
const cause = primaryError instanceof Error ? ` (${primaryError.message})` : "";
|
|
45
|
+
throw new Error(`Both the configured EVM RPC and connected wallet could not read the token contract on ${options.networkName || network.name}${cause}. Check RPC access and try again.`);
|
|
20
46
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
47
|
+
}
|
|
48
|
+
if (!code || code === "0x") {
|
|
49
|
+
throw new Error(`No token contract was found at the configured address on ${options.networkName || network.name}. Check the connected network and token address.`);
|
|
50
|
+
}
|
|
51
|
+
const token = new Contract(tokenAddress, ERC20_ABI, readProvider);
|
|
52
|
+
let symbol = options.symbol;
|
|
53
|
+
if (!symbol) {
|
|
27
54
|
try {
|
|
28
|
-
symbol = await
|
|
55
|
+
symbol = String(await token.symbol());
|
|
29
56
|
}
|
|
30
|
-
catch
|
|
31
|
-
|
|
32
|
-
symbol = "Unknown";
|
|
57
|
+
catch {
|
|
58
|
+
throw new Error("The configured EVM token does not expose ERC-20 symbol() on this network.");
|
|
33
59
|
}
|
|
34
|
-
return { balance: formattedBalance, symbol };
|
|
35
60
|
}
|
|
36
|
-
|
|
37
|
-
|
|
61
|
+
let decimals;
|
|
62
|
+
let rawBalance;
|
|
63
|
+
try {
|
|
64
|
+
decimals = options.decimals ?? Number(await token.decimals());
|
|
65
|
+
rawBalance = BigInt(await token.balanceOf(address));
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
if (readProvider !== provider && options.fallbackProvider) {
|
|
69
|
+
const fallbackToken = new Contract(tokenAddress, ERC20_ABI, options.fallbackProvider);
|
|
70
|
+
try {
|
|
71
|
+
decimals = options.decimals ?? Number(await fallbackToken.decimals());
|
|
72
|
+
rawBalance = BigInt(await fallbackToken.balanceOf(address));
|
|
73
|
+
readProvider = options.fallbackProvider;
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
throw new Error(`Could not read ${symbol} balance on ${options.networkName || network.name}. Check the token contract and RPC access.`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
balance: formatUnits(rawBalance, decimals),
|
|
85
|
+
rawBalance: rawBalance.toString(),
|
|
86
|
+
symbol,
|
|
87
|
+
decimals,
|
|
88
|
+
network: options.networkName || network.name,
|
|
89
|
+
chainId,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/** Legacy controller API kept for existing `useWalletStatus` and button callers. */
|
|
93
|
+
export default async function getTokenBalance(address, decimals) {
|
|
94
|
+
try {
|
|
95
|
+
const result = await readTokenBalance(address, { decimals });
|
|
96
|
+
return { balance: result.balance, symbol: result.symbol };
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
console.error("Error fetching token balance:", error);
|
|
38
100
|
return { balance: "0", symbol: "" };
|
|
39
101
|
}
|
|
40
102
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StellarButton.d.ts","sourceRoot":"","sources":["../../controllers/StellarButton.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"StellarButton.d.ts","sourceRoot":"","sources":["../../controllers/StellarButton.tsx"],"names":[],"mappings":"AAmBA,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,EAC1C,SAAS,EACT,cAAc,GACf,EAAE;IACD,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACtC,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE;QACxB,SAAS,EAAE,OAAO,CAAC;QACnB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB,KAAK,IAAI,CAAC;CACZ,+BAgFA"}
|
|
@@ -3,6 +3,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
3
3
|
import { useState } from "react";
|
|
4
4
|
import { requestAccess, getNetworkDetails, isConnected, } from "@stellar/freighter-api";
|
|
5
5
|
import img_src from "../config/Image.json";
|
|
6
|
+
import { isStellarEnvironmentConfigured } from "./loadStellarBalance";
|
|
6
7
|
const FREIGHTER_DOWNLOAD_URL = "https://www.freighter.app/";
|
|
7
8
|
function isMobileDevice() {
|
|
8
9
|
if (typeof navigator === "undefined")
|
|
@@ -19,6 +20,9 @@ export default function StellarWalletButton({ onConnect, onStatusChange, }) {
|
|
|
19
20
|
setErrorMsg(undefined);
|
|
20
21
|
onStatusChange?.({ isPending: true });
|
|
21
22
|
try {
|
|
23
|
+
if (!isStellarEnvironmentConfigured()) {
|
|
24
|
+
throw new Error("Stellar environment is not configured. Set NEXT_PUBLIC_STELLAR_HORIZON, NEXT_PUBLIC_STELLAR_CONTRACT_ID, and NEXT_PUBLIC_STELLAR_NETWORK_PASSPHRASE.");
|
|
25
|
+
}
|
|
22
26
|
const connection = await isConnected();
|
|
23
27
|
if (!connection.isConnected) {
|
|
24
28
|
const message = isMobileDevice()
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
export default function WalletButton({ onStatusChange }: {
|
|
2
|
+
export default function WalletButton({ onStatusChange, connectorGroup, }: {
|
|
3
3
|
onStatusChange?: (status: {
|
|
4
4
|
isPending: boolean;
|
|
5
5
|
error?: string;
|
|
6
6
|
}) => void;
|
|
7
|
+
connectorGroup?: "all" | "browser" | "walletconnect";
|
|
7
8
|
}): React.JSX.Element;
|
|
8
9
|
//# sourceMappingURL=WagmiButton.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WagmiButton.d.ts","sourceRoot":"","sources":["../../controllers/WagmiButton.tsx"],"names":[],"mappings":"AACA,OAAO,KAAoB,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"WagmiButton.d.ts","sourceRoot":"","sources":["../../controllers/WagmiButton.tsx"],"names":[],"mappings":"AACA,OAAO,KAAoB,MAAM,OAAO,CAAC;AAezC,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,EACnC,cAAc,EACd,cAAsB,GACvB,EAAE;IACD,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE;QAAE,SAAS,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IAC1E,cAAc,CAAC,EAAE,KAAK,GAAG,SAAS,GAAG,eAAe,CAAC;CACtD,qBA4CA"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
2
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import React, { useEffect } from "react";
|
|
4
4
|
import { useConnect } from "wagmi";
|
|
5
5
|
import Images from "../config/Image.json";
|
|
6
|
+
import { UI_Comp__css } from "../css";
|
|
7
|
+
import { isEvmEnvironmentConfigured } from "../config/walletConfig";
|
|
6
8
|
const ACTIVE_WALLET_SESSION_KEY = "cordy_minikit_active_wallet_session";
|
|
7
9
|
const ALWAYS_AVAILABLE_CONNECTOR_NAMES = new Set([
|
|
8
10
|
"WalletConnect",
|
|
@@ -10,34 +12,36 @@ const ALWAYS_AVAILABLE_CONNECTOR_NAMES = new Set([
|
|
|
10
12
|
"Coinbase Wallet",
|
|
11
13
|
"Trust Wallet",
|
|
12
14
|
]);
|
|
13
|
-
export default function WalletButton({ onStatusChange }) {
|
|
15
|
+
export default function WalletButton({ onStatusChange, connectorGroup = "all", }) {
|
|
14
16
|
const { connectors, connectAsync } = useConnect();
|
|
15
17
|
const [pendingConnector, setPendingConnector] = React.useState(null);
|
|
16
18
|
const [errorMsg, setErrorMsg] = React.useState(undefined);
|
|
17
19
|
const [isConnecting, setIsConnecting] = React.useState(false);
|
|
18
|
-
|
|
20
|
+
const visibleConnectors = connectors.filter((connector) => {
|
|
21
|
+
const isWalletConnect = connector.name.toLowerCase().includes("walletconnect");
|
|
22
|
+
if (connectorGroup === "walletconnect")
|
|
23
|
+
return isWalletConnect;
|
|
24
|
+
if (connectorGroup === "browser")
|
|
25
|
+
return !isWalletConnect;
|
|
26
|
+
return true;
|
|
27
|
+
});
|
|
19
28
|
useEffect(() => {
|
|
20
|
-
|
|
21
|
-
onStatusChange({
|
|
22
|
-
isPending: isConnecting,
|
|
23
|
-
error: errorMsg,
|
|
24
|
-
});
|
|
25
|
-
}
|
|
29
|
+
onStatusChange?.({ isPending: isConnecting, error: errorMsg });
|
|
26
30
|
}, [isConnecting, errorMsg, onStatusChange]);
|
|
27
|
-
return (_jsx(_Fragment, { children:
|
|
31
|
+
return (_jsx(_Fragment, { children: visibleConnectors.map((connector) => (_jsx(WalletOption, { connector: connector, onClick: async () => {
|
|
28
32
|
setIsConnecting(true);
|
|
29
33
|
setErrorMsg(undefined);
|
|
30
34
|
setPendingConnector(connector);
|
|
31
35
|
try {
|
|
36
|
+
if (!isEvmEnvironmentConfigured()) {
|
|
37
|
+
throw new Error("EVM environment is not configured. Set NEXT_PUBLIC_RPC_ENDPOINT, NEXT_PUBLIC_CHAIN_ID, and NEXT_PUBLIC_TOKENADDRESS.");
|
|
38
|
+
}
|
|
32
39
|
await connectAsync({ connector });
|
|
33
|
-
if (typeof window !== "undefined")
|
|
40
|
+
if (typeof window !== "undefined")
|
|
34
41
|
window.localStorage.setItem(ACTIVE_WALLET_SESSION_KEY, "evm");
|
|
35
|
-
}
|
|
36
42
|
}
|
|
37
43
|
catch (err) {
|
|
38
44
|
setErrorMsg(err?.message || "Connection failed");
|
|
39
|
-
setIsConnecting(false);
|
|
40
|
-
setPendingConnector(null);
|
|
41
45
|
}
|
|
42
46
|
finally {
|
|
43
47
|
setIsConnecting(false);
|
|
@@ -45,33 +49,29 @@ export default function WalletButton({ onStatusChange }) {
|
|
|
45
49
|
}
|
|
46
50
|
}, isPending: pendingConnector?.uid === connector.uid && isConnecting }, connector.uid))) }));
|
|
47
51
|
}
|
|
48
|
-
function WalletOption({ connector, onClick, isPending }) {
|
|
52
|
+
function WalletOption({ connector, onClick, isPending, }) {
|
|
49
53
|
const [installed, setInstalled] = React.useState(true);
|
|
50
|
-
const isTrustWallet = connector.name
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
const isTrustWallet = connector.name.toLowerCase().includes("trust");
|
|
55
|
+
const imageSource = Images[connector.name]
|
|
56
|
+
|| (isTrustWallet ? Images["Trust Wallet"] : undefined);
|
|
57
|
+
const [imageFailed, setImageFailed] = React.useState(false);
|
|
58
|
+
const displayName = isTrustWallet ? "Trust Wallet" : connector.name;
|
|
59
|
+
const fallbackInitial = displayName.trim().charAt(0).toUpperCase() || "?";
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
void (async () => {
|
|
53
62
|
try {
|
|
54
|
-
if (connector.type !== "injected" ||
|
|
55
|
-
ALWAYS_AVAILABLE_CONNECTOR_NAMES.has(connector.name) ||
|
|
56
|
-
isTrustWallet) {
|
|
63
|
+
if (connector.type !== "injected" || ALWAYS_AVAILABLE_CONNECTOR_NAMES.has(connector.name) || isTrustWallet) {
|
|
57
64
|
setInstalled(true);
|
|
58
65
|
return;
|
|
59
66
|
}
|
|
60
|
-
|
|
61
|
-
setInstalled(!!provider);
|
|
67
|
+
setInstalled(Boolean(await connector.getProvider()));
|
|
62
68
|
}
|
|
63
69
|
catch {
|
|
64
70
|
setInstalled(false);
|
|
65
71
|
}
|
|
66
72
|
})();
|
|
67
|
-
}, [connector]);
|
|
68
|
-
return (_jsxs("button", {
|
|
69
|
-
? "
|
|
70
|
-
: isTrustWallet
|
|
71
|
-
? "Trust Wallet"
|
|
72
|
-
: connector.name === "WalletConnect"
|
|
73
|
-
? "WalletConnect"
|
|
74
|
-
: !installed
|
|
75
|
-
? `${connector.name} (Not Installed)`
|
|
76
|
-
: connector.name] }));
|
|
73
|
+
}, [connector, isTrustWallet]);
|
|
74
|
+
return (_jsxs("button", { type: "button", disabled: !installed || isPending, onClick: onClick, children: [imageSource && !imageFailed
|
|
75
|
+
? _jsx("img", { src: imageSource, alt: "", "aria-hidden": "true", title: displayName, width: 23, height: 23, onError: () => setImageFailed(true) })
|
|
76
|
+
: _jsx("span", { className: UI_Comp__css.wallet_icon_fallback, "aria-hidden": "true", children: fallbackInitial }), isPending ? "Connecting…" : isTrustWallet ? "Trust Wallet" : connector.name === "WalletConnect" ? "WalletConnect" : !installed ? `${connector.name} (Not installed)` : connector.name] }));
|
|
77
77
|
}
|
|
@@ -1,7 +1,29 @@
|
|
|
1
|
+
export declare function isStellarEnvironmentConfigured(): boolean;
|
|
2
|
+
export type StellarBalanceAsset = {
|
|
3
|
+
symbol: string;
|
|
4
|
+
type: "native";
|
|
5
|
+
networkPassphrase?: string;
|
|
6
|
+
} | {
|
|
7
|
+
symbol: string;
|
|
8
|
+
type: "credit";
|
|
9
|
+
code: string;
|
|
10
|
+
issuer: string;
|
|
11
|
+
networkPassphrase?: string;
|
|
12
|
+
};
|
|
13
|
+
export type StellarBalanceOptions = {
|
|
14
|
+
assetSymbol?: string;
|
|
15
|
+
asset?: StellarBalanceAsset;
|
|
16
|
+
assets?: StellarBalanceAsset[];
|
|
17
|
+
horizonUrl?: string;
|
|
18
|
+
networkPassphrase?: string;
|
|
19
|
+
};
|
|
1
20
|
export type StellarBalanceResult = {
|
|
2
21
|
address: string;
|
|
3
22
|
network: string;
|
|
23
|
+
networkPassphrase: string;
|
|
4
24
|
balance: string;
|
|
25
|
+
asset: string;
|
|
5
26
|
};
|
|
6
|
-
|
|
27
|
+
/** Shared Stellar account balance reader for wallet status and Cordy APIs. */
|
|
28
|
+
export default function loadStellarBalance(accountId: string, options?: StellarBalanceOptions): Promise<StellarBalanceResult>;
|
|
7
29
|
//# sourceMappingURL=loadStellarBalance.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadStellarBalance.d.ts","sourceRoot":"","sources":["../../controllers/loadStellarBalance.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"loadStellarBalance.d.ts","sourceRoot":"","sources":["../../controllers/loadStellarBalance.ts"],"names":[],"mappings":"AAMA,wBAAgB,8BAA8B,IAAI,OAAO,CAMxD;AAED,MAAM,MAAM,mBAAmB,GAC3B;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,iBAAiB,CAAC,EAAE,MAAM,CAAA;CAAE,GAC9D;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,iBAAiB,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjG,MAAM,MAAM,qBAAqB,GAAG;IAClC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,mBAAmB,CAAC;IAC5B,MAAM,CAAC,EAAE,mBAAmB,EAAE,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,8EAA8E;AAC9E,wBAA8B,kBAAkB,CAC9C,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,oBAAoB,CAAC,CAsD/B"}
|
|
@@ -1,23 +1,56 @@
|
|
|
1
|
-
import { Horizon
|
|
1
|
+
import { Horizon } from "@stellar/stellar-sdk";
|
|
2
2
|
import { getNetworkDetails } from "@stellar/freighter-api";
|
|
3
|
-
const STELLAR_HORIZON = process.env.NEXT_PUBLIC_STELLAR_HORIZON
|
|
4
|
-
const STELLAR_NETWORK_PASSPHRASE = process.env.NEXT_PUBLIC_STELLAR_NETWORK_PASSPHRASE
|
|
5
|
-
export
|
|
3
|
+
const STELLAR_HORIZON = process.env.NEXT_PUBLIC_STELLAR_HORIZON;
|
|
4
|
+
const STELLAR_NETWORK_PASSPHRASE = process.env.NEXT_PUBLIC_STELLAR_NETWORK_PASSPHRASE;
|
|
5
|
+
export function isStellarEnvironmentConfigured() {
|
|
6
|
+
return Boolean(STELLAR_HORIZON &&
|
|
7
|
+
STELLAR_NETWORK_PASSPHRASE &&
|
|
8
|
+
process.env.NEXT_PUBLIC_STELLAR_CONTRACT_ID);
|
|
9
|
+
}
|
|
10
|
+
/** Shared Stellar account balance reader for wallet status and Cordy APIs. */
|
|
11
|
+
export default async function loadStellarBalance(accountId, options = {}) {
|
|
12
|
+
if (!isStellarEnvironmentConfigured()) {
|
|
13
|
+
throw new Error("Stellar environment is not configured. Set NEXT_PUBLIC_STELLAR_HORIZON, NEXT_PUBLIC_STELLAR_CONTRACT_ID, and NEXT_PUBLIC_STELLAR_NETWORK_PASSPHRASE.");
|
|
14
|
+
}
|
|
15
|
+
const horizonUrl = STELLAR_HORIZON;
|
|
16
|
+
const configuredPassphrase = STELLAR_NETWORK_PASSPHRASE;
|
|
6
17
|
const networkDetails = await getNetworkDetails();
|
|
18
|
+
const expectedPassphrase = options.networkPassphrase || configuredPassphrase;
|
|
7
19
|
const freighterPassphrase = networkDetails.networkPassphrase;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
20
|
+
if (freighterPassphrase && freighterPassphrase !== expectedPassphrase) {
|
|
21
|
+
throw new Error(`Freighter network mismatch. Expected ${expectedPassphrase}, got ${freighterPassphrase}.`);
|
|
22
|
+
}
|
|
23
|
+
const networkPassphrase = freighterPassphrase || expectedPassphrase;
|
|
24
|
+
const requestedSymbol = options.asset?.symbol || options.assetSymbol || "XLM";
|
|
25
|
+
let asset = options.asset;
|
|
26
|
+
if (!asset) {
|
|
27
|
+
const candidates = (options.assets ?? []).filter((candidate) => candidate.symbol.toLowerCase() === requestedSymbol.toLowerCase() &&
|
|
28
|
+
(!candidate.networkPassphrase || candidate.networkPassphrase === networkPassphrase));
|
|
29
|
+
if (candidates.length > 1) {
|
|
30
|
+
throw new Error(`More than one ${requestedSymbol} asset matches this Stellar network; make the JSON entries unique.`);
|
|
31
|
+
}
|
|
32
|
+
asset = candidates[0];
|
|
33
|
+
}
|
|
34
|
+
if (!asset && requestedSymbol.toLowerCase() === "xlm") {
|
|
35
|
+
asset = { symbol: "XLM", type: "native" };
|
|
36
|
+
}
|
|
37
|
+
if (!asset)
|
|
38
|
+
throw new Error(`No ${requestedSymbol} asset is configured for this Stellar network.`);
|
|
39
|
+
if (asset.networkPassphrase && asset.networkPassphrase !== networkPassphrase) {
|
|
40
|
+
throw new Error(`The ${asset.symbol} asset is not configured for the connected Stellar network.`);
|
|
14
41
|
}
|
|
15
|
-
const server = new Horizon.Server(
|
|
42
|
+
const server = new Horizon.Server(options.horizonUrl || horizonUrl);
|
|
16
43
|
const account = await server.loadAccount(accountId);
|
|
17
|
-
const
|
|
44
|
+
const balance = asset.type === "native"
|
|
45
|
+
? account.balances.find((item) => item.asset_type === "native")
|
|
46
|
+
: account.balances.find((item) => (item.asset_type === "credit_alphanum4" || item.asset_type === "credit_alphanum12") &&
|
|
47
|
+
item.asset_code === asset.code &&
|
|
48
|
+
item.asset_issuer === asset.issuer);
|
|
18
49
|
return {
|
|
19
50
|
address: accountId,
|
|
20
|
-
network: networkDetails.network || "
|
|
21
|
-
|
|
51
|
+
network: networkDetails.network || "Stellar",
|
|
52
|
+
networkPassphrase,
|
|
53
|
+
balance: balance?.balance ?? "0",
|
|
54
|
+
asset: asset.symbol,
|
|
22
55
|
};
|
|
23
56
|
}
|