|
1 | 1 | // Copyright (c) Mysten Labs, Inc.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -// TODO remove after the functions are implemented |
5 |
| -#![allow(unused_variables)] |
6 |
| -#![allow(dead_code)] |
7 |
| - |
| 4 | +use super::governance_api_v2::GovernanceReadApiV2; |
| 5 | +use crate::indexer_reader::IndexerReader; |
8 | 6 | use async_trait::async_trait;
|
9 |
| -use fastcrypto::encoding::Base64; |
10 |
| -use jsonrpsee::core::RpcResult; |
11 |
| -use jsonrpsee::RpcModule; |
12 |
| - |
13 |
| -use sui_json::SuiJsonValue; |
14 |
| -use sui_json_rpc::api::TransactionBuilderServer; |
15 |
| -use sui_json_rpc::SuiRpcModule; |
16 |
| -use sui_json_rpc_types::{ |
17 |
| - RPCTransactionRequestParams, SuiTransactionBlockBuilderMode, SuiTypeTag, TransactionBlockBytes, |
18 |
| -}; |
19 |
| -use sui_open_rpc::Module; |
20 |
| -use sui_types::base_types::{ObjectID, SuiAddress}; |
21 |
| -use sui_types::sui_serde::BigInt; |
22 |
| - |
23 |
| -use crate::store::PgIndexerStoreV2; |
| 7 | +use move_core_types::language_storage::StructTag; |
| 8 | +use sui_json_rpc::transaction_builder_api::TransactionBuilderApi; |
| 9 | +use sui_json_rpc_types::{SuiObjectDataOptions, SuiObjectResponse}; |
| 10 | +use sui_transaction_builder::DataReader; |
| 11 | +use sui_types::base_types::{ObjectID, ObjectInfo, SuiAddress}; |
| 12 | +use sui_types::object::Object; |
24 | 13 |
|
25 | 14 | pub(crate) struct TransactionBuilderApiV2 {
|
26 |
| - pg_store: PgIndexerStoreV2, |
| 15 | + inner: IndexerReader, |
27 | 16 | }
|
28 | 17 |
|
29 | 18 | impl TransactionBuilderApiV2 {
|
30 |
| - pub fn new(pg_store: PgIndexerStoreV2) -> Self { |
31 |
| - Self { pg_store } |
| 19 | + #[allow(clippy::new_ret_no_self)] |
| 20 | + pub fn new(inner: IndexerReader) -> TransactionBuilderApi { |
| 21 | + TransactionBuilderApi::new_with_data_reader(std::sync::Arc::new(Self { inner })) |
32 | 22 | }
|
33 | 23 | }
|
34 | 24 |
|
35 | 25 | #[async_trait]
|
36 |
| -impl TransactionBuilderServer for TransactionBuilderApiV2 { |
37 |
| - async fn transfer_object( |
38 |
| - &self, |
39 |
| - signer: SuiAddress, |
40 |
| - object_id: ObjectID, |
41 |
| - gas: Option<ObjectID>, |
42 |
| - gas_budget: BigInt<u64>, |
43 |
| - recipient: SuiAddress, |
44 |
| - ) -> RpcResult<TransactionBlockBytes> { |
45 |
| - unimplemented!() |
46 |
| - } |
47 |
| - |
48 |
| - async fn transfer_sui( |
49 |
| - &self, |
50 |
| - signer: SuiAddress, |
51 |
| - sui_object_id: ObjectID, |
52 |
| - gas_budget: BigInt<u64>, |
53 |
| - recipient: SuiAddress, |
54 |
| - amount: Option<BigInt<u64>>, |
55 |
| - ) -> RpcResult<TransactionBlockBytes> { |
56 |
| - unimplemented!() |
57 |
| - } |
58 |
| - |
59 |
| - async fn pay( |
| 26 | +impl DataReader for TransactionBuilderApiV2 { |
| 27 | + async fn get_owned_objects( |
60 | 28 | &self,
|
61 |
| - signer: SuiAddress, |
62 |
| - input_coins: Vec<ObjectID>, |
63 |
| - recipients: Vec<SuiAddress>, |
64 |
| - amounts: Vec<BigInt<u64>>, |
65 |
| - gas: Option<ObjectID>, |
66 |
| - gas_budget: BigInt<u64>, |
67 |
| - ) -> RpcResult<TransactionBlockBytes> { |
68 |
| - unimplemented!() |
69 |
| - } |
70 |
| - |
71 |
| - async fn pay_sui( |
| 29 | + address: SuiAddress, |
| 30 | + object_type: StructTag, |
| 31 | + ) -> Result<Vec<ObjectInfo>, anyhow::Error> { |
| 32 | + let stored_objects = self |
| 33 | + .inner |
| 34 | + .get_owned_objects_in_blocking_task( |
| 35 | + address, |
| 36 | + Some(object_type.to_canonical_string()), |
| 37 | + None, |
| 38 | + 50, // Limit the number of objects returned to 50 |
| 39 | + ) |
| 40 | + .await?; |
| 41 | + |
| 42 | + stored_objects |
| 43 | + .into_iter() |
| 44 | + .map(|object| { |
| 45 | + let object = Object::try_from(object)?; |
| 46 | + let object_ref = object.compute_object_reference(); |
| 47 | + let info = ObjectInfo::new(&object_ref, &object); |
| 48 | + Ok(info) |
| 49 | + }) |
| 50 | + .collect::<Result<Vec<_>, _>>() |
| 51 | + } |
| 52 | + |
| 53 | + async fn get_object_with_options( |
72 | 54 | &self,
|
73 |
| - signer: SuiAddress, |
74 |
| - input_coins: Vec<ObjectID>, |
75 |
| - recipients: Vec<SuiAddress>, |
76 |
| - amounts: Vec<BigInt<u64>>, |
77 |
| - gas_budget: BigInt<u64>, |
78 |
| - ) -> RpcResult<TransactionBlockBytes> { |
79 |
| - unimplemented!() |
80 |
| - } |
81 |
| - |
82 |
| - async fn pay_all_sui( |
83 |
| - &self, |
84 |
| - signer: SuiAddress, |
85 |
| - input_coins: Vec<ObjectID>, |
86 |
| - recipient: SuiAddress, |
87 |
| - gas_budget: BigInt<u64>, |
88 |
| - ) -> RpcResult<TransactionBlockBytes> { |
89 |
| - unimplemented!() |
90 |
| - } |
91 |
| - |
92 |
| - async fn publish( |
93 |
| - &self, |
94 |
| - sender: SuiAddress, |
95 |
| - compiled_modules: Vec<Base64>, |
96 |
| - dep_ids: Vec<ObjectID>, |
97 |
| - gas: Option<ObjectID>, |
98 |
| - gas_budget: BigInt<u64>, |
99 |
| - ) -> RpcResult<TransactionBlockBytes> { |
100 |
| - unimplemented!() |
101 |
| - } |
102 |
| - |
103 |
| - async fn split_coin( |
104 |
| - &self, |
105 |
| - signer: SuiAddress, |
106 |
| - coin_object_id: ObjectID, |
107 |
| - split_amounts: Vec<BigInt<u64>>, |
108 |
| - gas: Option<ObjectID>, |
109 |
| - gas_budget: BigInt<u64>, |
110 |
| - ) -> RpcResult<TransactionBlockBytes> { |
111 |
| - unimplemented!() |
112 |
| - } |
113 |
| - |
114 |
| - async fn split_coin_equal( |
115 |
| - &self, |
116 |
| - signer: SuiAddress, |
117 |
| - coin_object_id: ObjectID, |
118 |
| - split_count: BigInt<u64>, |
119 |
| - gas: Option<ObjectID>, |
120 |
| - gas_budget: BigInt<u64>, |
121 |
| - ) -> RpcResult<TransactionBlockBytes> { |
122 |
| - unimplemented!() |
123 |
| - } |
124 |
| - |
125 |
| - async fn merge_coin( |
126 |
| - &self, |
127 |
| - signer: SuiAddress, |
128 |
| - primary_coin: ObjectID, |
129 |
| - coin_to_merge: ObjectID, |
130 |
| - gas: Option<ObjectID>, |
131 |
| - gas_budget: BigInt<u64>, |
132 |
| - ) -> RpcResult<TransactionBlockBytes> { |
133 |
| - unimplemented!() |
134 |
| - } |
135 |
| - |
136 |
| - async fn move_call( |
137 |
| - &self, |
138 |
| - signer: SuiAddress, |
139 |
| - package_object_id: ObjectID, |
140 |
| - module: String, |
141 |
| - function: String, |
142 |
| - type_arguments: Vec<SuiTypeTag>, |
143 |
| - rpc_arguments: Vec<SuiJsonValue>, |
144 |
| - gas: Option<ObjectID>, |
145 |
| - gas_budget: BigInt<u64>, |
146 |
| - tx_builder_mode: Option<SuiTransactionBlockBuilderMode>, |
147 |
| - ) -> RpcResult<TransactionBlockBytes> { |
148 |
| - unimplemented!() |
149 |
| - } |
150 |
| - |
151 |
| - async fn batch_transaction( |
152 |
| - &self, |
153 |
| - signer: SuiAddress, |
154 |
| - params: Vec<RPCTransactionRequestParams>, |
155 |
| - gas: Option<ObjectID>, |
156 |
| - gas_budget: BigInt<u64>, |
157 |
| - tx_builder_mode: Option<SuiTransactionBlockBuilderMode>, |
158 |
| - ) -> RpcResult<TransactionBlockBytes> { |
159 |
| - unimplemented!() |
160 |
| - } |
161 |
| - |
162 |
| - async fn request_add_stake( |
163 |
| - &self, |
164 |
| - signer: SuiAddress, |
165 |
| - coins: Vec<ObjectID>, |
166 |
| - amount: Option<BigInt<u64>>, |
167 |
| - validator: SuiAddress, |
168 |
| - gas: Option<ObjectID>, |
169 |
| - gas_budget: BigInt<u64>, |
170 |
| - ) -> RpcResult<TransactionBlockBytes> { |
171 |
| - unimplemented!() |
172 |
| - } |
173 |
| - |
174 |
| - async fn request_withdraw_stake( |
175 |
| - &self, |
176 |
| - signer: SuiAddress, |
177 |
| - staked_sui: ObjectID, |
178 |
| - gas: Option<ObjectID>, |
179 |
| - gas_budget: BigInt<u64>, |
180 |
| - ) -> RpcResult<TransactionBlockBytes> { |
181 |
| - unimplemented!() |
182 |
| - } |
183 |
| -} |
184 |
| - |
185 |
| -impl SuiRpcModule for TransactionBuilderApiV2 { |
186 |
| - fn rpc(self) -> RpcModule<Self> { |
187 |
| - self.into_rpc() |
188 |
| - } |
189 |
| - |
190 |
| - fn rpc_doc_module() -> Module { |
191 |
| - sui_json_rpc::api::TransactionBuilderOpenRpc::module_doc() |
| 55 | + object_id: ObjectID, |
| 56 | + options: SuiObjectDataOptions, |
| 57 | + ) -> Result<SuiObjectResponse, anyhow::Error> { |
| 58 | + let result = self |
| 59 | + .inner |
| 60 | + .get_object_read_in_blocking_task(object_id) |
| 61 | + .await?; |
| 62 | + Ok((result, options).try_into()?) |
| 63 | + } |
| 64 | + |
| 65 | + async fn get_reference_gas_price(&self) -> Result<u64, anyhow::Error> { |
| 66 | + let epoch_info = GovernanceReadApiV2::new(self.inner.clone()) |
| 67 | + .get_epoch_info(None) |
| 68 | + .await?; |
| 69 | + Ok(epoch_info |
| 70 | + .reference_gas_price |
| 71 | + .ok_or_else(|| anyhow::anyhow!("missing latest reference_gas_price"))?) |
192 | 72 | }
|
193 | 73 | }
|
0 commit comments