You can get the balance of an account at any given block hash. For example:
const { nonce, data: balance } = await api.query.system.account.at(HASH, ADDR);
Or, alternatively, you can create a new api
object that corresponds to the state at block with hash HASH
:
let atApi = await api.at(HASH);
const { nonce, data: balance } = await atApi.query.system.account(ADDR);
Knowing this, your question is reduced and answered in two steps:
- First, you need to find the block hash at which the account address nonce number that you are interested in occurred.
- Then, using the above examples, you can read the account balance at that identified block hash.
To find the hash corresponding to the nonce you are interested in, you can either do a linear or binary search, but this entirely depends on which range you want to search. And how you do it exactly the same as described in the above steps:
for (let x = rangeStart; x > rangeStart; x--) {
const nonceAtBlock = (await api.query.system.account.at(currentHash, ADDR)).nonce;
if (nonceAtBlock.eq(7)) {
// found the nonce!
break;
}
// go to the parent block's hash and check that.
const parentHash = (await api.rpc.chain.getBlock(currentHash)).block.header.parentHash;
currentHash = parentHash;
}