Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/Paybutton/PaybuttonTrigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ export default ({ paybuttonId, emailCredits }: IProps): JSX.Element => {
<div>&lt;opReturn&gt;</div>
<div>&lt;signature&gt;</div>
<div>&lt;inputAddresses&gt;</div>
<div>&lt;value&gt;</div>

</div>
</div>
{/* Tooltip */}
Expand Down
3 changes: 2 additions & 1 deletion constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ export const TRIGGER_POST_VARIABLES = [
'<signature>',
'<timestamp>',
'<txId>',
'<inputAddresses>'
'<inputAddresses>',
'<value>'
]

export const PAYBUTTON_TRANSACTIONS_FILE_HEADERS = {
Expand Down
5 changes: 3 additions & 2 deletions services/transactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { OpReturnData, parseAddress } from 'utils/validators'
import { generatePaymentFromTx } from 'redis/paymentCache'
import { ButtonDisplayData, Payment } from 'redis/types'

export function getTransactionValue (transaction: TransactionWithPrices | TransactionsWithPaybuttonsAndPrices): QuoteValues {
export function getTransactionValue (transaction: TransactionWithPrices | TransactionsWithPaybuttonsAndPrices | SimplifiedTransaction): QuoteValues {
const ret: QuoteValues = {
usd: new Prisma.Decimal(0),
cad: new Prisma.Decimal(0)
Expand Down Expand Up @@ -62,7 +62,8 @@ export function getSimplifiedTrasaction (tx: TransactionWithAddressAndPrices, in
timestamp,
message: parsedOpReturn?.message ?? '',
rawMessage: parsedOpReturn?.rawMessage ?? '',
inputAddresses: inputAddresses ?? []
inputAddresses: inputAddresses ?? [],
prices: tx.prices
}

return simplifiedTransaction
Expand Down
12 changes: 9 additions & 3 deletions services/triggerService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { PaybuttonTrigger, Prisma, UserProfile } from '@prisma/client'
import axios from 'axios'
import { RESPONSE_MESSAGES, NETWORK_TICKERS_FROM_ID } from 'constants/index'
import { RESPONSE_MESSAGES, NETWORK_TICKERS_FROM_ID, SUPPORTED_QUOTES_FROM_ID } from 'constants/index'
import prisma from 'prisma/clientInstance'
import { EMPTY_OP_RETURN, OpReturnData, parseTriggerPostData } from 'utils/validators'
import { BroadcastTxData } from 'ws-service/types'
import { fetchPaybuttonById, fetchPaybuttonWithTriggers } from './paybuttonService'
import config from 'config'
import { MAIL_FROM, MAIL_HTML_REPLACER, MAIL_SUBJECT, getMailerTransporter, SendEmailParameters } from 'constants/mail'
import { getTransactionValue } from './transactionService'

const triggerWithPaybutton = Prisma.validator<Prisma.PaybuttonTriggerDefaultArgs>()({
include: { paybutton: true }
Expand Down Expand Up @@ -247,14 +248,16 @@ export async function executeAddressTriggers (broadcastTxData: BroadcastTxData,
rawMessage,
inputAddresses
} = tx

const values = getTransactionValue(tx)
const addressTriggers = await fetchTriggersForAddress(address)
if (addressTriggers.length === 0) return
console.log(`[TRIGGER ${currency}]: Will execute ${addressTriggers.length} triggers for tx ${hash} and address ${address}`)

// Send post requests
const posterTriggers = addressTriggers.filter(t => !t.isEmailTrigger)
await Promise.all(posterTriggers.map(async (trigger) => {
const userProfile = await fetchUserFromTriggerId(trigger.id)
const quoteSlug = SUPPORTED_QUOTES_FROM_ID[userProfile.preferredCurrencyId]
const postDataParameters: PostDataParameters = {
amount,
currency,
Expand All @@ -269,8 +272,10 @@ export async function executeAddressTriggers (broadcastTxData: BroadcastTxData,
rawMessage
}
: EMPTY_OP_RETURN,
inputAddresses
inputAddresses,
value: values[quoteSlug].toString()
}

await postDataForTrigger(trigger, postDataParameters)
}))

Expand Down Expand Up @@ -399,6 +404,7 @@ export interface PostDataParameters {
address: string
opReturn: OpReturnData
inputAddresses?: string[]
value: string
}

async function postDataForTrigger (trigger: TriggerWithPaybutton, postDataParameters: PostDataParameters): Promise<void> {
Expand Down
9 changes: 6 additions & 3 deletions tests/unittests/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ describe('Signature payload', () => {
message: 'my custom opReturn data',
paymentId: '123paymentId',
rawMessage: 'my custom opReturn data'
}
},
value: '0.0002189581274'
}
it('Gets payload for single variable', () => {
const postData = '{"myVar": 3, "amount": <amount>}'
Expand Down Expand Up @@ -457,7 +458,8 @@ describe('Signature payload', () => {
message: '',
paymentId: '',
rawMessage: ''
}
},
value: '0.0002189581274'
}
const postData = '{"id": <txId>, "coin": <currency>, "myVar": 3, "OP_RETURN": <opReturn>, "name": <buttonName>, "amount": <amount>, "ts": <timestamp>}'
const result = v.exportedForTesting.getSignaturePayload(postData, params)
Expand All @@ -482,7 +484,8 @@ describe('Sign post data', () => {
message: 'my custom opReturn data',
paymentId: '123paymentId',
rawMessage: 'my custom opReturn data'
}
},
value: '0.0002189581274'
}
it('Sign full payload', () => {
const postData = '{"id": <txId>, "coin": <currency>, "myVar": 3, "OP_RETURN": <opReturn>, "to": <address>, "name": <buttonName>, "amount": <amount>, "ts": <timestamp>}'
Expand Down
5 changes: 4 additions & 1 deletion utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ export function parseTriggerPostData ({ userId, postData, postDataParameters }:
.replace('<opReturn>', opReturn)
.replace('<signature>', `${JSON.stringify(signature, undefined, 2)}`)
.replace('<inputAddresses>', `${JSON.stringify(postDataParameters.inputAddresses, undefined, 2)}`)
.replace('<value>', `"${postDataParameters.value}"`)

const parsedResultingData = JSON.parse(resultingData)
return parsedResultingData
} catch (err: any) {
Expand Down Expand Up @@ -316,7 +318,8 @@ export const parsePaybuttonTriggerPOSTRequest = function (params: PaybuttonTrigg
address: '',
timestamp: 0,
opReturn: EMPTY_OP_RETURN,
inputAddresses: []
inputAddresses: [],
value: ''
}
const parsed = parseTriggerPostData({
userId: params.userId,
Expand Down
6 changes: 6 additions & 0 deletions ws-service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export interface SimplifiedTransaction {
address: string
rawMessage: string
inputAddresses: string[]
prices: Array<{
price: {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for all that. This is supposed to be simplified, so only take what is necessary, and for what I've gathered, it would be: value and quoteId.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then alter getSimplifiedTransaction to take out these fields.

value: Prisma.Decimal
quoteId: number
}
}>
}

export interface CreateQuoteAndShiftData {
Expand Down