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

fix: historical queries for account balances #653

Merged
merged 12 commits into from
Sep 2, 2021

Conversation

TarikGul
Copy link
Member

@TarikGul TarikGul commented Sep 1, 2021

closes: #634

This fixes historical queries for /accounts/{accountId}/balance-info.

@TarikGul TarikGul changed the title Tarik fix historical account balances fix: historical queries for account balances Sep 1, 2021
@TarikGul
Copy link
Member Author

TarikGul commented Sep 1, 2021

I need to confirm that this works for all runtimes before block ~1500000 (Kusama), currently they all work except when querying block 1400000's runtime.

@TarikGul
Copy link
Member Author

TarikGul commented Sep 2, 2021

All historical runtimes are now covered.

The initial issue was present in two cases: For example, when querying Kusama, the following 2 groups of runtimes have different storage formats.

v1054 --> v1050
v1049 --> ... (earliest runtime)


Presently we use 2 formats to get account storage information:

a) api.query.system.accounts.at(hash, address)
b) api.query.balances.account.at(hash, address)

But for runtime v1054 --> v1050 the api requires a historicalApi via await api.at(${BlockHash})

historicalApi.query.system.account(address)

And for runtime v1049 --> ...(earliest runtime) also requires a historicalApi

historicalApi.query.balances.freeBalance(address),
historicalApi.query.balances.locks(address),
historicalApi.query.balances.reservedBalance(address),
historicalApi.query.system.accountNonce(address),

height: header.number.toNumber().toString(10),
};

if (locks && free && reserved && nonce) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this is necessary, could we just do null if one of these fields doesn't exist?

Copy link
Member Author

Choose a reason for hiding this comment

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

When you say 'do null' do you mean a null response for the key value pair? Or do you mean just check one value and then throw the error.

Here I am just mimicking the error response logic that is in the existing service to keep it consistent with what response/errors it reports. So if we change this to something different it might be worth updating the rest as well.

Copy link
Member Author

@TarikGul TarikGul Sep 2, 2021

Choose a reason for hiding this comment

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

Ahh, I think I see what you are saying, It only makes sense to really check for one value. Will update

Copy link
Contributor

@emostov emostov left a comment

Choose a reason for hiding this comment

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

LGTM % a few questions where we might be able to improve performance. Also, if these derives https://github.com/polkadot-js/api/blob/master/packages/api-derive/src/balances/account.ts start supporting .at I wonder if it would be more robust to start using them in parts of this service in the future

TarikGul and others added 4 commits September 2, 2021 09:04
Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com>
…ritytech/substrate-api-sidecar into tarik-fix-historical-account-balances
@TarikGul
Copy link
Member Author

TarikGul commented Sep 2, 2021

Also, if these derives https://github.com/polkadot-js/api/blob/master/packages/api-derive/src/balances/account.ts start supporting .at I wonder if it would be more robust to start using them in parts of this service in the future

Yea I totally agree with this.

Copy link
Contributor

@maciejhirsz maciejhirsz left a comment

Choose a reason for hiding this comment

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

Looks good to me.

There is a bunch of places where you mock an async function by chaining a Promise.resolve with no value, and I'm not sure it's necessary when a regular async function could be used?

Comment on lines 22 to 25
freeBalance: () =>
Promise.resolve().then(() => {
return polkadotRegistry.createType('Balance', 123456789);
}),
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't that be easier?

Suggested change
freeBalance: () =>
Promise.resolve().then(() => {
return polkadotRegistry.createType('Balance', 123456789);
}),
freeBalance: async () => polkadotRegistry.createType('Balance', 123456789),

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch, yea I definitely agree, will fix it up now.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@TarikGul TarikGul merged commit e9d7d7b into master Sep 2, 2021
@TarikGul TarikGul deleted the tarik-fix-historical-account-balances branch September 2, 2021 18:51
@TarikGul TarikGul mentioned this pull request Sep 2, 2021
This pull request was closed.
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.

Cannot query historical balances on Kusama
4 participants