ameliatastic / seahorse-lang

Write Anchor-compatible Solana programs in Python
Apache License 2.0
327 stars 48 forks source link

Cannot find value in this scope error #84

Open denimcodes opened 1 year ago

denimcodes commented 1 year ago

So I ran on this issue:

image

When I use new created account as authority in the same instruction, I get this error.

Here's the code:

class Pool(Account):
  bump: u8
  authority: Pubkey

@instruction
def init_pool(
  authority: Signer, 
  empty_pool: Empty[Pool], 
  token_mint: TokenMint, 
  token_vault: Empty[TokenAccount]
):
  pool = empty_pool.init(
    payer=authority,
    seeds=["pool", token_mint]
  )
  token_vault.init(
    payer=authority,
    mint=token_mint,
    authority=pool,
    associated=True
  )
mcintyre94 commented 1 year ago

Hey, thanks for reporting this! I think this is a dupe of #39 which is unresolved, and should have the same (a bit hacky) workaround:

@instruction
def init_pool(
    authority: Signer,
    pool: Empty[Pool],
    token_mint: TokenMint,
    token_vault: Empty[TokenAccount]
):
    pool = pool.init(
        payer=authority,
        seeds=["pool", token_mint]
    )
    token_vault.init(
        payer=authority,
        mint=token_mint,
        authority=pool,
        associated=True
    )

Here I've renamed empty_pool to pool, and initialise it to the same variable.

The way we do init calls sequentially is a bit confusing and doesn't quite map to what Anchor is doing. There's more detail in my comments on #39 but basically we can't pass anything created in the function (in your case the pool variable) into an init call.

But you also can't use authority = empty_pool, because the Seahorse compiler won't let you use an empty account as an authority that might not have been initialised

So the workaround makes both Anchor and Seahorse happy: Anchor knows what pool is because it's an argument to the function, Seahorse can see that you're initialising it before using it as authority.

denimcodes commented 1 year ago

Got it. The workaround is working as expected. Thanks.