Skip to content

Commit

Permalink
DEEP Supply Endpoint (#20675)
Browse files Browse the repository at this point in the history
## Description 

DEEP Supply Endpoint: /deep_supply

Returns: 9997742312719287 (represents 9,997,742,312 DEEP)

## Test plan 

How did you test the new or updated feature?

Tested locally

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [x] Indexer: Deepbook Indexer
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
leecchh authored Dec 18, 2024
1 parent 0599c49 commit 715edf0
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions crates/sui-deepbook-indexer/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ pub const LEVEL2_MODULE: &str = "pool";
pub const LEVEL2_FUNCTION: &str = "get_level2_ticks_from_mid";
pub const DEEPBOOK_PACKAGE_ID: &str =
"0x2c8d603bc51326b8c13cef9dd07031a408a48dddb541963357661df5d3204809";
pub const DEEP_TOKEN_PACKAGE_ID: &str =
"0xdeeb7a4662eec9f2f3def03fb937a663dddaa2e215b8078a284d026b7946c270";
pub const DEEP_TREASURY_ID: &str =
"0x032abf8948dda67a271bcc18e776dbbcfb0d58c8d288a700ff0d5521e57a1ffe";
pub const DEEP_SUPPLY_MODULE: &str = "deep";
pub const DEEP_SUPPLY_FUNCTION: &str = "total_supply";
pub const DEEP_SUPPLY_PATH: &str = "/deep_supply";

pub fn run_server(socket_address: SocketAddr, state: PgDeepbookPersistent) -> JoinHandle<()> {
tokio::spawn(async move {
Expand Down Expand Up @@ -80,6 +87,7 @@ pub(crate) fn make_router(state: PgDeepbookPersistent) -> Router {
.route(TRADES_PATH, get(trades))
.route(ASSETS_PATH, get(assets))
.route(SUMMARY_PATH, get(summary))
.route(DEEP_SUPPLY_PATH, get(deep_supply))
.with_state(state)
}

Expand Down Expand Up @@ -1118,6 +1126,81 @@ async fn orderbook(
Ok(Json(result))
}

/// DEEP total supply
async fn deep_supply() -> Result<Json<u64>, DeepBookError> {
let sui_client = SuiClientBuilder::default().build(SUI_MAINNET_URL).await?;
let mut ptb = ProgrammableTransactionBuilder::new();

let deep_treasury_object_id = ObjectID::from_hex_literal(DEEP_TREASURY_ID)?;
let deep_treasury_object: SuiObjectResponse = sui_client
.read_api()
.get_object_with_options(
deep_treasury_object_id,
SuiObjectDataOptions::full_content(),
)
.await?;
let deep_treasury_data: &SuiObjectData =
deep_treasury_object
.data
.as_ref()
.ok_or(DeepBookError::InternalError(
"Incorrect Treasury ID".to_string(),
))?;

let deep_treasury_ref: ObjectRef = (
deep_treasury_data.object_id,
deep_treasury_data.version,
deep_treasury_data.digest,
);

let deep_treasury_input = CallArg::Object(ObjectArg::ImmOrOwnedObject(deep_treasury_ref));
ptb.input(deep_treasury_input)?;

let package = ObjectID::from_hex_literal(DEEP_TOKEN_PACKAGE_ID).map_err(|e| {
DeepBookError::InternalError(format!("Invalid deep token package ID: {}", e))
})?;
let module = DEEP_SUPPLY_MODULE.to_string();
let function = DEEP_SUPPLY_FUNCTION.to_string();

ptb.command(Command::MoveCall(Box::new(ProgrammableMoveCall {
package,
module,
function,
type_arguments: vec![],
arguments: vec![Argument::Input(0)],
})));

let builder = ptb.finish();
let tx = TransactionKind::ProgrammableTransaction(builder);

let result = sui_client
.read_api()
.dev_inspect_transaction_block(SuiAddress::default(), tx, None, None, None)
.await?;

let mut binding = result.results.ok_or(DeepBookError::InternalError(
"No results from dev_inspect_transaction_block".to_string(),
))?;

let total_supply = &binding
.first_mut()
.ok_or(DeepBookError::InternalError(
"No return values for total supply".to_string(),
))?
.return_values
.first_mut()
.ok_or(DeepBookError::InternalError(
"No total supply data found".to_string(),
))?
.0;

let total_supply_value: u64 = bcs::from_bytes(total_supply).map_err(|_| {
DeepBookError::InternalError("Failed to deserialize total supply".to_string())
})?;

Ok(Json(total_supply_value))
}

async fn get_net_deposits(
Path((asset_ids, timestamp)): Path<(String, String)>,
State(state): State<PgDeepbookPersistent>,
Expand Down

0 comments on commit 715edf0

Please sign in to comment.