@portal-hq/web 3.6.0-alpha → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,6 +49,13 @@ import {
49
49
  mockLifiGetStatusResponse,
50
50
  mockLifiGetRouteStepRequest,
51
51
  mockLifiGetRouteStepResponse,
52
+ mockZeroExQuoteV2Request,
53
+ mockZeroExQuoteV2Response,
54
+ mockZeroExSourcesV2Request,
55
+ mockZeroExSourcesV2Response,
56
+ mockZeroExPriceRequest,
57
+ mockZeroExPriceResponse,
58
+ mockZeroExOptions,
52
59
  } from '../__mocks/constants'
53
60
  import portalMock from '../__mocks/portal/portal'
54
61
  import { PortalMpcError } from './errors'
@@ -1496,7 +1503,7 @@ describe('Mpc', () => {
1496
1503
  })
1497
1504
 
1498
1505
  mpc
1499
- .getQuote(mockApikey, mockQuoteArgs, 'eip155:1')
1506
+ .getQuote('eip155:1', mockQuoteArgs, mockApikey)
1500
1507
  .then((data) => {
1501
1508
  expect(data).toEqual(res)
1502
1509
  done()
@@ -1536,7 +1543,7 @@ describe('Mpc', () => {
1536
1543
  })
1537
1544
 
1538
1545
  mpc
1539
- .getQuote(mockApikey, mockQuoteArgs, 'eip155:1')
1546
+ .getQuote('eip155:1', mockQuoteArgs, mockApikey)
1540
1547
  .then(() => {
1541
1548
  expect(0).toEqual(1)
1542
1549
  done()
@@ -1575,7 +1582,7 @@ describe('Mpc', () => {
1575
1582
  })
1576
1583
 
1577
1584
  mpc
1578
- .getSources(mockApikey, 'eip155:1')
1585
+ .getSources('eip155:1', mockApikey)
1579
1586
  .then((data) => {
1580
1587
  expect(data).toEqual(res)
1581
1588
  done()
@@ -1611,7 +1618,7 @@ describe('Mpc', () => {
1611
1618
  })
1612
1619
 
1613
1620
  mpc
1614
- .getSources(mockApikey, 'eip155:1')
1621
+ .getSources('eip155:1', mockApikey)
1615
1622
  .then(() => {
1616
1623
  expect(0).toEqual(1)
1617
1624
  done()
@@ -2945,4 +2952,334 @@ describe('Mpc', () => {
2945
2952
  })
2946
2953
  })
2947
2954
  })
2955
+
2956
+ describe('getSwapsQuoteV2', () => {
2957
+ const args = mockZeroExQuoteV2Request
2958
+ const options = mockZeroExOptions
2959
+ const res = mockZeroExQuoteV2Response
2960
+
2961
+ it('should successfully return the quote', (done) => {
2962
+ jest
2963
+ .spyOn(mpc.iframe?.contentWindow!, 'postMessage')
2964
+ .mockImplementation((message: any, origin?) => {
2965
+ const { type, data } = message
2966
+
2967
+ expect(type).toEqual('portal:swaps:getQuoteV2')
2968
+ expect(data).toEqual({ ...args, options })
2969
+ expect(origin).toEqual(mockHostOrigin)
2970
+
2971
+ window.dispatchEvent(
2972
+ new MessageEvent('message', {
2973
+ origin: mockHostOrigin,
2974
+ data: {
2975
+ type: 'portal:swaps:getQuoteV2Result',
2976
+ data: res,
2977
+ },
2978
+ }),
2979
+ )
2980
+ })
2981
+
2982
+ mpc
2983
+ .getSwapsQuoteV2(args, options)
2984
+ .then((data) => {
2985
+ expect(data).toEqual(res)
2986
+ done()
2987
+ })
2988
+ .catch((_) => {
2989
+ expect(0).toEqual(1)
2990
+ done()
2991
+ })
2992
+ })
2993
+
2994
+ it('should successfully return the quote without options', (done) => {
2995
+ jest
2996
+ .spyOn(mpc.iframe?.contentWindow!, 'postMessage')
2997
+ .mockImplementation((message: any, origin?) => {
2998
+ const { type, data } = message
2999
+
3000
+ expect(type).toEqual('portal:swaps:getQuoteV2')
3001
+ expect(data).toEqual({ ...args, options: undefined })
3002
+ expect(origin).toEqual(mockHostOrigin)
3003
+
3004
+ window.dispatchEvent(
3005
+ new MessageEvent('message', {
3006
+ origin: mockHostOrigin,
3007
+ data: {
3008
+ type: 'portal:swaps:getQuoteV2Result',
3009
+ data: res,
3010
+ },
3011
+ }),
3012
+ )
3013
+ })
3014
+
3015
+ mpc
3016
+ .getSwapsQuoteV2(args)
3017
+ .then((data) => {
3018
+ expect(data).toEqual(res)
3019
+ done()
3020
+ })
3021
+ .catch((_) => {
3022
+ expect(0).toEqual(1)
3023
+ done()
3024
+ })
3025
+ })
3026
+
3027
+ it('should error out if the iframe sends an error message', (done) => {
3028
+ jest
3029
+ .spyOn(mpc.iframe?.contentWindow!, 'postMessage')
3030
+ .mockImplementationOnce((message: any, origin?) => {
3031
+ const { type, data } = message
3032
+
3033
+ expect(type).toEqual('portal:swaps:getQuoteV2')
3034
+ expect(data).toEqual({ ...args, options })
3035
+ expect(origin).toEqual(mockHostOrigin)
3036
+
3037
+ window.dispatchEvent(
3038
+ new MessageEvent('message', {
3039
+ origin: mockHostOrigin,
3040
+ data: {
3041
+ type: 'portal:swaps:getQuoteV2Error',
3042
+ data: {
3043
+ code: 1,
3044
+ message: 'test',
3045
+ },
3046
+ },
3047
+ }),
3048
+ )
3049
+ })
3050
+
3051
+ mpc
3052
+ .getSwapsQuoteV2(args, options)
3053
+ .then(() => {
3054
+ expect(0).toEqual(1)
3055
+ done()
3056
+ })
3057
+ .catch((e) => {
3058
+ expect(e).toBeInstanceOf(PortalMpcError)
3059
+ expect(e.message).toEqual('test')
3060
+ expect(e.code).toEqual(1)
3061
+ done()
3062
+ })
3063
+ })
3064
+ })
3065
+
3066
+ describe('getSwapsSourcesV2', () => {
3067
+ const args = mockZeroExSourcesV2Request
3068
+ const options = mockZeroExOptions
3069
+ const res = mockZeroExSourcesV2Response
3070
+
3071
+ it('should successfully return the sources', (done) => {
3072
+ jest
3073
+ .spyOn(mpc.iframe?.contentWindow!, 'postMessage')
3074
+ .mockImplementation((message: any, origin?) => {
3075
+ const { type, data } = message
3076
+
3077
+ expect(type).toEqual('portal:swaps:getSourcesV2')
3078
+ expect(data).toEqual({ ...args, options })
3079
+ expect(origin).toEqual(mockHostOrigin)
3080
+
3081
+ window.dispatchEvent(
3082
+ new MessageEvent('message', {
3083
+ origin: mockHostOrigin,
3084
+ data: {
3085
+ type: 'portal:swaps:getSourcesV2Result',
3086
+ data: res,
3087
+ },
3088
+ }),
3089
+ )
3090
+ })
3091
+
3092
+ mpc
3093
+ .getSwapsSourcesV2(args, options)
3094
+ .then((data) => {
3095
+ expect(data).toEqual(res)
3096
+ done()
3097
+ })
3098
+ .catch((_) => {
3099
+ expect(0).toEqual(1)
3100
+ done()
3101
+ })
3102
+ })
3103
+
3104
+ it('should successfully return the sources without options', (done) => {
3105
+ jest
3106
+ .spyOn(mpc.iframe?.contentWindow!, 'postMessage')
3107
+ .mockImplementation((message: any, origin?) => {
3108
+ const { type, data } = message
3109
+
3110
+ expect(type).toEqual('portal:swaps:getSourcesV2')
3111
+ expect(data).toEqual({ ...args, options: undefined })
3112
+ expect(origin).toEqual(mockHostOrigin)
3113
+
3114
+ window.dispatchEvent(
3115
+ new MessageEvent('message', {
3116
+ origin: mockHostOrigin,
3117
+ data: {
3118
+ type: 'portal:swaps:getSourcesV2Result',
3119
+ data: res,
3120
+ },
3121
+ }),
3122
+ )
3123
+ })
3124
+
3125
+ mpc
3126
+ .getSwapsSourcesV2(args)
3127
+ .then((data) => {
3128
+ expect(data).toEqual(res)
3129
+ done()
3130
+ })
3131
+ .catch((_) => {
3132
+ expect(0).toEqual(1)
3133
+ done()
3134
+ })
3135
+ })
3136
+
3137
+ it('should error out if the iframe sends an error message', (done) => {
3138
+ jest
3139
+ .spyOn(mpc.iframe?.contentWindow!, 'postMessage')
3140
+ .mockImplementationOnce((message: any, origin?) => {
3141
+ const { type, data } = message
3142
+
3143
+ expect(type).toEqual('portal:swaps:getSourcesV2')
3144
+ expect(data).toEqual({ ...args, options })
3145
+ expect(origin).toEqual(mockHostOrigin)
3146
+
3147
+ window.dispatchEvent(
3148
+ new MessageEvent('message', {
3149
+ origin: mockHostOrigin,
3150
+ data: {
3151
+ type: 'portal:swaps:getSourcesV2Error',
3152
+ data: {
3153
+ code: 1,
3154
+ message: 'test',
3155
+ },
3156
+ },
3157
+ }),
3158
+ )
3159
+ })
3160
+
3161
+ mpc
3162
+ .getSwapsSourcesV2(args, options)
3163
+ .then(() => {
3164
+ expect(0).toEqual(1)
3165
+ done()
3166
+ })
3167
+ .catch((e) => {
3168
+ expect(e).toBeInstanceOf(PortalMpcError)
3169
+ expect(e.message).toEqual('test')
3170
+ expect(e.code).toEqual(1)
3171
+ done()
3172
+ })
3173
+ })
3174
+ })
3175
+
3176
+ describe('getSwapsPrice', () => {
3177
+ const args = mockZeroExPriceRequest
3178
+ const options = mockZeroExOptions
3179
+ const res = mockZeroExPriceResponse
3180
+
3181
+ it('should successfully return the price', (done) => {
3182
+ jest
3183
+ .spyOn(mpc.iframe?.contentWindow!, 'postMessage')
3184
+ .mockImplementation((message: any, origin?) => {
3185
+ const { type, data } = message
3186
+
3187
+ expect(type).toEqual('portal:swaps:getPrice')
3188
+ expect(data).toEqual({ ...args, options })
3189
+ expect(origin).toEqual(mockHostOrigin)
3190
+
3191
+ window.dispatchEvent(
3192
+ new MessageEvent('message', {
3193
+ origin: mockHostOrigin,
3194
+ data: {
3195
+ type: 'portal:swaps:getPriceResult',
3196
+ data: res,
3197
+ },
3198
+ }),
3199
+ )
3200
+ })
3201
+
3202
+ mpc
3203
+ .getSwapsPrice(args, options)
3204
+ .then((data) => {
3205
+ expect(data).toEqual(res)
3206
+ done()
3207
+ })
3208
+ .catch((_) => {
3209
+ expect(0).toEqual(1)
3210
+ done()
3211
+ })
3212
+ })
3213
+
3214
+ it('should successfully return the price without options', (done) => {
3215
+ jest
3216
+ .spyOn(mpc.iframe?.contentWindow!, 'postMessage')
3217
+ .mockImplementation((message: any, origin?) => {
3218
+ const { type, data } = message
3219
+
3220
+ expect(type).toEqual('portal:swaps:getPrice')
3221
+ expect(data).toEqual({ ...args, options: undefined })
3222
+ expect(origin).toEqual(mockHostOrigin)
3223
+
3224
+ window.dispatchEvent(
3225
+ new MessageEvent('message', {
3226
+ origin: mockHostOrigin,
3227
+ data: {
3228
+ type: 'portal:swaps:getPriceResult',
3229
+ data: res,
3230
+ },
3231
+ }),
3232
+ )
3233
+ })
3234
+
3235
+ mpc
3236
+ .getSwapsPrice(args)
3237
+ .then((data) => {
3238
+ expect(data).toEqual(res)
3239
+ done()
3240
+ })
3241
+ .catch((_) => {
3242
+ expect(0).toEqual(1)
3243
+ done()
3244
+ })
3245
+ })
3246
+
3247
+ it('should error out if the iframe sends an error message', (done) => {
3248
+ jest
3249
+ .spyOn(mpc.iframe?.contentWindow!, 'postMessage')
3250
+ .mockImplementationOnce((message: any, origin?) => {
3251
+ const { type, data } = message
3252
+
3253
+ expect(type).toEqual('portal:swaps:getPrice')
3254
+ expect(data).toEqual({ ...args, options })
3255
+ expect(origin).toEqual(mockHostOrigin)
3256
+
3257
+ window.dispatchEvent(
3258
+ new MessageEvent('message', {
3259
+ origin: mockHostOrigin,
3260
+ data: {
3261
+ type: 'portal:swaps:getPriceError',
3262
+ data: {
3263
+ code: 1,
3264
+ message: 'test',
3265
+ },
3266
+ },
3267
+ }),
3268
+ )
3269
+ })
3270
+
3271
+ mpc
3272
+ .getSwapsPrice(args, options)
3273
+ .then(() => {
3274
+ expect(0).toEqual(1)
3275
+ done()
3276
+ })
3277
+ .catch((e) => {
3278
+ expect(e).toBeInstanceOf(PortalMpcError)
3279
+ expect(e.message).toEqual('test')
3280
+ expect(e.code).toEqual(1)
3281
+ done()
3282
+ })
3283
+ })
3284
+ })
2948
3285
  })
package/src/mpc/index.ts CHANGED
@@ -67,8 +67,17 @@ import {
67
67
  LifiStepTransactionRequest,
68
68
  LifiStepTransactionResponse,
69
69
  } from '../../lifi-types'
70
+ import {
71
+ ZeroExOptions,
72
+ ZeroExPriceRequest,
73
+ ZeroExPriceResponse,
74
+ ZeroExQuoteRequest,
75
+ ZeroExQuoteResponse,
76
+ ZeroExSourcesRequest,
77
+ ZeroExSourcesResponse,
78
+ } from '../../zero-x'
70
79
 
71
- const WEB_SDK_VERSION = '3.6.0-alpha'
80
+ const WEB_SDK_VERSION = '3.6.0'
72
81
 
73
82
  class Mpc {
74
83
  public iframe?: HTMLIFrameElement
@@ -837,9 +846,9 @@ class Mpc {
837
846
  }
838
847
 
839
848
  public async getQuote(
840
- apiKey: string,
841
- args: QuoteArgs,
842
849
  chainId: string,
850
+ args: QuoteArgs,
851
+ apiKey?: string,
843
852
  ): Promise<QuoteResponse> {
844
853
  return new Promise((resolve, reject) => {
845
854
  const handleGetQuote = (event: MessageEvent<WorkerResult>) => {
@@ -881,8 +890,8 @@ class Mpc {
881
890
  }
882
891
 
883
892
  public async getSources(
884
- apiKey: string,
885
893
  chainId: string,
894
+ apiKey?: string,
886
895
  ): Promise<Record<string, string>> {
887
896
  return new Promise((resolve, reject) => {
888
897
  const handleGetSources = (event: MessageEvent<WorkerResult>) => {
@@ -1350,6 +1359,42 @@ class Mpc {
1350
1359
  })
1351
1360
  }
1352
1361
 
1362
+ public async getSwapsQuoteV2(
1363
+ data: ZeroExQuoteRequest,
1364
+ options?: ZeroExOptions,
1365
+ ): Promise<ZeroExQuoteResponse> {
1366
+ return this.handleRequestToIframeAndPost({
1367
+ methodMessage: 'portal:swaps:getQuoteV2',
1368
+ errorMessage: 'portal:swaps:getQuoteV2Error',
1369
+ resultMessage: 'portal:swaps:getQuoteV2Result',
1370
+ data: { ...data, options },
1371
+ })
1372
+ }
1373
+
1374
+ public async getSwapsSourcesV2(
1375
+ data: ZeroExSourcesRequest,
1376
+ options?: ZeroExOptions,
1377
+ ): Promise<ZeroExSourcesResponse> {
1378
+ return this.handleRequestToIframeAndPost({
1379
+ methodMessage: 'portal:swaps:getSourcesV2',
1380
+ errorMessage: 'portal:swaps:getSourcesV2Error',
1381
+ resultMessage: 'portal:swaps:getSourcesV2Result',
1382
+ data: { ...data, options },
1383
+ })
1384
+ }
1385
+
1386
+ public async getSwapsPrice(
1387
+ data: ZeroExPriceRequest,
1388
+ options?: ZeroExOptions,
1389
+ ): Promise<ZeroExPriceResponse> {
1390
+ return this.handleRequestToIframeAndPost({
1391
+ methodMessage: 'portal:swaps:getPrice',
1392
+ errorMessage: 'portal:swaps:getPriceError',
1393
+ resultMessage: 'portal:swaps:getPriceResult',
1394
+ data: { ...data, options },
1395
+ })
1396
+ }
1397
+
1353
1398
  /***************************
1354
1399
  * Private Methods
1355
1400
  ***************************/
@@ -248,6 +248,7 @@ class Provider {
248
248
  chainId: requestChainId,
249
249
  method,
250
250
  params,
251
+ sponsorGas,
251
252
  }: RequestArguments): Promise<any> {
252
253
  const isSignerMethod = signerMethods.includes(method)
253
254
  const chainId = this.getCAIP2ChainId(requestChainId)
@@ -278,6 +279,7 @@ class Provider {
278
279
  chainId,
279
280
  method,
280
281
  params,
282
+ sponsorGas,
281
283
  })
282
284
 
283
285
  if (transactionHash) {
@@ -432,6 +434,7 @@ class Provider {
432
434
  chainId,
433
435
  method,
434
436
  params,
437
+ sponsorGas,
435
438
  }: RequestArguments): Promise<any> {
436
439
  const isApproved = passiveSignerMethods.includes(method)
437
440
  ? true
@@ -467,6 +470,7 @@ class Provider {
467
470
  method,
468
471
  params: this.buildParams(method, params),
469
472
  rpcUrl: this.portal.getRpcUrl(chainId),
473
+ sponsorGas,
470
474
  })
471
475
  return result
472
476
  }
@@ -480,6 +484,7 @@ class Provider {
480
484
  method,
481
485
  params: this.buildParams(method, params),
482
486
  rpcUrl: this.portal.getRpcUrl(chainId),
487
+ sponsorGas,
483
488
  })
484
489
  return result
485
490
  }
package/types.d.ts CHANGED
@@ -73,6 +73,7 @@ export interface SendAssetParams {
73
73
  amount: string
74
74
  to: string
75
75
  token: string
76
+ sponsorGas?: boolean
76
77
  }
77
78
 
78
79
  export interface SendAssetResponse {
@@ -468,6 +469,7 @@ export interface RequestArguments {
468
469
  chainId?: string
469
470
  method: RequestMethod
470
471
  params?: unknown[] | SigningRequestParams
472
+ sponsorGas?: boolean
471
473
  }
472
474
 
473
475
  export interface RecoverArgs extends MpcOperationArgs {
@@ -502,6 +504,7 @@ export interface SignArgs {
502
504
  method: string
503
505
  params: string
504
506
  rpcUrl: string
507
+ sponsorGas?: boolean
505
508
  }
506
509
 
507
510
  export interface SignData {