1
- import { ScIntType , XdrLargeInt , xdr } from " stellar-base" ;
1
+ import { ScIntType , XdrLargeInt , xdr } from ' stellar-base' ;
2
2
3
- import { Address } from " stellar-base" ;
4
- import { Contract } from " stellar-base" ;
5
- import { scValToBigInt } from " stellar-base" ;
3
+ import { Address } from ' stellar-base' ;
4
+ import { Contract } from ' stellar-base' ;
5
+ import { scValToBigInt } from ' stellar-base' ;
6
6
7
7
export interface Union < T > {
8
8
tag : string ;
@@ -44,12 +44,12 @@ export class ContractSpec {
44
44
*/
45
45
constructor ( entries : xdr . ScSpecEntry [ ] | string [ ] ) {
46
46
if ( entries . length == 0 ) {
47
- throw new Error ( " Contract spec must have at least one entry" ) ;
47
+ throw new Error ( ' Contract spec must have at least one entry' ) ;
48
48
}
49
49
let entry = entries [ 0 ] ;
50
- if ( typeof entry === " string" ) {
50
+ if ( typeof entry === ' string' ) {
51
51
this . entries = ( entries as string [ ] ) . map ( ( s ) =>
52
- xdr . ScSpecEntry . fromXDR ( s , " base64" )
52
+ xdr . ScSpecEntry . fromXDR ( s , ' base64' )
53
53
) ;
54
54
} else {
55
55
this . entries = entries as xdr . ScSpecEntry [ ] ;
@@ -115,8 +115,8 @@ export class ContractSpec {
115
115
*/
116
116
funcResToNative ( name : string , val_or_base64 : xdr . ScVal | string ) : any {
117
117
let val =
118
- typeof val_or_base64 === " string"
119
- ? xdr . ScVal . fromXDR ( val_or_base64 , " base64" )
118
+ typeof val_or_base64 === ' string'
119
+ ? xdr . ScVal . fromXDR ( val_or_base64 , ' base64' )
120
120
: val_or_base64 ;
121
121
let func = this . getFunc ( name ) ;
122
122
let outputs = func . outputs ( ) ;
@@ -132,10 +132,7 @@ export class ContractSpec {
132
132
}
133
133
let output = outputs [ 0 ] ;
134
134
if ( output . switch ( ) . value === xdr . ScSpecType . scSpecTypeResult ( ) . value ) {
135
- return this . scValToNative (
136
- val ,
137
- output . result ( ) . okType ( )
138
- ) ;
135
+ return this . scValToNative ( val , output . result ( ) . okType ( ) ) ;
139
136
}
140
137
return this . scValToNative ( val , output ) ;
141
138
}
@@ -183,7 +180,7 @@ export class ContractSpec {
183
180
}
184
181
185
182
switch ( typeof val ) {
186
- case " object" : {
183
+ case ' object' : {
187
184
if ( val === null ) {
188
185
switch ( value ) {
189
186
case xdr . ScSpecType . scSpecTypeVoid ( ) . value :
@@ -280,11 +277,10 @@ export class ContractSpec {
280
277
return xdr . ScVal . scvMap ( entries ) ;
281
278
}
282
279
283
- if ( ( val . constructor ?. name ?? "" ) !== " Object" ) {
280
+ if ( ( val . constructor ?. name ?? '' ) !== ' Object' ) {
284
281
throw new TypeError (
285
- `cannot interpret ${
286
- val . constructor ?. name
287
- } value as ScVal (${ JSON . stringify ( val ) } )`
282
+ `cannot interpret ${ val . constructor
283
+ ?. name } value as ScVal (${ JSON . stringify ( val ) } )`
288
284
) ;
289
285
}
290
286
@@ -293,8 +289,8 @@ export class ContractSpec {
293
289
) ;
294
290
}
295
291
296
- case " number" :
297
- case " bigint" : {
292
+ case ' number' :
293
+ case ' bigint' : {
298
294
switch ( value ) {
299
295
case xdr . ScSpecType . scSpecTypeU32 ( ) . value :
300
296
return xdr . ScVal . scvU32 ( val as number ) ;
@@ -313,16 +309,16 @@ export class ContractSpec {
313
309
throw new TypeError ( `invalid type (${ ty } ) specified for integer` ) ;
314
310
}
315
311
}
316
- case " string" :
312
+ case ' string' :
317
313
return stringToScVal ( val , t ) ;
318
314
319
- case " boolean" : {
315
+ case ' boolean' : {
320
316
if ( value !== xdr . ScSpecType . scSpecTypeBool ( ) . value ) {
321
317
throw TypeError ( `Type ${ ty } was not bool, but value was bool` ) ;
322
318
}
323
319
return xdr . ScVal . scvBool ( val ) ;
324
320
}
325
- case " undefined" : {
321
+ case ' undefined' : {
326
322
if ( ! ty ) {
327
323
return xdr . ScVal . scvVoid ( ) ;
328
324
}
@@ -337,7 +333,7 @@ export class ContractSpec {
337
333
}
338
334
}
339
335
340
- case " function" : // FIXME: Is this too helpful?
336
+ case ' function' : // FIXME: Is this too helpful?
341
337
return this . nativeToScVal ( val ( ) , ty ) ;
342
338
343
339
default :
@@ -349,7 +345,7 @@ export class ContractSpec {
349
345
let entry = this . findEntry ( name ) ;
350
346
switch ( entry . switch ( ) ) {
351
347
case xdr . ScSpecEntryKind . scSpecEntryUdtEnumV0 ( ) :
352
- if ( typeof val !== " number" ) {
348
+ if ( typeof val !== ' number' ) {
353
349
throw new TypeError (
354
350
`expected number for enum ${ name } , but got ${ typeof val } `
355
351
) ;
@@ -418,7 +414,7 @@ export class ContractSpec {
418
414
if ( fields . some ( isNumeric ) ) {
419
415
if ( ! fields . every ( isNumeric ) ) {
420
416
throw new Error (
421
- " mixed numeric and non-numeric field names are not allowed"
417
+ ' mixed numeric and non-numeric field names are not allowed'
422
418
) ;
423
419
}
424
420
return xdr . ScVal . scvVec (
@@ -430,7 +426,7 @@ export class ContractSpec {
430
426
let name = field . name ( ) . toString ( ) ;
431
427
return new xdr . ScMapEntry ( {
432
428
key : this . nativeToScVal ( name , xdr . ScSpecTypeDef . scSpecTypeSymbol ( ) ) ,
433
- val : this . nativeToScVal ( val [ name ] , field . type ( ) ) ,
429
+ val : this . nativeToScVal ( val [ name ] , field . type ( ) )
434
430
} ) ;
435
431
} )
436
432
) ;
@@ -453,7 +449,7 @@ export class ContractSpec {
453
449
* @throws {Error } if ScVal cannot be converted to the given type
454
450
*/
455
451
scValStrToNative < T > ( scv : string , typeDef : xdr . ScSpecTypeDef ) : T {
456
- return this . scValToNative < T > ( xdr . ScVal . fromXDR ( scv , " base64" ) , typeDef ) ;
452
+ return this . scValToNative < T > ( xdr . ScVal . fromXDR ( scv , ' base64' ) , typeDef ) ;
457
453
}
458
454
459
455
/**
@@ -518,12 +514,16 @@ export class ContractSpec {
518
514
return new Map (
519
515
map . map ( ( entry ) => [
520
516
this . scValToNative ( entry . key ( ) , keyType ) ,
521
- this . scValToNative ( entry . val ( ) , valueType ) ,
517
+ this . scValToNative ( entry . val ( ) , valueType )
522
518
] )
523
519
) as T ;
524
520
}
525
521
throw new TypeError (
526
- `ScSpecType ${ t . name } was not map, but ${ JSON . stringify ( scv , null , 2 ) } is`
522
+ `ScSpecType ${ t . name } was not map, but ${ JSON . stringify (
523
+ scv ,
524
+ null ,
525
+ 2
526
+ ) } is`
527
527
) ;
528
528
}
529
529
@@ -541,7 +541,9 @@ export class ContractSpec {
541
541
value !== xdr . ScSpecType . scSpecTypeSymbol ( ) . value
542
542
) {
543
543
throw new Error (
544
- `ScSpecType ${ t . name } was not string or symbol, but ${ JSON . stringify ( scv , null , 2 ) } is`
544
+ `ScSpecType ${
545
+ t . name
546
+ } was not string or symbol, but ${ JSON . stringify ( scv , null , 2 ) } is`
545
547
) ;
546
548
}
547
549
return scv . value ( ) ?. toString ( ) as T ;
@@ -555,7 +557,11 @@ export class ContractSpec {
555
557
// in the fallthrough case, just return the underlying value directly
556
558
default :
557
559
throw new TypeError (
558
- `failed to convert ${ JSON . stringify ( scv , null , 2 ) } to native type from type ${ t . name } `
560
+ `failed to convert ${ JSON . stringify (
561
+ scv ,
562
+ null ,
563
+ 2
564
+ ) } to native type from type ${ t . name } `
559
565
) ;
560
566
}
561
567
}
@@ -656,9 +662,7 @@ function stringToScVal(str: string, ty: xdr.ScSpecType): xdr.ScVal {
656
662
}
657
663
658
664
default :
659
- throw new TypeError (
660
- `invalid type ${ ty . name } specified for string value`
661
- ) ;
665
+ throw new TypeError ( `invalid type ${ ty . name } specified for string value` ) ;
662
666
}
663
667
}
664
668
0 commit comments