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 #223

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
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 = ""
repository = ""
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.0"
thiserror = "1.0"
45 changes: 45 additions & 0 deletions basics/transfer-sol/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# TransferSol

**TransferSol** is a ...

## API

- [`Error`](api/src/error.rs) – Custom program errors.
- [`Instruction`](api/src/instruction.rs) – Declared instructions.

## Instructions

- [`TransferSolWithCpi`](program/src/transfer_sol_with_cpi.rs) – Invoke transfer SOL via CPI
- [`TransferSolWithProgram`](program/src/transfer_sol_with_program.rs) – Invoke transfer SOL via program

## How to run

Compile your program:

```sh
pnpm build
```

Run unit and integration tests:

```sh
steel test
```

or

```sh
pnpm test
```

Run build and test

```sh
pnpm build-and-test
```

Deploy your program:

```sh
pnpm deploy
```
18 changes: 18 additions & 0 deletions basics/transfer-sol/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "transfer-sol-api"
description = "API for interacting with the TransferSol program"
version.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
documentation.workspace = true
repository.workspace = true
readme.workspace = true
keywords.workspace = true

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

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
#[repr(u32)]
pub enum TransferSolError {
#[error("This is a dummy error")]
Dummy = 0,
}

error!(TransferSolError);
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 {
TransferSolWithCpi = 0,
TransferSolWithProgram = 1,
}

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

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

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

pub mod prelude {

pub use crate::error::*;
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 transfer_sol_with_cpi(payer: Pubkey, recipient: Pubkey, amount: u64) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(payer, true),
AccountMeta::new(recipient, false),
AccountMeta::new_readonly(system_program::ID, false),
],
data: TransferSolWithCpi {
amount: amount.to_le_bytes(),
}
.to_bytes(),
}
}

pub fn transfer_sol_with_program(payer: Pubkey, recipient: Pubkey, amount: u64) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(payer, false),
AccountMeta::new(recipient, false),
],
data: TransferSolWithProgram {
amount: amount.to_le_bytes(),
}
.to_bytes(),
}
}
29 changes: 29 additions & 0 deletions basics/transfer-sol/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "transfer-sol",
"version": "1.0.0",
"description": "",
"main": "index.js",
"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/account_data_program.so"
},
"keywords": [],
"author": "Leo Pham <[email protected]>",
"license": "ISC",
"dependencies": {
"@solana/web3.js": "^1.95.4"
},
"devDependencies": {
"@types/mocha": "^10.0.9",
"@types/node": "^22.7.9",
"borsh": "^2.0.0",
"mocha": "^10.7.3",
"solana-bankrun": "^0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "^5.6.3",
"chai": "^4.3.7",
"@types/chai": "^4.3.7"
}
}
Loading