Skip to content
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

add token programs to server #328

Merged
merged 7 commits into from
Jan 16, 2025
Merged

add token programs to server #328

merged 7 commits into from
Jan 16, 2025

Conversation

anihamde
Copy link
Contributor

This PR adds the pulling and caching of token programs for mints to the server side. The token programs are passed in the QuoteSvm object to the caller.

auction-server/src/state.rs Outdated Show resolved Hide resolved
auction-server/api-types/src/opportunity.rs Show resolved Hide resolved
auction-server/src/opportunity/service/get_quote.rs Outdated Show resolved Hide resolved
auction-server/src/state.rs Outdated Show resolved Hide resolved
@danimhr
Copy link
Contributor

danimhr commented Jan 15, 2025

The pre-commit is failing. please take care of it :D

@@ -99,6 +100,7 @@ impl Service<ChainTypeSvm> {
&self,
quote_create: entities::QuoteCreate,
program: &ProgramSvm,
config: &ConfigSvm,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing config, you can call self.get_config

},
)
})
.collect())
}

pub async fn get_token_program(&self, mint: Pubkey) -> anyhow::Result<Pubkey> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should put the cache inside the repo and make a service for get_token_program

auction-server/src/opportunity/service/mod.rs Show resolved Hide resolved
},
)
})
.collect())
}

pub async fn get_token_program(&self, mint: Pubkey) -> anyhow::Result<Pubkey> {
if let Some(program) = self.token_program_cache.read().await.get(&mint) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this part like below:

let token_program = match self.token_program_cache.read().await.get(&mint) {
   Ok(token_program) => token_program,
   None => {
       self
            .rpc_client
            .get_account(&mint)
            .await
            .map_err(|err| {
                tracing::error!(
                    "Failed to retrieve owner program for mint account {mint}: {:?}",
                    err,
                    mint = mint
                );
                anyhow!(
                    "Failed to retrieve owner program for mint account {mint}: {:?}",
                    err,
                    mint = mint
                )
            })?
            .owner;
            self.token_program_cache
            .write()
            .await
            .insert(mint, token_program);
            token_program
   }
};
if !self.accepted_token_programs.contains(&token_program) {
            return Err(anyhow!(
                "Token program {program} for mint account {mint} is not an approved token program",
                program = token_program,
                mint = mint
            ));
        }
        Ok(token_program)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the code above, you basically remove the duplication for checking if the token_program is an accepted address or not.

input: GetTokenProgramInput,
) -> Result<Pubkey, RestError> {
let config = self.get_config(&input.chain_id)?;
let cache_read = self.repo.in_memory_store.token_program_cache.read().await;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to add a functionality in repo fro this. The cache will be dropped automatically if you do so.
I mean like get_token_program.rs
We can have a function to read the cache in the repo instead of getting it here,

@anihamde anihamde merged commit 240b2ba into main Jan 16, 2025
1 check passed
@anihamde anihamde deleted the feat/server-get-token-programs branch January 16, 2025 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants