Skip to content

Commit

Permalink
feat(encoder): add viem support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Aug 19, 2024
1 parent b42e65a commit 61b72f2
Show file tree
Hide file tree
Showing 14 changed files with 1,767 additions and 150 deletions.
45 changes: 45 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { HardhatUserConfig } from "hardhat/config";
import "dotenv/config";

export const rpcUrl = process.env.MAINNET_RPC_URL;
if (!rpcUrl) throw Error(`no RPC url provided`);

const config: HardhatUserConfig = {
defaultNetwork: "hardhat",
networks: {
hardhat: {
chainId: 1,
forking: {
url: rpcUrl,
blockNumber: 19_909_475,
},
gasPrice: 0,
initialBaseFeePerGas: 0,
allowBlocksWithSameTimestamp: true,
accounts: { count: 2 },
},
},
solidity: {
compilers: [
{
version: "0.8.25",
settings: {
evmVersion: "cancun",
optimizer: {
enabled: true,
runs: 200,
},
viaIR: true,
},
},
],
},
gasReporter: {
currency: "EUR",
},
mocha: {
timeout: 300000,
},
};

export default config;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@lerna-lite/cli": "3.5.1",
"@lerna-lite/publish": "3.8.0",
"@lerna-lite/version": "^3.7.1",
"@types/node": "^22.4.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"ts-node": "^10.9.2",
Expand Down
165 changes: 23 additions & 142 deletions packages/executooor-ethers/src/ExecutorEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,11 @@ export class ExecutorEncoder {
);
}

aaveV2FlashLoan(
aaveV2PoolAddress: string,
requests: AssetRequest[],
premium: BigNumberish,
callbackCalls?: BytesLike[],
) {
aaveFlashLoan(aavePoolAddress: string, requests: AssetRequest[], premium: BigNumberish, callbackCalls?: BytesLike[]) {
callbackCalls ??= [];

return this.pushCall(
aaveV2PoolAddress,
aavePoolAddress,
0n,
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("flashLoan", [
this.address,
Expand All @@ -252,7 +247,7 @@ export class ExecutorEncoder {

return ExecutorEncoder.buildErc20Approve(
asset,
aaveV2PoolAddress,
aavePoolAddress,
amount + amount.percentMul(toBigInt(premium)),
);
}),
Expand All @@ -263,7 +258,7 @@ export class ExecutorEncoder {
0,
]),
{
sender: aaveV2PoolAddress,
sender: aavePoolAddress,
dataIndex: 4n, // executeOperation(address[],uint256[],uint256[],address,bytes)
},
);
Expand Down Expand Up @@ -514,18 +509,18 @@ export class ExecutorEncoder {

/* AAVE V2 */

aaveV2Supply(aaveV2PoolAddress: string, asset: string, amount: BigNumberish, onBehalfOf?: string) {
aaveSupply(aavePoolAddress: string, asset: string, amount: BigNumberish, onBehalfOf?: string) {
onBehalfOf ||= this.address;

return this.pushCall(
aaveV2PoolAddress,
aavePoolAddress,
0n,
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("deposit", [asset, amount, onBehalfOf!, 0]),
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("deposit", [asset, amount, onBehalfOf, 0]),
);
}

aaveV2Borrow(
aaveV2PoolAddress: string,
aaveBorrow(
aavePoolAddress: string,
asset: string,
amount: BigNumberish,
interestRateMode: BigNumberish,
Expand All @@ -534,14 +529,14 @@ export class ExecutorEncoder {
onBehalfOf ||= this.address;

return this.pushCall(
aaveV2PoolAddress,
aavePoolAddress,
0n,
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("borrow", [asset, amount, interestRateMode, 0, onBehalfOf!]),
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("borrow", [asset, amount, interestRateMode, 0, onBehalfOf]),
);
}

aaveV2Repay(
aaveV2PoolAddress: string,
aaveRepay(
aavePoolAddress: string,
asset: string,
amount: BigNumberish,
interestRateMode: BigNumberish,
Expand All @@ -550,144 +545,30 @@ export class ExecutorEncoder {
onBehalfOf ||= this.address;

return this.pushCall(
aaveV2PoolAddress,
aavePoolAddress,
0n,
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("repay", [asset, amount, interestRateMode, onBehalfOf!]),
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("repay", [asset, amount, interestRateMode, onBehalfOf]),
);
}

aaveV2Withdraw(aaveV2PoolAddress: string, asset: string, amount: BigNumberish, to?: string) {
aaveWithdraw(aavePoolAddress: string, asset: string, amount: BigNumberish, to?: string) {
to ||= this.address;

return this.pushCall(
aaveV2PoolAddress,
0n,
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("withdraw", [asset, amount, to!]),
);
}

/* AAVE V2 AMM */

aaveV2AmmSupply(aaveV2AmmPoolAddress: string, asset: string, amount: BigNumberish, onBehalfOf?: string) {
onBehalfOf ||= this.address;

return this.pushCall(
aaveV2AmmPoolAddress,
aavePoolAddress,
0n,
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("deposit", [asset, amount, onBehalfOf!, 0]),
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("withdraw", [asset, amount, to]),
);
}

aaveV2AmmBorrow(
aaveV2AmmPoolAddress: string,
asset: string,
amount: BigNumberish,
interestRateMode: BigNumberish,
onBehalfOf?: string,
) {
onBehalfOf ||= this.address;

return this.pushCall(
aaveV2AmmPoolAddress,
0n,
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("borrow", [asset, amount, interestRateMode, 0, onBehalfOf!]),
);
}

aaveV2AmmRepay(
aaveV2AmmPoolAddress: string,
asset: string,
amount: BigNumberish,
interestRateMode: BigNumberish,
onBehalfOf?: string,
) {
onBehalfOf ||= this.address;

return this.pushCall(
aaveV2AmmPoolAddress,
0n,
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("repay", [asset, amount, interestRateMode, onBehalfOf!]),
);
}

aaveV2AmmWithdraw(aaveV2AmmPoolAddress: string, asset: string, amount: BigNumberish, to?: string) {
to ||= this.address;

return this.pushCall(
aaveV2AmmPoolAddress,
0n,
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("withdraw", [asset, amount, to!]),
);
}

aaveV2AmmLiquidate(
aaveV2AmmPoolAddress: string,
collateral: string,
debt: string,
user: string,
amount: BigNumberish,
) {
aaveLiquidate(aavePoolAddress: string, collateral: string, debt: string, user: string, amount: BigNumberish) {
return this.pushCall(
aaveV2AmmPoolAddress,
aavePoolAddress,
0n,
ExecutorEncoder.POOL_V2_IFC.encodeFunctionData("liquidationCall", [collateral, debt, user, amount, false]),
);
}

/* AAVE V3 */

aaveV3Supply(aaveV3PoolAddress: string, asset: string, amount: BigNumberish, onBehalfOf?: string) {
onBehalfOf ||= this.address;

return this.pushCall(
aaveV3PoolAddress,
0n,
ExecutorEncoder.POOL_V3_IFC.encodeFunctionData("deposit", [asset, amount, onBehalfOf!, 0]),
);
}

aaveV3Borrow(
aaveV3PoolAddress: string,
asset: string,
amount: BigNumberish,
interestRateMode: BigNumberish,
onBehalfOf?: string,
) {
onBehalfOf ||= this.address;

return this.pushCall(
aaveV3PoolAddress,
0n,
ExecutorEncoder.POOL_V3_IFC.encodeFunctionData("borrow", [asset, amount, interestRateMode, 0, onBehalfOf!]),
);
}

aaveV3Repay(
aaveV3PoolAddress: string,
asset: string,
amount: BigNumberish,
interestRateMode: BigNumberish,
onBehalfOf?: string,
) {
onBehalfOf ||= this.address;

return this.pushCall(
aaveV3PoolAddress,
0n,
ExecutorEncoder.POOL_V3_IFC.encodeFunctionData("repay", [asset, amount, interestRateMode, onBehalfOf!]),
);
}

aaveV3Withdraw(aaveV3PoolAddress: string, asset: string, amount: BigNumberish, to?: string) {
to ||= this.address;

return this.pushCall(
aaveV3PoolAddress,
0n,
ExecutorEncoder.POOL_V3_IFC.encodeFunctionData("withdraw", [asset, amount, to!]),
);
}

/* UNISWAP V3 */

uniV3ExactInput(
Expand All @@ -705,7 +586,7 @@ export class ExecutorEncoder {
ExecutorEncoder.SWAP_ROUTER_V3_IFC.encodeFunctionData("exactInput", [
{
path,
recipient: recipient!,
recipient,
deadline: Math.ceil(Date.now() / 1000) + 90,
amountIn,
amountOutMinimum,
Expand All @@ -729,7 +610,7 @@ export class ExecutorEncoder {
ExecutorEncoder.SWAP_ROUTER_V3_IFC.encodeFunctionData("exactOutput", [
{
path,
recipient: recipient!,
recipient,
deadline: Math.ceil(Date.now() / 1000) + 90,
amountOut,
amountInMaximum,
Expand All @@ -738,7 +619,7 @@ export class ExecutorEncoder {
);
}

/* LIQUIDATION */
/* MORPHO */

morphoCompoundLiquidate(
morphoCompoundAddress: string,
Expand Down
14 changes: 7 additions & 7 deletions packages/executooor-ethers/test/ExecutorEncoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe("ExecutorEncoder", () => {

for (const { asset, amount } of requests) await deal(asset, encoder.address, amount.percentMul(premium));

await encoder.aaveV2FlashLoan(aaveV2PoolAddress, requests, premium).exec();
await encoder.aaveFlashLoan(aaveV2PoolAddress, requests, premium).exec();
});

it("should execute aaveV3 flashloan", async () => {
Expand Down Expand Up @@ -171,8 +171,8 @@ describe("ExecutorEncoder", () => {
await encoder
.balancerFlashLoan(balancerVaultAddress, requests)
.makerFlashLoan(makerVaultAddress, dai, BigInt.WAD * 1_000_000n)
.aaveV2FlashLoan(aaveV2PoolAddress, requests, aaveV2Premium)
.aaveV3FlashLoan(aaveV3PoolAddress, requests, aaveV3Premium)
.aaveFlashLoan(aaveV2PoolAddress, requests, aaveV2Premium)
.aaveFlashLoan(aaveV3PoolAddress, requests, aaveV3Premium)
.uniV3FlashLoan(computeV3PoolAddress(usdc, weth, 500n), assets, amounts, 500n)
.exec();
});
Expand All @@ -199,13 +199,13 @@ describe("ExecutorEncoder", () => {
[{ asset: dai, amount: collateralAmount }],
encoder
.erc20Approve(dai, aaveV2PoolAddress, collateralAmount)
.aaveV2Supply(aaveV2PoolAddress, dai, collateralAmount)
.aaveV2Borrow(aaveV2PoolAddress, weth, borrowedAmount, 2)
.aaveSupply(aaveV2PoolAddress, dai, collateralAmount)
.aaveBorrow(aaveV2PoolAddress, weth, borrowedAmount, 2)
.unwrapETH(weth, borrowedAmount)
.wrapETH(weth, borrowedAmount)
.erc20Approve(weth, aaveV2PoolAddress, borrowedAmount)
.aaveV2Repay(aaveV2PoolAddress, weth, borrowedAmount, 2)
.aaveV2Withdraw(aaveV2PoolAddress, dai, MaxUint256)
.aaveRepay(aaveV2PoolAddress, weth, borrowedAmount, 2)
.aaveWithdraw(aaveV2PoolAddress, dai, MaxUint256)
.flush(),
)
.exec();
Expand Down
1 change: 1 addition & 0 deletions packages/executooor-viem/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MAINNET_RPC_URL=
4 changes: 4 additions & 0 deletions packages/executooor-viem/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# hardhat
contracts/
artifacts/
cache/
9 changes: 9 additions & 0 deletions packages/executooor-viem/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import "@nomicfoundation/hardhat-chai-matchers";
import "@nomicfoundation/hardhat-viem";
import "evm-maths";
import "hardhat-deal";
import "hardhat-gas-reporter";
import "hardhat-tracer";
import "solidity-coverage";

export { default } from "../../hardhat.config";
Loading

0 comments on commit 61b72f2

Please sign in to comment.