|
7 | 7 | Contract, |
8 | 8 | scValToBigInt, |
9 | 9 | } from "@stellar/stellar-base"; |
10 | | -import { Ok } from "./rust_result"; |
| 10 | +import { Ok, Err } from "./rust_result"; |
11 | 11 | import { processSpecEntryStream } from "./utils"; |
12 | 12 | import { specFromWasm } from "./wasm_spec_parser"; |
13 | 13 |
|
@@ -277,8 +277,8 @@ function typeRef(typeDef: xdr.ScSpecTypeDef): JSONSchema7 { |
277 | 277 | return typeRef(opt.valueType()); |
278 | 278 | } |
279 | 279 | case xdr.ScSpecType.scSpecTypeResult().value: { |
280 | | - // throw new Error('Result type not supported'); |
281 | | - break; |
| 280 | + const result = typeDef.result(); |
| 281 | + return typeRef(result.okType()); |
282 | 282 | } |
283 | 283 | case xdr.ScSpecType.scSpecTypeVec().value: { |
284 | 284 | const arr = typeDef.vec(); |
@@ -368,11 +368,11 @@ function structToJsonSchema(udt: xdr.ScSpecUdtStructV0): object { |
368 | 368 | } |
369 | 369 | const description = udt.doc().toString(); |
370 | 370 | const { properties, required }: any = argsAndRequired(fields); |
371 | | - properties.additionalProperties = false; |
372 | 371 | return { |
373 | 372 | description, |
374 | 373 | properties, |
375 | 374 | required, |
| 375 | + additionalProperties: false, |
376 | 376 | type: "object", |
377 | 377 | }; |
378 | 378 | } |
@@ -623,6 +623,9 @@ export class Spec { |
623 | 623 | } |
624 | 624 | const output = outputs[0]; |
625 | 625 | if (output.switch().value === xdr.ScSpecType.scSpecTypeResult().value) { |
| 626 | + if (val.switch().value === xdr.ScValType.scvError().value) { |
| 627 | + return new Err({ message: val.error().toXDR("base64") }); |
| 628 | + } |
626 | 629 | return new Ok(this.scValToNative(val, output.result().okType())); |
627 | 630 | } |
628 | 631 | return this.scValToNative(val, output); |
@@ -802,9 +805,22 @@ export class Spec { |
802 | 805 | case "bigint": { |
803 | 806 | switch (value) { |
804 | 807 | case xdr.ScSpecType.scSpecTypeU32().value: |
805 | | - return xdr.ScVal.scvU32(val as number); |
| 808 | + if ( |
| 809 | + BigInt(val) < BigInt(xdr.Uint32.MIN_VALUE) || |
| 810 | + BigInt(val) > BigInt(xdr.Uint32.MAX_VALUE) |
| 811 | + ) { |
| 812 | + throw new RangeError(`Value ${val} is out of range for U32`); |
| 813 | + } |
| 814 | + return xdr.ScVal.scvU32(Number(val)); |
806 | 815 | case xdr.ScSpecType.scSpecTypeI32().value: |
807 | | - return xdr.ScVal.scvI32(val as number); |
| 816 | + if ( |
| 817 | + // TODO: remove the `-` cast on the min value once js-xdr fixes the issue where it treats the min value as unsigned |
| 818 | + BigInt(val) < -BigInt(xdr.Int32.MIN_VALUE) || |
| 819 | + BigInt(val) > BigInt(xdr.Int32.MAX_VALUE) |
| 820 | + ) { |
| 821 | + throw new RangeError(`Value ${val} is out of range for I32`); |
| 822 | + } |
| 823 | + return xdr.ScVal.scvI32(Number(val)); |
808 | 824 | case xdr.ScSpecType.scSpecTypeU64().value: |
809 | 825 | case xdr.ScSpecType.scSpecTypeI64().value: |
810 | 826 | case xdr.ScSpecType.scSpecTypeU128().value: |
@@ -1102,12 +1118,12 @@ export class Spec { |
1102 | 1118 | } |
1103 | 1119 | const name = vec[0].sym().toString(); |
1104 | 1120 | if (vec[0].switch().value !== xdr.ScValType.scvSymbol().value) { |
1105 | | - throw new Error(`{vec[0]} is not a symbol`); |
| 1121 | + throw new Error(`${vec[0]} is not a symbol`); |
1106 | 1122 | } |
1107 | 1123 | const entry = udt.cases().find(findCase(name)); |
1108 | 1124 | if (!entry) { |
1109 | 1125 | throw new Error( |
1110 | | - `failed to find entry ${name} in union {udt.name().toString()}`, |
| 1126 | + `failed to find entry ${name} in union ${udt.name().toString()}`, |
1111 | 1127 | ); |
1112 | 1128 | } |
1113 | 1129 | const res: Union<any> = { tag: name }; |
|
0 commit comments