Skip to content

Commit

Permalink
fix: numbers parsing; add start of test
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Jul 27, 2023
1 parent 55e704b commit 2ea4a64
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const version = require("../package.json").version;

// Expose all types
export * from "./soroban_rpc";
export * from "./strval";

// stellar-sdk classes to expose
export { Server } from "./server";
Expand Down
65 changes: 32 additions & 33 deletions src/strval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ import { Contract } from "..";
//@ts-ignore Does exist
import { ScInt, scValToBigInt } from "..";

interface Union<T> {
export interface Union<T> {
tag: string;
value: T;
}

export class ContractSpec {
constructor(public entries: xdr.ScSpecEntry[]) {}
public entries: xdr.ScSpecEntry[];
constructor(entries: xdr.ScSpecEntry[] | string) {
if (typeof entries === "string") {
throw new Error("");
} else {
this.entries = entries;
}
}

findEntry(name: string): xdr.ScSpecEntry {
let entry = this.entries.find(
Expand Down Expand Up @@ -285,36 +292,26 @@ export class ContractSpec {
case "number":
case "bigint": {
let ty = typeDef ?? xdr.ScSpecTypeDef.scSpecTypeU32();
let type_ = undefined;
switch (ty.switch().name) {
case "scSpecTypeU128":
type_ = "u128";
break;
case "scSpecTypeI128":
type_ = "i128";
break;
case "scSpecTypeU256":
type_ = "u256";
break;
case "scSpecTypeI256":
type_ = "i256";
break;
case "scSpecTypeU32":
type_ = "u32";
break;
return xdr.ScVal.scvU32(val as number);
case "scSpecTypeI32":
type_ = "i32";
break;
return xdr.ScVal.scvI32(val as number);
case "scSpecTypeU64":
type_ = "u64";
break;
return new ScInt(val, { type: "u64" }).toU64();
case "scSpecTypeI64":
type_ = "i64";
break;
return new ScInt(val, { type: "i64" }).toI64();
case "scSpecTypeU128":
return new ScInt(val, { type: "u128" }).toU128();
case "scSpecTypeI128":
return new ScInt(val, { type: "i128" }).toI128();
case "scSpecTypeU256":
return new ScInt(val, { type: "u256" }).toU256();
case "scSpecTypeI256":
return new ScInt(val, { type: "i256" }).toI256();
default:
throw new TypeError(`invalid type (${ty}) specified for integer`);
}

return new ScInt(val, { type: type_ }).toScVal();
}
case "string":
return stringToScVal(
Expand Down Expand Up @@ -528,9 +525,11 @@ export class ContractSpec {

case xdr.ScValType.scvString().value:
case xdr.ScValType.scvSymbol().value: {
const v = scv.value();
const v = scv.value();
if (typeof v !== "string") {
throw new Error(`Expected a string value from xdr but got ${v}: ${typeof v}`);
throw new Error(
`Expected a string value from xdr but got ${v}: ${typeof v}`
);
}
return v as T; // string
}
Expand Down Expand Up @@ -559,18 +558,17 @@ export class ContractSpec {

// in the fallthrough case, just return the underlying value directly
default:
throw new TypeError(`failed to convert ${scv} to native type from type ${typeDef}`);
throw new TypeError(
`failed to convert ${scv} to native type from type ${typeDef}`
);
}
}

scValUdtToNative(scv: xdr.ScVal, udt: xdr.ScSpecTypeUdt) {
let entry = this.findEntry(udt.name() as string);
switch (entry.switch()) {
case xdr.ScSpecEntryKind.scSpecEntryUdtEnumV0():
return this.enumToNative(
scv,
entry.value() as xdr.ScSpecUdtEnumV0
);
return this.enumToNative(scv, entry.value() as xdr.ScSpecUdtEnumV0);
case xdr.ScSpecEntryKind.scSpecEntryUdtStructV0():
return this.structToNative(scv, entry.value() as xdr.ScSpecUdtStructV0);
case xdr.ScSpecEntryKind.scSpecEntryUdtUnionV0():
Expand All @@ -591,7 +589,8 @@ export class ContractSpec {
throw new Error(`Enum must have a u32 value`);
}
let num = scv.value() as number;
if (udt.cases().some((entry) => entry.value() === num)) {}
if (udt.cases().some((entry) => entry.value() === num)) {
}
return num;
}
}
Expand Down
59 changes: 59 additions & 0 deletions test/unit/strval_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const xdr = SorobanClient.xdr;
const ScInt = SorobanClient.ScInt; // shorthand
const [scValToNative, nativeToScVal] = [
SorobanClient.scValToNative,
SorobanClient.nativeToScVal
];

describe('parsing and building ScVals', function () {


it('Can parse entries', function () {
CUSTOM_TYPES_XDR.forEach((entry, i) => {
console.log(entry)
console.log(xdr.ScSpecEntry.fromXDR(entry, 'base64'));
});

});

});


function custom_types_spec() {
return SorobanClient.ContractSpec(xdr.Sp)
}

const CUSTOM_TYPES_XDR = ["AAAAAQAAAC9UaGlzIGlzIGZyb20gdGhlIHJ1c3QgZG9jIGFib3ZlIHRoZSBzdHJ1Y3QgVGVzdAAAAAAAAAAABFRlc3QAAAADAAAAAAAAAAFhAAAAAAAABAAAAAAAAAABYgAAAAAAAAEAAAAAAAAAAWMAAAAAAAAR",
"AAAAAgAAAAAAAAAAAAAAClNpbXBsZUVudW0AAAAAAAMAAAAAAAAAAAAAAAVGaXJzdAAAAAAAAAAAAAAAAAAABlNlY29uZAAAAAAAAAAAAAAAAAAFVGhpcmQAAAA=",
"AAAAAwAAAAAAAAAAAAAACVJveWFsQ2FyZAAAAAAAAAMAAAAAAAAABEphY2sAAAALAAAAAAAAAAVRdWVlbgAAAAAAAAwAAAAAAAAABEtpbmcAAAAN",
"AAAAAQAAAAAAAAAAAAAAC1R1cGxlU3RydWN0AAAAAAIAAAAAAAAAATAAAAAAAAfQAAAABFRlc3QAAAAAAAAAATEAAAAAAAfQAAAAClNpbXBsZUVudW0AAA==",
"AAAAAgAAAAAAAAAAAAAAC0NvbXBsZXhFbnVtAAAAAAUAAAABAAAAAAAAAAZTdHJ1Y3QAAAAAAAEAAAfQAAAABFRlc3QAAAABAAAAAAAAAAVUdXBsZQAAAAAAAAEAAAfQAAAAC1R1cGxlU3RydWN0AAAAAAEAAAAAAAAABEVudW0AAAABAAAH0AAAAApTaW1wbGVFbnVtAAAAAAABAAAAAAAAAAVBc3NldAAAAAAAAAIAAAATAAAACwAAAAAAAAAAAAAABFZvaWQ=",
"AAAABAAAAAAAAAAAAAAABUVycm9yAAAAAAAAAQAAABpVbmtub3duIGVycm9yIGhhcyBvY2N1cnJlZAAAAAAABE9oTm8AAAAB",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