Giter VIP home page Giter VIP logo

example-messagefeed's Introduction

Build status

Solana Feed

This project demonstrates how to use the Solana Javascript API to build, deploy, and interact with programs on the Solana blockchain, implementing a simple feed of messages and prediction polls. To see it running go to https://solana-example-messagefeed.herokuapp.com/

Table of Contents

Overview

This project uses two Solana programs and a Node server to power a single webapp. The Message Feed program allows users to post messages and ban each other for bad behavior. The Prediction Poll program allows users to create wager-able polls that reward the winning side.

Message Feed

Messages are represented as a singly-linked list of Solana accounts.

Each Message account contains the message text, public key of the next message, and the public key of the User Account who posted it.

To post a new message, a User Account is required. The only way to obtain a User Account is to present credentials to the Https Server that created the first message in the chain.

A User Account contains a bit which indicates if they have been banned by another user.

User Login

Only the mechanism to obtain User Accounts is centralized. This will ensure each User Account is associated with a real person using whatever user authentication system (such as Google) is preferred. The only requirement on the authentication system is that that each authenticated user have a unique user id.

  1. Https Server loads the Message Feed program on Solana and posts the first message
  2. New User authenticates against Google and receives a JWT
  3. New User sends JWT to Https Server
  4. Https Server verifies the JWT contents and extracts their unique user id
  5. Https Server creates a User Account on behalf of New User if not already created
  6. Https Server returns the private key for the User Account to the New User, and the public key of the first message

Note: presently the JWT workflow is not fully implemented. No technical reason, just work that hasn't been done yet :)

Posting a message

  1. With the public key of the first message and a User Account, the user uses the RPC APIs of a Solana fullnode to fetch the latest message in the chain.
  2. The user constructs a new Transaction with the user message, the public key of their User Account and the public key of the latest message. The Transaction is signed with their User Account private key, then submitted to the Solana cluster.
  3. The Message Feed program processes the Transaction on-chain, confirms the user is not banned and links the new message to the chain.

Banning a user

Any user can construct a Transaction that bans any other user. All messages posted by a user contains the public key of their User Account, so it's easy to identify the origin of each post.

  1. The ban Transaction includes the public key of the User Account to ban, the public key of the banning user's User Account, and a message to include with the ban. The ban Transaction is signed with the banning user's User Account private key, then submitted to the Solana cluster.
  2. The Message Feed program processes the Transaction on chain, confirms the banning user is not also banned, and then sets the banned bit on the target user.

Learn about Solana

More information about how Solana works is available in the Book

Prediction Polls

Polls propose a question and 2 option to choose from. They allow users to wager tokens on which answer will be the most popular. The winning side gets to split up the losers' wagers!

Polls are stored in a single Collection account on Solana. The Collection account contains a list of public keys for the Poll accounts.

In addition to the display text, each poll also has an expiration block height and 2 tally keys for tracking wagers.

Tally Accounts record wagers for a particular poll option. When the poll expires, they are used to distribute winnings

Creating a poll

To create a new poll, a User Account is required. Similar to posting messages, the user account is retrieved from the server.

  1. The user signs in and fetches the prediction poll program id and the current collection key.
  2. The user inputs the poll header and options as well as a block timeout which will be added to the current block height to compute the poll expiration.
  3. A Transaction is constructed with instructions for creating the poll account and 2 tally accounts and an instruction for initializing the poll with the text and timeout.
  4. Solana then creates the accounts and the prediction poll program processes the poll initialization instruction to set the poll account data.

Voting

Voting on a poll involves a token wager which will be transferred to the poll account and recorded in the poll account data.

  1. A user selects a poll option and chooses an appropriate token wager
  2. A Transaction is constructed with instructions to create a one-off account with a balance equal to the token wager and submit a vote.
  3. The prediction poll program then drains the one-off account balance and records the wager in the poll account and the selected option tally account.

Claim winnings

Once the poll expires, anyone can trigger the distribution of the winnings.

  1. Transaction is created which references all of the winning wager keys in a claim instruction.
  2. The prediction poll program verifies that the poll has expired and then drains the poll account balance and proportionally distributes the tokens to the winners according to their wagers.

Limitations

  • The number of polls in a collection is limited to the size of the Collection account data
  • The number of participants in a tally are limited by the size of the Tally account data as well as the maximum size of a transaction. Serialized transactions must fit inside the MTU size of 1280 bytes.

Getting Started

The following dependencies are required to build and run this example, depending on your OS they may already be installed:

$ npm --version
$ docker -v
$ wget --version
$ rustc --version

Next fetch the npm dependencies, including @solana/web3.js, by running:

$ npm install

Select a Network

The example connects to a local Solana cluster by default.

To enable on-chain program logs, set the RUST_LOG environment variable:

$ export RUST_LOG=solana_runtime::system_instruction_processor=trace,solana_runtime::message_processor=info,solana_bpf_loader=debug,solana_rbpf=debug

To start a local Solana cluster run:

$ npm run localnet:update
$ npm run localnet:up

Solana cluster logs are available with:

$ npm run localnet:logs

To stop the local solana cluster run:

$ npm run localnet:down

For more details on working with a local cluster, see the full instructions.

Build the BPF program

The prediction poll program is only written in Rust. The build command will produce a BPF ELF shared object called dist/programs/prediction_poll.so.

$ npm run build:bpf-rust

or

$ npm run build:bpf-c

Start the web server

The message feed and prediction poll programs are deployed by the web server at src/server/index.js, so start it first:

$ npm run dev-server

Run the Command-Line Front End

After building the program and starting the web server, you can view the current message feed by running

$ npm run message-cli

and post a new message with:

$ npm run message-cli -- "This is a message"

and can create a test poll by running:

$ npm run poll-cli

Run the WebApp Front End

After building the program and starting the web server, start the webapp locally by running:

$ npm run dev

then go to http://localhost:8080/ in your browser.

example-messagefeed's People

Contributors

dependabot-preview[bot] avatar jackcmay avatar jstarry avatar mvines avatar dependabot-support avatar criesofcarrots avatar dependabot[bot] avatar pirate 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.