-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sign transactions early and dump them to spreadsheet (#79)
* Replace `jest` with `vitest` for testing * Create test for getting route from squidrouter * Use vitest dependencies * Remove all references to jest * Add usdce and allow to pass fromToken to getRoute * Add refactoring * Add spreadsheet packages * Add env variables for google credentials * Implement spreadsheet storage * Upgrade vite version * Increase vite test timeout * Refactor tests * Add small changes * Improve handling for missing credentials in unit tests * Refactor code * Amend merge * Update api-solang package * Create GlobalSpreadsheet * Add phase 'prepareTransactions' * Split nabla extrinsic creation and submission * Bump api-solang package * Fix spreadsheet creation * Split functions for transactions into creation and submission * Add phase to prepare transactions * Fix tests * Fix some lint errors * Move storage logic to backend * Remove mutex * Increase timebounds of Stellar transactions * Move nabla code before swap again * Remove unused vars and config * Turn off mockSep24
- Loading branch information
Showing
22 changed files
with
1,678 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require('dotenv').config(); | ||
|
||
const { spreadsheet } = require('../../config/vars'); | ||
const { initGoogleSpreadsheet, getOrCreateSheet, appendData } = require('../services/spreadsheet.service'); | ||
|
||
// These are the headers for the Google Spreadsheet | ||
exports.SHEET_HEADER_VALUES = [ | ||
'timestamp', | ||
'polygonAddress', | ||
'stellarEphemeralPublicKey', | ||
'pendulumEphemeralPublicKey', | ||
'nablaApprovalTx', | ||
'nablaSwapTx', | ||
'spacewalkRedeemTx', | ||
'stellarOfframpTx', | ||
'stellarCleanupTx', | ||
]; | ||
|
||
exports.storeData = async (req, res, next) => { | ||
try { | ||
// We expect the data to be an object that matches our schema | ||
const data = req.body; | ||
|
||
// Try dumping transactions to spreadsheet | ||
const sheet = await initGoogleSpreadsheet(spreadsheet.sheetId, spreadsheet.googleCredentials).then((doc) => { | ||
return getOrCreateSheet(doc, this.SHEET_HEADER_VALUES); | ||
}); | ||
|
||
if (sheet) { | ||
console.log('Appending data to sheet'); | ||
await appendData(sheet, data); | ||
return res.status(200).json({ message: 'Data stored successfully' }); | ||
} | ||
|
||
return res.status(500).json({ error: 'Failed to store data. Sheet unavailable.', details: error.message }); | ||
} catch (error) { | ||
console.error('Error in storeData:', error); | ||
return res.status(500).json({ error: 'Failed to store data', details: error.message }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const express = require('express'); | ||
const controller = require('../../controllers/storage.controller'); | ||
const { validateStorageInput } = require('../../middlewares/validators'); | ||
|
||
const router = express.Router({ mergeParams: true }); | ||
|
||
router.route('/create').post(validateStorageInput, controller.storeData); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const { GoogleSpreadsheet } = require('google-spreadsheet'); | ||
const { JWT } = require('google-auth-library'); | ||
|
||
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets']; | ||
|
||
// googleCredentials: { email: string, key: string }, | ||
exports.initGoogleSpreadsheet = async (sheetId, googleCredentials) => { | ||
// Initialize auth - see https://theoephraim.github.io/node-google-spreadsheet/#/guides/authentication | ||
if (!googleCredentials.email || !googleCredentials.key) { | ||
throw new Error('Missing some google credentials'); | ||
} | ||
|
||
const serviceAccountAuth = new JWT({ | ||
// env var values here are copied from service account credentials generated by google | ||
// see "Authentication" section in docs for more info | ||
email: googleCredentials.email, | ||
key: googleCredentials.key, | ||
scopes: SCOPES, | ||
}); | ||
|
||
const doc = new GoogleSpreadsheet(sheetId, serviceAccountAuth); | ||
try { | ||
await doc.loadInfo(); | ||
} catch (error) { | ||
console.error(`Error loading Google Spreadsheet ${sheetId}:`, error); | ||
throw error; | ||
} | ||
|
||
return doc; | ||
}; | ||
|
||
// doc: GoogleSpreadsheet, headerValues: string[] | ||
exports.getOrCreateSheet = async (doc, headerValues) => { | ||
let sheet = doc.sheetsByIndex[0]; | ||
try { | ||
await sheet.loadHeaderRow(); | ||
const sheetHeaders = sheet.headerValues; | ||
|
||
// Compare the header values to the expected header values | ||
if ( | ||
sheetHeaders.length !== headerValues.length && | ||
sheetHeaders.every((value, index) => value === headerValues[index]) | ||
) { | ||
// Create a new sheet if the headers don't match | ||
console.log('Creating new sheet'); | ||
sheet = await doc.addSheet({ headerValues }); | ||
} | ||
} catch (error) { | ||
// Assume the error is due to the sheet not having any rows | ||
await sheet.setHeaderRow(headerValues); | ||
} | ||
|
||
return sheet; | ||
}; | ||
|
||
// sheet: GoogleSpreadsheetWorksheet, data: Record<string, string> | ||
exports.appendData = async (sheet, data) => { | ||
await sheet.addRow(data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.