Skip to main content



Tamagotchi is a popular virtual pet game that was created in Japan in the late 1990s. This game provides an opportunity to care for a virtual pet, which is a tiny digital creature that lives on the screen of the device.

The main purpose of the game is to provide care, nurturing and care for your Tamagotchi. The pet requires constant attention: you need to feed it, groom it, play with it and monitor its overall condition. If you don't pay enough attention to your Tamagotchi, it may even die.

The game develops responsibility and care in the player, and promotes time management skills. Tamagotchi has become an iconic game and has many different versions and modifications.

The article explains the programming interface, data structure, basic functions and explains their purpose. It can be used as is or modified to suit your own scenarios. Anyone can easily create their own application and run it on the Vara Network. The source code is available on GitHub.

How to run

  1. Build a program

Additional details regarding this matter can be located within the README directory of the program.

  1. Upload the program to the Vara Network Testnet

Further details regarding the process of program uploading can be located within the Getting Started section.

  1. Build and run user interface

More information about this can be found in the README directory of the frontend.

Implementation details

Tamagotchi program description

The Tamagotchi program contains the following information

struct Tamagotchi {
name: String,
date_of_birth: u64,
owner: ActorId,
fed: u64,
fed_block: u64,
entertained: u64,
entertained_block: u64,
rested: u64,
rested_block: u64,
  • name - pet name
  • date_of_birth - pet's date of birth
  • owner - pet owner
  • fed - is the level of satiety
  • fed_block - is the last feeding time
  • entertained - is the level of entertainment satisfaction
  • entertained_block - is the time of last entertainment
  • rested - is the level of relaxation
  • rested_block - is the final resting time


To initialize a game program, it is only necessary to provide the pet's name.

pub struct TmgInit {
pub name: String,

During initialization, the pet is created with a complete set of all states.

extern fn init() {
let TmgInit { name } = msg::load().expect("Failed to decode Tamagotchi name");
let current_block = exec::block_timestamp();

let tmg = Tamagotchi {
date_of_birth: current_block,
owner: msg::source(),
fed_block: current_block,
entertained: MAX_VALUE,
entertained_block: current_block,
rested: MAX_VALUE,
rested_block: current_block,
unsafe {
TAMAGOTCHI = Some(tmg);


The Tamagotchi program offers the following activities:

pub enum TmgAction {
// get a pet name
// get the age of the pet
// feed the pet
// play with the pet
// let the pet sleep
// get basic information about the pet


For each activity, the program responds with the following replies:

pub enum TmgReply {
TmgInfo {
owner: ActorId,
name: String,
date_of_birth: u64,

Before proceeding with any action, it is necessary to check whether the pet is alive. For this purpose, values indicating the health of the pet are calculated


fn calculate_hunger(&self) -> u64 {
HUNGER_PER_BLOCK * ((exec::block_timestamp() - self.fed_block) / 1_000)

fn calculate_boredom(&self) -> u64 {
BOREDOM_PER_BLOCK * ((exec::block_timestamp() - self.entertained_block) / 1000)

fn calculate_energy(&self) -> u64 {
ENERGY_PER_BLOCK * ((exec::block_timestamp() - self.rested_block) / 1000)

If the value of all three tamagotchi state parameters is too high, the tamagotchi will die

fn tmg_is_dead(&self) -> bool {
let fed = self.fed.saturating_sub(self.calculate_hunger());
let entertained = self.entertained.saturating_sub(self.calculate_boredom());
let rested = self.rested.saturating_sub(self.calculate_energy());
fed == 0 && entertained == 0 && rested == 0

Program metadata and state

Metadata interface description:

pub struct ProgramMetadata;

impl Metadata for ProgramMetadata {
type Init = In<TmgInit>;
type Handle = InOut<TmgAction, TmgReply>;
type Reply = ();
type Others = ();
type Signal = ();
type State = Out<Tamagotchi>;

To display the program state information, the state() function is used:

extern fn state() {
let tmg = unsafe { TAMAGOTCHI.take().expect("Unexpected error in taking state") };
msg::reply(tmg, 0).expect("Failed to share state");

Source code

The source code of this example of Tamagotchi program and the example of an implementation of its testing is available on gear-foundation/dapp/contracts/tamagotchi.

See also an example of the program testing implementation based on gtest: gear-foundation/dapps/tamagotchi/tests.

For more details about testing programs written on Gear, refer to the Program Testing article.