Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/use aave client lib in tests #1

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*]
quote_type="single"
3 changes: 3 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TENDERLY_KEY=<Tenderly API key>
TENDERLY_ACCOUNT=<Tenderly account containing the project as seen in URL>
TENDERLY_PROJECT=<Tenderly project name as seen in URL>
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"license": "Unlicense",
"private": true,
"dependencies": {
"@aave/protocol-js": "2.7.3-cd25280d1fd2f4e08283e5d7bab70eb7e3005b5b.0",
"axios": "^0.21.1",
"dotenv": "^8.2.0",
"ethers": "^5.0.32",
"paraswap": "^3.2.0"
"paraswap": "^3.2.0",
"tslib": "^2.1.0"
}
}
158 changes: 79 additions & 79 deletions scripts/test-liquiditySwap-swapAndDeposit-swapAll-WETH-DAI.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
require('dotenv').config();
const { ethers } = require('ethers');
const { ParaSwap } = require('paraswap');
const { TenderlyFork } = require('../src/tenderly');
const { augustusFromAmountOffsetFromCalldata } = require('../src/augustus');
const {
AAVE_ADDRESSES_PROVIDER,
WETH,
DAI,
} = require('../src/addresses');
TxBuilderV2,
distinctContractAddressBetweenMarketsV2,
} = require('@aave/protocol-js');
const { TenderlyFork } = require('../src/tenderly');
const { WETH, DAI, API_ETH_MOCK_ADDRESS } = require('../src/addresses');

const IERC20_ARTIFACT = require('../artifacts/IERC20.json');
const IWETH_ARTIFACT = require('../artifacts/IWETH.json');
const ILendingPoolAddressesProvider_ARTIFACT = require('../artifacts/ILendingPoolAddressesProvider.json');
const ILendingPool_ARTIFACT = require('../artifacts/ILendingPool.json');
const ParaSwapLiquiditySwapAdapter_ARTIFACT = require('../artifacts/ParaSwapLiquiditySwapAdapter.json');

const FORK_NETWORK_ID = process.env.FORK_NETWORK_ID || '1';
const PARASWAP_API = process.env.PARASWAP_API || 'https://apiv2.paraswap.io/v2';
Expand All @@ -26,42 +23,59 @@ async function main() {
// Build a swap on ParaSwap
const amount_to_swap = ethers.utils.parseEther('10.01');
sakulstra marked this conversation as resolved.
Show resolved Hide resolved
const paraswap = new ParaSwap(parseInt(FORK_NETWORK_ID), PARASWAP_API);
const priceRoute = await paraswap.getRate('WETH', 'DAI', amount_to_swap.toString(), 'SELL', { referrer: 'aave', excludeDEXS: 'Balancer', excludeMPDEXS: 'Balancer' });
const priceRoute = await paraswap.getRate(
'WETH',
'DAI',
amount_to_swap.toString(),
'SELL',
{ referrer: 'aave', excludeDEXS: 'Balancer', excludeMPDEXS: 'Balancer' }
sakulstra marked this conversation as resolved.
Show resolved Hide resolved
);
const { others, ...priceRouteNoOthers } = priceRoute;
console.log('priceRoute:', JSON.stringify(priceRouteNoOthers, null, 2));
if (priceRoute.message) throw new Error('Error getting priceRoute');
const txParams = await paraswap.buildTx('WETH', 'DAI', priceRoute.srcAmount, priceRoute.priceWithSlippage, priceRouteNoOthers, signer.address, 'aave', undefined, { ignoreChecks: true, forceMultiSwap: true });
const txParams = await paraswap.buildTx(
'WETH',
'DAI',
priceRoute.srcAmount,
priceRoute.priceWithSlippage,
priceRouteNoOthers,
signer.address,
'aave',
undefined,
{ ignoreChecks: true, forceMultiSwap: true }
);
console.log('txParams:', txParams);
if (txParams.message) throw new Error('Error getting txParams');
const fromAmountOffset = augustusFromAmountOffsetFromCalldata(txParams.data);

// Create a fork on Tenderly
const fork = new TenderlyFork();
await fork.init();
console.log('Created fork ID', fork.fork_id);

// Connect the signer to the fork
const provider = new ethers.providers.JsonRpcProvider(fork.get_rpc_url(), parseInt(FORK_NETWORK_ID));
const provider = new ethers.providers.JsonRpcProvider(
fork.get_rpc_url(),
parseInt(FORK_NETWORK_ID)
);
signer = signer.connect(provider);

// Fund the account with fake ETH
await fork.fund_account(signer.address);
console.log('Balance was set to', ethers.utils.formatEther(await signer.getBalance()), 'ETH');

// Deploy the adapter
console.log('Deploying ParaSwapLiquiditySwapAdapter...');
const adapterFactory = new ethers.ContractFactory(
ParaSwapLiquiditySwapAdapter_ARTIFACT.abi,
ParaSwapLiquiditySwapAdapter_ARTIFACT.bytecode,
signer
);
const adapter = await adapterFactory.deploy(
AAVE_ADDRESSES_PROVIDER[FORK_NETWORK_ID]
console.log(
'Balance was set to',
ethers.utils.formatEther(await signer.getBalance()),
'ETH'
);
await adapter.deployTransaction.wait();
console.log('Deployed adapter at', adapter.address);

// Initialize the txBuilder
const txBuilder = new TxBuilderV2('mainnet', fork.get_rpc_url());

const lending_pool_address =
distinctContractAddressBetweenMarketsV2['proto']['mainnet']
.LENDINGPOOL_ADDRESS;

// Deposit ETH to get WETH
// I think we should be able to drop this manual wrapping as deposit is capable of handling it - didn't work though & I didn't yet figure out why
console.log('Depositing to WETH...');
const deposit_amount = ethers.utils.parseEther('10');
const weth = new ethers.Contract(
Expand All @@ -70,82 +84,68 @@ async function main() {
signer
);
await (await weth.deposit({ value: deposit_amount })).wait();
console.log('Deposited', ethers.utils.formatEther(deposit_amount), 'ETH as WETH');

// Set allowance on LendingPool
console.log('Setting allowance for LendingPool...');
const addresses_provider = new ethers.Contract(
AAVE_ADDRESSES_PROVIDER[FORK_NETWORK_ID],
ILendingPoolAddressesProvider_ARTIFACT.abi,
signer
console.log(
'Deposited',
ethers.utils.formatEther(deposit_amount),
'ETH as WETH'
);
const lending_pool_address = await addresses_provider.getLendingPool();
await (await weth.approve(lending_pool_address, deposit_amount)).wait();
console.log('Allowance set successfully');

// Deposit WETH in Aave AToken
console.log('Depositing to AToken...');
const depositTxs = await txBuilder.getLendingPool('proto').deposit({
reserve: WETH[FORK_NETWORK_ID], // API_ETH_MOCK_ADDRESS
amount: '10',
user: signer.address,
});
for (const tx of depositTxs) {
await (await signer.sendTransaction(await tx.tx())).wait();
}
const lending_pool = new ethers.Contract(
lending_pool_address,
ILendingPool_ARTIFACT.abi,
signer
);
await (await lending_pool.deposit(
WETH[FORK_NETWORK_ID],
deposit_amount,
signer.address,
0
)).wait();
const aWETH_address = (await lending_pool.getReserveData(WETH[FORK_NETWORK_ID])).aTokenAddress;
const aWETH = new ethers.Contract(
aWETH_address,
IERC20_ARTIFACT.abi,
signer
);
const aWETH_address = (
await lending_pool.getReserveData(WETH[FORK_NETWORK_ID])
).aTokenAddress;
const aWETH = new ethers.Contract(aWETH_address, IERC20_ARTIFACT.abi, signer);
const aWETH_balance_before = await aWETH.balanceOf(signer.address);
console.log(
'Successfully deposited',
ethers.utils.formatEther(aWETH_balance_before),
'WETH in AToken'
);

// Approve adapter to spend ATokens
console.log('Setting allowance for adapter...');
await (await aWETH.approve(adapter.address, priceRoute.srcAmount)).wait();
console.log('Allowance set successfully');

// Perform the swap on the adapter
console.log('Performing swap using swapAndDeposit...');
await (await adapter.swapAndDeposit(
WETH[FORK_NETWORK_ID],
DAI[FORK_NETWORK_ID],
priceRoute.srcAmount,
priceRoute.priceWithSlippage,
fromAmountOffset,
txParams.data,
txParams.to,
{
amount: 0,
deadline: 0,
v: 0,
r: '0x0000000000000000000000000000000000000000000000000000000000000000',
s: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
{ gasLimit: 5000000 }
)).wait();
console.log('Performing swap using swapCollateral...');
const swapTxs = await txBuilder.getLendingPool('proto').swapCollateral({
fromAsset: WETH[FORK_NETWORK_ID],
toAsset: DAI[FORK_NETWORK_ID],
swapAll: true,
fromAToken: aWETH_address,
maxSlippage: '2',
fromAmount: ethers.utils.formatUnits(priceRoute.srcAmount, 18),
toAmount: ethers.utils.formatUnits(priceRoute.priceWithSlippage, 18),
user: signer.address,
flash: false,
augustus: txParams.to,
swapCallData: txParams.data,
});
for (const tx of swapTxs) {
await (await signer.sendTransaction(await tx.tx())).wait();
}
console.log('Swap performed successfully!');
const aDAI_address = (await lending_pool.getReserveData(DAI[FORK_NETWORK_ID])).aTokenAddress;
const aDAI = new ethers.Contract(
aDAI_address,
IERC20_ARTIFACT.abi,
signer
);
const aDAI_address = (await lending_pool.getReserveData(DAI[FORK_NETWORK_ID]))
.aTokenAddress;
const aDAI = new ethers.Contract(aDAI_address, IERC20_ARTIFACT.abi, signer);
const aWETH_balance_after = await aWETH.balanceOf(signer.address);
const aDAI_balance_after = await aDAI.balanceOf(signer.address);
console.log(
'Final balances',
ethers.utils.formatEther(aWETH_balance_after), 'aWETH',
ethers.utils.formatUnits(aDAI_balance_after, 18), 'aDAI'
ethers.utils.formatEther(aWETH_balance_after),
'aWETH',
ethers.utils.formatUnits(aDAI_balance_after, 18),
'aDAI'
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/addresses.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const AAVE_ADDRESSES_PROVIDER = {
'1': '0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5',
1: '0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5',
};
const WETH = {
'1': '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
1: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
};
const DAI = {
'1': '0x6B175474E89094C44Da98b954EedeAC495271d0F',
1: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
};
const API_ETH_MOCK_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';

module.exports = {
AAVE_ADDRESSES_PROVIDER,
API_ETH_MOCK_ADDRESS,
WETH,
DAI,
};
Loading