Skip to content

Commit

Permalink
feat: added encode functions for using optimized path
Browse files Browse the repository at this point in the history
  • Loading branch information
grothem committed Oct 24, 2023
1 parent 2e22958 commit f5b8cfb
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 90 deletions.
26 changes: 26 additions & 0 deletions packages/contract-helpers/src/commons/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,41 @@ export type BorrowTxBuilder = {
useOptimizedPath,
encodedTxData,
}: LPBorrowParamsType) => PopulatedTransaction;
encodeBorrowParams: ({
reserve,
amount,
interestRateMode,
referralCode,
}: Omit<LPBorrowParamsType, 'user'>) => Promise<string>;
};

export type RepayTxBuilder = {
generateTxData: (params: LPRepayParamsType) => PopulatedTransaction;
generateSignedTxData: (
params: LPSignedRepayParamsType,
) => PopulatedTransaction;
encodeRepayParams: ({
reserve,
amount,
interestRateMode,
}: Omit<LPRepayParamsType, 'user'>) => Promise<string>;
encodeRepayWithPermitParams: ({
reserve,
amount,
interestRateMode,
deadline,
signature,
}: Pick<
LPSignedRepayParamsType,
'reserve' | 'amount' | 'interestRateMode' | 'signature' | 'deadline'
>) => Promise<[string, string, string]>;
};

export type RepayWithATokensTxBuilder = {
generateTxData: (params: LPRepayWithATokensType) => PopulatedTransaction;
encodeRepayWithATokensParams: ({
reserve,
amount,
rateMode,
}: Omit<LPRepayWithATokensType, 'user'>) => Promise<string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
InterestRate,
LendingPoolMarketConfig,
ProtocolAction,
RepayTxBuilder,
tEthereumAddress,
} from '../commons/types';
import {
Expand All @@ -20,7 +21,6 @@ import {
import {
LPBorrowParamsType,
LPDepositParamsType,
LPRepayParamsType,
} from '../lendingPool-contract/lendingPoolTypes';
import {
ILendingPool,
Expand All @@ -44,12 +44,10 @@ export type DepositTxBuilder = {
getApprovedAmount: ({ user, token }: TokenOwner) => Promise<ApproveType>;
};

export type LPRepayTxBuilder = {
generateTxData: (args: LPRepayParamsType) => PopulatedTransaction;
};

export interface LendingPoolBundleInterface {
depositTxBuilder: DepositTxBuilder;
borrowTxBuilder: Pick<BorrowTxBuilder, 'generateTxData'>;
repayTxBuilder: Pick<RepayTxBuilder, 'generateTxData'>;
}

export class LendingPoolBundle
Expand All @@ -69,8 +67,8 @@ export class LendingPoolBundle
readonly wethGatewayAddress: tEthereumAddress;

depositTxBuilder: DepositTxBuilder;
borrowTxBuilder: Omit<BorrowTxBuilder, 'useOptimizedPath' | 'encodedTxData'>;
repayTxBuilder: LPRepayTxBuilder;
borrowTxBuilder: Pick<BorrowTxBuilder, 'generateTxData'>;
repayTxBuilder: Pick<RepayTxBuilder, 'generateTxData'>;

constructor(
provider: providers.Provider,
Expand Down
216 changes: 133 additions & 83 deletions packages/contract-helpers/src/v3-pool-contract-bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ import {
import { IPool, IPoolInterface } from '../v3-pool-contract/typechain/IPool';
import { IPool__factory } from '../v3-pool-contract/typechain/IPool__factory';
import { L2Pool, L2PoolInterface } from '../v3-pool-rollups';
import {
LPSupplyWithPermitType as LPSupplyWithPermitTypeL2,
LPBorrowParamsType as LPBorrowParamsTypeL2,
} from '../v3-pool-rollups/poolTypes';
import {
WETHGatewayInterface,
WETHGatewayService,
Expand All @@ -64,6 +60,23 @@ export type SupplyTxBuilder = {
encodedTxData,
}: LPSignedSupplyParamsType) => PopulatedTransaction;
getApprovedAmount: ({ user, token }: TokenOwner) => Promise<ApproveType>;
encodeSupplyParams: ({
reserve,
amount,
referralCode,
}: Pick<
LPSupplyParamsType,
'reserve' | 'amount' | 'referralCode'
>) => Promise<string>;
encodeSupplyWithPermitParams: ({
reserve,
amount,
referralCode,
signature,
}: Pick<
LPSignedSupplyParamsType,
'reserve' | 'amount' | 'referralCode' | 'signature' | 'deadline'
>) => Promise<[string, string, string]>;
};

export interface PoolBundleInterface {
Expand Down Expand Up @@ -156,6 +169,13 @@ export class PoolBundle
useOptimizedPath,
encodedTxData,
}: LPSupplyParamsType): PopulatedTransaction => {
if (useOptimizedPath && encodedTxData) {
return this.l2PoolService.generateEncodedSupplyTxData({
encodedTxData,
user,
});
}

let actionTx: PopulatedTransaction = {};
const onBehalfOfParam = onBehalfOf ?? user;
const referralCodeParam = referralCode ?? '0';
Expand All @@ -167,22 +187,6 @@ export class PoolBundle
onBehalfOf: onBehalfOfParam,
referralCode: referralCodeParam,
});
} else if (useOptimizedPath) {
if (encodedTxData) {
actionTx = this.l2PoolService.generateEncodedSupplyTxData({
encodedTxData,
user,
});
} else {
const args: LPSupplyParamsType = {
user,
reserve,
amount,
onBehalfOf: onBehalfOfParam,
referralCode: referralCodeParam,
};
actionTx = this.l2PoolService.generateSupplyTxData(args);
}
} else {
const txData = this.contractInterface.encodeFunctionData('supply', [
reserve,
Expand Down Expand Up @@ -211,58 +215,69 @@ export class PoolBundle
deadline,
encodedTxData,
}: LPSignedSupplyParamsType): PopulatedTransaction => {
if (useOptimizedPath && encodedTxData) {
return this.l2PoolService.generateEncodedSupplyWithPermitTxData({
encodedTxData,
user,
signature,
});
}

const decomposedSignature: Signature = splitSignature(signature);
let populatedTx: PopulatedTransaction = {};
const populatedTx: PopulatedTransaction = {};
const onBehalfOfParam = onBehalfOf ?? user;
const referralCodeParam = referralCode ?? '0';
if (useOptimizedPath) {
if (encodedTxData) {
populatedTx =
this.l2PoolService.generateEncodedSupplyWithPermitTxData({
encodedTxData,
user,
signature,
});
} else {
const args: LPSupplyWithPermitTypeL2 = {
user,
reserve,
amount,
referralCode: referralCodeParam,
onBehalfOf: onBehalfOfParam,
permitR: decomposedSignature.r,
permitS: decomposedSignature.s,
permitV: decomposedSignature.v,
deadline: Number(deadline),
};
populatedTx =
this.l2PoolService.generateSupplyWithPermitTxData(args);
}
} else {
const txData = this.contractInterface.encodeFunctionData(
'supplyWithPermit',
[
reserve,
amount,
onBehalfOfParam,
referralCodeParam,
deadline,
decomposedSignature.v,
decomposedSignature.r,
decomposedSignature.s,
],
);
populatedTx.to = this.poolAddress;
populatedTx.from = user;
populatedTx.data = txData;
populatedTx.gasLimit = BigNumber.from(
gasLimitRecommendations[ProtocolAction.supplyWithPermit]
.recommended,
);
}
const txData = this.contractInterface.encodeFunctionData(
'supplyWithPermit',
[
reserve,
amount,
onBehalfOfParam,
referralCodeParam,
deadline,
decomposedSignature.v,
decomposedSignature.r,
decomposedSignature.s,
],
);
populatedTx.to = this.poolAddress;
populatedTx.from = user;
populatedTx.data = txData;
populatedTx.gasLimit = BigNumber.from(
gasLimitRecommendations[ProtocolAction.supplyWithPermit].recommended,
);

return populatedTx;
},
encodeSupplyParams: async ({
reserve,
amount,
referralCode,
}): Promise<string> => {
return this.l2PoolService
.getEncoder()
.encodeSupplyParams(reserve, amount, referralCode ?? '0');
},
encodeSupplyWithPermitParams: async ({
reserve,
amount,
signature,
deadline,
referralCode,
}): Promise<[string, string, string]> => {
const decomposedSignature: Signature = splitSignature(signature);
return this.l2PoolService
.getEncoder()
.encodeSupplyWithPermitParams(
reserve,
amount,
referralCode ?? '0',
deadline,
decomposedSignature.v,
decomposedSignature.r,
decomposedSignature.s,
);
},
};

this.borrowTxBuilder = {
Expand All @@ -277,6 +292,13 @@ export class PoolBundle
useOptimizedPath,
encodedTxData,
}: LPBorrowParamsType): PopulatedTransaction => {
if (useOptimizedPath && encodedTxData) {
return this.l2PoolService.generateEncodedBorrowTxData({
encodedTxData,
user,
});
}

let actionTx: PopulatedTransaction = {};
const referralCodeParam = referralCode ?? '0';
const onBehalfOfParam = onBehalfOf ?? user;
Expand All @@ -297,23 +319,6 @@ export class PoolBundle
interestRateMode,
referralCode: referralCodeParam,
});
} else if (useOptimizedPath) {
if (encodedTxData) {
actionTx = this.l2PoolService.generateEncodedBorrowTxData({
encodedTxData,
user,
});
} else {
const args: LPBorrowParamsTypeL2 = {
user,
reserve,
amount,
onBehalfOf: onBehalfOfParam,
referralCode: referralCodeParam,
numericRateMode,
};
actionTx = this.l2PoolService.generateBorrowTxData(args);
}
} else {
const txData = this.contractInterface.encodeFunctionData('borrow', [
reserve,
Expand All @@ -332,6 +337,21 @@ export class PoolBundle

return actionTx;
},
encodeBorrowParams: async ({
reserve,
amount,
interestRateMode,
referralCode,
}): Promise<string> => {
return this.l2PoolService
.getEncoder()
.encodeBorrowParams(
reserve,
amount,
interestRateMode,
referralCode ?? '0',
);
},
};

this.repayTxBuilder = {
Expand Down Expand Up @@ -424,6 +444,31 @@ export class PoolBundle
);
return populatedTx;
},
encodeRepayParams: async ({ reserve, amount, interestRateMode }) => {
return this.l2PoolService
.getEncoder()
.encodeRepayParams(reserve, amount, interestRateMode);
},
encodeRepayWithPermitParams: async ({
reserve,
amount,
interestRateMode,
signature,
deadline,
}) => {
const decomposedSignature: Signature = splitSignature(signature);
return this.l2PoolService
.getEncoder()
.encodeRepayWithPermitParams(
reserve,
amount,
interestRateMode,
deadline,
decomposedSignature.v,
decomposedSignature.r,
decomposedSignature.s,
);
},
};

this.repayWithATokensTxBuilder = {
Expand Down Expand Up @@ -467,6 +512,11 @@ export class PoolBundle

return actionTx;
},
encodeRepayWithATokensParams: async ({ reserve, amount, rateMode }) => {
return this.l2PoolService
.getEncoder()
.encodeRepayWithATokensParams(reserve, amount, rateMode);
},
};
}
}
Loading

0 comments on commit f5b8cfb

Please sign in to comment.