Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tokens/transfer-hook-transfer-switch/anchor #135

Closed
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
7 changes: 7 additions & 0 deletions tokens/transfer-hook-transfer-switch/anchor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.anchor
.DS_Store
target
**/*.rs.bk
node_modules
test-ledger
.yarn
7 changes: 7 additions & 0 deletions tokens/transfer-hook-transfer-switch/anchor/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.anchor
.DS_Store
target
node_modules
dist
build
test-ledger
18 changes: 18 additions & 0 deletions tokens/transfer-hook-transfer-switch/anchor/Anchor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[toolchain]

[features]
resolution = true
skip-lint = false

[programs.localnet]
anchor = "7tUBaLEw5BVFmqWF8ixfxVSb7WM7CqRTXMmY8kK6uf1N"

[registry]
url = "https://api.apr.dev"

[provider]
cluster = "devnet"
wallet = "~/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
14 changes: 14 additions & 0 deletions tokens/transfer-hook-transfer-switch/anchor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[workspace]
members = [
"programs/*"
]
resolver = "2"

[profile.release]
overflow-checks = true
lto = "fat"
codegen-units = 1
[profile.release.build-override]
opt-level = 3
incremental = false
codegen-units = 1
132 changes: 132 additions & 0 deletions tokens/transfer-hook-transfer-switch/anchor/READEM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Transfer Hook Transfer Switch

This Solana program implements a transfer hook that can be toggled on or off for
any token. It allows you to control whether transfers are allowed for a given wallet.

## Features

- **Initialize Transfer Control**: Set up a wallet with a default transfer status for a specified token mint.
- **Toggle Transfer On/Off**: Enable or disable token transfers for specific token mints.
- **Transfer Hook**: Enforces a transfer hook that checks the transfer status before allowing any token transfers.

## Prerequisites

- Rust and Cargo (for building the Solana program)
- Solana CLI (for interacting with Solana network)
- Anchor Framework (for Solana smart contract development)
- `pnpm` (for managing JavaScript dependencies)
- `solana-bankrun` for testing

## Installation

1. Clone the repository:
```
git clone https://github.com/solana-developers/program-examples.git
cd program-examples/tokens/transfer-hook-transfer-switch/anchor
```

2. Install dependencies:
```
pnpm install
```

## Building the Program

To build the program, run:

```
anchor build
```

## Testing

To run the test suite:

```
anchor test
```

## Program Structure

The program consists of three main instructions:

1. `initialize`: Creates and initializes the transfer switch account.
2. `toggle_transfer`: Toggles the transfer switch on or off.
3. `transfer_hook`: Checks if transfers are allowed before a token transfer.

## Usage

### Initializing the Transfer Switch

```typescript
await program.methods
.initialize()
.accounts({
walletState: walletStatePda,
owner: wallet.publicKey,
tokenMint: tokenMintPublicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
```

### Toggling the Transfer Switch

```typescript
await program.methods
.toggleTransferSwitch(tokenMintPublicKey)
.accounts({
walletState: walletStatePda,
owner: wallet.publicKey,
})
.rpc();
```

### Using the Transfer Hook

```typescript
await program.methods
.transferTokens(new anchor.BN(amount))
.accounts({
from: fromTokenAccount,
to: toTokenAccount,
senderState: senderWalletStatePda,
owner: ownerPublicKey,
authority: transferAuthority,
tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
})
.rpc();
```

## Account Structure

The `TransferSwitch` account has the following structure:

```rust
pub struct WalletState {
pub owner: Pubkey, // The owner of the wallet
pub transfer_status: Vec<(Pubkey, bool)>, // List of token mints and their transfer status
}
```

## Error Handling

The program defines a custom error:

```rust
#[error_code]
pub enum ErrorCode {
#[msg("Transfers are disabled for this wallet.")]
TransfersDisabled, // Triggered when transfers are disabled
#[msg("Token not initialized for transfer management.")]
TokenNotInitialized, // Triggered when a token mint has not been registered
}

```

This error is thrown when a transfer is attempted while the transfer switch is disabled.

## Security Considerations

- The `authority` of the `TransferSwitch` account has the power to toggle transfers on and off. Ensure this authority is properly managed and secured.
- This program does not implement any access control on who can transfer tokens. It only provides a global on/off switch for all transfers.
12 changes: 12 additions & 0 deletions tokens/transfer-hook-transfer-switch/anchor/migrations/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Migrations are an early feature. Currently, they're nothing more than this
// single deploy script that's invoked from the CLI, injecting a provider
// configured from the workspace's Anchor.toml.

const anchor = require("@coral-xyz/anchor");

module.exports = async function (provider) {
// Configure client to use the provider.
anchor.setProvider(provider);

// Add your deploy script here.
};
24 changes: 24 additions & 0 deletions tokens/transfer-hook-transfer-switch/anchor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"license": "ISC",
"scripts": {
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
},
"dependencies": {
"@coral-xyz/anchor": "^0.30.1",
"@project-serum/anchor": "0.26.0",
"@solana/spl-token": "0.4.9"
},
"devDependencies": {
"chai": "^4.3.4",
"mocha": "^9.0.3",
"ts-mocha": "^10.0.0",
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0",
"typescript": "^4.3.5",
"prettier": "^2.6.2",
"solana-bankrun": "^0.3.0",
"anchor-bankrun": "^0.4.0"
}
}
Loading
Loading