This token-swap
project is rewritten using Anchor, forked from solana-program-library/token-swap.
-
Unpack & Pack:
Using macros like
#[account]
and#[derive(AnchorSerialize, AnchorDeserialize)]
can simply implement theunpack
andpack
methods, which can reduce chunky of boliplate code. -
Configuable Constraints:
The configurable validations are a great enhancement compared to the original code using lots of comparasion. Below is an example:
#[account( token::token_program = token_swap.token_program_id, token::mint = pool_mint.key(), constraint = host_fee_account.owner != authority.key() @ SwapError::InvalidOwner )] pub host_fee_account: Option<InterfaceAccount<'info, TokenAccount>>,
-
Client friendly:
It automatically generates client types & even methods for typescript client.
-
Other features:
init
can combine create the account via system CPI and initialize the account in one step.#[derive(InitSpace)]
can measure the initial size of the account.seeds
Configurable PDA supportCpiContext
Strongly typed for CPI calling.
Javascript is the default client language, unfortunately, it's power is not well utilized in some partss the original code. Below are the notable changes (tracked here: #2):
-
Misuse of async javascript
-
It's a rather common misconception that main typescript client executes the transaction one after another (sequentially). In fact, that can be perfectly submitted in batches. You know the reason why solana is faster is because it supports parallel processing.
-
sleep
shouldn't be used, as javascript is event-driven, there is always an async way to be informed when a transaction is done, usingsleep
to wait for a fixed-period of time can make the execution much ineffective, because the actual time of transaction execution can be vary for different traffic.
-
-
Fix a bug on
account validation
I detected an business logic issue, details are here: #3
-
Pre-commit hooks
This project uses pre-commit
to format the code before each commits #4.
- build:
anchor build
- unit test & proptest:
cargo test
- integration test:
anchor test
(make sureyarn install
has been executed to ensure all javascript/typescript dependencies are installed) - run two above tests in one go:
yarn test
This project is licensed under the MIT License. See the LICENSE file for details.