@quirks/store
Version:
123 lines (122 loc) • 3.71 kB
JavaScript
import { createInvalidWalletName as r, openWCDeeplink as p } from "@quirks/core";
import { ConnectionStates as c, SetupStates as l } from "./index5.js";
const u = {
walletName: void 0,
wallet: void 0,
status: c.DISCONNECTED,
setupStatus: l.DEINITIALIZED,
connectionError: void 0,
connecting: !1,
options: {
autoSuggestions: !0,
autoAccountChange: !0
}
}, d = (o, n) => ({
...u,
setWallet: async (i) => {
var a, t, s, e;
o(() => ({ wallet: i, setupStatus: l.DEINITIALIZED })), i && ((a = n().wallet) == null || a.addListeners(), n().options.autoAccountChange && ((t = n().wallet) == null || t.events.on("keystorechange", () => {
n().getWalletData();
})), (s = n().wallet) == null || s.events.on("session_delete", () => {
n().disconnect();
}), (e = n().wallet) == null || e.events.on("session_expire", () => {
n().disconnect();
}), o(() => ({ setupStatus: l.INITIALIZED })), await n().getWalletData());
},
getWalletData: async () => {
const i = n().wallet;
if (i) {
const a = [];
let t = "";
for (const s of n().chains) {
const e = await i.getAccount(s.chain_id);
e && (t = e.name, a.push({
...e,
chainId: s.chain_id,
chainName: s.chain_name
}));
}
o({
accountName: t,
accounts: a
});
}
},
suggestChains: async (i) => {
const a = n().wallets.find(
(t) => t.options.wallet_name === i
);
if (a) {
const t = n().chains.map((s) => ({
chain: s,
name: s.chain_name,
assetList: n().assetsLists.find(
(e) => e.chain_name === s.chain_name
)
}));
return a.suggestChains(t);
}
},
connect: async (i) => {
try {
const a = n().wallets.find(
(t) => t.options.wallet_name === i
);
if (!a)
throw r(i);
if (o(() => ({
walletName: i,
status: c.DISCONNECTED,
connecting: !0
})), a.options.connection_type === "wallet_connect") {
o({
pairingURI: void 0
}), await a.init(n().providerOpts);
const t = a;
t.events.on("display_uri", (s) => {
o({
pairingURI: s
}), p(
t,
n().openDeeplink,
t.pairingDeeplinks
);
}), await t.generateURI({
namespaces: n().namespaces
});
}
n().options.autoSuggestions && await n().suggestChains(i), await a.enable(n().chains.map((t) => t.chain_id)), await n().setWallet(a), o(() => ({ status: c.CONNECTED }));
} catch (a) {
const t = a instanceof Error ? a : new Error(a === "string" ? a : JSON.stringify(a));
throw o(() => ({
connectionError: t,
status: c.REJECTED
})), t;
} finally {
o(() => ({ connecting: !1 }));
}
},
reconnect: async (i) => {
try {
const a = n().wallets.find(
(s) => s.options.wallet_name === i
);
if (!a)
throw r(i);
const t = a.options.connection_type === "wallet_connect" ? n().providerOpts : void 0;
await a.init(t), o(() => ({ connecting: !0 })), n().options.autoSuggestions && await n().suggestChains(i), await a.enable(n().chains.map((s) => s.chain_id)), await n().setWallet(a);
} catch (a) {
throw console.error(a), n().disconnect(), a;
} finally {
o(() => ({ connecting: !1 }));
}
},
disconnect: () => {
var i, a;
(i = n().wallet) == null || i.disable(n().chains.map((t) => t.chain_id)), (a = n().wallet) == null || a.removeListeners(), n().reset();
}
});
export {
u as connectInitialState,
d as createConnectSlice
};