ContractSpec

ContractSpec

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.

Constructor

new ContractSpec(entries)

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

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

the XDR spec entries

Throws:

if entries is invalid

Type
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}

Methods

findEntry(name) → {xdr.ScSpecEntry}

Finds the XDR spec entry for the given name.

Source:
Parameters:
Name Type Description
name string

the name to find

Throws:

if no entry with the given name exists

Type
Error
Returns:
Type:
xdr.ScSpecEntry

the entry

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

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

Source:
Parameters:
Name Type Description
name string

the name of the function

args Object

the arguments object

Throws:

if argument is missing or incorrect type

Type
Error
Returns:
Type:
Array.<xdr.ScVal>

the converted arguments

Example
```js
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.

Source:
Parameters:
Name Type Description
name string

the name of the function

val_or_base64 xdr.ScVal | string

the result ScVal or base64 encoded string

Throws:

if return type mismatch or invalid input

Type
Error
Returns:
Type:
any

the converted native value

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

getFunc(name) → {xdr.ScSpecFunctionV0}

Gets the XDR function spec for the given function name.

Source:
Parameters:
Name Type Description
name string

the name of the function

Throws:

if no function with the given name exists

Type
Error
Returns:
Type:
xdr.ScSpecFunctionV0

the function spec

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

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

Source:
Parameters:
Name Type Attributes Description
val any

the native JS value

ty xdr.ScSpecTypeDef <optional>

the expected type

Throws:

if value cannot be converted to the given type

Type
Error
Returns:
Type:
xdr.ScVal

the converted ScVal

scValStrToNative(scv, typeDef) → {any}

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

Source:
Parameters:
Name Type Description
scv string

the base64 encoded ScVal

typeDef xdr.ScSpecTypeDef

the expected type

Throws:

if ScVal cannot be converted to the given type

Type
Error
Returns:
Type:
any

the converted native JS value

scValToNative(scv, typeDef) → {any}

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

Source:
Parameters:
Name Type Description
scv xdr.ScVal

the ScVal

typeDef xdr.ScSpecTypeDef

the expected type

Throws:

if ScVal cannot be converted to the given type

Type
Error
Returns:
Type:
any

the converted native JS value