Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-486][ECO-538] Add balance update support #9

Merged
merged 12 commits into from
Oct 3, 2023
Merged

[ECO-486][ECO-538] Add balance update support #9

merged 12 commits into from
Oct 3, 2023

Conversation

alnoki
Copy link
Member

@alnoki alnoki commented Oct 2, 2023

econia-labs/econia#511

  • Delete deprecated Rust README
  • Add processor for MarketAccounts table handles
  • Add missing postgres views to old migrations
  • Add processor for MarketAccount table entries
  • Add view function to join on table handle for balance updates

To verify

health_check_port: 8085
server_config:
  processor_config:
    type: econia_transaction_processor
    econia_address: 0xc0de11113b427d35ece1d8991865a941c0578b0f349acabbe9753863c24109ff
  postgres_connection_string: postgres://econia:econia@postgres:5432/econia
  indexer_grpc_data_service_address: https://grpc.testnet.aptoslabs.com:443
  indexer_grpc_http2_ping_interval_in_secs: 60
  indexer_grpc_http2_ping_timeout_in_secs: 10
  auth_token: <my_auth_token>
  starting_version: 683453241

Run Docker compose instructions per the docs site then see http://0.0.0.0:3001/balance_updates

@alnoki alnoki changed the title [ECO-486][ECO-535] Add balance update support [ECO-486][ECO-538] Add balance update support Oct 2, 2023
.expect("Failed to parse MarketAccounts");
market_account_handles.push(MarketAccountHandle {
user: resource.address.clone(),
handle: data["map"]["handle"].as_str().unwrap().to_string(),
Copy link

@elliottdehn elliottdehn Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment on econia-labs/econia#511, I think we should include a time field here so that we can know what time a user's accounts were first active.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Included as of 306a3cb

Comment on lines 689 to 703
base_total: u64::from_str(data["base_total"].as_str().unwrap())
.unwrap()
.into(),
base_available: u64::from_str(data["base_available"].as_str().unwrap())
.unwrap()
.into(),
base_ceiling: u64::from_str(data["base_ceiling"].as_str().unwrap())
.unwrap()
.into(),
quote_total: u64::from_str(data["quote_total"].as_str().unwrap())
.unwrap()
.into(),
quote_available: u64::from_str(
data["quote_available"].as_str().unwrap(),
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid the use of unwrap just like above in the code. I'd add a function just like opt_value_to_{string,bool,big_decimal} but for a u64 from str just for this (opt_value_str_to_u64) that just returns a result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwraps removed per 306a3cb

Comment on lines 671 to 682
let table_data = write.data.as_ref().expect("No WriteTableItem data");
if table_data.value_type
!= format!("{}::user::MarketAccount", econia_address)
{
continue;
}
let table_key: serde_json::Value = serde_json::from_str(&table_data.key)
.expect("Failed to parse market account ID");
let market_account_id =
u128::from_str(table_key.as_str().unwrap()).unwrap();
let data: serde_json::Value = serde_json::from_str(&table_data.value)
.expect("Failed to parse MarketAccount");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid unwrap here as well. See opt_value_to_* for examples on how to do that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwraps removed per 306a3cb

.expect("Failed to parse MarketAccount");
balance_updates.push(BalanceUpdate {
txn_version: txn_version.into(),
handle: write.handle.to_string(),
Copy link

@elliottdehn elliottdehn Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include the user id associated with the handle in this table, so that events notifying balance updates contain it--the handle is kind of useless without referring to the other table. Since the ID associated with the handle doesn't change, we can save the look-up and make things easier for people by including it in this table too.

You might need to pre-process all of the WriteResource events.

Suggesting this as it feeds into/affects ECO-578

Copy link
Member Author

@alnoki alnoki Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is accounted for via the balance_updates natural join view (see the associated up.sql migration).

market_account_handles:

Screenshot 2023-10-02 at 15 41 49

balance_updates_by_handle:

Screenshot 2023-10-02 at 15 44 25

balance_updates (the natural join view function):

balance_updates

Copy link

@elliottdehn elliottdehn Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't attach notifications to that view though (at least I don't think so) and cross-referencing every event to that view in order to get the user address is both expensive (it's a full table join) and unnecessary (the data can be included).

Are you expecting that people will perform a full table join every time they get a balance update event in order to get the user's address?

There might be tens of thousands of balance updates in that table, and remember people will be running this is GCP or whatever provider so they'll be getting billed for that work every trade.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are multiple possible solutions here, none of which require that the processor cache extra data or read from the database:

  1. Create a trigger that simply sends out balance_update_by_handle entries verbatim. In this case, the listener simply has to look up their handle in market_account_handles one time and then knows which events to retain in the future
  2. Create a trigger that sends out the equivalent of an entry in balance_updates, but without doing a join. The function needs to take a balance_updates_by_handle entry and lookup the handle in market_account_handles, then substitute in the corresponding user field. This can be done purely in migrations, though it will require that the db insertion does market account handle insertions before balance updates (currently they happen after per the build_transaction().

I lean toward (1) as it eliminates internal dependencies and is a simple trigger

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with option 1 then I think we should just get rid of the natural join view. I don't think it has such value or utility that it should be part of the API surface area.

@alnoki alnoki marked this pull request as draft October 2, 2023 22:20
@alnoki alnoki marked this pull request as ready for review October 2, 2023 22:42
@alnoki alnoki marked this pull request as draft October 2, 2023 22:50
@alnoki alnoki marked this pull request as ready for review October 2, 2023 22:54
@alnoki alnoki merged commit 1a68026 into econia Oct 3, 2023
1 of 5 checks passed
@alnoki alnoki deleted the ECO-486 branch October 3, 2023 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants