Skip to content

fix: add native flavor for token-swap example - #777

Closed
SuperPiIot wants to merge 1 commit into
solana-foundation:mainfrom
SuperPiIot:fix-issue-755
Closed

SuperPiIot wants to merge 1 commit into
solana-foundation:mainfrom
SuperPiIot:fix-issue-755

Conversation

@SuperPiIot

Copy link
Copy Markdown

Summary

This PR adds a native implementation of the token-swap example, following the structure of the existing anchor implementation. This allows learners to compare the differences and costs between anchor/native/pinocchio frameworks.

Closes #755
/claim #755

@SuperPiIot
SuperPiIot requested a review from dev-jodee as a code owner October 30, 2026 05:30
@github-actions github-actions Bot added the needs-issue PR does not reference an issue a maintainer has accepted label Oct 30, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every commit in a Solana Foundation repository needs signature verification, and these are unverified:

  • f90b941 fix: add native flavor for token-swap example

Set up signing, then sign the existing commits (git rebase --exec 'git commit --amend --no-edit -S' main) and force-push. This review is dismissed automatically once every commit verifies.

@github-actions

Copy link
Copy Markdown

Hi @SuperPiIot,

Thanks for the pull request. We ask that every change references an issue a maintainer has accepted, and there isn't one linked here.

These examples are a teaching resource and each one has to stay small enough to read in one sitting, so whether a new example or framework flavor belongs here is worth settling before anyone writes it. Agreeing on the approach in an issue first means nobody writes code that can't be merged. See CONTRIBUTING.md for the full workflow.

Please open an issue describing the change, or comment on the existing one. Once a maintainer has labelled it accepted, put Fixes #<issue> in this description and reopen this pull request; the check runs again and clears itself. Typos, broken links and comment-only fixes are exempt: write Linked issue: trivial instead.

@github-actions github-actions Bot closed this Oct 30, 2026
@greptile-apps

greptile-apps Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 0/5

[Low impact] The PR adds a separate native example without changing the existing Anchor program.

This PR needs changes before merging because the native example cannot build, run its test, or swap tokens.

Findings

  1. P1 Token swaps do nothing ▶
  2. P1 No deployable program ▶
  3. P1 Cargo rejects the crate ▶
  4. P1 Tests lack TypeScript support ▶
  5. P1 Test dependency is missing ▶
  6. P1 Program address is invalid ▶
  7. P1 Test omits required account ▶

Summary

Adds a native Rust program, package manifest, and TypeScript test for the token-swap example.

  • Learners can inspect a native Solana entrypoint beside the Anchor token-swap example.

Reviews (1) · Last reviewed commit: "fix: add native flavor for token-swap ex..." · Reviewed by Greptile

Comment on lines +28 to +30
msg!("Hello, Native Token Swap!");

Ok(())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Token swaps do nothing

process_instruction never reads instruction_data, creates pool state, or moves tokens. It only checks one account's owner and returns success. Unlike the Anchor example, this native version cannot create a pool, accept deposits, swap tokens, or withdraw liquidity. Implement those instructions before presenting this as a token-swap example.

Knowledge Base Used:

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

[package]
name = "token-swap-native"
version = "0.1.0"
edition = "2021"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 No deployable program

Cargo.toml does not declare a cdylib target. Rust therefore builds this library as an rlib, not the .so needed to deploy a Solana program. Add [lib] with crate-type = ["cdylib", "lib"], as the other native examples do.

Suggested change
edition = "2021"
edition = "2021"
[lib]
crate-type = ["cdylib", "lib"]

Comment on lines +1 to +4
[package]
name = "token-swap-native"
version = "0.1.0"
edition = "2021"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Cargo rejects the crate

The new crate sits under the root Cargo workspace but is neither listed in members nor excluded, and it has no local [workspace]. Building it directly makes Cargo reject it as a package that believes it belongs to the parent workspace. Add it to the root workspace, or explicitly make it standalone.

"name": "token-swap-native",
"version": "0.1.0",
"scripts": {
"build-and-test": "mocha --timeout 1000000 tests/**/*.test.ts"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Tests lack TypeScript support

If Node does not provide built-in TypeScript support, build-and-test fails while loading the test's imports. The command runs Mocha directly without registering a TypeScript loader. Installing ts-node or ts-mocha does not activate either one. Invoke ts-mocha or explicitly register a loader.

import { Connection, Keypair, PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { Program, AnchorProvider, web3 } from '@project-serum/anchor';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Test dependency is missing

The test imports AnchorProvider from @project-serum/anchor, but neither this package nor the root package declares that dependency. A fresh install cannot resolve the import, even after fixing the TypeScript loader. Declare the dependency or use a native test setup that does not need Anchor.

const provider = AnchorProvider.env();
AnchorProvider.env().wallet;
const connection = provider.connection;
const programId = new PublicKey('<SEU_PROGRAM_ID_AQUI>');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Program address is invalid

'<SEU_PROGRAM_ID_AQUI>' is not a valid public key. Once the provider setup succeeds, new PublicKey(...) throws while Mocha defines the suite, before the test runs. Load the actual deployed program address from the test setup instead of shipping this placeholder.

it('Is initialized!', async () => {
const transaction = new Transaction().add(
new TransactionInstruction({
keys: [],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Test omits required account

The instruction passes keys: [], but the native handler immediately calls next_account_info. After the program address and setup are fixed, this call still fails with NotEnoughAccountKeys. Create a suitable program-owned account and include it in the instruction, or change the handler's account requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-issue PR does not reference an issue a maintainer has accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tokens/token-swap: add the native (Solana Native) flavor — example currently ships anchor-only

1 participant