From 83672e2494c841757ff97f4e6917e1566d193dce Mon Sep 17 00:00:00 2001 From: valentine Date: Thu, 4 Jul 2024 03:08:10 +0300 Subject: [PATCH] hasStakingAssetBalance should check notes on a specific address index --- .../src/view-service/transaction-planner/index.ts | 2 +- packages/storage/src/indexed-db/index.ts | 8 ++++++-- packages/types/src/indexed-db.ts | 2 +- packages/wasm/crate/src/storage.rs | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/services/src/view-service/transaction-planner/index.ts b/packages/services/src/view-service/transaction-planner/index.ts index b2452ba06e..6cd09336a7 100644 --- a/packages/services/src/view-service/transaction-planner/index.ts +++ b/packages/services/src/view-service/transaction-planner/index.ts @@ -14,7 +14,7 @@ export const transactionPlanner: Impl['transactionPlanner'] = async (req, ctx) = const { indexedDb } = await services.getWalletServices(); // Query IndexedDB directly to check for the existence of staking token - const nativeToken = await indexedDb.hasStakingAssetBalance(); + const nativeToken = await indexedDb.hasStakingAssetBalance(req.source); // Initialize the gas fee token using the native staking token's asset ID // If there is no native token balance, extract and use an alternate gas fee token diff --git a/packages/storage/src/indexed-db/index.ts b/packages/storage/src/indexed-db/index.ts index 77529f5143..7240d67909 100644 --- a/packages/storage/src/indexed-db/index.ts +++ b/packages/storage/src/indexed-db/index.ts @@ -820,7 +820,7 @@ export class IndexedDb implements IndexedDbInterface { }; } - async hasStakingAssetBalance(): Promise { + async hasStakingAssetBalance(addressIndex: AddressIndex | undefined): Promise { const spendableUMNotes = await this.db.getAllFromIndex( 'SPENDABLE_NOTES', 'assetId', @@ -829,7 +829,11 @@ export class IndexedDb implements IndexedDbInterface { return spendableUMNotes.some(note => { const umNote = SpendableNoteRecord.fromJson(note); - return umNote.heightSpent === 0n && !isZero(getAmountFromRecord(umNote)); + return ( + umNote.heightSpent === 0n && + !isZero(getAmountFromRecord(umNote)) && + umNote.addressIndex?.equals(addressIndex) + ); }); } } diff --git a/packages/types/src/indexed-db.ts b/packages/types/src/indexed-db.ts index 83b5603a3c..d5ef7faa44 100644 --- a/packages/types/src/indexed-db.ts +++ b/packages/types/src/indexed-db.ts @@ -146,7 +146,7 @@ export interface IndexedDbInterface { auctionId: AuctionId, ): Promise<{ input: Value; output: Value } | undefined>; - hasStakingAssetBalance(): Promise; + hasStakingAssetBalance(addressIndex: AddressIndex | undefined): Promise; } export interface PenumbraDb extends DBSchema { diff --git a/packages/wasm/crate/src/storage.rs b/packages/wasm/crate/src/storage.rs index 58bc5fd909..eff821d981 100644 --- a/packages/wasm/crate/src/storage.rs +++ b/packages/wasm/crate/src/storage.rs @@ -323,14 +323,14 @@ impl IndexedDBStorage { .transpose()?) } - pub async fn get_gas_prices_by_asset_id(&self, asset_is: Id) -> WasmResult> { + pub async fn get_gas_prices_by_asset_id(&self, asset_id: Id) -> WasmResult> { let tx = self .db .transaction_on_one(&self.constants.tables.gas_prices)?; let store = tx.object_store(&self.constants.tables.gas_prices)?; Ok(store - .get_owned(byte_array_to_base64(&asset_is.to_proto().inner))? + .get_owned(byte_array_to_base64(&asset_id.to_proto().inner))? .await?) // TODO GasPrices is missing domain type impl, requiring this // .map(serde_wasm_bindgen::from_value)