Repository navigation
fix: add native flavor for token-swap example - #777
SuperPiIot wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Every commit in a Solana Foundation repository needs signature verification, and these are unverified:
f90b941fix: 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.
|
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 |
[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
SummaryAdds a native Rust program, package manifest, and TypeScript test for the token-swap example.
Reviews (1) · Last reviewed commit: "fix: add native flavor for token-swap ex..." · Reviewed by Greptile |
| msg!("Hello, Native Token Swap!"); | ||
|
|
||
| Ok(()) |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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.
| edition = "2021" | |
| edition = "2021" | |
| [lib] | |
| crate-type = ["cdylib", "lib"] |
| [package] | ||
| name = "token-swap-native" | ||
| version = "0.1.0" | ||
| edition = "2021" |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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>'); |
There was a problem hiding this comment.
| it('Is initialized!', async () => { | ||
| const transaction = new Transaction().add( | ||
| new TransactionInstruction({ | ||
| keys: [], |
There was a problem hiding this comment.
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.
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