Skip to content

Commit

Permalink
Merge pull request tahowallet#428 from tallycash/total-addressable-da…
Browse files Browse the repository at this point in the history
…tabase

Total Addressable Database: Update IndexedDB to use address instead of account where relevant
  • Loading branch information
Gergő Nagy authored Nov 17, 2021
2 parents bb24163 + 6b9eaae commit 1809d30
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
35 changes: 17 additions & 18 deletions background/services/chain/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Transaction = AnyEVMTransaction & {
}

type AccountAssetTransferLookup = {
accountNetwork: AddressNetwork
addressNetwork: AddressNetwork
retrievedAt: UNIXTime
startBlock: bigint
endBlock: bigint
Expand All @@ -30,8 +30,7 @@ export class ChainDatabase extends Dexie {
* Accounts whose transaction and balances should be tracked on a particular
* network.
*
* Keyed by the [account, network name, network chain ID] triplet. Note that
* "account" in this context refers to e.g. an Ethereum address.
* Keyed by the [address, network name, network chain ID] triplet.
*/
private accountsToTrack!: Dexie.Table<
AddressNetwork,
Expand Down Expand Up @@ -75,11 +74,11 @@ export class ChainDatabase extends Dexie {
this.version(1).stores({
migrations: "++id,appliedAt",
accountsToTrack:
"&[account+network.name+network.chainID],account,network.family,network.chainID,network.name",
"&[address+network.name+network.chainID],address,network.family,network.chainID,network.name",
accountAssetTransferLookups:
"++id,[accountNetwork.account+accountNetwork.network.name+accountNetwork.network.chainID],[accountNetwork.account+accountNetwork.network.name+accountNetwork.network.chainID+startBlock],[accountNetwork.account+accountNetwork.network.name+accountNetwork.network.chainID+endBlock],accountNetwork.account,accountNetwork.network.chainID,accountNetwork.network.name,startBlock,endBlock",
"++id,[addressNetwork.address+addressNetwork.network.name+addressNetwork.network.chainID],[addressNetwork.address+addressNetwork.network.name+addressNetwork.network.chainID+startBlock],[addressNetwork.address+addressNetwork.network.name+addressNetwork.network.chainID+endBlock],addressNetwork.address,addressNetwork.network.chainID,addressNetwork.network.name,startBlock,endBlock",
balances:
"++id,account,assetAmount.amount,assetAmount.asset.symbol,network.name,blockHeight,retrievedAt",
"++id,address,assetAmount.amount,assetAmount.asset.symbol,network.name,blockHeight,retrievedAt",
chainTransactions:
"&[hash+network.name],hash,from,[from+network.name],to,[to+network.name],nonce,[nonce+from+network.name],blockHash,blockNumber,network.name,firstSeen,dataSource",
blocks:
Expand Down Expand Up @@ -188,26 +187,26 @@ export class ChainDatabase extends Dexie {
return balanceCandidates.length > 0 ? balanceCandidates[0] : null
}

async addAccountToTrack(accountNetwork: AddressNetwork): Promise<void> {
await this.accountsToTrack.put(accountNetwork)
async addAccountToTrack(addressNetwork: AddressNetwork): Promise<void> {
await this.accountsToTrack.put(addressNetwork)
}

async setAccountsToTrack(
accountAndNetworks: Set<AddressNetwork>
addressesAndNetworks: Set<AddressNetwork>
): Promise<void> {
await this.transaction("rw", this.accountsToTrack, () => {
this.accountsToTrack.clear()
this.accountsToTrack.bulkAdd([...accountAndNetworks])
this.accountsToTrack.bulkAdd([...addressesAndNetworks])
})
}

async getOldestAccountAssetTransferLookup(
accountNetwork: AddressNetwork
addressNetwork: AddressNetwork
): Promise<bigint | null> {
// TODO this is inefficient, make proper use of indexing
const lookups = await this.accountAssetTransferLookups
.where("accountNetwork.account")
.equals(accountNetwork.address)
.where("addressNetwork.address")
.equals(addressNetwork.address)
.toArray()
return lookups.reduce(
(oldestBlock: bigint | null, lookup) =>
Expand All @@ -219,12 +218,12 @@ export class ChainDatabase extends Dexie {
}

async getNewestAccountAssetTransferLookup(
accountNetwork: AddressNetwork
addressNetwork: AddressNetwork
): Promise<bigint | null> {
// TODO this is inefficient, make proper use of indexing
const lookups = await this.accountAssetTransferLookups
.where("accountNetwork.account")
.equals(accountNetwork.address)
.where("addressNetwork.address")
.equals(addressNetwork.address)
.toArray()
return lookups.reduce(
(newestBlock: bigint | null, lookup) =>
Expand All @@ -236,12 +235,12 @@ export class ChainDatabase extends Dexie {
}

async recordAccountAssetTransferLookup(
accountNetwork: AddressNetwork,
addressNetwork: AddressNetwork,
startBlock: bigint,
endBlock: bigint
): Promise<void> {
await this.accountAssetTransferLookups.add({
accountNetwork,
addressNetwork,
startBlock,
endBlock,
retrievedAt: Date.now(),
Expand Down
6 changes: 3 additions & 3 deletions background/services/indexing/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class IndexingDatabase extends Dexie {
migrations: "++id,appliedAt",
prices: "++id,time,[asset1ID+asset2ID]",
balances:
"++id,account,assetAmount.amount,assetAmount.asset.symbol,network.name,blockHeight,retrievedAt",
"++id,address,assetAmount.amount,assetAmount.asset.symbol,network.name,blockHeight,retrievedAt",
tokenLists: "++id,url,retrievedAt",
customAssets:
"&[contractAddress+homeNetwork.name],contractAddress,symbol,homeNetwork.chainId,homeNetwork.name",
Expand All @@ -164,7 +164,7 @@ export class IndexingDatabase extends Dexie {
}

async getLatestAccountBalance(
account: string,
address: string,
network: Network,
asset: FungibleAsset
): Promise<AccountBalance | null> {
Expand All @@ -174,7 +174,7 @@ export class IndexingDatabase extends Dexie {
.above(Date.now() - 7 * 24 * 60 * 60 * 1000)
.filter(
(balance) =>
balance.address === account &&
balance.address === address &&
balance.assetAmount.asset.symbol === asset.symbol &&
balance.network.name === network.name
)
Expand Down

0 comments on commit 1809d30

Please sign in to comment.