solana-labs / solana-program-library

A collection of Solana programs maintained by Solana Labs
https://solanalabs.com
Apache License 2.0
3.5k stars 2.04k forks source link

[spl-token-client] - 'ProgramClient' trait requires rpc client to have 'static lifetime #2989

Closed shravanshetty1 closed 2 years ago

shravanshetty1 commented 2 years ago

Trying to write a test for my smart contract, encountering issues with "spl-token-client" crate.

extern crate solana_program;
extern crate solana_sdk;
extern crate spl_token_client;

use solana_client::rpc_client::RpcClient;
use solana_program::native_token::LAMPORTS_PER_SOL;
use solana_sdk::signature::{Keypair, Signer};
use spl_token_client::client::{ProgramClient, ProgramRpcClient, ProgramRpcClientSendTransaction};
use spl_token_client::token::Token;
use std::sync::Arc;

#[tokio::test]
async fn basic() -> Result<(), Box<dyn std::error::Error>> {
    let existing_token_mint = Keypair::new();
    let client = RpcClient::new("http://localhost:8899".to_string());
    client.request_airdrop(&existing_token_mint.pubkey(), LAMPORTS_PER_SOL * 10)?;

    let mint_account_1 = Keypair::new();
    let program_client = Arc::new(ProgramRpcClient::new(
        &client,
        ProgramRpcClientSendTransaction,
    ));

    Token::create_mint(
        program_client,
        Keypair::from_bytes(&existing_token_mint.to_bytes())?,
        &mint_account_1,
        &existing_token_mint.pubkey(),
        None,
        9,
    )
    .await?;

    Ok(())
}
warning: unused import: `ProgramClient`
 --> tests/basic.rs:8:32
  |
8 | use spl_token_client::client::{ProgramClient, ProgramRpcClient, ProgramRpcClientSendTransaction};
  |                                ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0597]: `client` does not live long enough
  --> tests/basic.rs:20:9
   |
20 |         &client,
   |         ^^^^^^^ borrowed value does not live long enough
...
25 |         program_client,
   |         -------------- cast requires that `client` is borrowed for `'static`
...
35 | }
   | - `client` dropped here while still borrowed
shravanshetty1 commented 2 years ago

https://github.com/shravanshetty1/tokenitis - source code for reference

askibin commented 2 years ago

you can try to use lazy_static. Add it to Cargo.toml and then in you test file in the global scope:

use lazy_static::lazy_static;
lazy_static! {
  pub static ref client: RpcClient = RpcClient::new("http://localhost:8899".to_string());
}

let me know if that worked

shravanshetty1 commented 2 years ago

@askibin I opened the issue assuming it wasnt intended - if it is intended, I can close the issue