-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add transaction submit eventHandler for nightfall-client
- Loading branch information
1 parent
7f12f8f
commit d1ac399
Showing
6 changed files
with
146 additions
and
3 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
57 changes: 57 additions & 0 deletions
57
nightfall-client/src/event-handlers/transaction-submitted.mjs
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,57 @@ | ||
import logger from '@polygon-nightfall/common-files/utils/logger.mjs'; | ||
import constants from '@polygon-nightfall/common-files/constants/index.mjs'; | ||
import { getTransactionSubmittedCalldata } from '../services/process-calldata.mjs'; | ||
import { countCommitments, countNullifiers } from '../services/commitment-storage.mjs'; | ||
import { saveTransaction } from '../services/database.mjs'; | ||
|
||
const { ZERO } = constants; | ||
|
||
async function doesAnyOfCommitmentsExistInDB(commitments) { | ||
const count = await countCommitments(commitments); | ||
return Boolean(count); | ||
} | ||
|
||
async function doesAnyOfNullifiersExistInDB(nullifiers) { | ||
const count = await countNullifiers(nullifiers); | ||
return Boolean(count); | ||
} | ||
|
||
/** | ||
* This handler runs whenever a new transaction is submitted to the blockchain | ||
*/ | ||
async function transactionSubmittedEventHandler(eventParams) { | ||
const { offchain = false, ...data } = eventParams; | ||
let saveTxInDb = false; | ||
|
||
const transaction = await getTransactionSubmittedCalldata(data); | ||
transaction.blockNumber = data.blockNumber; | ||
transaction.transactionHashL1 = data.transactionHash; | ||
|
||
// logic: if any of non zero commitment in transaction alraedy exist in db | ||
// i.e transaction belong to user using this nightfall-client. | ||
// for example: for deposit we store commitment while transaction submit, | ||
// similarly for transfer we store change commitment while transaction submit | ||
|
||
// filter out non zero commitments and nullifiers | ||
const nonZeroCommitments = transaction.commitments.filter(c => c !== ZERO); | ||
const nonZeroNullifiers = transaction.nullifiers.filter(n => n !== ZERO); | ||
|
||
if (await doesAnyOfCommitmentsExistInDB(nonZeroCommitments)) { | ||
saveTxInDb = true; | ||
} else if (doesAnyOfNullifiersExistInDB(nonZeroNullifiers)) { | ||
saveTxInDb = true; | ||
} | ||
|
||
if (saveTxInDb) { | ||
await saveTransaction({ ...transaction }); | ||
} | ||
|
||
logger.info({ | ||
msg: 'Client Transaction Handler - New transaction received.', | ||
transaction, | ||
offchain, | ||
saveTxInDb, | ||
}); | ||
} | ||
|
||
export default transactionSubmittedEventHandler; |
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