@@ -3,12 +3,11 @@ import {
33 asBoolean ,
44 asDate ,
55 asEither ,
6+ asMaybe ,
67 asNumber ,
78 asObject ,
8- asOptional ,
99 asString ,
10- asValue ,
11- Cleaner
10+ asValue
1211} from 'cleaners'
1312import {
1413 EdgeCorePluginOptions ,
@@ -29,17 +28,21 @@ import { EdgeCurrencyPluginId } from '../../util/edgeCurrencyPluginIds'
2928import {
3029 checkWhitelistedMainnetCodes ,
3130 CurrencyPluginIdSwapChainCodeMap ,
32- denominationToNative ,
3331 ensureInFuture ,
3432 getContractAddresses ,
3533 getMaxSwappable ,
3634 makeSwapPluginQuote ,
3735 mapToRecord ,
38- nativeToDenomination ,
3936 SwapOrder
4037} from '../../util/swapHelpers'
41- import { convertRequest , getAddress , memoType } from '../../util/utils'
42- import { EdgeSwapRequestPlugin } from '../types'
38+ import {
39+ convertRequest ,
40+ denominationToNative ,
41+ getAddress ,
42+ memoType ,
43+ nativeToDenomination
44+ } from '../../util/utils'
45+ import { EdgeSwapRequestPlugin , StringMap } from '../types'
4346
4447const pluginId = 'xgram'
4548
@@ -51,8 +54,7 @@ export const swapInfo: EdgeSwapInfo = {
5154}
5255
5356const asInitOptions = asObject ( {
54- apiKey : asString ,
55- affiliateId : asOptional ( asString )
57+ apiKey : asString
5658} )
5759
5860const orderUri = 'https://xgram.io/exchange/order?id='
@@ -64,8 +66,11 @@ export const MAINNET_CODE_TRANSCRIPTION: CurrencyPluginIdSwapChainCodeMap = mapT
6466 xgramMapping
6567)
6668
67- const swapType : FlowType = 'fixed'
68- type FlowType = 'fixed' | 'float'
69+ const addressTypeMap : StringMap = {
70+ zcash : 'transparentAddress'
71+ }
72+
73+ const swapType = 'fixed' as const
6974
7075export function makeXgramPlugin ( opts : EdgeCorePluginOptions ) : EdgeSwapPlugin {
7176 const { io } = opts
@@ -85,8 +90,14 @@ export function makeXgramPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
8590 const { fromWallet, toWallet, nativeAmount } = request
8691
8792 const [ fromAddress , toAddress ] = await Promise . all ( [
88- getAddress ( fromWallet ) ,
89- getAddress ( toWallet )
93+ getAddress (
94+ request . fromWallet ,
95+ addressTypeMap [ request . fromWallet . currencyInfo . pluginId ]
96+ ) ,
97+ getAddress (
98+ request . toWallet ,
99+ addressTypeMap [ request . toWallet . currencyInfo . pluginId ]
100+ )
90101 ] )
91102
92103 const { fromContractAddress, toContractAddress } = getContractAddresses (
@@ -132,10 +143,11 @@ export function makeXgramPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
132143
133144 const orderResponseJson = await orderResponse . json ( )
134145 const quoteFor = request ?. quoteFor === 'from' ? 'from' : 'to'
135- const quoteReply = asTemplateQuoteReply ( orderResponseJson )
146+ const quoteReply = asXgramQuoteReply ( orderResponseJson )
136147
137148 if ( 'errors' in quoteReply ) {
138149 const errors = quoteReply . errors
150+
139151 if ( errors . find ( error => error . code === 'REGION_UNSUPPORTED' ) != null ) {
140152 throw new SwapPermissionError ( swapInfo , 'geoRestriction' )
141153 }
@@ -145,7 +157,9 @@ export function makeXgramPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
145157 ) {
146158 throw new SwapCurrencyError ( swapInfo , request )
147159 }
148- const limitError = errors . find ( isTemplateLimitError )
160+ const limitError = errors
161+ . map ( e => asMaybe ( asXgramLimitError ) ( e ) )
162+ . find ( e => e != null )
149163
150164 if ( limitError ?. code === 'BELOW_LIMIT' ) {
151165 const sourceAmountLimit = denominationToNative (
@@ -297,15 +311,6 @@ export function makeXgramPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
297311 return out
298312}
299313
300- export function asOptionalBlank < T > (
301- cleaner : Cleaner < T >
302- ) : Cleaner < T | undefined > {
303- return function asIgnoredBlank ( raw : any ) {
304- if ( raw == null || raw === '' ) return undefined
305- return cleaner ( raw )
306- }
307- }
308-
309314interface XgramResponse {
310315 id : string
311316 fromAmount : string
@@ -315,48 +320,28 @@ interface XgramResponse {
315320 validUntil ?: Date | null
316321}
317322
318- // Type guard to narrow Xgram limit errors in the union type
319- function isTemplateLimitError (
320- error : any
321- ) : error is {
322- code : 'BELOW_LIMIT' | 'ABOVE_LIMIT'
323- destinationAmountLimit : string
324- error : string
325- sourceAmountLimit : string
326- } {
327- return (
328- error != null &&
329- ( error . code === 'BELOW_LIMIT' || error . code === 'ABOVE_LIMIT' ) &&
330- typeof error . sourceAmountLimit === 'string'
331- )
332- }
333-
334- const asTemplateLimitError = asObject ( {
323+ const asXgramLimitError = asObject ( {
335324 code : asValue ( 'BELOW_LIMIT' , 'ABOVE_LIMIT' ) ,
336325 destinationAmountLimit : asString ,
337326 error : asString ,
338327 sourceAmountLimit : asString
339328} )
340329
341- const asTemplateRegionError = asObject ( {
330+ const asXgramRegionError = asObject ( {
342331 code : asValue ( 'REGION_UNSUPPORTED' ) ,
343332 message : asString
344333} )
345334
346- const asTemplateCurrencyError = asObject ( {
335+ const asXgramCurrencyError = asObject ( {
347336 code : asValue ( 'CURRENCY_UNSUPPORTED' ) ,
348337 error : asString
349338} )
350- const asTemplateError = asObject ( {
339+ const asXgramError = asObject ( {
351340 errors : asArray (
352- asEither (
353- asTemplateLimitError ,
354- asTemplateRegionError ,
355- asTemplateCurrencyError
356- )
341+ asEither ( asXgramLimitError , asXgramRegionError , asXgramCurrencyError )
357342 )
358343} )
359- const asTemplateQuote = asObject ( {
344+ const asXgramQuote = asObject ( {
360345 ccyAmountToExpected : asNumber ,
361346 depositAddress : asString ,
362347 depositTag : asString ,
@@ -365,4 +350,4 @@ const asTemplateQuote = asObject({
365350 expiresAt : asDate ,
366351 ccyAmountFrom : asString
367352} )
368- const asTemplateQuoteReply = asEither ( asTemplateQuote , asTemplateError )
353+ const asXgramQuoteReply = asEither ( asXgramQuote , asXgramError )
0 commit comments