Skip to content

Commit

Permalink
refactor: Fetch allowances for vault only once in join flow (#3642)
Browse files Browse the repository at this point in the history
  • Loading branch information
garethfuller authored Jul 6, 2023
1 parent f29329c commit bd9a333
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/composables/approvals/useTokenApprovalActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface Params {
spender: string;
actionType: ApprovalAction;
forceMax?: boolean;
skipAllowanceCheck?: boolean;
}

interface ApproveTokenParams {
Expand Down Expand Up @@ -90,12 +91,19 @@ export default function useTokenApprovalActions() {
}
}

async function updateAllowancesFor(spender: string): Promise<void> {
await injectSpenders([spender]);
await refetchAllowances();
}

async function getApprovalsRequired(
amountsToApprove: AmountToApprove[],
spender: string
spender: string,
skipAllowanceCheck = false
): Promise<AmountToApprove[]> {
await injectSpenders([spender]);
await refetchAllowances();
if (!skipAllowanceCheck) {
await updateAllowancesFor(spender);
}

return approvalsRequired(amountsToApprove, spender);
}
Expand All @@ -104,8 +112,7 @@ export default function useTokenApprovalActions() {
amountToApprove: AmountToApprove,
spender: string
): Promise<boolean> {
await injectSpenders([spender]);
await refetchAllowances();
await updateAllowancesFor(spender);

return !approvalRequired(
amountToApprove.address,
Expand Down Expand Up @@ -183,10 +190,12 @@ export default function useTokenApprovalActions() {
spender,
actionType,
forceMax = true,
skipAllowanceCheck = false,
}: Params): Promise<TransactionActionInfo[]> {
const approvalsRequired = await getApprovalsRequired(
amountsToApprove,
spender
spender,
skipAllowanceCheck
);

return approvalsRequired.map(amountToApprove => {
Expand Down Expand Up @@ -217,5 +226,6 @@ export default function useTokenApprovalActions() {
return {
approveToken,
getTokenApprovalActions,
updateAllowancesFor,
};
}
7 changes: 6 additions & 1 deletion src/providers/local/join-pool.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ export const joinPoolProvider = (
const { transactionDeadline } = useApp();
const { txState, txInProgress, resetTxState } = useTxState();
const relayerApproval = useRelayerApprovalTx(RelayerType.BATCH);
const { getTokenApprovalActions } = useTokenApprovalActions();
const { getTokenApprovalActions, updateAllowancesFor } =
useTokenApprovalActions();
const { relayerSignature, relayerApprovalAction } = useRelayerApproval(
RelayerType.BATCH
);
Expand Down Expand Up @@ -277,6 +278,7 @@ export const joinPoolProvider = (
amountsToApprove: amountsToApprove.value,
spender: appNetworkConfig.addresses.vault,
actionType: ApprovalAction.AddLiquidity,
skipAllowanceCheck: true, // Done once beforeMount
});

approvalActions.value = shouldSignRelayer.value
Expand Down Expand Up @@ -422,6 +424,9 @@ export const joinPoolProvider = (
// Ensure prices are fetched for token tree. When pool architecture is
// refactored probably won't be required.
injectTokens(poolJoinTokens.value);

// Make sure allowances on the vault are up to date.
updateAllowancesFor(appNetworkConfig.addresses.vault);
});

onMounted(() => (isMounted.value = true));
Expand Down

1 comment on commit bd9a333

@vercel
Copy link

@vercel vercel bot commented on bd9a333 Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.