Skip to content

Commit b9f6a87

Browse files
committed
fixup! Add 0xGasless plugin
1 parent af2fdfc commit b9f6a87

File tree

1 file changed

+51
-44
lines changed

1 file changed

+51
-44
lines changed

src/partners/0xgasless.ts

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
asNumber,
77
asObject,
88
asString,
9+
asUnknown,
910
asValue
1011
} from 'cleaners'
1112
import URL from 'url-parse'
@@ -98,50 +99,12 @@ export async function query0xGasless(
9899
}
99100
const responseBody = asGetGaslessTradesResponse(responseJson)
100101

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)
113104

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
145108
}
146109
}
147110

@@ -199,10 +162,54 @@ export const zeroxgasless: PartnerPlugin = {
199162
pluginId: '0xgasless'
200163
}
201164

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+
202209
const asGetGaslessTradesResponse = asJSON(
203210
asObject({
204211
nextCursor: asEither(asString, asNull),
205-
trades: asArray(v => asGaslessTrade(v))
212+
trades: asArray(asUnknown)
206213
})
207214
)
208215

0 commit comments

Comments
 (0)