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

feat: whitelist and unwhitelist SPL tokens #41

Merged
merged 13 commits into from
Oct 8, 2024
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ The PDA account address (derived from seeds `b"meta"` and canonical bump) is
2f9SLuUNb7TNeM6gzBwT4ZjbL5ZyKzzHg1Ce9yiquEjj
```


# Introduction


This repository hosts the smart contract (program) deployed on the Solana network to enable ZetaChain's cross-chain functionality. It consists of a single program that supports the following actions:

1. Users on the Solana network can send SOL to the program to deposit into ZetaChain, with the option to invoke a ZetaChain EVM contract.
Expand Down Expand Up @@ -112,4 +110,4 @@ brew install gnu-tar
# Put this in ~/.zshrc
export PATH="/usr/local/opt/gnu-tar/libexec/gnubin:$PATH"
```
see https://solana.stackexchange.com/questions/4499/blockstore-error-when-starting-solana-test-validator-on-macos-13-0-1/16319#16319
see https://solana.stackexchange.com/questions/4499/blockstore-error-when-starting-solana-test-validator-on-macos-13-0-1/16319#16319
62 changes: 62 additions & 0 deletions programs/protocol-contracts-solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ pub mod gateway {
Ok(())
}

// whitelisting SPL tokens
pub fn whitelist_spl_mint(_ctx: Context<Whitelist>) -> Result<()> {
Ok(())
}

pub fn unwhitelist_spl_mint(_ctx: Context<Unwhitelist>) -> Result<()> {
Ok(())
}

// deposit SOL into this program and the `receiver` on ZetaChain zEVM
// will get corresponding ZRC20 credit.
// amount: amount of lamports (10^-9 SOL) to deposit
Expand Down Expand Up @@ -351,6 +360,11 @@ pub struct DepositSplToken<'info> {
#[account(seeds = [b"meta"], bump)]
pub pda: Account<'info, Pda>,

#[account(seeds=[b"whitelist", mint_account.key().as_ref()], bump)]
pub whitelist_entry: Account<'info, WhitelistEntry>, // attach whitelist entry to show the mint_account is whitelisted

pub mint_account: Account<'info, Mint>,
Copy link
Member

Choose a reason for hiding this comment

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

What does mint mean?

There should be no minting involved since we lock/unlock SPL tokens?

Copy link
Contributor

Choose a reason for hiding this comment

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

according to description Whitelisting is implemented as a special PDA for each mint account (SPL token id). is needed to create pda for mint account and existence of that pda marks that token is whitelisted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What does mint mean?

There should be no minting involved since we lock/unlock SPL tokens?

mint (short for mint account) is the Solana equivalent for ERC20 contract address.
This is Solana jargon.


pub token_program: Program<'info, Token>,

#[account(mut)]
Expand Down Expand Up @@ -414,6 +428,51 @@ pub struct UpdatePaused<'info> {
pub signer: Signer<'info>,
}

#[derive(Accounts)]
pub struct Whitelist<'info> {
#[account(
init,
space=8,
payer=authority,
seeds=[
b"whitelist",
whitelist_candidate.key().as_ref()
],
bump
)]
pub whitelist_entry: Account<'info, WhitelistEntry>,
pub whitelist_candidate: Account<'info, Mint>,

#[account(mut, seeds = [b"meta"], bump, has_one = authority)]
pub pda: Account<'info, Pda>,
#[account(mut)]
pub authority: Signer<'info>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

It means the signer (the authority) of the Whitelist instruction must be one of the fields in pda struct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes,
the b"meta" PDA contains the authority.


pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Unwhitelist<'info> {
#[account(
mut,
seeds=[
b"whitelist",
whitelist_candidate.key().as_ref()
],
bump,
close = authority,
)]
pub whitelist_entry: Account<'info, WhitelistEntry>,
pub whitelist_candidate: Account<'info, Mint>,

#[account(mut, seeds = [b"meta"], bump, has_one = authority)]
pub pda: Account<'info, Pda>,
#[account(mut)]
pub authority: Signer<'info>,

pub system_program: Program<'info, System>,
}

#[account]
pub struct Pda {
nonce: u64, // ensure that each signature can only be used once
Expand All @@ -423,6 +482,9 @@ pub struct Pda {
deposit_paused: bool,
}

#[account]
pub struct WhitelistEntry {}
Copy link
Contributor

Choose a reason for hiding this comment

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

just curious if is there a pros/cons for whitelisting in 2 following ways:

  1. init/close account
  2. store flag within pda marking if its whitelisted or not

is it because having long lived pda in solana is not efficient if not needed because of rent, or something else?

Copy link
Contributor Author

@brewmaster012 brewmaster012 Oct 8, 2024

Choose a reason for hiding this comment

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

For whitelisting we really need is a mapping like Ethereum address=>bool

But Solana does not have mapping as a data structure like Ethereum mapping.
You'll need to roll your own. Say you want to create a mapping data account
and then store a hashmap or list in the PDA to implement the mapping.
There are two issues:

  1. size of the data structure and therefore the account size could dynamically change;
  2. hard to get deterministic constant access time (key-value lookup)

The PDA for each key-value pair is the idiomatic way to do Ethereum mapping on Solana.
Think of part of the seed as the key (mint account address), and value is the PDA account.

Copy link
Collaborator

Choose a reason for hiding this comment

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

good explanation


#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading
Loading