Class

Spec

contract.Spec(entries)

Provides a ContractSpec class which can contains the XDR types defined by the contract. This allows the class to be used to convert between native and raw xdr.ScVals.

Constructs a new ContractSpec from an array of XDR spec entries.

Constructor

# new Spec(entries)

Parameters:
Name Type Description
entries Array.<xdr.ScSpecEntry> | Array.<string>

the XDR spec entries

View Source lib/contract/spec.js, line 490

if entries is invalid

Error
Example
const specEntries = [...]; // XDR spec entries of a smart contract
const contractSpec = new ContractSpec(specEntries);

// Convert native value to ScVal
const args = {
  arg1: 'value1',
  arg2: 1234
};
const scArgs = contractSpec.funcArgsToScVals('funcName', args);

// Call contract
const resultScv = await callContract(contractId, 'funcName', scArgs);

// Convert result ScVal back to native value
const result = contractSpec.funcResToNative('funcName', resultScv);

console.log(result); // {success: true}

Members

# entries

The XDR spec entries.

View Source lib/contract/spec.js, line 494

Methods

# errorCases() → {Array.<xdr.ScSpecFunctionV0>}

Gets the XDR error cases from the spec.

View Source lib/contract/spec.js, line 1021

all contract functions

Array.<xdr.ScSpecFunctionV0>

# findEntry(name) → {xdr.ScSpecEntry}

Finds the XDR spec entry for the given name.

Parameters:
Name Type Description
name string

the name to find

View Source lib/contract/spec.js, line 594

if no entry with the given name exists

Error

the entry

xdr.ScSpecEntry

# funcArgsToScVals(name, args) → {Array.<xdr.ScVal>}

Converts native JS arguments to ScVals for calling a contract function.

Parameters:
Name Type Description
name string

the name of the function

args object

the arguments object

View Source lib/contract/spec.js, line 547

if argument is missing or incorrect type

Error

the converted arguments

Array.<xdr.ScVal>
Example
const args = {
  arg1: 'value1',
  arg2: 1234
};
const scArgs = contractSpec.funcArgsToScVals('funcName', args);

# funcResToNative(name, val_or_base64) → {any}

Converts the result ScVal of a function call to a native JS value.

Parameters:
Name Type Description
name string

the name of the function

val_or_base64 xdr.ScVal | string

the result ScVal or base64 encoded string

View Source lib/contract/spec.js, line 565

if return type mismatch or invalid input

Error

the converted native value

any
Example
const resultScv = 'AAA=='; // Base64 encoded ScVal
const result = contractSpec.funcResToNative('funcName', resultScv);

# funcs() → {Array.<xdr.ScSpecFunctionV0>}

Gets the XDR functions from the spec.

View Source lib/contract/spec.js, line 511

all contract functions

Array.<xdr.ScSpecFunctionV0>

# getFunc(name) → {xdr.ScSpecFunctionV0}

Gets the XDR function spec for the given function name.

Parameters:
Name Type Description
name string

the name of the function

View Source lib/contract/spec.js, line 523

if no function with the given name exists

Error

the function spec

xdr.ScSpecFunctionV0

# jsonSchema(funcNameopt) → {JSONSchema7}

Converts the contract spec to a JSON schema.

If funcName is provided, the schema will be a reference to the function schema.

Parameters:
Name Type Attributes Description
funcName string <optional>

the name of the function to convert

View Source lib/contract/spec.js, line 1035

if the contract spec is invalid

Error

the converted JSON schema

JSONSchema7

# nativeToScVal(val, tyopt) → {xdr.ScVal}

Converts a native JS value to an ScVal based on the given type.

Parameters:
Name Type Attributes Description
val any

the native JS value

ty xdr.ScSpecTypeDef <optional>

the expected type

View Source lib/contract/spec.js, line 611

if value cannot be converted to the given type

Error

the converted ScVal

xdr.ScVal

# scValStrToNative(scv, typeDef) → {any}

Converts an base64 encoded ScVal back to a native JS value based on the given type.

Parameters:
Name Type Description
scv string

the base64 encoded ScVal

typeDef xdr.ScSpecTypeDef

the expected type

View Source lib/contract/spec.js, line 862

if ScVal cannot be converted to the given type

Error

the converted native JS value

any

# scValToNative(scv, typeDef) → {any}

Converts an ScVal back to a native JS value based on the given type.

Parameters:
Name Type Description
scv xdr.ScVal

the ScVal

typeDef xdr.ScSpecTypeDef

the expected type

View Source lib/contract/spec.js, line 875

if ScVal cannot be converted to the given type

Error

the converted native JS value

any