Skip to content

Commit

Permalink
chore(tx-builder): prefer a schema with optional ver instead max ver
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 12, 2023
1 parent 7e6c587 commit b8087d8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/tx/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { txSchema } from './schema';
import { TxUnpacked, TxParams, TxParamsAsync } from './schema.generated';
import { Tag } from './constants';
import { buildContractId, readInt } from './helpers';
import { ArgumentError, DecodeError, SchemaNotFoundError } from '../../utils/errors';
import {
ArgumentError, DecodeError, InternalError, SchemaNotFoundError,
} from '../../utils/errors';

/**
* JavaScript-based Transaction builder
Expand All @@ -17,7 +19,11 @@ import { ArgumentError, DecodeError, SchemaNotFoundError } from '../../utils/err
export function getSchema(tag: Tag, version?: number): Array<[string, Field]> {
const schemas = txSchema.filter((s) => s.tag.constValue === tag);
if (schemas.length === 0) throw new SchemaNotFoundError(`${Tag[tag]} (${tag})`, 0);
version ??= Math.max(...schemas.map((schema) => schema.version.constValue));
if (version == null) {
const defaultSchema = schemas.find((schema) => schema.version.constValueOptional);
if (defaultSchema == null) throw new InternalError(`Can't find default schema of ${Tag[tag]} (${tag})`);
version = defaultSchema.version.constValue;
}
const schema = schemas.find((s) => s.version.constValue === version);
if (schema == null) throw new SchemaNotFoundError(`${Tag[tag]} (${tag})`, version);
return Object.entries(schema);
Expand Down

0 comments on commit b8087d8

Please sign in to comment.