Giter VIP home page Giter VIP logo

solana_counter_program's Introduction

Solana Counter

This is a simple Solana program that manages a counter. It allows incrementing, decrementing, updating, and resetting the counter.

Instructions

The program supports the following instructions:

  1. Increment: Increases the counter by a specified value.
  2. Decrement: Decreases the counter by a specified value. If the value is greater than the current counter value, the counter is set to 0.
  3. Update: Sets the counter to a specified value.
  4. Reset: Resets the counter to 0.

Usage

To use this program, you can interact with it by creating transactions that include the appropriate instructions. The instructions are encoded as specified in the CounterInstructions enum.

Instruction Encoding

  • Increment: 0 followed by a little-endian encoding of the increment value (u32).
  • Decrement: 1 followed by a little-endian encoding of the decrement value (u32).
  • Update: 2 followed by a little-endian encoding of the new value (u32).
  • Reset: 3 (no additional data).

Example

Here is a simple example of how to interact with the Solana Counter Program using the provided test:

#[test]
fn test_counter() {
    // Setup accounts and program ID
    // ...

    // Increment
    let mut increment_instruction_data: Vec<u8> = vec![0];
    let increment_value = 40u32;
    increment_instruction_data.extend_from_slice(&increment_value.to_le_bytes());
    process_instruction(&program_id, &accounts, &increment_instruction_data).unwrap();
    assert_eq!(/* ... */);

    // Decrement
    let mut decrement_instruction_data: Vec<u8> = vec![1];
    let decrement_value = 20u32;
    decrement_instruction_data.extend_from_slice(&decrement_value.to_le_bytes());
    process_instruction(&program_id, &accounts, &decrement_instruction_data).unwrap();
    assert_eq!(/* ... */);

    // Update
    let mut update_instruction_data: Vec<u8> = vec![2];
    let update_value = 33u32;
    update_instruction_data.extend_from_slice(&update_value.to_le_bytes());
    process_instruction(&program_id, &accounts, &update_instruction_data).unwrap();
    assert_eq!(/* ... */);

    // Reset
    let reset_instruction_data: Vec<u8> = vec![3];
    process_instruction(&program_id, &accounts, &reset_instruction_data).unwrap();
    assert_eq!(/* ... */);
}

solana_counter_program's People

Contributors

tundx0 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.