我正在学习Solana Spotify项目的涂料课程,并在YouTube上学习。请告诉我代码会产生这样的错误,但一切都写得正确而清晰。我不知道我忘记在哪里了。
use anchor_lang::prelude::*;
use anchor_lang::solana_program::entrypoint::ProgramResult;
use anchor_spl::token::{self,Token};
use std::mem::size_of;
// This is your program's public key and it will update
// automatically when you build the project.
declare_id!("11111111111111111111111111111111");
#[program]
mod spotify_clone {
use super::*;
pub fn accept_payment(ctx: Context<PayerContext>, data: u64) -> ProgramResult {
let payer_wallet = &mut ctx.accounts.payer_wallet;
payer_wallet.wallet = ctx.accounts.authority.key();
let ix = anchor_lang::solana_program::system_instruction::transfer(
&ctx.accounts.authority.key(),
&ctx.accounts.receiver.key(),
100000000,
);
anchor_lang::solana_program::program::invoke(
&ix,
&[
ctx.accounts.authority.to_account_info(),
ctx.accounts.receiver.to_account_info(),
],
)
}
}
#[derive(Accounts)]
pub struct PayerContext<'info> {
#[account(
init,
seeds = [b"payer".as_ref(), authority.key().as_ref()],
bump,
payer = authority,
space = size_of::<PayerAccount>() + 8,
)]
pub payer_wallet: Account<'info, PayerAccount>,
//Specific param to object
#[account(mut)]
pub receiver: AccountInfo<'info>,
//Specific param to object
//Authority[this is signer who paid transaction fee]
#[account(mut)]
pub authority: Signer<'info>,
pub system_programm: UncheckedAccount <'info>,
//Token Programm
#[account(constraint = token_program.key == &token::ID)]
pub token_program: Program<'info, Token>,
//Clock to the Save Your Time
pub clock: Sysvar<'info,Clock>,
}
#[account]
pub struct PayerAccount{
pub wallet: Pubkey,
}
错误出现在这里。我认为代码中的主要问题在这里:
pub payer_wallet: Account<'info, PayerAccount>,
错误代码E0277:
error[E0277]: the trait bound `PayerContext<'_>: anchor_lang::Accounts<'_>` is not satisfied
--> /src/lib.rs:10:1
|
10 | #[program]
| ^^^^^^^^^^ the trait `anchor_lang::Accounts<'_>` is not implemented for `PayerContext<'_>`
|
note: required by `anchor_lang::context::Context::<'a, 'b, 'c, 'info, T>::new`
第二个错误E0432:
error[E0432]: unresolved import `crate`
--> /src/lib.rs:10:1
|
10 | #[program]
| ^^^^^^^^^^ could not find `__client_accounts_payer_context` in the crate root
|
= note: this error originates in the attribute macro `program` (in Nightly builds, run with -Z macro-backtrace for more info)
第三个错误E0599:
error[E0599]: no method named `exit` found for struct `PayerContext` in the current scope
--> /src/lib.rs:10:1
|
10 | #[program]
| ^^^^^^^^^^ method not found in `PayerContext<'_>`
...
34 | pub struct PayerContext<'info> {
| ------------------------------ method `exit` not found for this
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `exit`, perhaps you need to implement it:
candidate #1: `anchor_lang::AccountsExit`
= note: this error originates in the attribute macro `program` (in Nightly builds, run with -Z macro-backtrace for more info)
尝试添加
#[derive(Accounts)] to PayerContext
这样的
#[derive(Accounts)]
pub struct PayerContext<'info> {}