Skip to content

Commit c5aaf5a

Browse files
author
xgram
committed
PR comments fixed - errors, fetch
1 parent e2491a3 commit c5aaf5a

File tree

1 file changed

+61
-109
lines changed

1 file changed

+61
-109
lines changed

src/swap/central/xgram.ts

Lines changed: 61 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import { gt, lt } from 'biggystring'
2-
import {
3-
asArray,
4-
asBoolean,
5-
asMaybe,
6-
asObject,
7-
asOptional,
8-
asString,
9-
Cleaner
10-
} from 'cleaners'
2+
import { asNumber, asObject, asOptional, asString, Cleaner } from 'cleaners'
113
import {
124
EdgeCorePluginOptions,
135
EdgeMemo,
@@ -17,7 +9,8 @@ import {
179
EdgeSwapQuote,
1810
EdgeSwapRequest,
1911
SwapAboveLimitError,
20-
SwapBelowLimitError
12+
SwapBelowLimitError,
13+
SwapCurrencyError
2114
} from 'edge-core-js/types'
2215

2316
import {
@@ -121,15 +114,13 @@ export const MAINNET_CODE_TRANSCRIPTION: CurrencyPluginIdSwapChainCodeMap = {
121114
sonic: null
122115
}
123116

124-
let chainCodeTickerMap: ChainCodeTickerMap = new Map()
125-
let lastUpdated = 0
126-
const EXPIRATION = 1000 * 60 * 60
117+
const chainCodeTickerMap: ChainCodeTickerMap = new Map()
127118

128119
const swapType: FlowType = 'fixed'
129120
type FlowType = 'fixed' | 'float'
130121

131122
export function xgramPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
132-
const { io, log } = opts
123+
const { io } = opts
133124

134125
const fetchCors = io.fetch
135126
const { apiKey } = asInitOptions(opts.initOptions)
@@ -139,53 +130,9 @@ export function xgramPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
139130
'x-api-key': apiKey
140131
}
141132

142-
async function fetchSupportedAssets(): Promise<void> {
143-
if (lastUpdated > Date.now() - EXPIRATION && lastUpdated !== 0) return
144-
try {
145-
const response = await fetchCors(`${uri}list-currency-options`, {
146-
headers
147-
})
148-
149-
if (!response.ok) {
150-
log.warn('Xgram: list-currency-options HTTP error', response.status)
151-
return
152-
}
153-
154-
const json = await response.json()
155-
const jsonArr = Object.entries(json).map(([key, data]) => ({
156-
...(data as object),
157-
coinName: key
158-
}))
159-
const assets = asXgramAssets(jsonArr)
160-
161-
const mainnetValueToPlugin: Record<string, string> = {}
162-
for (const [pluginIdKey, mainnetCode] of Object.entries(
163-
MAINNET_CODE_TRANSCRIPTION
164-
)) {
165-
if (mainnetCode != null) mainnetValueToPlugin[mainnetCode] = pluginIdKey
166-
}
167-
168-
const out: ChainCodeTickerMap = new Map()
169-
for (const asset of assets) {
170-
const pluginKey = mainnetValueToPlugin[asset.coinName]
171-
172-
const tokenCodes = out.get(pluginKey) ?? []
173-
tokenCodes.push({
174-
tokenCode: asset.coinName,
175-
contractAddress: asset.contract ?? null
176-
})
177-
out.set(pluginKey, tokenCodes)
178-
}
179-
180-
chainCodeTickerMap = out
181-
lastUpdated = Date.now()
182-
} catch (e) {
183-
log.warn('Xgram: Error updating supported assets', e)
184-
}
185-
}
186-
187133
const fetchSwapQuoteInner = async (
188-
request: EdgeSwapRequestPlugin
134+
request: EdgeSwapRequestPlugin,
135+
opts: { promoCode?: string }
189136
): Promise<SwapOrder> => {
190137
const { fromWallet, toWallet, nativeAmount } = request
191138

@@ -235,8 +182,6 @@ export function xgramPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
235182
)
236183

237184
if (!orderResponse.ok) {
238-
const text = await orderResponse.text().catch(() => '')
239-
log.warn('Xgram: create order HTTP error', orderResponse.status, text)
240185
throw new Error('Xgram create order failed')
241186
}
242187

@@ -294,44 +239,52 @@ export function xgramPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
294239
const marketRangeResponse = await fetchCors(uri + rateQs, { headers })
295240

296241
if (!marketRangeResponse.ok) {
297-
const text = await marketRangeResponse.text().catch(() => '')
298-
log.warn(
299-
'Xgram: retrieve-rate-value HTTP error',
300-
marketRangeResponse.status,
301-
text
302-
)
303242
throw new Error('Xgram retrieve-rate-value failed')
304243
}
244+
const marketRangeResponseJson: {
245+
result: boolean
246+
error?: string
247+
} = await marketRangeResponse.json()
248+
249+
// Below/Above limits errors
250+
// Unsupported currency error
251+
if (!marketRangeResponseJson.result) {
252+
const { minFromCcyAmount, maxFromCcyAmount } = asMarketRange(
253+
marketRangeResponseJson
254+
)
255+
const minFrom = minFromCcyAmount.toString()
256+
const maxFrom = maxFromCcyAmount.toString()
305257

306-
const marketRangeResponseJson = await marketRangeResponse.json()
307-
const { minFrom, maxFrom } = asMarketRange(marketRangeResponseJson)
258+
if (marketRangeResponseJson.error === 'Pair not found') {
259+
throw new SwapCurrencyError(swapInfo, request)
260+
}
308261

309-
if (lt(largeDenomAmount, minFrom)) {
310-
const minNativeAmount = denominationToNative(
311-
isSelling ? request.fromWallet : request.toWallet,
312-
minFrom,
313-
isSelling ? request.fromTokenId : request.toTokenId
314-
)
315-
throw new SwapBelowLimitError(
316-
swapInfo,
317-
minNativeAmount,
318-
isSelling ? undefined : 'to'
319-
)
320-
}
262+
if (lt(largeDenomAmount, minFrom)) {
263+
const minNativeAmount = denominationToNative(
264+
isSelling ? request.fromWallet : request.toWallet,
265+
minFrom,
266+
isSelling ? request.fromTokenId : request.toTokenId
267+
)
268+
throw new SwapBelowLimitError(
269+
swapInfo,
270+
minNativeAmount,
271+
isSelling ? undefined : 'to'
272+
)
273+
}
321274

322-
if (gt(largeDenomAmount, maxFrom)) {
323-
const maxNativeAmount = denominationToNative(
324-
isSelling ? request.fromWallet : request.toWallet,
325-
maxFrom,
326-
isSelling ? request.fromTokenId : request.toTokenId
327-
)
328-
throw new SwapAboveLimitError(
329-
swapInfo,
330-
maxNativeAmount,
331-
isSelling ? undefined : 'to'
332-
)
275+
if (gt(largeDenomAmount, maxFrom)) {
276+
const maxNativeAmount = denominationToNative(
277+
isSelling ? request.fromWallet : request.toWallet,
278+
maxFrom,
279+
isSelling ? request.fromTokenId : request.toTokenId
280+
)
281+
throw new SwapAboveLimitError(
282+
swapInfo,
283+
maxNativeAmount,
284+
isSelling ? undefined : 'to'
285+
)
286+
}
333287
}
334-
335288
const {
336289
fromAmount,
337290
toAmount,
@@ -421,19 +374,27 @@ export function xgramPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
421374
const out: EdgeSwapPlugin = {
422375
swapInfo,
423376

424-
async fetchSwapQuote(req: EdgeSwapRequest): Promise<EdgeSwapQuote> {
377+
async fetchSwapQuote(
378+
req: EdgeSwapRequest,
379+
userSettings: Object | undefined,
380+
opts: { promoCode?: string }
381+
): Promise<EdgeSwapQuote> {
425382
const request = convertRequest(req)
426383

427-
await fetchSupportedAssets()
384+
// await fetchSupportedAssets()
428385

429386
checkWhitelistedMainnetCodes(
430387
MAINNET_CODE_TRANSCRIPTION,
431388
request,
432389
swapInfo
433390
)
434391

435-
const newRequest = await getMaxSwappable(fetchSwapQuoteInner, request)
436-
const swapOrder = await fetchSwapQuoteInner(newRequest)
392+
const newRequest = await getMaxSwappable(
393+
fetchSwapQuoteInner,
394+
request,
395+
opts
396+
)
397+
const swapOrder = await fetchSwapQuoteInner(newRequest, opts)
437398
return await makeSwapPluginQuote(swapOrder)
438399
}
439400
}
@@ -450,8 +411,8 @@ export function asOptionalBlank<T>(
450411
}
451412

452413
const asMarketRange = asObject({
453-
maxFrom: asString,
454-
minFrom: asString
414+
maxFromCcyAmount: asNumber,
415+
minFromCcyAmount: asNumber
455416
})
456417

457418
interface XgramResponse {
@@ -462,12 +423,3 @@ interface XgramResponse {
462423
payinAddress: string
463424
validUntil?: Date | null
464425
}
465-
466-
const asXgramAssets = asArray(
467-
asObject({
468-
coinName: asString,
469-
network: asString,
470-
available: asBoolean,
471-
contract: asMaybe(asString, null)
472-
})
473-
)

0 commit comments

Comments
 (0)