Simulator Testbed Class

Use this class to run scenarios against the simulator.

Example

const Scenario1: TransactionObj[] = [
{
blockheight: 1,
amount: 6_0000_0000n,
sender: 1n,
recipient: 2n,
},
{
blockheight: 3,
amount: 5000_0000n,
sender: 1n,
recipient: 2n,
},
// ...
]

const ContractPath = join(__dirname + './contract.smart.c')

const Testbed = new SimulatorTestbed()
Testbed.loadContract(ContractPath)
.runScenario(Scenario1);

const bc = Testbed.Node.Blockchain;
const tx = bc.transactions;
const map = testbed.getContractMap().filter(({k1, k2, value}) => ...)

INITIALIZE CONTRACT

To initialize variables in the contract you can use the initializer parameter when loading the contract.

To apply initialization a code injection is needed and requires the developer to use defines pre-pended with TESTBED_ source code, i.e.

// initializable variables
// a good practice to have them declared as first variables in your contract
long var1, var2, var3;

// Define values if running on testbed
#ifdef TESTBED
const var1 = TESTBED_var1;
const var2 = TESTBED_var2;
const var3 = TESTBED_var3;
#endif

// This way your variable can be tested with different values easily in testbed

You may load the contract into the testbed as follows:

const ContractPath = join(__dirname + './contract.smart.c')

const testbed = SimulatorTestbed
.loadContract(ContractPath, {
var1: "Text",
var2: 1,
var3: 100n
} // initial values
).runScenario(Scenario1);

This testbed loads a SmartC Contract and a scenario (set of transactions) and forges all necessary blocks. It's possible to inspect all the results, i.e. transactions, kkv-maps, accounts, in-memory variables, and test them against expected result sets. This class is meant to be used with Test Runners like Vitest or Jest.

Constructors

Properties

node: SimNode

Accessors

  • get blockchain(): BLOCKCHAIN
  • Leaky abstraction that provides access to the underlying blockchain object

    Returns BLOCKCHAIN

Methods

  • Retrieves the account with the specified account ID.

    Parameters

    • accountId: bigint

      The ID of the account to retrieve.

    Returns null | AccountObj

    The account with the specified ID, or undefined if no account is found.

  • Retrieves a given contract by address.

    Parameters

    • Optional address: bigint

      The contract address (default: the last deployed).

    Returns CONTRACT

    The contract.

    Throws

    Error if invalid contract

  • Retrieves the maps per contract.

    Parameters

    • Optional address: bigint

      The contract address (default: the last deployed).

    Returns MapObj[]

    The maps per contract.

  • Retrieves a value from a map per slot.

    Parameters

    • key1: bigint

      1st Key

    • key2: bigint

      2nd Key

    • Optional address: bigint

      The contract address (default: the last deployed).

    Returns bigint

    The value or 0 if not exists.

  • Retrieves a list of (key-value)-tuples from a map per slot.

    Parameters

    • key1: bigint

      1st Key

    • Optional address: bigint

      The contract address (default: the last deployed).

    Returns MapObj[]

    The value or 0 if not exists.

  • Retrieves the contract memory for a given slot.

    Parameters

    • Optional address: bigint

      The contract address (default: the last deployed).

    Returns MemoryObj[]

    An array of MemoryObj representing the contract memory.

  • Retrieves the value of a contract memory variable by name, e.g. myvalue or inside a function func_myvalue

    Parameters

    • name: string

      The name of the variable to retrieve.

    • Optional address: bigint

      The contract address (default: the last deployed).

    Returns null | bigint

    • The value of the variable if found, otherwise null.
  • Returns a single transaction by Index

    Parameters

    • index: number

      Index of transaction (not Id!), must not be negative nor greater than the transaction list

    Returns BlockchainTransactionObj

  • Returns a single transaction by Id

    Parameters

    • id: bigint

      Transaction Id

    Returns null | BlockchainTransactionObj

  • Return an array of BlockchainTransactionObj representing the transactions in the blockchain.

    Returns BlockchainTransactionObj[]

    • An array of BlockchainTransactionObj objects.
  • Retrieves all the transactions sent by the contract at a given height. Nice to get contract responses.

    Parameters

    • blockheight: number

      The blockheight of transactions to fetch.

    • Optional address: bigint

      The contract address (default: the last deployed).

    Returns BlockchainTransactionObj[]

    An array of MemoryObj representing the contract memory.

  • Parameters

    • code: string
    • initializers: Record<string, string | number | bigint>

    Returns string

  • Loads a contract from the specified code path and eventually initializes the contract with the provided initializers

    Parameters

    • codePath: string

      The path to the SmartC code file.

    • Optional initializers: Record<string, string | number | bigint>

      The initializer object for the contract - Initialization is prepended.

    Returns SimulatorTestbed

    The simulator testbed with the loaded contract.

  • Runs a scenario by simulating a series of user transactions.

    Parameters

    • scenario: TransactionObj[] = []

      The array of user transactions representing the scenario.

    Returns SimulatorTestbed

    • Returns the current instance of the class.
  • Sends the argument transactions at the next blockheight and returns all transactions from the selected contract in the subsequent height. Input transactions are modified to match blockheight and contract address. This method forges two blocks in order to get the response.

    Parameters

    • transactions: TransactionObj[]

      Transactions to send.

    • Optional address: bigint

      The target contract address (default: the last deployed).

    Returns BlockchainTransactionObj[]

    An array of transactions, or empty array if no one was found.

  • Parameters

    • scenario: TransactionObj[]

    Returns string