|
6 | 6 | asNumber, |
7 | 7 | asObject, |
8 | 8 | asString, |
| 9 | + asUnknown, |
9 | 10 | asValue |
10 | 11 | } from 'cleaners' |
11 | 12 | import URL from 'url-parse' |
@@ -98,50 +99,12 @@ export async function query0xGasless( |
98 | 99 | } |
99 | 100 | const responseBody = asGetGaslessTradesResponse(responseJson) |
100 | 101 |
|
101 | | - for (const trade of responseBody.trades) { |
102 | | - const buySymbol = trade.tokens.find(t => t.address === trade.buyToken) |
103 | | - ?.symbol |
104 | | - const sellSymbol = trade.tokens.find( |
105 | | - t => t.address === trade.sellToken |
106 | | - )?.symbol |
107 | | - |
108 | | - if (buySymbol == null || sellSymbol == null) { |
109 | | - throw new Error( |
110 | | - `Could not find buy or sell symbol for trade with txid ${trade.transactionHash}` |
111 | | - ) |
112 | | - } |
| 102 | + for (const rawTx of responseBody.trades) { |
| 103 | + const standardTx = process0xGaslessTx(rawTx) |
113 | 104 |
|
114 | | - const { |
115 | | - isoDate: tradeIsoDate, |
116 | | - timestamp: tradeTimestamp |
117 | | - } = smartIsoDateFromTimestamp(trade.timestamp * 1000) |
118 | | - |
119 | | - // If trade is 2 days or older, then it's finalized according to 0x |
120 | | - // documentation. |
121 | | - const status: Status = |
122 | | - tradeTimestamp + 2 * 24 * 60 * 60 * 1000 < now |
123 | | - ? 'complete' |
124 | | - : 'pending' |
125 | | - |
126 | | - const ssTx: StandardTx = { |
127 | | - status, |
128 | | - orderId: trade.transactionHash, |
129 | | - depositTxid: trade.transactionHash, |
130 | | - depositAddress: undefined, |
131 | | - depositCurrency: sellSymbol, |
132 | | - depositAmount: Number(trade.sellAmount), |
133 | | - payoutTxid: trade.transactionHash, |
134 | | - payoutAddress: trade.taker ?? undefined, |
135 | | - payoutCurrency: buySymbol, |
136 | | - payoutAmount: Number(trade.buyAmount), |
137 | | - timestamp: tradeTimestamp, |
138 | | - isoDate: tradeIsoDate, |
139 | | - usdValue: parseFloat(trade.volumeUsd), |
140 | | - rawTx: trade |
141 | | - } |
142 | | - ssFormatTxs.push(ssTx) |
143 | | - if (ssTx.isoDate > latestBlockIsoDate) { |
144 | | - latestBlockIsoDate = ssTx.isoDate |
| 105 | + ssFormatTxs.push(standardTx) |
| 106 | + if (standardTx.isoDate > latestBlockIsoDate) { |
| 107 | + latestBlockIsoDate = standardTx.isoDate |
145 | 108 | } |
146 | 109 | } |
147 | 110 |
|
@@ -199,10 +162,54 @@ export const zeroxgasless: PartnerPlugin = { |
199 | 162 | pluginId: '0xgasless' |
200 | 163 | } |
201 | 164 |
|
| 165 | +export function process0xGaslessTx(rawTx: unknown): StandardTx { |
| 166 | + const trade = asGaslessTrade(rawTx) |
| 167 | + const buySymbol = trade.tokens.find(t => t.address === trade.buyToken)?.symbol |
| 168 | + const sellSymbol = trade.tokens.find(t => t.address === trade.sellToken) |
| 169 | + ?.symbol |
| 170 | + |
| 171 | + if (buySymbol == null || sellSymbol == null) { |
| 172 | + throw new Error( |
| 173 | + `Could not find buy or sell symbol for trade with txid ${trade.transactionHash}` |
| 174 | + ) |
| 175 | + } |
| 176 | + |
| 177 | + const { |
| 178 | + isoDate: tradeIsoDate, |
| 179 | + timestamp: tradeTimestamp |
| 180 | + } = smartIsoDateFromTimestamp(trade.timestamp * 1000) |
| 181 | + |
| 182 | + // If trade is 2 days or older, then it's finalized according to 0x |
| 183 | + // documentation. |
| 184 | + const status: Status = |
| 185 | + tradeTimestamp + 2 * 24 * 60 * 60 * 1000 < Date.now() |
| 186 | + ? 'complete' |
| 187 | + : 'pending' |
| 188 | + |
| 189 | + const standardTx: StandardTx = { |
| 190 | + status, |
| 191 | + orderId: trade.transactionHash, |
| 192 | + depositTxid: trade.transactionHash, |
| 193 | + depositAddress: undefined, |
| 194 | + depositCurrency: sellSymbol, |
| 195 | + depositAmount: Number(trade.sellAmount), |
| 196 | + payoutTxid: trade.transactionHash, |
| 197 | + payoutAddress: trade.taker ?? undefined, |
| 198 | + payoutCurrency: buySymbol, |
| 199 | + payoutAmount: Number(trade.buyAmount), |
| 200 | + timestamp: tradeTimestamp, |
| 201 | + isoDate: tradeIsoDate, |
| 202 | + usdValue: parseFloat(trade.volumeUsd), |
| 203 | + rawTx: trade |
| 204 | + } |
| 205 | + |
| 206 | + return standardTx |
| 207 | +} |
| 208 | + |
202 | 209 | const asGetGaslessTradesResponse = asJSON( |
203 | 210 | asObject({ |
204 | 211 | nextCursor: asEither(asString, asNull), |
205 | | - trades: asArray(v => asGaslessTrade(v)) |
| 212 | + trades: asArray(asUnknown) |
206 | 213 | }) |
207 | 214 | ) |
208 | 215 |
|
|
0 commit comments