-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathfaast.ts
More file actions
140 lines (132 loc) · 3.8 KB
/
faast.ts
File metadata and controls
140 lines (132 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { asArray, asNumber, asObject, asString, asUnknown } from 'cleaners'
import crypto from 'crypto'
import fetch from 'node-fetch'
import { PartnerPlugin, PluginParams, PluginResult, StandardTx } from '../types'
import { datelog } from '../util'
import { queryDummy } from './dummy'
const asFaastTx = asObject({
swap_id: asString,
order_id: asString,
transaction_id: asString,
deposit_address: asString,
deposit_currency: asString,
amount_deposited: asNumber,
withdrawal_address: asString,
withdrawal_currency: asString,
amount_withdrawn: asNumber,
created_at: asString
})
const asRawFaastTx = asObject({
status: asString
})
const asFaastResult = asObject({
orders: asArray(asUnknown)
})
const PAGE_LIMIT = 50
const QUERY_LOOKBACK = 60 * 60 * 24 * 5 // 5 days
export async function queryFaast(
pluginParams: PluginParams
): Promise<PluginResult> {
let page = 1
const standardTxs: StandardTx[] = []
let signature = ''
let latestTimeStamp = 0
if (typeof pluginParams.settings.latestTimeStamp === 'number') {
latestTimeStamp = pluginParams.settings.latestTimeStamp
}
const nonce = String(Date.now())
if (typeof pluginParams.apiKeys.faastSecret === 'string') {
signature = crypto
.createHmac('sha256', pluginParams.apiKeys.faastSecret)
.update(nonce)
.digest('hex')
} else {
return {
settings: {
latestTimeStamp: latestTimeStamp
},
transactions: []
}
}
let newLatestTimeStamp = latestTimeStamp
let done = false
while (!done) {
const url = `https://api.faa.st/api/v2/public/affiliate/swaps?limit=${PAGE_LIMIT}&page=${page}`
const headers = {
'affiliate-id': `${pluginParams.apiKeys.faastAffiliateId}`,
nonce,
signature
}
let jsonObj: ReturnType<typeof asFaastResult>
let resultJSON
try {
const result = await fetch(url, { method: 'GET', headers: headers })
resultJSON = await result.json()
jsonObj = asFaastResult(resultJSON)
} catch (e) {
datelog(e)
throw e
}
const txs = jsonObj.orders
for (const rawtx of txs) {
if (asRawFaastTx(rawtx).status === 'complete') {
const standardTx = processFaastTx(rawtx)
standardTxs.push(standardTx)
if (standardTx.timestamp > newLatestTimeStamp) {
newLatestTimeStamp = standardTx.timestamp
}
if (standardTx.timestamp < latestTimeStamp - QUERY_LOOKBACK) {
done = true
}
}
}
if (txs.length < PAGE_LIMIT) {
break
}
page++
}
const out: PluginResult = {
settings: { latestTimeStamp: newLatestTimeStamp },
transactions: standardTxs
}
return out
}
export const faast: PartnerPlugin = {
// queryFunc will take PluginSettings as arg and return PluginResult
queryFunc: queryDummy,
// results in a PluginResult
pluginName: 'Faast',
pluginId: 'faast'
}
export function processFaastTx(rawTx: unknown): StandardTx {
const tx = asFaastTx(rawTx)
const date = new Date(tx.created_at)
const timestamp = date.getTime() / 1000
const standardTx: StandardTx = {
status: 'complete',
orderId: tx.swap_id,
countryCode: null,
depositTxid: undefined,
depositAddress: tx.deposit_address,
depositCurrency: tx.deposit_currency.toUpperCase(),
depositChainPluginId: undefined,
depositEvmChainId: undefined,
depositTokenId: undefined,
depositAmount: tx.amount_deposited,
direction: null,
exchangeType: 'swap',
paymentType: null,
payoutTxid: tx.transaction_id,
payoutAddress: tx.withdrawal_address,
payoutCurrency: tx.withdrawal_currency.toUpperCase(),
payoutChainPluginId: undefined,
payoutEvmChainId: undefined,
payoutTokenId: undefined,
payoutAmount: tx.amount_withdrawn,
timestamp,
isoDate: tx.created_at,
usdValue: -1,
rawTx
}
return standardTx
}