Skip to content

Commit

Permalink
Merge pull request #39 from arihantbansal/master
Browse files Browse the repository at this point in the history
Add example to showcase use of String and Vec types
  • Loading branch information
ShrinathNR authored Nov 27, 2024
2 parents 8949d56 + f4097f3 commit 82ef730
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
4 changes: 1 addition & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ cargo install --git https://github.com/Turbin3/poseidon
poseidon compile --input "input.ts" --output "output.rs"
```

Check out examples in the repo to learn how to write Poseidon Typescript which can be transpiled to anchor programs. There are vote, vault and escrow(.ts files and their tranpiled .rs files)

## Tutorial & Examples

Go to [docs/src/tutorial.md](./docs/src/tutorial.md) to learn how to write your first Solana program in TypeScript using Poseidon and Anchor!

For more examples, check out the [examples](./examples) directory. You’ll find examples of vote, vault, and escrow programs in both TypeScript and the corresponding Rust programs transpiled by Poseidon.
For more examples, check out the [examples](./examples) directory. You’ll find examples of [vote](./examples/vote), [vault](./examples/vault), [escrow](./examples/escrow), and [favorites](./examples/favorites) programs in both TypeScript and the corresponding Rust programs transpiled by Poseidon.
11 changes: 11 additions & 0 deletions docs/src/mapping-into-anchor/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,14 @@ state.vote = state.vote.add(1);
// decrement the state
state.vote = state.vote.sub(1);
```

## Poseidon Type Reference

| Type | Anchor | Poseidon |
| ------- | --- | --- |
| Boolean | `bool` | `Boolean` |
| Integer | `u8/u16/u32/i8/i16/i32` | `u8/u16/u32/i8/i16/i32` |
| String | `String` | `String<N>` |
| Vector | `Vec<T>` | `Vec<T, N>` |

where `N` is the max length of the type.
2 changes: 2 additions & 0 deletions docs/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ Check out [examples](https://github.com/Turbin3/poseidon/tree/master/examples) i
2. Vault ([Rust](https://github.com/Turbin3/poseidon/blob/master/examples/vault/rust/vault.rs), [TypeScript](https://github.com/Turbin3/poseidon/blob/master/examples/vault/typescript/vault.ts))

3. Escrow ([Rust](https://github.com/Turbin3/poseidon/blob/master/examples/escrow/rust/escrow.rs), [TypeScript](https://github.com/Turbin3/poseidon/blob/master/examples/escrow/typescript/escrow.ts))

4. Favorites ([Rust](https://github.com/Turbin3/poseidon/blob/master/examples/favorites/rust/favorites.rs), [TypeScript](https://github.com/Turbin3/poseidon/blob/master/examples/favorites/typescript/favorites.ts))
39 changes: 39 additions & 0 deletions examples/favorites/rust/favorites.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use anchor_lang::prelude::*;
declare_id!("11111111111111111111111111111111");
#[program]
pub mod testoor {
use super::*;
pub fn set_favorites(
ctx: Context<SetFavoritesContext>,
number: u64,
color: String,
hobbies: Vec<String>,
) -> Result<()> {
ctx.accounts.favorites.number = number;
ctx.accounts.favorites.color = color;
ctx.accounts.favorites.hobbies = hobbies;
Ok(())
}
}
#[derive(Accounts)]
pub struct SetFavoritesContext<'info> {
#[account(mut)]
pub owner: Signer<'info>,
#[account(
init_if_needed,
payer = owner,
space = 344,
seeds = [b"favorites",
owner.key().as_ref()],
bump,
)]
pub favorites: Account<'info, Favorites>,
pub system_program: Program<'info, System>,
}

#[account]
pub struct Favorites {
pub number: u64,
pub color: String,
pub hobbies: Vec<String>,
}
33 changes: 33 additions & 0 deletions examples/favorites/typescript/favorites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
Account,
Pubkey,
Result,
u64,
Signer,
Vec,
String,
} from "@solanaturbine/poseidon";

export default class FavoritesProgram {
static PROGRAM_ID = new Pubkey("11111111111111111111111111111111");

setFavorites(
owner: Signer,
number: u64,
color: String<50>,
hobbies: Vec<String<50>, 5>,
favorites: Favorites,
): Result {
favorites.derive(["favorites", owner.key]).initIfNeeded(owner);

favorites.number = number;
favorites.color = color;
favorites.hobbies = hobbies;
}
}

export interface Favorites extends Account {
number: u64;
color: String<50>;
hobbies: Vec<String<50>, 5>;
}

0 comments on commit 82ef730

Please sign in to comment.