af_switchboard_wrapper/
update.rs

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