-
Notifications
You must be signed in to change notification settings - Fork 54
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
PDAs as token account authorities are throwing an unusual error #39
Comments
Hmm okay I see what's going on here. First of all, here's a version of your playground that builds and that I think does what you want: https://beta.solpg.io/635e831677ea7f12846aef09 The change I made is that I do Now this is what it's compiling to (just showing the important parts of the accounts struct for your instruction): #[derive(Accounts)]
# [instruction (noise : u64 , signal : u64 , metadata : String)]
pub struct InitializeStatic<'info> {
<snipped stuff>,
# [account (
init ,
space = <snip>,
payer = payer,
seeds = <snip> ,
bump
)]
pub static_acc: Box<Account<'info, dot::program::StaticShell>>,
# [account (
init ,
payer = payer ,
seeds = <snip>,
bump ,
token :: mint = mint ,
token :: authority = static_acc
)]
pub token: Box<Account<'info, TokenAccount>>,
} In Anchor this structure is independent of the instruction itself. It can receive some data passed into the instruction (like metadata in your seeds is fine), but it can't receive stuff constructed directly in your instruction, like So then the question is should we allow you to pass IMO the best approach here is to leave Seahorse as-is and require you to explicitly reassign My concern is otherwise you could compile code like this: @instruction
def initialize_static(
payer: Signer,
static_acc: Empty[StaticShell],
mint: TokenMint,
token: Empty[TokenAccount],
noise: u64,
signal: u64,
metadata: str,
):
# oops I forgot to init static_acc
token_account = token.init(
payer=payer,
seeds=['token-account'],
mint=mint,
authority=static_acc
)
# currently these would error too
# static_acc.noise = noise
# static_acc.signal = signal
# static_acc.metadata = metadata
# static_acc.bump = static_acc.bump And you'd have @ameliatastic Would be curious if you have any thoughts on this! |
Okay but then what about the |
Hmm nope you're right, that should be So basically, there's definitely a compiler issue here. Summarising:
In terms of possible fixes:
|
Okay I'm pretty sure this works as a workaround: https://beta.solpg.io/635e9d2077ea7f12846aef0a You can set |
Well this does work indeed, but can you get the bump before the |
Yep you can. Unfortunately this is again just down to knowledge about how Anchor works, but basically the initialization actually happens before any of your other code, when you do This is also why |
hmm that's interesting, well i didn't know seahorse processed the |
Also I was facing a similar issue with token account bumps but i applied the same thing as you did with other accounts and it kinda works |
Whew that was hella confusing but hopefully this discussion would be of help to others who face this issue, thanks! |
Yep this is one of the areas where the Anchor control flow leaks through a bit. Mainly that you can't define a new variable then use it in an Unless @ameliatastic disagrees I think we should leave Seahorse as-is here. Summarising everything:
|
Wow! This issue goes surprisingly deep, and it's definitely one of the more difficult things to resolve about Seahorse. So I'm glad that you two did the detective work here. I think in v1, I had tried to add a limitation that made it so that all
Also, it's funny to see that simply redeclaring the same name |
Yep I remember that limitation, it might have stopped the trick to store the
I think this is a nice solution, and covers my concern about forgetting to initialize.
I'm not at all familiar with native Solana TBH! One thing I'd be concerned about is whether we'd lose the Anchor IDL if we did this? Probably also a lot of security things that become concerns of the Seahorse compiler and currently aren't because we use Anchor's init syntax. I do agree that this would best match the way Seahorse is presenting the instruction, but at the same time I'm not sure anyone has really been limited by the way Anchor requires it to happen at the beginning.
Agree! Took a bit of decoding to figure out that'd work lol. It feels quite weird that Rust will happily change the type of a variable like that, a lot of grumpy static languages don't and would be way more annoying to compile to! |
Would need to look into it. I think it wouldn't change things too badly, but you're right that there's probably nobody being limited by our sticking to Anchor inits. On the topic of mixing inits and regular code, I just realized that it might lead to some security problems: class Safe(Account):
important_data: u64
@instruction
def do_a_little_trolling(
signer: Signer,
alternate: Signer,
safe: Empty[Safe],
use_alternate: bool
):
# This does nothing
if use_alternate:
signer = alternate
safe.init(
payer = signer,
seeds = ['safe', signer]
) Here you have some code that looks like two signers agree on whose pubkey is used as the signer seed for the Forcing inits to be at the start of an instruction solves this instance, but there's still some room for trickery. |
Hmm good point! Definitely agreed that's an issue. I can imagine less malicious examples that are just confusing too. Like conditionally setting seeds and things like that. Maybe the answer is inits first + allow empty as authority (preferably with the check it's initialized first) then? So the original code in this issue would work, and you'd need to use a new name so you can still do |
I noticed this example had some errors, related to #39 - `hello` is re-assigned to the initialised account, so that it can be used as `authority` - `bump` is stored at the start of the instruction, before this re-assign - Similarly `hello.bump` is stored at the start of `say_hello`, doing `hello.bump` inside `mint.mint` was erroring With these changes it compiles correctly: https://beta.solpg.io/636d27b177ea7f12846aef21
I tried passing PDAs as token account authorities but I get an unusual error like this

the issue wasn't happening in seahorse v0.1.6
try this out yourself on Solana playground..
https://beta.solpg.io/635d590977ea7f12846aef06
The text was updated successfully, but these errors were encountered: