Skip to content

Commit

Permalink
feat: use optimized path
Browse files Browse the repository at this point in the history
  • Loading branch information
grothem committed Oct 25, 2023
1 parent 3f27df4 commit 999c851
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 35 additions & 9 deletions src/components/transactions/Repay/RepayActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -130,31 +136,51 @@ 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
: parseUnits(amountToRepay, poolReserve.decimals).toString(),
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);
Expand Down
66 changes: 50 additions & 16 deletions src/store/poolSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -108,9 +101,13 @@ export interface PoolSlice {
claimRewards: (args: ClaimRewardsActionsProps) => Promise<EthereumTransactionTypeExtended[]>;
// TODO: optimize types to use only neccessary properties
swapCollateral: (args: SwapActionProps) => Promise<EthereumTransactionTypeExtended[]>;
repay: (args: RepayArgs) => PopulatedTransaction;
withdrawAndSwitch: (args: WithdrawAndSwitchActionProps) => PopulatedTransaction;
repayWithPermit: (args: RepayWithPermitArgs) => PopulatedTransaction;
repay: (args: RepayArgs) => PopulatedTransaction;
encodeRepayParams: (args: RepayArgs) => Promise<string>;
repayWithPermit: (args: Omit<LPRepayWithPermitParamsType, 'user'>) => PopulatedTransaction;
encodeRepayWithPermitParams: (
args: Omit<LPRepayWithPermitParamsType, 'user'>
) => Promise<[string, string, string]>;
poolComputed: {
minRemainingBaseTokenBalance: string;
};
Expand Down Expand Up @@ -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) {
Expand All @@ -573,6 +570,7 @@ export const createPoolSlice: StateCreator<
amount: amountToRepay,
useOptimizedPath: get().useOptimizedPath(),
rateMode: debtType,
encodedTxData,
});
} else {
return poolBundle.repayTxBuilder.generateTxData({
Expand All @@ -581,6 +579,7 @@ export const createPoolSlice: StateCreator<
amount: amountToRepay,
useOptimizedPath: get().useOptimizedPath(),
interestRateMode: debtType,
encodedTxData,
});
}
} else {
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@aave/[email protected]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/[email protected]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"

Expand Down

0 comments on commit 999c851

Please sign in to comment.