af_pyth_wrapper/
update.rs

1use af_ptbuilder::{Argument, ProgrammableTransactionBuilder, ptbuilder};
2use af_sui_pkg_sdk::ObjectId;
3use af_sui_pkg_sdk::af_sui_types::ObjectArg;
4
5/// Groups the [ProgrammableTransactionBuilder] arguments for updating AfOracle `PriceFeed`s.
6pub struct UpdateAfOracleArguments {
7    /// `state::State` object from Pyth.
8    pub pyth_state: Argument,
9    /// `wrapper::PythWrapper` object.
10    pub pyth_wrapper: Argument,
11    /// Mapping from AfOracle `PriceFeedStorage` -> Pyth `PriceInfoObject`
12    pub pfs_to_source: Vec<(Argument, Argument)>,
13}
14
15#[extension_traits::extension(pub trait ProgrammableTransactionBuilderExt)]
16impl ProgrammableTransactionBuilder {
17    /// Construct the PTB arguments to be used in [`update_af_oracle_pyth_feed`].
18    ///
19    /// This is separate from [`update_af_oracle_pyth_feed`] since the caller may want to use some of the
20    /// arguments created here in subsequent PTB calls.
21    ///
22    /// [`update_af_oracle_pyth_feed`]: ProgrammableTransactionBuilderExt::update_af_oracle_pyth_feed
23    fn update_af_oracle_pyth_feed_args(
24        &mut self,
25        pyth_state: ObjectArg,
26        pyth_wrapper: ObjectArg,
27        pfs_to_source: Vec<(ObjectArg, ObjectArg)>,
28    ) -> Result<UpdateAfOracleArguments, af_ptbuilder::Error> {
29        ptbuilder!(self {
30            input obj pyth_state;
31            input obj pyth_wrapper;
32        });
33        let mut vars = UpdateAfOracleArguments {
34            pfs_to_source: vec![],
35            pyth_state,
36            pyth_wrapper,
37        };
38
39        for (pfs, pio) in pfs_to_source {
40            ptbuilder!(self {
41                input obj pfs;
42                input obj pio;
43            });
44            vars.pfs_to_source.push((pfs, pio));
45        }
46
47        Ok(vars)
48    }
49
50    /// Add a PythWrapper update to the PTB being built.
51    fn update_af_oracle_pyth_feed(
52        &mut self,
53        pyth_wrapper_pkg: ObjectId,
54        arguments: UpdateAfOracleArguments,
55    ) -> Result<(), af_ptbuilder::Error> {
56        let UpdateAfOracleArguments {
57            pfs_to_source,
58            pyth_wrapper,
59            pyth_state,
60        } = arguments;
61        ptbuilder!(self {
62            package pyth_wrapper_pkg;
63            input obj clock: ObjectArg::CLOCK_IMM;
64        });
65        for (price_feed_storage, price_info_object) in pfs_to_source {
66            ptbuilder!(self {
67                pyth_wrapper_pkg::wrapper::update_price_feed(
68                    price_feed_storage,
69                    pyth_wrapper,
70                    pyth_state,
71                    price_info_object,
72                    clock,
73                );
74            });
75        }
76        Ok(())
77    }
78}