Event-Driven Smart Contract using Soroban AssemblyScript SDK
Tags: assemblyscript, soroban, stellar, smartcontract
Published: 2023-03-30 00:48:42

Hi! in my [previous post](https://dev.to/darkvallen/storing-and-retrieving-data-using-soroban-assembly-script-sdk-5e9j/), 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 events in a Soroban smart contract using Soroban Assembly Script SDK.

We will explore how to create an event-driven smart contract on the Soroban smart contract platform using AssemblyScript SDK. This example will showcase `publishEvent` function provided by the `as-soroban-sdk` library.

##The Contract Code
The contract code will be similar to my previous post with additional code to emits an event each time it is called.Let's dive into the code:
```TypeScript
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 {publishEvent} from 'as-soroban-sdk/lib/context';
```
In this section, two additional modules added vec and context module. These modules will give us functionality to emits event.
```TypeScript
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));

  let topics = new Vec();
  topics.pushBack(fromSymbolStr(data));
  topics.pushBack(fromSymbolStr("increment"));
  publishEvent(topics, fromU32(counter));

  return ledger.getDataFor(data);

}
```

The `increment` function works exactly like the code before with additional `topics.pushBack()` function to add topics to the events, and `publishEvent()` function to publish the event. A new `Vec` instance to store the event's topics. Two topics are added to the `Vec`: the COUNTER `data` and an "increment" label. Both are converted to their `RawVal` representation using the `fromSymbolStr` function. The `publishEvent` function is then called with the topics and the updated counter value as arguments. 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.

```json
{
    "name": "Store and Retrieve Data Contract (With Events)",
    "version": "0.1.0",
    "description": "example",
    "host_functions_version": 29,
    "functions": [
        {
            "name" : "increment",
            "arguments": [],
            "returns" : "u32"
        }
    ]
}
```

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

```json
{
  "extends": "as-soroban-sdk/sdkasconfig",
  "targets": {
    "release": {
      "outFile": "build/release.wasm",
      "textFile": "build/release.wat"
    },
    "debug": {
      "outFile": "build/debug.wasm",
      "textFile": "build/debug.wat"
    }
  }
}
```
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
```
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 :

```bash
soroban contract invoke --wasm build/release.wasm --id 1 --fn increment
```
You should get the output:

```bash
1
#0: event: {"ext":"v0","contract_id":"0000000000000000000000000000000000000000000000000000000000000009","type_":"contract","body":{"v0":{"topics":[{"symbol":"COUNTER"},{"symbol":"increment"}],"data":{"u32":1}}}}
```
Run it once more, you should get output:
```bash
2
#0: event: {"ext":"v0","contract_id":"0000000000000000000000000000000000000000000000000000000000000009","type_":"contract","body":{"v0":{"topics":[{"symbol":"COUNTER"},{"symbol":"increment"}],"data":{"u32":2}}}}
```

![event](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qluq5s9wb8aj04zgjk40.png)

##Closing
This example demonstrates how to create an event-driven 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 publish events. With this foundation, you can move on to build more advanced and feature-rich smart contracts on the Soroban platform. Happy coding!