Skip to content

Commit ed1de76

Browse files
committed
indexer: implement transaction builder api for indexer v2
1 parent 3c68f3a commit ed1de76

File tree

7 files changed

+66
-178
lines changed

7 files changed

+66
-178
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sui-indexer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ sui-types.workspace = true
4242
sui-protocol-config.workspace = true
4343
telemetry-subscribers.workspace = true
4444
sui-rest-api.workspace = true
45+
sui-transaction-builder.workspace = true
4546

4647
move-core-types.workspace = true
4748
move-bytecode-utils.workspace = true

crates/sui-indexer/src/apis/governance_api_v2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl GovernanceReadApiV2 {
3535
Self { inner }
3636
}
3737

38-
async fn get_epoch_info(&self, epoch: Option<EpochId>) -> Result<EpochInfo, IndexerError> {
38+
pub async fn get_epoch_info(&self, epoch: Option<EpochId>) -> Result<EpochInfo, IndexerError> {
3939
match self
4040
.inner
4141
.spawn_blocking(move |this| this.get_epoch_info(epoch))

crates/sui-indexer/src/apis/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub(crate) use indexer_api_v2::IndexerApiV2;
99
pub(crate) use move_utils::MoveUtilsApi;
1010
pub(crate) use read_api::ReadApi;
1111
pub(crate) use transaction_builder_api::TransactionBuilderApi;
12+
pub(crate) use transaction_builder_api_v2::TransactionBuilderApiV2;
1213
pub(crate) use write_api::WriteApi;
1314

1415
mod coin_api;
Lines changed: 56 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,73 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

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;
86
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;
2413

2514
pub(crate) struct TransactionBuilderApiV2 {
26-
pg_store: PgIndexerStoreV2,
15+
inner: IndexerReader,
2716
}
2817

2918
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 }))
3222
}
3323
}
3424

3525
#[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(
6028
&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(
7254
&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"))?)
19272
}
19373
}

crates/sui-indexer/src/indexer_v2.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::apis::IndexerApiV2;
4+
use crate::apis::{IndexerApiV2, TransactionBuilderApiV2};
55
use crate::errors::IndexerError;
66
use crate::indexer_reader::IndexerReader;
77
use crate::metrics::IndexerMetrics;
@@ -103,6 +103,7 @@ pub async fn build_json_rpc_server(
103103
// TODO: Register modules here
104104

105105
builder.register_module(IndexerApiV2::new(reader.clone()))?;
106+
builder.register_module(TransactionBuilderApiV2::new(reader.clone()))?;
106107
// builder.register_module()...
107108

108109
let default_socket_addr: SocketAddr = SocketAddr::new(

crates/sui-json-rpc/src/transaction_builder_api.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ impl TransactionBuilderApi {
3333
let reader = Arc::new(AuthorityStateDataReader::new(state));
3434
Self(TransactionBuilder::new(reader))
3535
}
36+
37+
pub fn new_with_data_reader(data_reader: Arc<dyn DataReader + Sync + Send>) -> Self {
38+
Self(TransactionBuilder::new(data_reader))
39+
}
3640
}
3741

3842
pub struct AuthorityStateDataReader(Arc<dyn StateRead>);

0 commit comments

Comments
 (0)