A proof-of-concept crypto index and investment contract running on the Solana blockchain using the Pyth Network for market data.
Allows a user to invest in an index through buying and selling fungible tokens.
- Stores admin settings in a PDA (price accounts used for exchange rates etc.)
- First user to call function becomes the admin, no other users can call this function afterwards
- Transfers specified number of lamports from user (investor) to the programs wallet
- Index tokens are minted to the users associated token wallet rounded to the nearest integer
- Burns specified number of index tokens from user's associated token wallet
- Transfers lamports back to user
$$lamportsWithdrawn=\frac{tokensBurned \times indexValueInLamports}{lamportsPerSol}$$
- Uses the provided Pyth network public keys to read data from price accounts, writing to a PDA owned by the requesting user. The same PDA is used to update the value for each re-run.
ℹ️ If you are using Windows, consider installing Windows Subsystem for Linux 2 - the following instructions are much easier to follow in a Linux environment where apt
and bash
are available (there is no official Rust installer for Windows).
Used for the deployed Solana program.
- Install rustup (use the stable build and default profile).
- If using VSCode, the
rust-lang.rust
extension will be useful. For PyCharm/IntelliJ try the JetBrains Rust plugin. - Run
cargo install cargo-edit
. This will enable adding dependencies usingcargo add
(see cargo-edit docs here). - Install the Solana Tool Suite:
sh -c "$(curl -sSfL https://release.solana.com/stable/install)"
. - Install required build tools:
sudo apt update && sudo apt install -y pkg-config libudev-dev
.
Used for the program client and testing.
- Install NVM:
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
. - Set up the project Node:
nvm install
(installs Node version from .nvmrc). - Install Anchor CLI
- Install dependencies:
yarn install
.
Run formatting on rust files:
cargo fmt --all
Run clippy (bug checker) on rust files:
cargo clippy --no-deps --fix
Start a local cluster (will run until terminal exits with ctrl-c):
solana-test-validator
Build and deploy the program (in a new terminal):
anchor build
anchor deploy