-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
.expect("Failed to parse MarketAccounts"); | ||
market_account_handles.push(MarketAccountHandle { | ||
user: resource.address.clone(), | ||
handle: data["map"]["handle"].as_str().unwrap().to_string(), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Included as of 306a3cb
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(), | ||
) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unwraps removed per 306a3cb
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"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(), |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- 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 inmarket_account_handles
one time and then knows which events to retain in the future - Create a trigger that sends out the equivalent of an entry in
balance_updates
, but without doing a join. The function needs to take abalance_updates_by_handle
entry and lookup the handle inmarket_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 thebuild_transaction()
.
I lean toward (1) as it eliminates internal dependencies and is a simple trigger
There was a problem hiding this comment.
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.
econia-labs/econia#511
MarketAccounts
table handlesMarketAccount
table entriesTo verify
Run Docker compose instructions per the docs site then see http://0.0.0.0:3001/balance_updates