Skip to content

Commit

Permalink
kta module, naming cleanup
Browse files Browse the repository at this point in the history
asset_account renamed to kta, created separate module for kta functionality.

kta module supports get_many to fetch many kta_keys
  • Loading branch information
madninja committed Jun 13, 2024
1 parent e106389 commit 3cb4715
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 154 deletions.
111 changes: 20 additions & 91 deletions helium-lib/src/asset.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
dao::Dao,
entity_key::{self, AsEntityKey},
keypair::{serde_opt_pubkey, serde_pubkey, Keypair, Pubkey},
keypair::{serde_opt_pubkey, serde_pubkey, Pubkey},
kta,
result::{DecodeError, Error, Result},
settings::{DasClient, DasSearchAssetsParams, Settings},
};
Expand All @@ -11,100 +12,29 @@ use serde::{Deserialize, Serialize};
use solana_sdk::bs58;
use std::{collections::HashMap, result::Result as StdResult, str::FromStr};

pub mod account_cache {
use super::*;
use crate::keypair::VoidKeypair;
use std::sync::{Arc, OnceLock, RwLock};

static CACHE: OnceLock<AccountCache> = OnceLock::new();

struct AccountCache {
program: anchor_client::Program<Arc<VoidKeypair>>,
cache: RwLock<HashMap<Pubkey, helium_entity_manager::KeyToAssetV0>>,
}

impl AccountCache {
fn new(settings: &Settings) -> Result<Self> {
let anchor_client = settings.mk_anchor_client(Keypair::void())?;
let program = anchor_client.program(helium_entity_manager::id())?;
let cache = RwLock::new(HashMap::new());
Ok(Self { program, cache })
}

async fn get(&self, asset_key: &Pubkey) -> Result<helium_entity_manager::KeyToAssetV0> {
if let Some(account) = self
.cache
.read()
.expect("cache read lock poisoned")
.get(asset_key)
{
return Ok(account.clone());
}

let asset_account = self
.program
.account::<helium_entity_manager::KeyToAssetV0>(*asset_key)
.await?;
self.cache
.write()
.expect("cache write lock poisoned")
.insert(*asset_key, asset_account.clone());
Ok(asset_account)
}
}

pub fn init(settings: &Settings) -> Result<()> {
let _ = CACHE.set(AccountCache::new(settings)?);
Ok(())
}

pub async fn for_asset(asset_key: &Pubkey) -> Result<helium_entity_manager::KeyToAssetV0> {
let cache = CACHE
.get()
.ok_or_else(|| anchor_client::ClientError::AccountNotFound)?;
cache.get(asset_key).await
}
}

pub async fn account_for_entity_key<E>(
entity_key: &E,
) -> Result<helium_entity_manager::KeyToAssetV0>
where
E: AsEntityKey,
{
let asset_key = Dao::Hnt.key_to_asset_key(entity_key);
account_for_asset(&asset_key).await
}

pub async fn account_for_asset(asset_key: &Pubkey) -> Result<helium_entity_manager::KeyToAssetV0> {
account_cache::for_asset(asset_key).await
}

pub async fn for_entity_key<E>(settings: &Settings, entity_key: &E) -> Result<Asset>
where
E: AsEntityKey,
{
let asset_account = account_for_entity_key(entity_key).await?;
get(settings, &asset_account).await
let kta = kta::for_entity_key(entity_key).await?;
for_kta(settings, &kta).await
}

pub async fn get(
pub async fn for_kta(
settings: &Settings,
asset_account: &helium_entity_manager::KeyToAssetV0,
kta: &helium_entity_manager::KeyToAssetV0,
) -> Result<Asset> {
let jsonrpc = settings.mk_jsonrpc_client()?;
let asset_responase: Asset = jsonrpc.get_asset(&asset_account.asset).await?;
let asset_responase: Asset = jsonrpc.get_asset(&kta.asset).await?;
Ok(asset_responase)
}

pub async fn get_with_proof(
pub async fn for_kta_with_proof(
settings: &Settings,
asset_account: &helium_entity_manager::KeyToAssetV0,
kta: &helium_entity_manager::KeyToAssetV0,
) -> Result<(Asset, AssetProof)> {
let (asset, asset_proof) = futures::try_join!(
get(settings, asset_account),
proof::get(settings, asset_account)
)?;
let (asset, asset_proof) =
futures::try_join!(for_kta(settings, kta), proof::get(settings, kta))?;
Ok((asset, asset_proof))
}

Expand Down Expand Up @@ -133,11 +63,10 @@ pub mod proof {

pub async fn get(
settings: &Settings,
asset_account: &helium_entity_manager::KeyToAssetV0,
kta: &helium_entity_manager::KeyToAssetV0,
) -> Result<AssetProof> {
let jsonrpc = settings.mk_jsonrpc_client()?;
let asset_proof_response: AssetProof =
jsonrpc.get_asset_proof(&asset_account.asset).await?;
let asset_proof_response: AssetProof = jsonrpc.get_asset_proof(&kta.asset).await?;

Ok(asset_proof_response)
}
Expand All @@ -146,8 +75,8 @@ pub mod proof {
where
E: AsEntityKey,
{
let asset_account = account_for_entity_key(entity_key).await?;
get(settings, &asset_account).await
let kta = kta::for_entity_key(entity_key).await?;
get(settings, &kta).await
}
}

Expand Down Expand Up @@ -238,7 +167,7 @@ pub struct AssetProof {
}

impl Asset {
pub fn account_key(&self) -> Result<Pubkey> {
pub fn kta_key(&self) -> Result<Pubkey> {
if let Some(creator) = self.creators.get(1) {
return Ok(creator.address);
}
Expand All @@ -259,12 +188,12 @@ impl Asset {
helium_entity_manager::KeySerialization::B58
};
let entity_key = entity_key::from_string(entity_key_str, key_serialization)?;
let asset_key = Dao::Hnt.key_to_asset_key(&entity_key);
Ok(asset_key)
let kta_key = Dao::Hnt.entity_key_to_kta_key(&entity_key);
Ok(kta_key)
}

pub async fn asset_account(&self) -> Result<helium_entity_manager::KeyToAssetV0> {
account_for_asset(&self.account_key()?).await
pub async fn get_kta(&self) -> Result<helium_entity_manager::KeyToAssetV0> {
kta::get(&self.kta_key()?).await
}
}

Expand Down
17 changes: 7 additions & 10 deletions helium-lib/src/dao.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::{
entity_key::AsEntityKey,
keypair::Pubkey,
programs::{MPL_BUBBLEGUM_PROGRAM_ID, TOKEN_METADATA_PROGRAM_ID},
result::Result,
entity_key::AsEntityKey, keypair::Pubkey, programs::TOKEN_METADATA_PROGRAM_ID, result::Result,
token::Token,
};
use helium_anchor_gen::{data_credits, helium_entity_manager, helium_sub_daos, lazy_distributor};
Expand Down Expand Up @@ -75,13 +72,13 @@ impl Dao {

pub fn merkle_tree_authority(&self, merkle_tree: &Pubkey) -> Pubkey {
let (tree_authority, _ta_bump) =
Pubkey::find_program_address(&[merkle_tree.as_ref()], &MPL_BUBBLEGUM_PROGRAM_ID);
Pubkey::find_program_address(&[merkle_tree.as_ref()], &mpl_bubblegum::ID);
tree_authority
}

pub fn bubblegum_signer(&self) -> Pubkey {
let (bubblegum_signer, _bs_bump) =
Pubkey::find_program_address(&[b"collection_cpi"], &MPL_BUBBLEGUM_PROGRAM_ID);
Pubkey::find_program_address(&[b"collection_cpi"], &mpl_bubblegum::ID);
bubblegum_signer
}

Expand All @@ -93,7 +90,7 @@ impl Dao {
key
}

pub fn key_to_asset_key<E: AsEntityKey + ?Sized>(&self, entity_key: &E) -> Pubkey {
pub fn entity_key_to_kta_key<E: AsEntityKey + ?Sized>(&self, entity_key: &E) -> Pubkey {
let hash = Sha256::digest(entity_key.as_entity_key());
let (key, _) = Pubkey::find_program_address(
&[b"key_to_asset", self.key().as_ref(), hash.as_ref()],
Expand Down Expand Up @@ -170,7 +167,7 @@ impl SubDao {
key
}

pub fn escrow_account_key(&self, delegated_dc_key: &Pubkey) -> Pubkey {
pub fn escrow_key(&self, delegated_dc_key: &Pubkey) -> Pubkey {
let (key, _) = Pubkey::find_program_address(
&[b"escrow_dc_account", delegated_dc_key.as_ref()],
&data_credits::id(),
Expand Down Expand Up @@ -217,12 +214,12 @@ impl SubDao {
key
}

pub fn asset_key_to_receipient_key(&self, asset_key: &Pubkey) -> Pubkey {
pub fn receipient_key_from_kta(&self, kta: &helium_entity_manager::KeyToAssetV0) -> Pubkey {
let (key, _) = Pubkey::find_program_address(
&[
b"recipient",
self.lazy_distributor_key().as_ref(),
asset_key.as_ref(),
kta.asset.as_ref(),
],
&lazy_distributor::id(),
);
Expand Down
2 changes: 1 addition & 1 deletion helium-lib/src/dc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub async fn delegate<C: Clone + Deref<Target = impl Signer> + GetPubkey>(
sub_dao: subdao.key(),
owner: keypair.pubkey(),
from_account: Token::Dc.associated_token_adress(&keypair.pubkey()),
escrow_account: subdao.escrow_account_key(&delegated_data_credits),
escrow_account: subdao.escrow_key(&delegated_data_credits),
payer: keypair.pubkey(),
associated_token_program: anchor_spl::associated_token::ID,
token_program: anchor_spl::token::ID,
Expand Down
Loading

0 comments on commit 3cb4715

Please sign in to comment.