-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwallet-data-loader.tsx
More file actions
171 lines (156 loc) · 5.36 KB
/
wallet-data-loader.tsx
File metadata and controls
171 lines (156 loc) · 5.36 KB
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
import { Button } from "@/components/ui/button";
import { RefreshCw } from "lucide-react";
import useAppWallet from "@/hooks/useAppWallet";
import { useEffect, useRef, useState } from "react";
import { getProvider } from "@/components/common/cardano-objects/get-provider";
import { useWalletsStore } from "@/lib/zustand/wallets";
import { api } from "@/utils/api";
import { OnChainTransaction, TxInfo } from "@/types/transaction";
import { useSiteStore } from "@/lib/zustand/site";
import { LAST_UPDATED_THRESHOLD } from "@/config/wallet";
import { Asset } from "@meshsdk/core";
export default function WalletDataLoader() {
const { appWallet } = useAppWallet();
const [loading, setLoading] = useState<boolean>(false);
const walletsUtxos = useWalletsStore((state) => state.walletsUtxos);
const setWalletsUtxos = useWalletsStore((state) => state.setWalletsUtxos);
const setWalletTransactions = useWalletsStore(
(state) => state.setWalletTransactions,
);
const walletLastUpdated = useWalletsStore((state) => state.walletLastUpdated);
const setWalletLastUpdated = useWalletsStore(
(state) => state.setWalletLastUpdated,
);
const setWalletAssets = useWalletsStore((state) => state.setWalletAssets);
const setWalletAssetMetadata = useWalletsStore(
(state) => state.setWalletAssetMetadata,
);
const fetchingTransactions = useRef(false);
const prevWalletIdRef = useRef<string | null>(null);
const ctx = api.useUtils();
const network = useSiteStore((state) => state.network);
const setRandomState = useSiteStore((state) => state.setRandomState);
async function fetchUtxos() {
if (appWallet) {
setWalletsUtxos(appWallet?.id, []);
const blockchainProvider = getProvider(network);
const utxos = await blockchainProvider.fetchAddressUTxOs(
appWallet.address,
);
setWalletsUtxos(appWallet?.id, utxos);
}
}
async function getTransactionsOnChain() {
try {
if (appWallet) {
const maxPage = 4;
const _transactions: OnChainTransaction[] = [];
const blockchainProvider = getProvider(network);
for (let i = 1; i <= maxPage; i++) {
const transactions: TxInfo[] = await blockchainProvider.get(
`/addresses/${appWallet.address}/transactions?page=${i}&order=desc`,
);
if (transactions.length === 0) {
break;
}
for (const tx of transactions) {
const txData = await blockchainProvider.get(
`/txs/${tx.tx_hash}/utxos`,
);
_transactions.push({
hash: tx.tx_hash,
tx: tx,
inputs: txData.inputs,
outputs: txData.outputs,
});
}
}
if (_transactions.length > 0) {
setWalletTransactions(appWallet?.id, _transactions);
} else {
setWalletTransactions(appWallet?.id, []);
}
setWalletLastUpdated(appWallet?.id, Date.now());
}
} catch (error) {
if (appWallet) {
setWalletTransactions(appWallet?.id, []);
setWalletLastUpdated(appWallet?.id, Date.now());
}
}
}
async function getWalletAssets() {
try {
const blockchainProvider = getProvider(network);
const assets = await blockchainProvider.get(
`/addresses/${appWallet?.address}/`,
);
const walletAssets: Asset[] = [];
if (assets.amount) {
for (const asset of assets.amount) {
walletAssets.push({
unit: asset.unit,
quantity: asset.quantity,
});
if (asset.unit === "lovelace") continue;
const assetInfo = await blockchainProvider.get(
`/assets/${asset.unit}`,
);
setWalletAssetMetadata(
asset.unit,
assetInfo?.metadata?.name ||
assetInfo?.onchain_metadata?.name ||
assetInfo?.policyId ||
asset.unit,
assetInfo?.metadata?.decimals || 0,
assetInfo?.metadata?.logo ||
assetInfo?.onchain_metadata?.image ||
"",
assetInfo?.metadata?.ticker ||
assetInfo?.metadata?.name ||
assetInfo?.onchain_metadata?.name ||
assetInfo?.policyId ||
asset.unit,
assetInfo?.policy_id || "",
);
}
setWalletAssets(walletAssets);
} else {
setWalletAssets([]);
}
} catch (error) {
setWalletAssets([]);
setWalletAssetMetadata("", "", 0, "", "", "");
}
}
async function refreshWallet() {
if (fetchingTransactions.current) return;
fetchingTransactions.current = true;
setLoading(true);
await fetchUtxos();
await getTransactionsOnChain();
await getWalletAssets();
void ctx.transaction.getPendingTransactions.invalidate();
void ctx.transaction.getAllTransactions.invalidate();
setRandomState();
setLoading(false);
fetchingTransactions.current = false;
}
useEffect(() => {
if (appWallet && prevWalletIdRef.current !== appWallet.id) {
refreshWallet();
prevWalletIdRef.current = appWallet.id;
}
}, [appWallet]);
return (
<Button
variant="secondary"
size="icon"
className="rounded-full"
onClick={() => refreshWallet()}
disabled={loading}
>
<RefreshCw className={`h-4 w-4 ${loading && "animate-spin"}`} />
</Button>
);
}