|
| 1 | +const { google } = require('googleapis'); |
| 2 | + |
| 3 | +const SPREADSHEET_ID = '15CZGF-GyqfimwZgrkFTIAkp8xEfHlLQhkp83flXqI84'; |
| 4 | +const SHEET_NAME = 'live!A2'; // First row is for the existing header |
| 5 | +const GRAPHQL_ENDPOINT = 'https://api.subquery.network/sq/agoric-labs/internal'; |
| 6 | + |
| 7 | +async function appendToGoogleSheet(data) { |
| 8 | + const auth = new google.auth.GoogleAuth({ |
| 9 | + keyFile: 'service-account.json', // Update with your service account key file |
| 10 | + scopes: ['https://www.googleapis.com/auth/spreadsheets'], |
| 11 | + }); |
| 12 | + |
| 13 | + const sheets = google.sheets({ version: 'v4', auth }); |
| 14 | + |
| 15 | + console.log(`Appending ${data.length} rows to Google Sheets...`); |
| 16 | + |
| 17 | + try { |
| 18 | + await sheets.spreadsheets.values.update({ |
| 19 | + spreadsheetId: SPREADSHEET_ID, |
| 20 | + range: SHEET_NAME, |
| 21 | + valueInputOption: 'USER_ENTERED', // https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption |
| 22 | + requestBody: { values: data }, |
| 23 | + }); |
| 24 | + |
| 25 | + console.log('Data successfully pushed to Google Sheets'); |
| 26 | + } catch (error) { |
| 27 | + console.error('Error appending data to Google Sheets:', error.response?.data || error.message); |
| 28 | + throw error; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +exports.fetchAndStoreTransactions = async (req, res) => { |
| 33 | + try { |
| 34 | + console.log('Fetching transactions from GraphQL API...'); |
| 35 | + |
| 36 | + const query = ` |
| 37 | + query TransactionsQuery { |
| 38 | + fastUsdcTransactions(orderBy: SOURCE_BLOCK_TIMESTAMP_DESC) { |
| 39 | + edges { |
| 40 | + node { |
| 41 | + id |
| 42 | + sourceAddress |
| 43 | + sourceChainId |
| 44 | + sourceBlockTimestamp |
| 45 | + eud |
| 46 | + usdcAmount |
| 47 | + status |
| 48 | + statusHeight |
| 49 | + contractFee |
| 50 | + poolFee |
| 51 | + risksIdentified |
| 52 | + heightObserved |
| 53 | + heightAdvanced |
| 54 | + heightDisbursed |
| 55 | + timeObserved |
| 56 | + timeAdvanced |
| 57 | + timeDisbursed |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + }`; |
| 62 | + |
| 63 | + const response = await fetch(GRAPHQL_ENDPOINT, { |
| 64 | + method: 'POST', |
| 65 | + headers: { 'Content-Type': 'application/json' }, |
| 66 | + body: JSON.stringify({ query }), |
| 67 | + }); |
| 68 | + |
| 69 | + if (!response.ok) { |
| 70 | + throw new Error(`GraphQL request failed: ${response.statusText}`); |
| 71 | + } |
| 72 | + |
| 73 | + const { data } = await response.json(); |
| 74 | + if (!data) throw new Error('No data received from GraphQL API'); |
| 75 | + |
| 76 | + console.log('Raw GraphQL response:', JSON.stringify(data, null, 2)); |
| 77 | + |
| 78 | + const transactions = data.fastUsdcTransactions.edges.map((edge) => edge.node); |
| 79 | + console.log(`Fetched ${transactions.length} transactions`); |
| 80 | + |
| 81 | + const rows = transactions.map((txn) => { |
| 82 | + return [ |
| 83 | + txn.id, |
| 84 | + txn.sourceAddress, |
| 85 | + txn.sourceChainId, |
| 86 | + txn.sourceBlockTimestamp, |
| 87 | + txn.eud, |
| 88 | + txn.usdcAmount, |
| 89 | + txn.status, |
| 90 | + txn.statusHeight, |
| 91 | + txn.contractFee, |
| 92 | + txn.poolFee, |
| 93 | + Array.isArray(txn.risksIdentified) ? txn.risksIdentified.join(', ') : txn.risksIdentified, // Convert array to string |
| 94 | + txn.heightObserved, |
| 95 | + txn.heightAdvanced, |
| 96 | + txn.heightDisbursed, |
| 97 | + txn.timeObserved, |
| 98 | + txn.timeAdvanced, |
| 99 | + txn.timeDisbursed, |
| 100 | + ]; |
| 101 | + }); |
| 102 | + |
| 103 | + await appendToGoogleSheet(rows); |
| 104 | + |
| 105 | + res.status(200).send('Transactions stored successfully!'); |
| 106 | + } catch (error) { |
| 107 | + console.error('Error fetching or storing transactions:', error.response?.data || error.message); |
| 108 | + res.status(500).send('Error fetching or storing transactions'); |
| 109 | + } |
| 110 | +}; |
0 commit comments