Logging Contract using Soroban AssemblyScript SDK

darkvallen

darkvallen

Posted on March 30, 2023

Logging Contract using Soroban AssemblyScript SDK

Hi! in my previous post, i already illustrates how to use the Soroban AssemblyScript SDK to store and retrieve data in a simple smart contract, and now we will dive into the realm of contract logging in a Soroban smart contract using Soroban Assembly Script SDK.

We will explore how to log a Soroban smart contract using AssemblyScript SDK. This example will showcase logFtm function from context module provided by the as-soroban-sdk library.

The Contract Code

The contract code will be similar to my previous post with additional code to do logging each time it is called. Let's dive into the code:

import {RawVal, fromU32, fromSymbolStr, toU32} from 'as-soroban-sdk/lib/value';
import * as ledger from "as-soroban-sdk/lib/ledger";
import {Vec} from 'as-soroban-sdk/lib/vec';
import * as context from 'as-soroban-sdk/lib/context';
Enter fullscreen mode Exit fullscreen mode

In this section, two additional modules added vec and context module. These modules will give us functionality to do logging.

export function increment(): RawVal {

  let data = "COUNTER";
  var counter = 0;
  let args = new Vec();

  if (ledger.hasDataFor(data)) {
    let dataObj = ledger.getDataFor(data);
    counter = toU32(dataObj);
  }
  args.pushBack(fromU32(counter));
  counter ++;
  ledger.putDataFor(data, fromU32(counter));
  args.pushBack(fromU32(counter));
  context.logFtm("Before Increment = {} and After increment = {}", args);
  return ledger.getDataFor(data);

}
Enter fullscreen mode Exit fullscreen mode

The increment function works exactly like the code before with additional args.pushBack() function to add logged arguments, and context.logFtm() to create the logs. A new Vec instance to store the log's arguments. Two arguments are added to to the Vec: counter value before increment and counter value after increment. Both are converted to their RawVal representation using the fromU32 function. The context.logFtm function called to print the before and after value log. Finally, the updated counter value is retrieved from the ledger and returned as the function's output.

Next, create contract.json file in your project directory, this file contains metadata for the contract.

{
    "name": "Store and Retrieve Data Contract (Logging)",
    "version": "0.1.0",
    "description": "example",
    "host_functions_version": 29,
    "functions": [
        {
            "name" : "increment",
            "arguments": [],
            "returns" : "u32"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Before compiling the contract, we need to edit the asconfig.json file of your project. Replace its content with the following:

{
  "extends": "as-soroban-sdk/sdkasconfig",
  "targets": {
    "release": {
      "outFile": "build/release.wasm",
      "textFile": "build/release.wat"
    },
    "debug": {
      "outFile": "build/debug.wasm",
      "textFile": "build/debug.wat"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The asconfig.json file is used by the AssemblyScript compiler (asc) to define the configuration for your project.

Compiling the Contract

You need to compile it into WebAssembly first. To do this, you'll use the following command :

npx asc assembly/index.ts --target release
Enter fullscreen mode Exit fullscreen mode

Now you should see two new files in the build/ directory: release.wasm and release.wat.

Running The Contract on Sandbox

Let's run the contract to see if it's works, we're gonna run the contract using soroban-cli on sandbox using the following command :

soroban contract invoke --wasm build/release.wasm --id 1 --fn increment
Enter fullscreen mode Exit fullscreen mode

You should get the output:

1
#0: debug: Before Increment = U32(0) and After increment = U32(1)
Enter fullscreen mode Exit fullscreen mode

Run it once more, you should get output:

2
#0: debug: Before Increment = U32(1) and After increment = U32(2)
Enter fullscreen mode Exit fullscreen mode

1st

2nd

Closing

This example demonstrates how to log a soroban smart contract using the Soroban AssemblyScript SDK. By leveraging the ledger, context, value, and vec modules, we can easily interact with the blockchain storage, manage data, and do logging. With this foundation, you can move on to build more advanced and feature-rich smart contracts on the Soroban platform. Happy Sorobaning!

💖 💪 🙅 🚩
darkvallen
darkvallen

Posted on March 30, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related