Cowchain Farm: Soroban Dapp on Stellar TestNET
Tags: soroban, stellar, smartcontract, flutter
Published: 2023-10-10 07:47:39

In this article, I will explain the latest upgrade of **Cowchain Farm** with **Soroban Preview 11** on **Stellar Testnet**.

For the Stellar Futurenet version of Cowchain Farm, you can check out my previous post [here](https://dev.to/hastodev/cowchain-farm-a-dapp-built-with-soroban-and-flutter-2me0).

---

## Quick Recap

**Cowchain Farm** is an idle game concept based on a smart contract app built using Soroban on Stellar blockchain and Flutter as the front-end framework.

You can Buy, Feed, and Sell your cow in the app. The cow price will be increased if you feed them on time.

---

## What's New on TestNET

The latest upgrade for Cowchain Farm brings several new exciting features that will benefit the user greatly. These features include:

1. Random Gender
2. Unique Name
3. Auctions
4. Notification Service

Also, on this upgrade, Cowchain Farm will start emitting soroban events for every crucial function related to the user.

Now, let's dig into those new features one by one.

---

## Random Gender

Soroban Preview 11 brings PRNG to the field.

PRNGs are very useful for creating random generators. Therefore, on this upgrade, the Cowchain Farm utilizes PRNG to assign gender randomly every time the user buys a cow.

---

## Unique Name

The idea of this feature is that each cow has a different name, and only one type of name can exist at a time.

So every time the user buys a cow, apart from the cow data, the cow name will also be stored in temporary storage. This data will live as long as the cow is still alive.

For example, if someone already has a cow named **"Cloud"**, we can't buy a cow and give it the same name.

Instead, we can name it **"CLouD"**, as the contract will interpret it as a different name.

---

## Auctions

Auction is one of the most exciting improvements on Cowchain Farm. Now, users can interact with each other.

User with unique cow names, a specific gender, good stats, and more mature cow can auction their cow at a higher price.
The combination of these factors can pique other user's interest in bidding.

In the soroban smart contract parts, the following functions are responsible for the auction features:

```rust
fn register_auction(
        env: Env,
        user: Address,
        cow_id: String,
        auction_id: String,
        price: u32,
    ) -> AuctionResult;

fn bidding(env: Env, user: Address, auction_id: String, bid_price: u32) -> AuctionResult;

fn finalize_auction(env: Env, auction_id: String) -> AuctionResult;

fn get_all_auction(env: Env) -> AuctionResult;
```

Following is the sequence of processes that occur when a user starts an auction:
<br>

#### <u>register_function</u>

When this function is called, the user's cow will be registered to be auctioned and put into the auction list.

Once started, the auction will be alive for 12 hours.
<br>

#### <u>bidding</u>

This will be called whenever the user bids at an auction.

This function will transfer user funds to a temporary holding account for the bid amount.

If someone outbid the highest bidder, then the funds of the previous highest bidder will be refunded.
<br>

#### <u>finalize_auction</u>

This function must be called every time an auction is ended.

This will transfer the ownership of the cow to the auction winner and transfer the funds to the user who started the auction.

This function can be called manually by the user or automatically handled by the notification service.
<br>

#### <u>get_all_auction</u>

This function will return the data of all ongoing auctions that happen inside the Cowchain Farm smart contract.

---

## Notification Service

The notification service is the most significant improvement on Cowchain Farm, which benefits users greatly.

Now we have a service that will let us know when:

1. Your cow starts to feel hungry.
2. You win an auction.
3. Your funds are refunded because someone outbid you at an auction.

This service consists of 2 parts, the Dart CLI and the Notification Mobile App.
<br>

#### <u>Dart CLI</u>

Built using [Dart](https://dart.dev/tutorials/server/get-started) and [Flutter Stellar SDK](https://pub.dev/packages/stellar_flutter_sdk), this tool runs on a Cloud Server listening to any event emitted by Cowchain Farm smart contract.

All events received by the Dart CLI will be processed, and the Dart CLI will determine whether the notification should be sent immediately or scheduled to be sent later.

This Dart CLI will also finalize the auctions that are due. This way, users can rest assured knowing that the auction will be processed automatically.
<br>

#### <u>Notification Mobile App</u>

User need to install the Notification Mobile App on their phone to receive notifications about Cowchain Farm.

Inside the app, users then need to register the Account ID or Public Key of the Stellar wallet they used to play Cowchain Farm.

That's all you need to do to receive the notification.

![Mobile App Notification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcgovga4trm0s175ar5r.png)

How to install the app?
Well, there are 2 ways you can do it:

1. Clone the [Cowchain Farm App repository](https://github.com/hasToDev/cowchain-farm-app), build the Android APK, and install it directly to your phone.

2. Download the prebuilt Android APK at [this DropBox link](https://www.dropbox.com/scl/fi/q6qksqguoi30dgfo3t5c2/cowchain_farm_200.apk?rlkey=7n0xadf3j6r01hp65jxsdo0fo&raw=1).

Due to some development limitations, for now, the app is only available on Android.

---

## Tech Stack and Source Code

Following is the tech stack and source code used to build the Cowchain Farm Dapp environment:
<br>

#### 1. <u>Cowchain Farm Smart Contract</u>

Built using [Rust](https://www.rust-lang.org/) and [Soroban SDK](https://docs.rs/soroban-sdk/20.0.0-rc2/soroban_sdk/index.html).

You can find the source code and documentation at [Cowchain Farm Soroban repository](https://github.com/hasToDev/cowchain-farm-soroban).
<br>

#### 2. <u>Cowchain Farm Web App & Mobile Notification</u>

Built using [Flutter](https://flutter.dev), [Flutter Stellar SDK](https://pub.dev/packages/stellar_flutter_sdk) and [OneSignal](https://onesignal.com).

You can find the source code and documentation at [Cowchain Farm App repository](https://github.com/hasToDev/cowchain-farm-app).
<br>

#### 3. <u>Cowchain Farm Notification Service</u>

Built using [Dart](https://dart.dev/tutorials/server/get-started), [Flutter Stellar SDK](https://pub.dev/packages/stellar_flutter_sdk) and [OneSignal](https://onesignal.com).

The Dart CLI for notification service was deployed inside a [Docker](https://www.docker.com) container in a [cloud server](https://www.vultr.com/?ref=9253209) that runs [Ubuntu](https://ubuntu.com/).

And to create Dart CLI's executable that run on Linux, we use a continuous integration (CI) provider, [Codemagic](https://codemagic.io/start/).

You can find the source code and documentation at [Cowchain Farm Alert repository](https://github.com/hasToDev/cowchain-farm-alert).

---

## Live Web App & Youtube

You can watch the Web Demo & brief explanation about Cowchain Farm's latest upgrade on the Youtube [link here](https://youtu.be/DbLDbTfABYc).

You can also try the live application on [Cowchain Farm WebApp](https://cowchain.hasto.dev/).

Ensure that you already have a [Stellar TESTNET account](https://laboratory.stellar.org/#account-creator?network=test) and have the [Freighter](https://www.freighter.app/) extension installed and enabled in your browser.

Also, ensure the **Experimental Mode** is enabled in the Freighter extension settings.