af_pyth_wrapper/
update.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use af_ptbuilder::{ptbuilder, Argument, ProgrammableTransactionBuilder};
use af_sui_pkg_sdk::af_sui_types::ObjectArg;
use af_sui_pkg_sdk::ObjectId;

/// Groups the [ProgrammableTransactionBuilder] arguments for updating AfOracle `PriceFeed`s.
pub struct UpdateAfOracleArguments {
    /// `state::State` object from Pyth.
    pub pyth_state: Argument,
    /// `wrapper::PythWrapper` object.
    pub pyth_wrapper: Argument,
    /// Mapping from AfOracle `PriceFeedStorage` -> Pyth `PriceInfoObject`
    pub pfs_to_source: Vec<(Argument, Argument)>,
}

#[extension_traits::extension(pub trait ProgrammableTransactionBuilderExt)]
impl ProgrammableTransactionBuilder {
    /// Construct the PTB arguments to be used in [`update_af_oracle_pyth_feed`].
    ///
    /// This is separate from [`update_af_oracle_pyth_feed`] since the caller may want to use some of the
    /// arguments created here in subsequent PTB calls.
    ///
    /// [`update_af_oracle_pyth_feed`]: ProgrammableTransactionBuilderExt::update_af_oracle_pyth_feed
    fn update_af_oracle_pyth_feed_args(
        &mut self,
        pyth_state: ObjectArg,
        pyth_wrapper: ObjectArg,
        pfs_to_source: Vec<(ObjectArg, ObjectArg)>,
    ) -> Result<UpdateAfOracleArguments, af_ptbuilder::Error> {
        ptbuilder!(self {
            input obj pyth_state;
            input obj pyth_wrapper;
        });
        let mut vars = UpdateAfOracleArguments {
            pfs_to_source: vec![],
            pyth_state,
            pyth_wrapper,
        };

        for (pfs, pio) in pfs_to_source {
            ptbuilder!(self {
                input obj pfs;
                input obj pio;
            });
            vars.pfs_to_source.push((pfs, pio));
        }

        Ok(vars)
    }

    /// Add a PythWrapper update to the PTB being built.
    fn update_af_oracle_pyth_feed(
        &mut self,
        pyth_wrapper_pkg: ObjectId,
        arguments: UpdateAfOracleArguments,
    ) -> Result<(), af_ptbuilder::Error> {
        let UpdateAfOracleArguments {
            pfs_to_source,
            pyth_wrapper,
            pyth_state,
        } = arguments;
        ptbuilder!(self {
            package pyth_wrapper_pkg;
            input obj clock: ObjectArg::CLOCK_IMM;
        });
        for (price_feed_storage, price_info_object) in pfs_to_source {
            ptbuilder!(self {
                pyth_wrapper_pkg::wrapper::update_price_feed(
                    price_feed_storage,
                    pyth_wrapper,
                    pyth_state,
                    price_info_object,
                    clock,
                );
            });
        }
        Ok(())
    }
}