Skip to content

Commit

Permalink
Add source to swapClaims
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 committed Jul 5, 2024
1 parent b9aca7e commit 8532216
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
10 changes: 7 additions & 3 deletions apps/minifront/src/state/swap/instant-swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [],
Expand Down Expand Up @@ -141,7 +142,7 @@ export const createInstantSwapSlice = (): SliceCreator<InstantSwapSlice> => (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 = '';
Expand Down Expand Up @@ -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 });
};

Expand Down
7 changes: 5 additions & 2 deletions apps/minifront/src/state/unclaimed-swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,7 +63,9 @@ export const createUnclaimedSwapsSlice = (): SliceCreator<UnclaimedSwapsSlice> =
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();
},
Expand Down
6 changes: 3 additions & 3 deletions packages/services/src/view-service/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
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);

// 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.
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)
);
});
}
}
12 changes: 6 additions & 6 deletions packages/types/src/indexed-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<DBTypes extends PenumbraDb, StoreName extends StoreNames<DBTypes>> {
table: StoreName;
Expand Down 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

0 comments on commit 8532216

Please sign in to comment.