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 mint constraints and test #562

Merged
merged 38 commits into from
Aug 15, 2021

Conversation

Henry-E
Copy link
Contributor

@Henry-E Henry-E commented Jul 30, 2021

Added new mint constraints so that it's possible to initialize a PDA mint account in the same way as token accounts.

New Constraint Tokens / Keywords

mint_decimals: a u8 corresponding to the decimals argument of the InitializeMint function in the SPL token program
mint_authority: a program account, account info or cpi account that will be assigned as the mint's authority. Corresponds to mint_authority argument of the InitializeMint function in the SPL token program

Example usage

pub struct TestTokenSeedsInit<'info> {
    #[account(
        init,
        mint_decimals = 6 as u8,
        mint_authority = authority,
        seeds = [b"my-mint-seed".as_ref(), &[mint_bump]],
        payer = authority,
        space = Mint::LEN,
    )]
    pub mint: CpiAccount<'info, Mint>,
    #[account(
        init,
        token = mint,
        authority = authority,
        seeds = [b"my-token-seed".as_ref(), &[token_bump]],
        payer = authority,
        space = TokenAccount::LEN,
    )]
    pub my_pda: CpiAccount<'info, TokenAccount>,
    pub authority: AccountInfo<'info>,
    pub system_program: AccountInfo<'info>,
    pub rent: Sysvar<'info, Rent>,
    pub token_program: AccountInfo<'info>,
}

@secretshardul
Copy link
Contributor

secretshardul commented Aug 5, 2021

EDIT: Resolved, solution in comments below

Getting an error when I import your repo to use this feature.

Finished release [optimized] target(s) in 8.27s
Running: /Users/hp/.local/share/solana/install/releases/1.7.3/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /Users/hp/Documents/solana-season-hack/cyclos/target/deploy/cyclos.so

To deploy this program:
  $ solana program deploy /Users/hp/Documents/solana-season-hack/cyclos/target/deploy/cyclos.so
thread 'main' panicked at 'Code not parseable: Error("Invalid attribute")', lang/syn/src/idl/file.rs:351:62
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::result::unwrap_failed
   3: anchor_syn::idl::file::parse_account_derives
   4: anchor_syn::idl::file::parse
   5: anchor::extract_idl
   6: anchor::build_cwd
   7: anchor::build
   8: anchor::main

Here's my code

    #[account(
        init,
        mint_decimals = 6 as u8,
        mint_authority = open_orders_authority,
        seeds = [b"Mint".as_ref(), &[_mint_bump]],
        payer = authority,
        space = size_of::<Mint>()
    )]
    pub mint: CpiAccount<'info, Mint>,

Imported your repo in Cargo.toml in this way:

[dependencies]
anchor-lang = { git = "https://github.com/Henry-E/anchor", branch = "add_mint_constraints" }
anchor-spl = { git = "https://github.com/Henry-E/anchor", branch = "add_mint_constraints" }

Travis build points to an error, is this the reason why I'm getting this issue?

error: this expression borrows a reference (`&anchor_lang::prelude::Pubkey`) that is immediately dereferenced by the compiler
   --> spl/src/token.rs:139:9
    |
139 |         &authority,
    |         ^^^^^^^^^^ help: change this to: `authority`

@armaniferrante
Copy link
Member

armaniferrante commented Aug 5, 2021

Getting an error when I import your repo to use this feature.

Finished release [optimized] target(s) in 8.27s
Running: /Users/hp/.local/share/solana/install/releases/1.7.3/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /Users/hp/Documents/solana-season-hack/cyclos/target/deploy/cyclos.so

To deploy this program:
  $ solana program deploy /Users/hp/Documents/solana-season-hack/cyclos/target/deploy/cyclos.so
thread 'main' panicked at 'Code not parseable: Error("Invalid attribute")', lang/syn/src/idl/file.rs:351:62
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::result::unwrap_failed
   3: anchor_syn::idl::file::parse_account_derives
   4: anchor_syn::idl::file::parse
   5: anchor::extract_idl
   6: anchor::build_cwd
   7: anchor::build
   8: anchor::main

Here's my code

    #[account(
        init,
        mint_decimals = 6 as u8,
        mint_authority = open_orders_authority,
        seeds = [b"Mint".as_ref(), &[_mint_bump]],
        payer = authority,
        space = size_of::<Mint>()
    )]
    pub mint: CpiAccount<'info, Mint>,

Imported your repo in Cargo.toml in this way:

[dependencies]
anchor-lang = { git = "https://github.com/Henry-E/anchor", branch = "add_mint_constraints" }
anchor-spl = { git = "https://github.com/Henry-E/anchor", branch = "add_mint_constraints" }

Travis build points to an error, is this the reason why I'm getting this issue?

error: this expression borrows a reference (`&anchor_lang::prelude::Pubkey`) that is immediately dereferenced by the compiler
   --> spl/src/token.rs:139:9
    |
139 |         &authority,
    |         ^^^^^^^^^^ help: change this to: `authority`

You'll need to use the CLI compiled with these changes as well.

@Henry-E
Copy link
Contributor Author

Henry-E commented Aug 5, 2021

Yeh, you need to use cargo run --manifest-path ../anchor/Cargo.toml test or something similar when running a local version of the CLI

@secretshardul
Copy link
Contributor

Yeh, you need to use cargo run --manifest-path ../anchor/Cargo.toml test or something similar when running a local version of the CLI

Resolved, thanks a lot

@Henry-E
Copy link
Contributor Author

Henry-E commented Aug 6, 2021

Updated the pull request using cargo fmt so that it will pass the build requirement.

Also reduced the number of system program calls from 3 to 1. The original code from the associated token program splits call into three because it can't be sure if the account has already been initialized with some lamports. We don't have that same requirement.

@armaniferrante With regards to open orders initializing, I don't actually feel comfortable enough with all of the different syntax being used to access account variables to add it. Apologies!

@Henry-E
Copy link
Contributor Author

Henry-E commented Aug 6, 2021

It says the builds are still failing after running cargo fmt is there anything else it might be?

@secretshardul
Copy link
Contributor

The second macro could be extended to create associated token accounts? This will be more convenient than spl_associated_token_account::create_associated_token_account. Or there's already a way?

@Henry-E
Copy link
Contributor Author

Henry-E commented Aug 9, 2021

omg, all the builds finally passed

if self.mint_decimals.is_none() {
return Err(ParseError::new(
c.span(),
"mint decimals must be provided before authority",
Copy link
Member

Choose a reason for hiding this comment

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

Debatable whether this should be enforced. 🤷

Copy link
Member

Choose a reason for hiding this comment

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

It made more sense with the old token = <mint>, authority = <auth> syntax, since having the token first made things more clear. But with the new syntax, it's not really necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, there's no reason to enforce this.

@armaniferrante armaniferrante merged commit fef207d into coral-xyz:master Aug 15, 2021
@Henry-E
Copy link
Contributor Author

Henry-E commented Aug 15, 2021

We will have to add in the fallback option eventually for the case where a PDA account already has some lamports in it.

Maybe the fallback could just transfer any lamports inside the account to the authority? Then there should be 0 lamports inside the account when it is passed to createAccount.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants