ameliatastic / seahorse-lang

Write Anchor-compatible Solana programs in Python
Apache License 2.0
313 stars 43 forks source link

Can't use account fields in seed array #79

Open firefly-sol opened 1 year ago

firefly-sol commented 1 year ago

Context

I wanted to have a tokenMint signed for by a program, so it uses a protocol as the mint and freeze authority

Issue

When trying to pass in the protocol and its bump as signer, I got an error message

# random
# Built with Seahorse v0.2.5

from seahorse.prelude import *

declare_id("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS")

class Protocol(Account):
    bump: u8

@instruction
def init_protocol(signer: Signer, protocol: Empty[Protocol]):
    protocol = protocol.init(payer=signer, seeds=["protocol"])
    bump = Pubkey.find_program_address(["protocol"])[1]

    protocol.bump = bump

@instruction
def init_mint(signer: Signer, mint: Empty[TokenMint], protocol: Protocol):
    mint = mint.init(payer=signer, authority=protocol, decimals=u8(6))

@instruction
def use_bump(
    signer: Signer,
    protocol: Protocol,
    mint: TokenMint,
    destination_token_account: TokenAccount,
):

    bump = protocol.bump
    mint.mint(
        authority=protocol,
        to=destination_token_account,
        amount=u64(100000),
        signer=["protocol", protocol.bump],
    )

output

Error: anchor build -p bugreport failed:

This is most likely a bug in the Seahorse compiler!

If you want to help the project, you can report this:
  - on the Seahorse Discord (http://discord.gg/4sFzH5pus8)
  - or as a Github issue (https://github.com/ameliatastic/seahorse-lang/issues).

Thanks!

   Compiling bugreport v0.1.0 (/Users/purpleparakeet/work/bugreport/programs/bugreport)
error[E0609]: no field `bump` on type `seahorse_util::Mutable<LoadedProtocol<'info, '_>>`
  --> programs/bugreport/src/dot/program.rs:84:26
   |
84 |                 protocol.bump.to_le_bytes().as_ref(),
   |                          ^^^^ unknown field

For more information about this error, try `rustc --explain E0609`.
error: could not compile `bugreport` due to previous error

but when I assign protocol.bump to a variable and pass it in the same way, I get no such message

# random
# Built with Seahorse v0.2.5

from seahorse.prelude import *

declare_id("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS")

class Protocol(Account):
    bump: u8

@instruction
def init_protocol(signer: Signer, protocol: Empty[Protocol]):
    protocol = protocol.init(payer=signer, seeds=["protocol"])
    bump = Pubkey.find_program_address(["protocol"])[1]

    protocol.bump = bump

@instruction
def init_mint(signer: Signer, mint: Empty[TokenMint], protocol: Protocol):
    mint = mint.init(payer=signer, authority=protocol, decimals=u8(6))

@instruction
def use_bump(
    signer: Signer,
    protocol: Protocol,
    mint: TokenMint,
    destination_token_account: TokenAccount,
):

    bump = protocol.bump
    mint.mint(
        authority=protocol,
        to=destination_token_account,
        amount=u64(100000),
        signer=["protocol", bump],
    )

I think this behavior is surprising, so I'm filing it as a bug.