Storing and Retrieving Data using Soroban Assembly Script SDK

darkvallen

darkvallen

Posted on March 29, 2023

Storing and Retrieving Data using Soroban Assembly Script SDK

Hi! in my previous post, i already covered how to initialize an assembly script directory, install soroban assembly script SDK and write simple "Hello World" contract, and now we will dive into the realm of storing data in a Soroban smart contract using Soroban Assembly Script SDK.

We will explore a simple example that demonstrates how to store in a Soroban smart contract. This example will showcase key functions provided by the as-soroban-sdk library, such as getDataFor, hasDataFor, and putDataFor.

The Contract Code

Let's begin by examining the code block:

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

In this section, we import the necessary modules from the as-soroban-sdk library. The ledger module provides functions for interacting with the blockchain's storage. The RawVal, toU32, and fromU32 type is imported from the value module.

export function increment(): RawVal {

  let data = "COUNTER";
  var counter = 0;
  if (ledger.hasDataFor(data)) {
    let dataObj = ledger.getDataFor(data);
    counter = toU32(dataObj);
  }
  counter ++;
  ledger.putDataFor(data, fromU32(counter));
  return ledger.getDataFor(data);

}
Enter fullscreen mode Exit fullscreen mode

Next, increment()function will be our smart contract entry point. Inside, it will check if there is already data stored under the key "COUNTER" using ledger.hasDataFor(). If so, retrieve it using ledger.getDataFor() and convert it to a number using toU32(). Otherwise, initialize the counter to 0.

Then increment the counter and store the new value using ledger.putDataFor(), converting the number to a RawVal using fromU32(). Finally, return the updated counter value.

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

{
    "name": "Store and Retrieve Data Contract",
    "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
Enter fullscreen mode Exit fullscreen mode

Run it once more, you should get output:

2
Enter fullscreen mode Exit fullscreen mode

Running The Contract

Closing

This example illustrates how to use the Soroban AssemblyScript SDK to store and retrieve data in a simple smart contract. By utilizing the ledger and value modules, we can easily interact with the blockchain storage and retrieve data. With this foundation, you can move on to build more complex and feature-rich smart contracts on the Soroban platform. Happy coding!

💖 💪 🙅 🚩
darkvallen
darkvallen

Posted on March 29, 2023

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

Sign up to receive the latest update from our blog.

Related