Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basics/transfer-sol/steel #232

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions basics/transfer-sol/steel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
test-ledger
21 changes: 21 additions & 0 deletions basics/transfer-sol/steel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[workspace]
resolver = "2"
members = ["api", "program"]

[workspace.package]
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
homepage = ""
documentation = ""
respository = ""
readme = "./README.md"
keywords = ["solana"]

[workspace.dependencies]
transfer-sol-api = { path = "./api", version = "0.1.0" }
bytemuck = "1.14"
num_enum = "0.7"
solana-program = "1.18"
steel = "2.1"
thiserror = "1.0"
27 changes: 27 additions & 0 deletions basics/transfer-sol/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Transfer Sol

Simple example of a Solana program that transfers SOL between accounts.

For more information, see this [README](../README.md).

## Building

```sh
cargo build-sbf

```
## Tests

This project includes both:
- Rust tests: [`program/tests`](/program/tests) directory.
- Node.js tests using [Bankrun](https://kevinheavey.github.io/solana-bankrun/): [`tests`](/tests) directory.

```sh
# rust tests
cargo test-sbf

# node tests
pnpm build-and-test # this will also build the program
#or
pnpm test # if you have already built the program
```
11 changes: 11 additions & 0 deletions basics/transfer-sol/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "transfer-sol-api"
version = "0.1.0"
edition = "2021"

[dependencies]
bytemuck.workspace = true
num_enum.workspace = true
solana-program.workspace = true
steel.workspace = true
thiserror.workspace = true
23 changes: 23 additions & 0 deletions basics/transfer-sol/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use steel::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum TransferSolInstruction {
WithCpi = 0,
WithProgram = 1,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct WithCpi {
pub amount: [u8; 8],
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct WithProgram {
pub amount: [u8; 8],
}

instruction!(TransferSolInstruction, WithCpi);
instruction!(TransferSolInstruction, WithProgram);
12 changes: 12 additions & 0 deletions basics/transfer-sol/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub mod instruction;
pub mod sdk;

pub mod prelude {
pub use crate::instruction::*;
pub use crate::sdk::*;
}

use steel::*;

// TODO Set program id
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
32 changes: 32 additions & 0 deletions basics/transfer-sol/steel/api/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use steel::*;

use crate::prelude::*;

pub fn with_cpi(payer: Pubkey, receiver: Pubkey, amount: u64) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(payer, true),
AccountMeta::new(receiver, false),
AccountMeta::new_readonly(system_program::ID, false),
],
data: WithCpi {
amount: amount.to_le_bytes(),
}
.to_bytes(),
}
}

pub fn with_program(payer: Pubkey, receiver: Pubkey, amount: u64) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(payer, false),
AccountMeta::new(receiver, false),
],
data: WithProgram {
amount: amount.to_le_bytes(),
}
.to_bytes(),
}
}
28 changes: 28 additions & 0 deletions basics/transfer-sol/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "transfer-sol",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/*.test.ts",
"build-and-test": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./tests/fixtures && pnpm test",
"build": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so",
"deploy": "solana program deploy ./program/target/so/transfer_sol_program.so"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@solana/web3.js": "^1.95.4"
},
"devDependencies": {
"@types/chai": "^4.3.7",
"@types/mocha": "10.0.9",
"@types/node": "^22.7.4",
"borsh": "^2.0.0",
"chai": "^4.3.7",
"mocha": "10.7.3",
"solana-bankrun": "0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "5.6.3"
}
}
Loading