Skip to content

Commit

Permalink
hasStakingAssetBalance should check notes on a specific address index
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentine1898 committed Jul 4, 2024
1 parent 67e8df6 commit 83672e2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions packages/storage/src/indexed-db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ export class IndexedDb implements IndexedDbInterface {
};
}

async hasStakingAssetBalance(): Promise<boolean> {
async hasStakingAssetBalance(addressIndex: AddressIndex | undefined): Promise<boolean> {
const spendableUMNotes = await this.db.getAllFromIndex(
'SPENDABLE_NOTES',
'assetId',
Expand All @@ -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)
);
});
}
}
2 changes: 1 addition & 1 deletion packages/types/src/indexed-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export interface IndexedDbInterface {
auctionId: AuctionId,
): Promise<{ input: Value; output: Value } | undefined>;

hasStakingAssetBalance(): Promise<boolean>;
hasStakingAssetBalance(addressIndex: AddressIndex | undefined): Promise<boolean>;
}

export interface PenumbraDb extends DBSchema {
Expand Down
4 changes: 2 additions & 2 deletions packages/wasm/crate/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,14 @@ impl IndexedDBStorage {
.transpose()?)
}

pub async fn get_gas_prices_by_asset_id(&self, asset_is: Id) -> WasmResult<Option<JsValue>> {
pub async fn get_gas_prices_by_asset_id(&self, asset_id: Id) -> WasmResult<Option<JsValue>> {
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)
Expand Down

0 comments on commit 83672e2

Please sign in to comment.