-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_to_frontend.js
173 lines (140 loc) · 5.72 KB
/
backend_to_frontend.js
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
172
173
const userAddress = "0x1985EA6E9c68E1C272d8209f3B478AC2Fdb25c87"
let chain = "BASE"
let request = {
user_address: userAddress,
chain: chain,
}
const LOCALHOST = "127.0.0.1"
const port = 8080
var method = "erc20"
async function makeRequest() {
try {
const response = await fetch(`http://${LOCALHOST}:${port}/${method}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(request),
});
if (!response.ok) {
return response.json().then(errData => {
// Log the error data for debugging
console.error('Error Data:', errData);
// Throw an error with a more descriptive message
throw new Error(errData.message || 'Unknown error occurred');
});
}
const data = await response.json(); // Parse response as JSON
// Access the nested fields
const userDetails = data.user_details;
const details = userDetails.details;
const portfolioValue = userDetails.portfolio_value;
const err = data.err;
if (err) {
console.error('Error:', err);
} else {
console.log('Details:', details);
console.log('Portfolio Value:', portfolioValue);
}
// Example to get details of one token:
token_name = details[0].token_name
token_decimals = details[0].token_decimals
token_balance = parseInt(details[0].token_balance, 16) / token_decimals
token_chain = details[0].chain
token_price = details[0].token_price // can be null
// console.log(token_balance, token_price, token_name, token_chain, token_decimals)
} catch (error) {
console.error('There was a problem with the fetch operation:', error);
}
}
// Call the async function
makeRequest();
// method = "erc721"
// request = {
// user_address: "0x439c36f21d961Dc81Bfb39331845FbDC8C9E8be8",
// chain: "BASE",
// }
// async function makeNftRequest() {
// try {
// const response = await fetch(`http://${LOCALHOST}:${port}/${method}`, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify(request),
// });
// if (!response.ok) {
// return response.json().then(errData => {
// // Log the error data for debugging
// console.error('Error Data:', errData);
// // Throw an error with a more descriptive message
// throw new Error(errData.message || 'Unknown error occurred');
// });
// }
// const data = await response.json(); // Parse response as JSON
// console.log("Response data:", data); // Log the response data
// console.log("NFT urls:", data.nft_details[0].media_urls); // Log the response data
// // Example NFT Data
// nft1 = data.nft_details[0]
// nft1_name = nft1.name
// nft1_symbol = nft1.symbol
// nft1_description = nft1.description
// nft1_mediaUrls = nft1.media_urls[0]
// nft1_collectionName = nft1.collection_name
// nft1_floorPriceEth = nft1.floor_price_eth
// nft1_lastTradedPriceUsd = nft1.last_traded_price_usd
// // console.log(nft1_name,nft1_symbol,nft1_description,nft1_mediaUrls,nft1_collectionName,nft1_floorPriceEth,nft1_lastTradedPriceUsd)
// } catch (error) {
// console.error('There was a problem with the fetch operation:', error);
// }
// }
// makeNftRequest();
method = "transaction-history"
chain = "BASE"
request = {
user_address: userAddress,
chain: chain,
}
async function makeTransactionRequest() {
try {
const response = await fetch(`http://${LOCALHOST}:${port}/${method}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(request),
});
if (!response.ok) {
return response.json().then(errData => {
// Log the error data for debugging
console.error('Error Data:', errData);
// Throw an error with a more descriptive message
throw new Error(errData.message || 'Unknown error occurred');
});
}
const data = await response.json(); // Parse response as JSON
console.log("Response data:", data); // Log the response data
// Example Transaction Data
complete_array_of_transaction_details = data.transaction_details
transaction1 = data.transaction_details[0]
transaction1_from = transaction1.from
transaction1_to = transaction1.to
transaction1_value = parseInt(transaction1.value, 16)
transaction1_blockNumber = parseInt(transaction1.block_number, 16)
transaction1_EtherScanUrl = getTransactionUrl(transaction1.txn_hash, chain)
// console.log(transaction1_from,transaction1_to,transaction1_value,transaction1_blockNumber,transaction1_EtherScanUrl)
} catch (error) {
console.error('There was a problem with the fetch operation:', error);
}
}
makeTransactionRequest();
function getTransactionUrl(txHash, network) {
switch (network) {
case "ETHEREUM":
return `https://etherscan.io/tx/${txHash}`;
case "BASE":
return `https://basescan.org/tx/${txHash}`;
default:
return `Unknown network: ${network}`;
}
}