Skip to content

Commit a635086

Browse files
committed
Factor out processXanpoolTx function
1 parent aebf68e commit a635086

File tree

2 files changed

+55
-63
lines changed

2 files changed

+55
-63
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"Swapuz",
1616
"Switchain",
1717
"Transak",
18-
"Wyre"
18+
"Wyre",
19+
"Xanpool"
1920
]
2021
}

src/partners/xanpool.ts

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async function queryXanpool(pluginParams: PluginParams): Promise<PluginResult> {
5959
return { settings: { latestIsoDate }, transactions: [] }
6060
}
6161

62-
const ssFormatTxs: StandardTx[] = []
62+
const standardTxs: StandardTx[] = []
6363
let previousTimestamp = new Date(latestIsoDate).getTime() - QUERY_LOOKBACK
6464
if (previousTimestamp < 0) previousTimestamp = 0
6565
const previousLatestIsoDate = new Date(previousTimestamp).toISOString()
@@ -81,69 +81,17 @@ async function queryXanpool(pluginParams: PluginParams): Promise<PluginResult> {
8181
break
8282
}
8383
for (const rawTx of txs) {
84-
const {
85-
id,
86-
status,
87-
type,
88-
blockchainTxId,
89-
wallet,
90-
createdAt,
91-
currency,
92-
fiat,
93-
cryptoCurrency,
94-
crypto,
95-
depositWallets
96-
} = asXanpoolTx(rawTx)
97-
let ssTx: StandardTx
98-
if (type === 'buy') {
99-
ssTx = {
100-
status: status === 'completed' ? 'complete' : 'expired',
101-
orderId: id,
102-
depositTxid: undefined,
103-
depositAddress: undefined,
104-
depositCurrency: currency,
105-
depositAmount: fiat,
106-
payoutTxid: blockchainTxId,
107-
payoutAddress: wallet,
108-
payoutCurrency: cryptoCurrency,
109-
payoutAmount: crypto,
110-
timestamp: smartIsoDateFromTimestamp(new Date(createdAt).getTime())
111-
.timestamp,
112-
isoDate: createdAt,
113-
usdValue: -1,
114-
rawTx
115-
}
116-
} else if (type === 'sell') {
117-
ssTx = {
118-
status: status === 'completed' ? 'complete' : 'expired',
119-
orderId: id,
120-
depositTxid: blockchainTxId,
121-
depositAddress: Object.values(depositWallets ?? {})[0],
122-
depositCurrency: cryptoCurrency,
123-
depositAmount: crypto,
124-
payoutTxid: undefined,
125-
payoutAddress: undefined,
126-
payoutCurrency: currency,
127-
payoutAmount: fiat,
128-
timestamp: smartIsoDateFromTimestamp(new Date(createdAt).getTime())
129-
.timestamp,
130-
isoDate: createdAt,
131-
usdValue: -1,
132-
rawTx
133-
}
134-
} else {
135-
throw new Error(`Invalid tx type ${type}`)
84+
const standardTx = processXanpoolTx(rawTx)
85+
standardTxs.push(standardTx)
86+
if (standardTx.isoDate > latestIsoDate) {
87+
latestIsoDate = standardTx.isoDate
13688
}
137-
ssFormatTxs.push(ssTx)
138-
if (ssTx.isoDate > latestIsoDate) {
139-
latestIsoDate = ssTx.isoDate
89+
if (standardTx.isoDate < oldestIsoDate) {
90+
oldestIsoDate = standardTx.isoDate
14091
}
141-
if (ssTx.isoDate < oldestIsoDate) {
142-
oldestIsoDate = ssTx.isoDate
143-
}
144-
if (ssTx.isoDate < previousLatestIsoDate && !done) {
92+
if (standardTx.isoDate < previousLatestIsoDate && !done) {
14593
datelog(
146-
`Xanpool done: date ${ssTx.isoDate} < ${previousLatestIsoDate}`
94+
`Xanpool done: date ${standardTx.isoDate} < ${previousLatestIsoDate}`
14795
)
14896
done = true
14997
}
@@ -158,7 +106,7 @@ async function queryXanpool(pluginParams: PluginParams): Promise<PluginResult> {
158106
settings: {
159107
latestIsoDate
160108
},
161-
transactions: ssFormatTxs
109+
transactions: standardTxs
162110
}
163111
return out
164112
}
@@ -170,3 +118,46 @@ export const xanpool: PartnerPlugin = {
170118
pluginName: 'Xanpool',
171119
pluginId: 'xanpool'
172120
}
121+
122+
export function processXanpoolTx(rawTx: unknown): StandardTx {
123+
const tx = asXanpoolTx(rawTx)
124+
if (tx.type === 'buy') {
125+
return {
126+
status: tx.status === 'completed' ? 'complete' : 'expired',
127+
orderId: tx.id,
128+
depositTxid: undefined,
129+
depositAddress: undefined,
130+
depositCurrency: tx.currency,
131+
depositAmount: tx.fiat,
132+
payoutTxid: tx.blockchainTxId,
133+
payoutAddress: tx.wallet,
134+
payoutCurrency: tx.cryptoCurrency,
135+
payoutAmount: tx.crypto,
136+
timestamp: smartIsoDateFromTimestamp(new Date(tx.createdAt).getTime())
137+
.timestamp,
138+
isoDate: tx.createdAt,
139+
usdValue: -1,
140+
rawTx
141+
}
142+
} else if (tx.type === 'sell') {
143+
return {
144+
status: tx.status === 'completed' ? 'complete' : 'expired',
145+
orderId: tx.id,
146+
depositTxid: tx.blockchainTxId,
147+
depositAddress: Object.values(tx.depositWallets ?? {})[0],
148+
depositCurrency: tx.cryptoCurrency,
149+
depositAmount: tx.crypto,
150+
payoutTxid: undefined,
151+
payoutAddress: undefined,
152+
payoutCurrency: tx.currency,
153+
payoutAmount: tx.fiat,
154+
timestamp: smartIsoDateFromTimestamp(new Date(tx.createdAt).getTime())
155+
.timestamp,
156+
isoDate: tx.createdAt,
157+
usdValue: -1,
158+
rawTx
159+
}
160+
} else {
161+
throw new Error(`Invalid tx type ${tx.type}`)
162+
}
163+
}

0 commit comments

Comments
 (0)