Skip to main content

Convert a ScVal to other type

Soroban Contract Value (ScVal) is a custom type defined within the Soroban runtime environment that represents other data types such as strings, bytes, and more complex structures used within smart contracts in a format that that the soroban runtime can process, store and retrieve efficiently.

ScVal to bytesNโ€‹

const StellarSdk = require('@stellar/stellar-sdk');

// Convert ScVal to bytes
const bytesFromScVal = scVal.bytes();

.bytes() converts an ScVal value to bytes. scVal is the ScVal value to be converted to bytes.

ScVal to addressโ€‹

const StellarSdk = require('@stellar/stellar-sdk');

const addressFromScVal = StellarSdk.Address.fromScVal(scVal);
addressFromScVal.toString();

StellarSdk.Address.fromScVal() converts an ScVal value to an address. scVal is the ScVal value to be converted to an address.

ScVal to Stringโ€‹

const StellarSdk = require('@stellar/stellar-sdk');

const stringFromScVal = scVal.toString('utf-8');

scVal.toString() converts an ScVal value to a string. scVal is the ScVal value to be converted to a string. utf-8 is the encoding format for the string.