Skip to content

Commit

Permalink
Add affiliateAddress and xClientId params
Browse files Browse the repository at this point in the history
- xClientId helps prevent timeouts
- snooze to also help prevent timeouts
- use affiliateAddress to filter txs not created in app
  • Loading branch information
paullinator committed Nov 1, 2024
1 parent 7cb6f19 commit 15a67ba
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/partners/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import {
asString,
asUnknown
} from 'cleaners'
import fetch from 'node-fetch'
import { HeadersInit } from 'node-fetch'

import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { datelog, smartIsoDateFromTimestamp } from '../util'
import { datelog, retryFetch, smartIsoDateFromTimestamp, snooze } from '../util'

const asThorchainTx = asObject({
date: asString,
metadata: asObject({
swap: asOptional(
asObject({
affiliateAddress: asOptional(asString)
})
)
}),
in: asArray(
asObject({
address: asString,
Expand Down Expand Up @@ -52,7 +59,11 @@ const asThorchainResult = asObject({
})

const asThorchainPluginParams = asObject({
apiKeys: asObject({ thorchainAddress: asString }),
apiKeys: asObject({
thorchainAddress: asString,
affiliateAddress: asString,
xClientId: asOptional(asString)
}),
settings: asObject({
latestIsoDate: asOptional(asString, '1970-01-01T00:00:00.000Z')
})
Expand All @@ -79,7 +90,7 @@ const makeThorchainPlugin = (info: ThorchainInfo): PartnerPlugin => {
const ssFormatTxs: StandardTx[] = []

const { settings, apiKeys } = asThorchainPluginParams(pluginParams)
const { thorchainAddress } = apiKeys
const { affiliateAddress, thorchainAddress, xClientId } = apiKeys
let { latestIsoDate } = settings

let previousTimestamp = new Date(latestIsoDate).getTime() - QUERY_LOOKBACK
Expand All @@ -91,12 +102,21 @@ const makeThorchainPlugin = (info: ThorchainInfo): PartnerPlugin => {

let offset = 0

let headers: HeadersInit | undefined
if (xClientId != null) {
headers = {
'x-client-id': xClientId
}
}

while (!done) {
const url = `https://${midgardUrl}/v2/actions?address=${thorchainAddress}&type=swap,refund&affiliate=ej&offset=${offset}&limit=${LIMIT}`
let jsonObj: ThorchainResult
try {
const result = await fetch(url, {
method: 'GET'
await snooze(500)
const result = await retryFetch(url, {
method: 'GET',
headers
})
if (!result.ok) {
const text = await result.text()
Expand All @@ -112,12 +132,17 @@ const makeThorchainPlugin = (info: ThorchainInfo): PartnerPlugin => {
for (const rawTx of txs) {
const {
date,
metadata,
in: txIns,
out: txOuts,
pools,
status: txStatus
} = asThorchainTx(rawTx) // Check RAW trasaction

const { swap } = metadata
if (swap?.affiliateAddress !== affiliateAddress) {
continue
}
const status = 'complete'
if (txStatus !== 'success') {
continue
Expand Down

0 comments on commit 15a67ba

Please sign in to comment.