diff --git a/package.json b/package.json index 6398eade1e..aac028ba0e 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "test:coverage": "jest --coverage" }, "dependencies": { - "@aave/contract-helpers": "1.21.1-2e2295889aa7b15bb4e9c6d5065432703f7c1542.0", + "@aave/contract-helpers": "1.21.1-0efe71955bfd19d178a3edef77416ca0612598b9.0", "@aave/math-utils": "1.20.1-8d191bf193b4e5f74af4086481eb6f23a17f39e5.0", "@bgd-labs/aave-address-book": "^2.7.0", "@emotion/cache": "11.10.3", diff --git a/src/components/transactions/Repay/RepayActions.tsx b/src/components/transactions/Repay/RepayActions.tsx index 2b3daf2535..9a23efc709 100644 --- a/src/components/transactions/Repay/RepayActions.tsx +++ b/src/components/transactions/Repay/RepayActions.tsx @@ -48,17 +48,23 @@ export const RepayActions = ({ const [ repay, repayWithPermit, + encodeRepayParams, + encodeRepayWithPermit, tryPermit, walletApprovalMethodPreference, estimateGasLimit, addTransaction, + optimizedPath, ] = useRootStore((store) => [ store.repay, store.repayWithPermit, + store.encodeRepayParams, + store.encodeRepayWithPermitParams, store.tryPermit, store.walletApprovalMethodPreference, store.estimateGasLimit, store.addTransaction, + store.useOptimizedPath, ]); const { sendTx } = useWeb3Context(); const { refetchGhoData, refetchIncentiveData, refetchPoolData } = useBackgroundDataProvider(); @@ -130,24 +136,33 @@ export const RepayActions = ({ let action = ProtocolAction.default; if (usePermit && signatureParams) { - action = ProtocolAction.repayWithPermit; - console.log(1); - const signedRepayWithPermitTxData = repayWithPermit({ - amountToRepay: + const repayWithPermitParams = { + amount: amountToRepay === '-1' ? amountToRepay : parseUnits(amountToRepay, poolReserve.decimals).toString(), - poolAddress, - debtType, + reserve: poolAddress, + interestRateMode: debtType, signature: signatureParams.signature, deadline: signatureParams.deadline, + }; + + let encodedParams: [string, string, string] | undefined; + if (optimizedPath()) { + encodedParams = await encodeRepayWithPermit(repayWithPermitParams); + } + + action = ProtocolAction.repayWithPermit; + let signedRepayWithPermitTxData = repayWithPermit({ + ...repayWithPermitParams, + encodedTxData: encodedParams ? encodedParams[0] : undefined, }); - // signedRepayWithPermitTxData = await estimateGasLimit(signedRepayWithPermitTxData); + + signedRepayWithPermitTxData = await estimateGasLimit(signedRepayWithPermitTxData); response = await sendTx(signedRepayWithPermitTxData); await response.wait(1); } else { - action = ProtocolAction.repay; - let repayTxData = repay({ + const repayParams = { amountToRepay: amountToRepay === '-1' ? amountToRepay @@ -155,6 +170,17 @@ export const RepayActions = ({ poolAddress, repayWithATokens, debtType, + }; + + let encodedParams: string | undefined; + if (optimizedPath()) { + encodedParams = await encodeRepayParams(repayParams); + } + + action = ProtocolAction.repay; + let repayTxData = repay({ + ...repayParams, + encodedTxData: encodedParams, }); repayTxData = await estimateGasLimit(repayTxData); response = await sendTx(repayTxData); diff --git a/src/store/poolSlice.ts b/src/store/poolSlice.ts index 6af8f6029e..6e984510e7 100644 --- a/src/store/poolSlice.ts +++ b/src/store/poolSlice.ts @@ -36,11 +36,11 @@ import { LPWithdrawParamsType, } from '@aave/contract-helpers/dist/esm/lendingPool-contract/lendingPoolTypes'; import { + LPRepayWithPermitParamsType, LPSignERC20ApprovalType, LPSupplyParamsType, LPSupplyWithPermitType, } from '@aave/contract-helpers/dist/esm/v3-pool-contract/lendingPoolTypes'; -import { SignatureLike } from '@ethersproject/bytes'; import dayjs from 'dayjs'; import { BigNumber, PopulatedTransaction, Signature, utils } from 'ethers'; import { splitSignature } from 'ethers/lib/utils'; @@ -67,19 +67,12 @@ export type PoolReserve = { userReserves?: UserReserveDataHumanized[]; }; -type RepayWithPermitArgs = { - amountToRepay: string; - poolAddress: string; - debtType: InterestRate; - signature: SignatureLike; - deadline: string; -}; - type RepayArgs = { amountToRepay: string; poolAddress: string; debtType: InterestRate; repayWithATokens: boolean; + encodedTxData?: string; }; // TODO: add chain/provider/account mapping @@ -108,9 +101,13 @@ export interface PoolSlice { claimRewards: (args: ClaimRewardsActionsProps) => Promise; // TODO: optimize types to use only neccessary properties swapCollateral: (args: SwapActionProps) => Promise; - repay: (args: RepayArgs) => PopulatedTransaction; withdrawAndSwitch: (args: WithdrawAndSwitchActionProps) => PopulatedTransaction; - repayWithPermit: (args: RepayWithPermitArgs) => PopulatedTransaction; + repay: (args: RepayArgs) => PopulatedTransaction; + encodeRepayParams: (args: RepayArgs) => Promise; + repayWithPermit: (args: Omit) => PopulatedTransaction; + encodeRepayWithPermitParams: ( + args: Omit + ) => Promise<[string, string, string]>; poolComputed: { minRemainingBaseTokenBalance: string; }; @@ -562,7 +559,7 @@ export const createPoolSlice: StateCreator< }, }); }, - repay: ({ repayWithATokens, amountToRepay, poolAddress, debtType }) => { + repay: ({ repayWithATokens, amountToRepay, poolAddress, debtType, encodedTxData }) => { const poolBundle = get().getCorrectPoolBundle(); const currentAccount = get().account; if (poolBundle instanceof PoolBundle) { @@ -573,6 +570,7 @@ export const createPoolSlice: StateCreator< amount: amountToRepay, useOptimizedPath: get().useOptimizedPath(), rateMode: debtType, + encodedTxData, }); } else { return poolBundle.repayTxBuilder.generateTxData({ @@ -581,6 +579,7 @@ export const createPoolSlice: StateCreator< amount: amountToRepay, useOptimizedPath: get().useOptimizedPath(), interestRateMode: debtType, + encodedTxData, }); } } else { @@ -593,20 +592,55 @@ export const createPoolSlice: StateCreator< }); } }, - repayWithPermit: ({ poolAddress, amountToRepay, debtType, deadline, signature }) => { + repayWithPermit: ({ + reserve, + amount, + interestRateMode, + deadline, + signature, + encodedTxData, + }) => { const poolBundle = get().getCorrectPoolBundle() as PoolBundle; const currentAccount = get().account; const stringSignature = utils.joinSignature(signature); return poolBundle.repayTxBuilder.generateSignedTxData({ user: currentAccount, - reserve: poolAddress, - amount: amountToRepay, + reserve, + amount, useOptimizedPath: get().useOptimizedPath(), - interestRateMode: debtType, + interestRateMode, + deadline, + signature: stringSignature, + encodedTxData, + }); + }, + encodeRepayWithPermitParams: ({ reserve, amount, interestRateMode, deadline, signature }) => { + const poolBundle = get().getCorrectPoolBundle() as PoolBundle; + const stringSignature = utils.joinSignature(signature); + return poolBundle.repayTxBuilder.encodeRepayWithPermitParams({ + reserve, + amount, + interestRateMode, deadline, signature: stringSignature, }); }, + encodeRepayParams: ({ amountToRepay, poolAddress, debtType, repayWithATokens }) => { + const poolBundle = get().getCorrectPoolBundle() as PoolBundle; + if (repayWithATokens) { + return poolBundle.repayWithATokensTxBuilder.encodeRepayWithATokensParams({ + reserve: poolAddress, + amount: amountToRepay, + rateMode: debtType, + }); + } else { + return poolBundle.repayTxBuilder.encodeRepayParams({ + reserve: poolAddress, + amount: amountToRepay, + interestRateMode: debtType, + }); + } + }, swapCollateral: async ({ poolReserve, targetReserve, diff --git a/yarn.lock b/yarn.lock index bfd5b7d8a0..b1e95417b5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,10 +2,10 @@ # yarn lockfile v1 -"@aave/contract-helpers@1.21.1-2e2295889aa7b15bb4e9c6d5065432703f7c1542.0": - version "1.21.1-2e2295889aa7b15bb4e9c6d5065432703f7c1542.0" - resolved "https://registry.yarnpkg.com/@aave/contract-helpers/-/contract-helpers-1.21.1-2e2295889aa7b15bb4e9c6d5065432703f7c1542.0.tgz#875d199308e37ad5c4850800ceabf09b473bb425" - integrity sha512-ogZbmm4g/w5KqiFk50nKT7IVqCAwptN4hEAW0BIkYnUI9FdTaSY3SNN4TsejSfz7jKtS+Tz5OTo4F68cbmSCeA== +"@aave/contract-helpers@1.21.1-0efe71955bfd19d178a3edef77416ca0612598b9.0": + version "1.21.1-0efe71955bfd19d178a3edef77416ca0612598b9.0" + resolved "https://registry.yarnpkg.com/@aave/contract-helpers/-/contract-helpers-1.21.1-0efe71955bfd19d178a3edef77416ca0612598b9.0.tgz#ff068c6b1cc84c989a8566ae5cb0de8674fabdb2" + integrity sha512-04FPIti7B2U4KXKUTzXAXbL+NOP1Q+KtDFZR1mo5OWJZoW6Ky8xCTqSpFNMQbxMoejiJaYYqkeEQnOgVOuwEDQ== dependencies: isomorphic-unfetch "^3.1.0"