Skip to content

Commit

Permalink
fix: send opReturn
Browse files Browse the repository at this point in the history
  • Loading branch information
lissavxo committed Feb 22, 2024
1 parent 4c94e6e commit ba7f4a4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion services/chronikService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function getNullDataScriptData (outputScript: string): OpReturnData | nul
const dataString = decoder.decode(dataHexBuffer)

const ret: OpReturnData = {
data: parseOpReturnData(dataString),
message: parseOpReturnData(dataString),
paymentId: ''
}

Expand Down
26 changes: 15 additions & 11 deletions services/transactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import { appendTxsToFile } from 'prisma/seeds/transactions'
import _ from 'lodash'
import { CacheSet } from 'redis/index'
import { SimplifiedTransaction } from 'ws-service/types'
import { OpReturnData } from 'utils/validators'

export async function getTransactionValue (transaction: TransactionWithPrices): Promise<QuoteValues> {
const ret: QuoteValues = {
usd: new Prisma.Decimal(0),
cad: new Prisma.Decimal(0)
}
if (transaction.prices.length !== N_OF_QUOTES) {
throw new Error(`txid${transaction.id}, ts${transaction.timestamp} ${RESPONSE_MESSAGES.MISSING_PRICE_FOR_TRANSACTION_400.message}: found ${transaction.prices.length}.`)
throw new Error(`Error: ${RESPONSE_MESSAGES.MISSING_PRICE_FOR_TRANSACTION_400.message} found in ${transaction.prices.length}. txId: ${transaction.id}, at ${transaction.timestamp}`)
}
for (const p of transaction.prices) {
if (p.price.quoteId === USD_QUOTE_ID) {
Expand All @@ -30,15 +31,15 @@ export async function getTransactionValue (transaction: TransactionWithPrices):
}

export function getSimplifiedTransactions (transactionsToPersist: TransactionWithAddressAndPrices[]): SimplifiedTransaction[] {
const mappedTransactions: SimplifiedTransaction[] = []
const simplifiedTransactions: SimplifiedTransaction[] = []
transactionsToPersist.forEach(
tx => {
const newSimplifiedTransaction = getSimplifiedTrasaction(tx)
const simplifiedTransaction = getSimplifiedTrasaction(tx)

mappedTransactions.push(newSimplifiedTransaction)
simplifiedTransactions.push(simplifiedTransaction)
}
)
return mappedTransactions
return simplifiedTransactions
}

export function getSimplifiedTrasaction (tx: TransactionWithAddressAndPrices): SimplifiedTransaction {
Expand All @@ -47,26 +48,29 @@ export function getSimplifiedTrasaction (tx: TransactionWithAddressAndPrices): S
amount,
confirmed,
opReturn,
address
address,
timestamp
} = tx

const parsedOpReturn = resolveOpReturn(opReturn)

const newSimplifiedTransaction: SimplifiedTransaction = {
const simplifiedTransaction: SimplifiedTransaction = {
hash,
amount,
paymentId: parsedOpReturn?.paymentId,
confirmed,
address,
message: parsedOpReturn?.message
timestamp,
message: parsedOpReturn?.message ?? '',
opReturn: parsedOpReturn ?? undefined
}

return newSimplifiedTransaction
return simplifiedTransaction
}

const resolveOpReturn = (opr: string): { paymentId: string, message: string } | null => {
const resolveOpReturn = (opReturn: string): OpReturnData | null => {
try {
return opr === '' ? null : JSON.parse(opr)
return opReturn === '' ? null : JSON.parse(opReturn)
} catch (e) {
console.error(RESPONSE_MESSAGES.FAILED_TO_PARSE_TX_OP_RETURN_500.message)
return null
Expand Down
29 changes: 17 additions & 12 deletions services/triggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,28 @@ interface PostDataTriggerLog {
export async function executeAddressTriggers (broadcastTxData: BroadcastTxData): Promise<void> {
const address = broadcastTxData.address
const tx = broadcastTxData.txs[0]
const amount = tx.amount
const currency = NETWORK_TICKERS_FROM_ID[tx.address.networkId]
const txId = tx.hash
const timestamp = tx.timestamp
let opReturn: OpReturnData = EMPTY_OP_RETURN

if (tx.opReturn !== '') {
opReturn = JSON.parse(tx.opReturn)
}
const {
amount,
hash,
timestamp,
paymentId,
message,
opReturn
} = tx

const addressTriggers = await fetchTriggersForAddress(address)
await Promise.all(addressTriggers.map(async (trigger) => {

const postDataParameters: PostDataParameters = {
amount,
currency,
txId,
txId: hash,
buttonName: trigger.paybutton.name,
address,
timestamp,
opReturn
paymentId: paymentId ?? '',
message,
opReturn: opReturn ?? EMPTY_OP_RETURN
}
const hmac = await hashPostData(trigger.paybutton.providerUserId, postDataParameters)
await postDataForTrigger(trigger, {
Expand All @@ -217,6 +218,8 @@ export interface PostDataParameters {
txId: string
buttonName: string
address: string
paymentId: string
message: string
opReturn: OpReturnData
}

Expand All @@ -227,8 +230,10 @@ export interface PostDataParametersHashed {
txId: string
buttonName: string
address: string
opReturn: OpReturnData
paymentId: string
message: string
hmac: string
opReturn: OpReturnData
}

async function hashPostData (userId: string, { amount, currency, address, timestamp, txId }: PostDataParameters): Promise<string> {
Expand Down
7 changes: 5 additions & 2 deletions utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,16 @@ export function parseTriggerPostData (postData: string, postDataParametersHashed
buttonName: '',
address: '',
timestamp: 0,
paymentId: '',
message: '',
opReturn: EMPTY_OP_RETURN,
hmac: ''
}
}
try {
const buttonName = JSON.stringify(postDataParametersHashed.buttonName)
const opReturn = JSON.stringify(postDataParametersHashed.opReturn, undefined, 2)

resultingData = postData
.replace('<amount>', postDataParametersHashed.amount.toString())
.replace('<currency>', `"${postDataParametersHashed.currency}"`)
Expand Down Expand Up @@ -346,12 +349,12 @@ export function parseStringToArray (str: string): string | string[] {
}

export interface OpReturnData {
data: string
message: string
paymentId: string
}

export const EMPTY_OP_RETURN: OpReturnData = {
data: '',
message: '',
paymentId: ''
}

Expand Down
5 changes: 3 additions & 2 deletions ws-service/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Address, Prisma } from '@prisma/client'
import { OpReturnData } from 'utils/validators'

type TxBroadcastType = 'NewTx' | 'OldTx'

Expand All @@ -13,8 +14,8 @@ export interface SimplifiedTransaction {
amount: Prisma.Decimal
paymentId?: string
confirmed?: boolean
message?: string
opReturn?: string
message: string
opReturn?: OpReturnData
timestamp: number
address: Address
}

0 comments on commit ba7f4a4

Please sign in to comment.