Codigo-io / platform

Código is an AI-Powered Code Generation Platform for blockchain developers and web3 teams that saves development time and increases the security of the code across a variety of blockchains.
https://codigo.ai
11 stars 8 forks source link

Incorrect code generation for creating a Regular Token Account #8

Closed julianzamt closed 1 year ago

julianzamt commented 1 year ago

Describe the bug Not all Token accounts must be ATAs. Some users may prefer the old pattern, where N token accounts from a Mint can be owned by the same wallet. However, using csl_spl_token.initialize_account3 in combination with csl_spl_token.Account generates code as if the account to be initialized is a PDA, but it cannot be, as it must be created beforehand. This is wrong and must be fixed.

Also, a phantasmagoric account is generated, unnecessarilly polluting the accounts array.

To Reproduce

methods:
  - name: create_mint_and_account
    uses:
      - csl_spl_token.initialize_mint2
      - csl_spl_token.initialize_account3
    inputs:
      - name: mint
        type: csl_spl_token.Mint
        solana:
          attributes: [ init ]
      - name: regular_token_account
        type: csl_spl_token.Account
        solana:
          attributes: [ init ]

processor: Wrong "PDA" code

Unnecessary extra seeds args:

pub fn process_create_nft_without_optional_fields(
program_id: &Pubkey,
accounts: &[AccountInfo],
regular_token_account_seed_wallet: Pubkey,
regular_token_account_seed_token_program: Pubkey,
regular_token_account_seed_mint: Pubkey,
    )
// Derive PDAs
let (regular_token_account_pubkey, regular_token_account_bump) = Pubkey::find_program_address(
    &[regular_token_account_seed_wallet.as_ref(), regular_token_account_seed_token_program.as_ref(), 
regular_token_account_seed_mint.as_ref()],
&Pubkey::from_str("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL").unwrap(),
);

And also, PDA creation, which is wrong, but further, it is created as if it is owned by the local program

let space = spl_token::state::Account::LEN;
let rent = Rent::get()?;
let rent_minimum_balance = rent.minimum_balance(space);

invoke_signed(
    &create_account(
    &fee_payer_info.key,
    &regular_token_account_info.key,
    rent_minimum_balance,
    space as u64,
    program_id,
),
&[fee_payer_info.clone(), regular_token_account_info.clone()],
&[&[regular_token_account_seed_wallet.as_ref(), regular_token_account_seed_token_program.as_ref(), regular_token_account_seed_mint.as_ref(), &[regular_token_account_bump]]],
)?;

"Phantasmagoric" unrequired extra account:

let account_info_iter = &mut accounts.iter();
let fee_payer_info = next_account_info(account_info_iter)?;
let mint_info = next_account_info(account_info_iter)?;
let regular_token_account_info = next_account_info(account_info_iter)?;
let system_program_info = next_account_info(account_info_iter)?;
***let account_info = next_account_info(account_info_iter)?;*** ----------------> ???????
let csl_spl_token_v_0_0_0_info = next_account_info(account_info_iter)?;

Expected behavior To generate code for regular account creation.

Código CLI version:

JazielGuerrero commented 1 year ago

The account named account is not a ghost account. Because your ix is using initialize_account3, this ix has an input named account and is coming from there:

  - name: initialize_account3
    solana:
      default-payer: false
    summary: Like InitializeAccount2, but does not require the Rent sysvar to be provided
    inputs:
      - name: account
        type: sol:account_info
        description: The account to initialize.
        solana:
          attributes: [ mut ]
      - name: mint
        type: Mint
        description: The mint this account will be associated with.
      - name: owner
        type: sol:pubkey
        description: The new account's owner/multisignature.
JazielGuerrero commented 1 year ago

I created the following issue: https://github.com/Codigo-io/issues/issues/26 to keep track of the other part of this ticket.