From 853221679d4fe17009b2b456be7eb90262ee02a3 Mon Sep 17 00:00:00 2001 From: Gabe Rodriguez Date: Fri, 5 Jul 2024 22:51:45 +0200 Subject: [PATCH] Add source to swapClaims --- apps/minifront/src/state/swap/instant-swap.ts | 10 +++++++--- apps/minifront/src/state/unclaimed-swaps.ts | 7 +++++-- packages/services/src/view-service/fees.ts | 6 +++--- .../src/view-service/transaction-planner/index.ts | 2 +- packages/storage/src/indexed-db/index.ts | 8 ++++++-- packages/types/src/indexed-db.ts | 12 ++++++------ 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/apps/minifront/src/state/swap/instant-swap.ts b/apps/minifront/src/state/swap/instant-swap.ts index 394702b869..daa6fe6c36 100644 --- a/apps/minifront/src/state/swap/instant-swap.ts +++ b/apps/minifront/src/state/swap/instant-swap.ts @@ -31,6 +31,7 @@ import { divideAmounts } from '@penumbra-zone/types/amount'; import { bech32mAssetId } from '@penumbra-zone/bech32m/passet'; import { SwapSlice } from '.'; import { sendSimulateTradeRequest } from './helpers'; +import { AddressIndex } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb'; const getMetadataByAssetId = async ( traces: SwapExecution_Trace[] = [], @@ -141,7 +142,7 @@ export const createInstantSwapSlice = (): SliceCreator => (set const swapReq = await assembleSwapRequest(get().swap); const swapTx = await planBuildBroadcast('swap', swapReq); const swapCommitment = getSwapCommitmentFromTx(swapTx); - await issueSwapClaim(swapCommitment); + await issueSwapClaim(swapCommitment, swapReq.source); set(state => { state.swap.amount = ''; @@ -195,8 +196,11 @@ const assembleSwapRequest = async ({ // Swap claims don't need authenticationData, so `witnessAndBuild` is used. // This way it won't trigger a second, unnecessary approval popup. // @see https://protocol.penumbra.zone/main/zswap/swap.html#claiming-swap-outputs -export const issueSwapClaim = async (swapCommitment: StateCommitment) => { - const req = new TransactionPlannerRequest({ swapClaims: [{ swapCommitment }] }); +export const issueSwapClaim = async ( + swapCommitment: StateCommitment, + source: AddressIndex | undefined, +) => { + const req = new TransactionPlannerRequest({ swapClaims: [{ swapCommitment }], source }); await planBuildBroadcast('swapClaim', req, { skipAuth: true }); }; diff --git a/apps/minifront/src/state/unclaimed-swaps.ts b/apps/minifront/src/state/unclaimed-swaps.ts index 78332c763c..55de0aec89 100644 --- a/apps/minifront/src/state/unclaimed-swaps.ts +++ b/apps/minifront/src/state/unclaimed-swaps.ts @@ -2,9 +2,10 @@ import { SliceCreator, useStore } from '.'; import { SwapRecord } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb'; import { issueSwapClaim } from './swap/instant-swap'; import { getSwapRecordCommitment } from '@penumbra-zone/getters/swap-record'; -import { ZQueryState, createZQuery } from '@penumbra-zone/zquery'; +import { createZQuery, ZQueryState } from '@penumbra-zone/zquery'; import { Metadata } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb'; import { fetchUnclaimedSwaps } from '../fetchers/unclaimed-swaps'; +import { viewClient } from '../clients'; type SwapCommitmentId = string; @@ -62,7 +63,9 @@ export const createUnclaimedSwapsSlice = (): SliceCreator = setStatus('add', id); const commitment = getSwapRecordCommitment(swap); - await issueSwapClaim(commitment); + + const { addressIndex } = await viewClient.indexByAddress({ address: swap.swap?.claimAddress }); + await issueSwapClaim(commitment, addressIndex); setStatus('remove', id); get().unclaimedSwaps.unclaimedSwaps.revalidate(); }, diff --git a/packages/services/src/view-service/fees.ts b/packages/services/src/view-service/fees.ts index a70068ab21..2ea90fb5ac 100644 --- a/packages/services/src/view-service/fees.ts +++ b/packages/services/src/view-service/fees.ts @@ -24,11 +24,11 @@ export const extractAltFee = async ( const swapCommitment = request.swapClaims .map(swapClaim => swapClaim.swapCommitment) .find(Boolean); + if (swapCommitment) { const swaps = await indexedDb.getSwapByCommitment(swapCommitment); - if (swaps?.swap?.claimFee?.assetId) { - return swaps.swap.claimFee.assetId; - } + // If the claimFee assetId is undefined, it means the swap was made with the stakingTokenAsset + return swaps?.swap?.claimFee?.assetId ?? indexedDb.stakingTokenAssetId; } throw new Error('Could not extract alternative fee assetId from TransactionPlannerRequest'); diff --git a/packages/services/src/view-service/transaction-planner/index.ts b/packages/services/src/view-service/transaction-planner/index.ts index 5d893f1d6a..83175f74cb 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); // Check if we should use the native token or extract an alternate gas fee token. // Special cased for swap claims as gas fee needs to match the claimFee on the corresponding swap. 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..b358e1cc08 100644 --- a/packages/types/src/indexed-db.ts +++ b/packages/types/src/indexed-db.ts @@ -15,6 +15,10 @@ import { Metadata, Value, } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb'; +import { + AuctionId, + DutchAuctionDescription, +} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/auction/v1/auction_pb'; import { Position, PositionId, @@ -44,12 +48,8 @@ import { SwapRecord, TransactionInfo, } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb'; -import type { Jsonified } from './jsonified'; -import { - AuctionId, - DutchAuctionDescription, -} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/auction/v1/auction_pb'; import { PartialMessage } from '@bufbuild/protobuf'; +import type { Jsonified } from './jsonified'; export interface IdbUpdate> { table: StoreName; @@ -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 {