Skip to content
Open
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
56 changes: 8 additions & 48 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To ensure a smooth and effective contribution process, please take a moment to r

We welcome contributions in the form of code, documentation, bug reports, feature requests, and other forms of feedback. Here are some ways you can contribute:

- **Code Contributions:** You can contribute code examples in Rust, Python, or Solidity that demonstrate various Solana program functionalities. You can also contribute improvements to existing examples, such as bug fixes, optimizations, or additional features.
- **Code Contributions:** You can contribute code examples in Rust that demonstrate various Solana program functionalities. You can also contribute improvements to existing examples, such as bug fixes, optimizations, or additional features.

- **Bug Reports, Ideas or Feedback:** If you encounter any issues or have ideas for new examples, please submit a bug report or feature request. Your feedback is valuable and helps us improve the quality and relevance of the examples.

Expand All @@ -22,40 +22,13 @@ Specifically for code in this repo:

1. Use pnpm as the default package manager for the project. You can [install pnpm by following the instructions](https://pnpm.io/installation). Commit `pnpm-lock.yaml` to the repository.

2. Solana Programs written for Anchor framework should be in directory (`anchor`)[https://www.anchor-lang.com], Solana Native in (`native`)[https://solana.com/developers/guides/getstarted/intro-to-native-rust], Steel Framework in (`steel`)[https://github.com/regolith-labs/steel], TypeScript in (`poseidon`)[https://github.com/Turbin3/poseidon], respectively.
2. Solana Programs written for the Anchor framework should be in directory [`anchor`](https://www.anchor-lang.com), Solana Native in [`native`](https://solana.com/developers/guides/getstarted/intro-to-native-rust), respectively.
- Project path structure: `/program-examples/category/example-name/<framework_name>`
- Project path structure example for anchor: `/program-examples/category/example-name/anchor`

3. Tests for Solana native programs, steel framework programs, and Anchor should be written with [solana-bankrun](https://kevinheavey.github.io/solana-bankrun)
3. Tests for Anchor and Solana native programs should be written with [solana-bankrun](https://kevinheavey.github.io/solana-bankrun).

4. Steel framework programs must be organized as a Cargo workspace with separate projects for API and program:
- Project path structure: `/program-examples/category/example-name/steel`
- Initialise project using `steel new <name>`
- Must be a Cargo workspace with two separate projects:
- `api`: Contains API-related code
- `program`: Contains the program implementation
- Steel projects should NOT be added in the root [`Cargo.toml` file](https://github.com/solana-developers/program-examples/blob/main/Cargo.toml)

This structure ensures proper organization and separation of concerns.

5. For Steel framework programs:
- Steel CLI is the recommended way to build and test programs:
```bash
# Install Steel CLI (one-time setup)
cargo install steel-cli

# Create a new Steel project
steel new <name>

# Build the program
steel build

# Run tests
steel test
```
- Alternatively, you can use package.json scripts if you need custom build/test configurations as Solana native one described below.

6. For Solana native programs ensure adding these mandatory pnpm run scripts to your `package.json` file for successful CI/CD builds:
4. For Solana native programs ensure adding these mandatory pnpm run scripts to your `package.json` file for successful CI/CD builds:

```json
"scripts": {
Expand All @@ -66,35 +39,22 @@ Specifically for code in this repo:
},
```

Alternatively, You can add `steel test` and `steel build` as commands according to your project.

"scripts": {
"test": "steel test",
"build-and-test": "steel build && steel test",
"build": "steel build",
"deploy": "solana program deploy ./program/target/so/program.so"
},

7. Test command for Anchor should execute `pnpm test` instead of `yarn run test` for anchor programs. Replace `yarn` with `pnpm` in `[script]` table inside [Anchor.toml file.](https://www.anchor-lang.com/docs/manifest#scripts-required-for-testing)
5. Test command for Anchor should execute `pnpm test` instead of `yarn run test` for anchor programs. Replace `yarn` with `pnpm` in `[script]` table inside [Anchor.toml file.](https://www.anchor-lang.com/docs/manifest#scripts-required-for-testing)

```
[scripts]
test = "pnpm ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
```

8. TypeScript, JavaScript and JSON files are formatted and linted using
6. TypeScript, JavaScript and JSON files are formatted and linted using
[Biome](https://biomejs.dev/). Execute the following command to format and lint your code at the root of this project before submitting a pull request:

8. Some projects can be ignored from the building and testing process by adding the project name to the `.ghaignore` file.
When removing or updating an example, please ensure that the example is removed from the `.ghaignore` file
and there's a change in that example's directory.

```bash
pnpm fix
```

9. Some projects can be ignored from the building and testing process by adding the project name to the `.gitignore` file.
When removing or updating an example, please ensure that the example is removed from the `.gitignore` file
7. Some projects can be ignored from the building and testing process by adding the project name to the `.ghaignore` file.
When removing or updating an example, please ensure that the example is removed from the `.ghaignore` file
and there's a change in that example's directory.

## Code of Conduct
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Program Examples

## Onchain program examples for :anchor: Anchor :crab: Native Rust, and [TS] TypeScript.
## Onchain program examples for :anchor: Anchor and :crab: Native Rust.

[![Native](https://github.com/solana-developers/program-examples/actions/workflows/solana-native.yml/badge.svg?event=schedule)](https://github.com/solana-developers/program-examples/actions/workflows/solana-native.yml) [![Anchor](https://github.com/solana-developers/program-examples/actions/workflows/anchor.yml/badge.svg?event=schedule)](https://github.com/solana-developers/program-examples/actions/workflows/anchor.yml)
[![Anchor](https://github.com/solana-developers/program-examples/actions/workflows/anchor.yml/badge.svg?event=schedule)](https://github.com/solana-developers/program-examples/actions/workflows/anchor.yml) [![Native](https://github.com/solana-developers/program-examples/actions/workflows/solana-native.yml/badge.svg?event=schedule)](https://github.com/solana-developers/program-examples/actions/workflows/solana-native.yml)

This repo contains Solana onchain programs (referred to as 'Smart Contracts' in other blockchains).

Expand Down Expand Up @@ -175,7 +175,7 @@ Allow two users to swap digital assets with each other, each getting 100% of wha

### Basics - create token mints, mint tokens, and transfer tokens with Token Extensions

Create token mints, mint tokens, and transferr tokens using Token Extensions.
Create token mints, mint tokens, and transfer tokens using Token Extensions.

[anchor](./tokens/token-2022/basics/anchor)

Expand Down Expand Up @@ -221,13 +221,13 @@ Create tokens that store their onchain metadata inside the token mint, without n

[anchor](./tokens/token-2022/metadata/anchor)

### Allow a designedated account to close a mint
### Allow a designated account to close a mint

Allow a designated account to close a Mint.

[anchor](./tokens/token-2022/mint-close-authority/anchor) [native](./tokens/token-2022/mint-close-authority/native)

### Usng multiple token extensions
### Using multiple token extensions

Use multiple Token Extensions at once.

Expand Down
3 changes: 3 additions & 0 deletions basics/account-data/anchor/Anchor.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[toolchain]
solana_version = "3.1.8"

[features]
seeds = false
skip-lint = false
Expand Down
3 changes: 2 additions & 1 deletion basics/account-data/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"dependencies": {
"@solana/web3.js": "^1.47.3",
"fs": "^0.0.1-security"
"fs": "^0.0.1-security",
"borsh": "^2.0.0"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
Expand Down
10 changes: 10 additions & 0 deletions basics/account-data/native/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 19 additions & 35 deletions basics/account-data/native/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,25 @@ import {
import * as borsh from "borsh";
import { start } from "solana-bankrun";

class Assignable {
constructor(properties) {
for (const [key, value] of Object.entries(properties)) {
this[key] = value;
}
}
}
const AddressInfoSchema = {
struct: {
name: "string",
house_number: "u8",
street: "string",
city: "string",
},
};

class AddressInfo extends Assignable {
street: string;
city: string;
type AddressInfo = {
name: string;
house_number: number;
toBuffer() {
return Buffer.from(borsh.serialize(AddressInfoSchema, this));
}
street: string;
city: string;
};

static fromBuffer(buffer: Buffer) {
return borsh.deserialize(AddressInfoSchema, AddressInfo, buffer);
}
function borshSerialize(schema: borsh.Schema, data: object): Buffer {
return Buffer.from(borsh.serialize(schema, data));
}
const AddressInfoSchema = new Map([
[
AddressInfo,
{
kind: "struct",
fields: [
["name", "string"],
["house_number", "u8"],
["street", "string"],
["city", "string"],
],
},
],
]);

describe("Account Data!", async () => {
const addressInfoAccount = Keypair.generate();
Expand Down Expand Up @@ -73,12 +57,12 @@ describe("Account Data!", async () => {
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
],
programId: PROGRAM_ID,
data: new AddressInfo({
data: borshSerialize(AddressInfoSchema, {
name: "Joe C",
house_number: 136,
street: "Mile High Dr.",
city: "Solana Beach",
}).toBuffer(),
}),
});

const blockhash = context.lastBlockhash;
Expand All @@ -92,13 +76,13 @@ describe("Account Data!", async () => {
test("Read the new account's data", async () => {
const accountInfo = await client.getAccount(addressInfoAccount.publicKey);

const readAddressInfo = AddressInfo.fromBuffer(
const readAddressInfo = borsh.deserialize(
AddressInfoSchema,
Buffer.from(accountInfo.data),
);
) as AddressInfo;
console.log(`Name : ${readAddressInfo.name}`);
console.log(`House Num: ${readAddressInfo.house_number}`);
console.log(`Street : ${readAddressInfo.street}`);
console.log(`City : ${readAddressInfo.city}`);
});
});

3 changes: 3 additions & 0 deletions basics/checking-accounts/anchor/Anchor.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[toolchain]
solana_version = "3.1.8"

[features]
seeds = false
[programs.localnet]
Expand Down
3 changes: 3 additions & 0 deletions basics/close-account/anchor/Anchor.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[toolchain]
solana_version = "3.1.8"

[features]
seeds = false
skip-lint = false
Expand Down
2 changes: 2 additions & 0 deletions basics/close-account/native/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions basics/counter/anchor/Anchor.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[toolchain]
solana_version = "3.1.8"

[features]
seeds = false
skip-lint = false
Expand Down
3 changes: 2 additions & 1 deletion basics/counter/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"typescript": "^4.3.5"
},
"dependencies": {
"@solana/web3.js": "^1.91.4"
"@solana/web3.js": "^1.91.4",
"bn.js": "^5.2.2"
}
}
15 changes: 10 additions & 5 deletions basics/counter/native/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions basics/create-account/anchor/Anchor.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[toolchain]
solana_version = "3.1.8"

[features]
seeds = false
skip-lint = false
Expand Down
2 changes: 1 addition & 1 deletion basics/create-account/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@types/chai": "^4.3.1",
"@types/mocha": "^9.1.1",
"chai": "^4.3.4",
"litesvm": "^0.3.3",
"solana-bankrun": "^0.3.0",
"mocha": "^9.0.3",
"ts-mocha": "^10.0.0",
"typescript": "^4.3.5"
Expand Down
Loading
Loading