# new Spec(entries)
Parameters:
Name | Type | Description |
---|---|---|
entries |
Array.<xdr.ScSpecEntry>
|
Array.<string>
|
the XDR spec entries |
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
Methods
# errorCases() → {Array.<xdr.ScSpecFunctionV0>}
Gets the XDR error cases from the spec.
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 |
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 |
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 |
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.
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 |
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 |
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 |
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 |
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 |
if ScVal cannot be converted to the given type
Error
the converted native JS value
any