Skip to content

Commit b3ef74d

Browse files
Create READEM.md
1 parent 69f40ba commit b3ef74d

File tree

1 file changed

+132
-0
lines changed
  • tokens/transfer-hook-transfer-switch/anchor

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Transfer Hook Transfer Switch
2+
3+
This Solana program implements a transfer hook that can be toggled on or off for
4+
any token. It allows you to control whether transfers are allowed for a given wallet.
5+
6+
## Features
7+
8+
- **Initialize Transfer Control**: Set up a wallet with a default transfer status for a specified token mint.
9+
- **Toggle Transfer On/Off**: Enable or disable token transfers for specific token mints.
10+
- **Transfer Hook**: Enforces a transfer hook that checks the transfer status before allowing any token transfers.
11+
12+
## Prerequisites
13+
14+
- Rust and Cargo (for building the Solana program)
15+
- Solana CLI (for interacting with Solana network)
16+
- Anchor Framework (for Solana smart contract development)
17+
- `pnpm` (for managing JavaScript dependencies)
18+
- `solana-bankrun` for testing
19+
20+
## Installation
21+
22+
1. Clone the repository:
23+
```
24+
git clone https://github.com/solana-developers/program-examples.git
25+
cd program-examples/tokens/transfer-hook-transfer-switch/anchor
26+
```
27+
28+
2. Install dependencies:
29+
```
30+
pnpm install
31+
```
32+
33+
## Building the Program
34+
35+
To build the program, run:
36+
37+
```
38+
anchor build
39+
```
40+
41+
## Testing
42+
43+
To run the test suite:
44+
45+
```
46+
anchor test
47+
```
48+
49+
## Program Structure
50+
51+
The program consists of three main instructions:
52+
53+
1. `initialize`: Creates and initializes the transfer switch account.
54+
2. `toggle_transfer`: Toggles the transfer switch on or off.
55+
3. `transfer_hook`: Checks if transfers are allowed before a token transfer.
56+
57+
## Usage
58+
59+
### Initializing the Transfer Switch
60+
61+
```typescript
62+
await program.methods
63+
.initialize()
64+
.accounts({
65+
walletState: walletStatePda,
66+
owner: wallet.publicKey,
67+
tokenMint: tokenMintPublicKey,
68+
systemProgram: anchor.web3.SystemProgram.programId,
69+
})
70+
.rpc();
71+
```
72+
73+
### Toggling the Transfer Switch
74+
75+
```typescript
76+
await program.methods
77+
.toggleTransferSwitch(tokenMintPublicKey)
78+
.accounts({
79+
walletState: walletStatePda,
80+
owner: wallet.publicKey,
81+
})
82+
.rpc();
83+
```
84+
85+
### Using the Transfer Hook
86+
87+
```typescript
88+
await program.methods
89+
.transferTokens(new anchor.BN(amount))
90+
.accounts({
91+
from: fromTokenAccount,
92+
to: toTokenAccount,
93+
senderState: senderWalletStatePda,
94+
owner: ownerPublicKey,
95+
authority: transferAuthority,
96+
tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
97+
})
98+
.rpc();
99+
```
100+
101+
## Account Structure
102+
103+
The `TransferSwitch` account has the following structure:
104+
105+
```rust
106+
pub struct WalletState {
107+
pub owner: Pubkey, // The owner of the wallet
108+
pub transfer_status: Vec<(Pubkey, bool)>, // List of token mints and their transfer status
109+
}
110+
```
111+
112+
## Error Handling
113+
114+
The program defines a custom error:
115+
116+
```rust
117+
#[error_code]
118+
pub enum ErrorCode {
119+
#[msg("Transfers are disabled for this wallet.")]
120+
TransfersDisabled, // Triggered when transfers are disabled
121+
#[msg("Token not initialized for transfer management.")]
122+
TokenNotInitialized, // Triggered when a token mint has not been registered
123+
}
124+
125+
```
126+
127+
This error is thrown when a transfer is attempted while the transfer switch is disabled.
128+
129+
## Security Considerations
130+
131+
- The `authority` of the `TransferSwitch` account has the power to toggle transfers on and off. Ensure this authority is properly managed and secured.
132+
- This program does not implement any access control on who can transfer tokens. It only provides a global on/off switch for all transfers.

0 commit comments

Comments
 (0)