signum-smartc-testbed
    Preparing search index...

    Class SimulatorTestbed

    Simulator Testbed Class

    Use this class to run scenarios against the simulator.

    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.

    Index

    Constructors

    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 AccountObj | null

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

    • Returns all contracts deployed on the simulated blockchain.

      Returns CONTRACT[]

      All deployed contracts.

    • Retrieves a given contract by address.

      Parameters

      • Optionaladdress: bigint

        The contract address (default: the last deployed).

      Returns CONTRACT

      The contract.

      Error if invalid contract

    • Retrieves the maps per contract.

      Parameters

      • Optionaladdress: 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

      • Optionaladdress: 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

      • Optionaladdress: 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

      • Optionaladdress: 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.

      • Optionaladdress: bigint

        The contract address (default: the last deployed).

      Returns bigint | null

      • 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 BlockchainTransactionObj | null

    • 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.

      • Optionaladdress: bigint

        The contract address (default: the last deployed).

      Returns BlockchainTransactionObj[]

      An array of MemoryObj representing the contract memory.

    • Loads a contract from the specified code path with optional creator, contractId, and initializers.

      The last loaded contract becomes the active contract, which is the default target for getContract, getContractMap, and sendTransactionAndGetResponse when called without an explicit address. In multi-contract setups, load helper/dependency contracts first and the contract under test last so it is active by default.

      Parameters

      • codePath: string

        The path to the SmartC code file.

      • Optionaloptions: LoadContractOptions

        Optional creator (default: 555n), contractId (default: auto-incremented), and initializers.

      Returns this

      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.

      • Optionaladdress: bigint

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

      Returns BlockchainTransactionObj[]

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