-
Notifications
You must be signed in to change notification settings - Fork 7
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
Conversation
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, |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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
}, | ||
) | ||
}) | ||
.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) { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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,
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.