1use af_ptbuilder::ptb;
3use af_sui_types::{
4 Address as SuiAddress,
5 ObjectArg,
6 ObjectId,
7 TransactionKind,
8 encode_base64_default,
9};
10use sui_framework_sdk::object::ID;
11use sui_jsonrpc::api::WriteApiClient;
12
13pub type Result<T> = std::result::Result<T, Error>;
14
15#[derive(thiserror::Error, Debug)]
16pub enum Error {
17 #[error(transparent)]
18 JsonRpcClient(#[from] sui_jsonrpc::error::JsonRpcClientError),
19 #[error("In ProgrammableTransactionBuilder: {0}")]
20 PtBuilder(#[from] af_ptbuilder::Error),
21 #[error(transparent)]
22 FromHex(#[from] hex::FromHexError),
23 #[error("Serializing to BCS: {0}")]
24 Bcs(#[from] bcs::Error),
25 #[error("DevInspectResults.results is None")]
26 DevInspectResults,
27}
28
29pub async fn get_price_info_object_id_from_pyth_state<C>(
34 client: &C,
35 package: ObjectId,
36 price_identifier_hex: String,
37 pyth_state: ObjectArg,
38) -> Result<ObjectId>
39where
40 C: WriteApiClient + Sync,
41{
42 let price_identifier_bytes = &hex::decode(price_identifier_hex.replace("0x", ""))?;
43
44 let inspect_tx = ptb!(
45 package pyth: package;
46
47 input obj pyth_state;
48 input pure price_identifier_bytes;
49
50 pyth::state::get_price_info_object_id(pyth_state, price_identifier_bytes);
51 );
52
53 let mut results = {
54 let tx_bytes = encode_base64_default(bcs::to_bytes(
55 &TransactionKind::ProgrammableTransaction(inspect_tx),
56 )?);
57 let resp = client
58 .dev_inspect_transaction_block(
59 SuiAddress::ZERO, tx_bytes,
61 None,
62 None,
63 None,
64 )
65 .await?;
66 resp.results.ok_or(Error::DevInspectResults)?
67 };
68 let sui_exec_result = results.swap_remove(0);
69 let mut return_values = sui_exec_result.return_values;
70 let (bytes, _sui_type_tag) = return_values.swap_remove(0);
71 let id: ID = bcs::from_bytes(&bytes)?;
72 Ok(id.bytes)
73}