Subscribe to our free newsletter

To make sure you won't miss any valuable content we share with our community.

Using Brownie for Interacting with Aave Protocol (Borrowing & Repaying)

This article is about what to do for using Brownie for Aave protocol after depositing WETH in the Aave lending pool. In our next step, we will try to borrow some Dai. To do this, we should add some dependency contracts to the contracts folder such as AggregatorV3Interface.sol and LinkTokenInterface.sol. We should also modify the deploy.py and brownie-config.yaml files to adapt to our new functionality.

Adding the necessary contracts to the contracts folder

In this 3rd part of the tutorial, after depositing ETH into the pool, it is time to borrow some DAI using our WETH collateral.
There are 2 interfaces to add to the interfaces folder:

    1. AggregatorV3Interface.sol
    2. LinkTokenInterface.sol


pragma solidity ^ 0.6.6;

interface LinkTokenInterface {
	function allowance(address owner, address spender) external view returns (uint256 remaining);
	function approve(address spender, uint256 value) external returns (bool success);
	function balanceOf(address owner) external view returns (uint256 balance);
	function decimals() external view returns (uint8 decimalPlaces);
	function decreaseApproval(address spender, uint256 addedValue) external returns (bool success);
	function increaseApproval(address spender, uint256 subtractedValue) external;
	function name() external view returns (string memory tokenName);
	function symbol() external view returns (string memory tokenSymbol);
	function totalSupply() external view returns (uint256 totalTokensIssued);
	function transfer(address to, uint256 value) external returns (bool success);
	function transferAndCall(address to, uint256 value, bytes calldata data) external returns (bool 				 
		success);
	function transferFrom(address from, address to, uint256 value) external returns (bool success);
}



Modifying the Deploy.py

In the last part we went up to depositing WETH.



def main():
	account = get_account()
	erc20_address = config["networks"][network.show_active()]["weth_token"]
	if network.show_active() in ["mainnet-fork"]:
		get_weth(account=account)
	lending_pool = get_lending_pool()
	approve_erc20(amount, lending_pool.address, erc20_address, account)
	print("Depositing...")
	lending_pool.deposit(erc20_address, amount, account.address, 0, {"from": account})
	print("Deposited!")


We continue the def main () like this to borrow DAI:


borrowable_eth, total_debt_eth = get_borrowable_data(lending_pool, account)
	print(f"LETS BORROW IT ALL")
	erc20_eth_price = get_asset_price()
	amount_erc20_to_borrow = (1 / erc20_eth_price) * (borrowable_eth * 0.95)
	print(f"We are going to borrow {amount_erc20_to_borrow} DAI")
	borrow_erc20(lending_pool, amount_erc20_to_borrow, account)
	borrowable_eth, total_debt_eth = get_borrowable_data(lending_pool, account)


The above process includes:

    1. Getting the data of how much we can borrow based on how much we have deposited and how much debt we have.
    2. Getting the price of the asset we are going to borrow
    3. Calculating the amount that we are going to borrow (it is multiplied by 0.95 as we can only borrow 95 percent of our collateral at most.)
    4. Borrowing the DAI.


def get_borrowable_data(lending_pool, account):
		(total_collateral_eth,
		total_debt_eth,
		available_borrow_eth,
		current_liquidation_threshold,
		tlv,
		health_factor, ) = lending_pool.getUserAccountData(account.address)
	available_borrow_eth = Web3.fromWei(available_borrow_eth, "ether")
	total_collateral_eth = Web3.fromWei(total_collateral_eth, "ether")
	total_debt_eth = Web3.fromWei(total_debt_eth, "ether")
	print(f"You have {total_collateral_eth} worth of ETH deposited.")
	print(f"You have {total_debt_eth} worth of ETH borrowed.")
	print(f"You can borrow {available_borrow_eth} worth of ETH.")
	return (float(available_borrow_eth), float(total_debt_eth))


The above function retrieves the data related to how much we can borrow based on our account and the lending pool we have deposited our money.


def borrow_erc20(lending_pool, amount, account, erc20_address=None):
	erc20_address = (
	erc20_address
	if erc20_address
	else config["networks"][network.show_active()]["aave_dai_token"])
	transaction = lending_pool.borrow(
		erc20_address,
		Web3.toWei(amount, "ether"),
		1,
		0,
		account.address,
		{"from": account},
		)
	transaction.wait(1)
	print(f"Congratulations! We have just borrowed {amount}")


The above function executes the borrowing.


def get_asset_price():
	dai_eth_price_feed = interface.AggregatorV3Interface(
		config["networks"][network.show_active()]["dai_eth_price_feed"])
	latest_price = Web3.fromWei(dai_eth_price_feed.latestRoundData()[1], 														 
		"ether")
	print(f"The DAI/ETH price is {latest_price}")
	return float(latest_price)


Using the above function, we get the DAI/ETH pair price.

Brownie_config.yaml

The complete brownie-config.yaml file goes like this:


dependencies:
	- smartcontractkit/[email protected]
	- aave/[email protected]
compiler:
	solc:
		remappings:
			- "@chainlink=smartcontractkit/chainlink-brownie-												[email protected]"
			- "@aave=aave/[email protected]"
autofetch_sources: True
dotenv: .env
verify: False
networks:
	default: mainnet-fork
	mainnet-fork:
		lending_pool_addresses_provider: "0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5"
		weth_token: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
		link_token: "0x514910771af9ca656af840dff83e8264ecf986ca"
		aave_link_token: "0x514910771af9ca656af840dff83e8264ecf986ca"
		aave_dai_token: "0x6b175474e89094c44da98b954eedeac495271d0f"
		dai_token: "0x6b175474e89094c44da98b954eedeac495271d0f"
		link_eth_price_feed: "0xDC530D9457755926550b59e8ECcdaE7624181557"
		dai_eth_price_feed: "0x773616E4d11A78F511299002da57A0a94577F1f4"
	kovan:
		vrf_coordinator: "0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9"
		aave_link_token: "0xAD5ce863aE3E4E9394Ab43d4ba0D80f419F61789"
		aave_dai_token: "0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD"
		link_token: "0xa36085F69e2889c224210F603D836748e7dC0088"
		keyhash: "0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4"
		fee: 100000000000000000
		oracle: "0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e"
		jobId: "29fa9aa13bf1468788b7cc4a500a45b8"
		eth_usd_price_feed: "0x9326BFA02ADD2366b30bacB125260Af641031331"
		link_eth_price_feed: "0x3Af8C569ab77af5230596Acf0E8c2F9351d24C38"
		dai_eth_price_feed: "0x22B58f1EbEDfCA50feF632bD73368b2FdA96D541"

		lending_pool_addresses_provider: "0x88757f2f99175387ab4c6a4b3067c77a695b0349"
		weth_token: "0xd0a1e359811322d97991e03f863a0c30c2cf029c"
wallets:
	from_key: ${PRIVATE_KEY}



Deploying the Contracts

Now, we can finally borrow our desired asset:

brownie run scripts/aave_borrow.py --network kovan

Result:

Brownie v1.18.1 - Python development framework for Ethereum AaveBrownieProject is the active project. Running 'scripts/aave_borrow.py::main'... Approving ERC20... Transaction sent: 0x0612e2c08897577cedef93950fb35d4e8710c29032d6ae5dcb0f202a27d9f3b2 Gas price: 2.500000007 gwei Gas limit: 50695 Nonce: 15 WETH9_.approve confirmed Block: 30943344 Gas used: 46087 (90.91%) WETH9_.approve confirmed Block: 30943344 Gas used: 46087 (90.91%) Approved! Depositing... Transaction sent: 0xf2fe4bb2e82189d2173cf7b147c24af47fa172061a0ac7c37a434ddc68bd984f Fetching source of 0x2646FcF7F0AbB1ff279ED9845AdE04019C907EBE from api-kovan.etherscan.io... Gas price: 2.500000007 gwei Gas limit: 207906 Nonce: 16 Transaction confirmed Block: 30943347 Gas used: 176916 (85.09%) Deposited! You have 0.400410656100777314 worth of ETH deposited. You have 1.1287963056E-8 worth of ETH borrowed. You can borrow 0.320328513592658795 worth of ETH. LETS BORROW IT ALL The DAI/ETH price is 0.00030355 We are going to borrow 1002.5105844606354 DAI Transaction sent: 0xd1d8b7f23e2ea35eb12dc30f5142d5614068a652e77fa811c6830dde766f1f26 Fetching source of 0x2646FcF7F0AbB1ff279ED9845AdE04019C907EBE from api-kovan.etherscan.io... Gas price: 2.500000007 gwei Gas limit: 353287 Nonce: 17 Transaction confirmed Block: 30943350 Gas used: 316618 (89.62%) This transaction already has 2 confirmations. Congratulations! We have just borrowed 1002.5105844606354 You have 0.400410804230975243 worth of ETH deposited. You have 0.304312101427770057 worth of ETH borrowed. You can borrow 0.016016541957010137 worth of ETH.

And as you can see we have got nearly 1002 DAI tokens as a result of borrowing from the Aave pool. Notice that the amount of deposited ETH is 0.4 which is the result of multiple times we have tested the deposit function and also a little more than 0.4 is because of the yield that Aave pool has considered for us as a result of depositing our ETH. In the next part, we are going to repay all of the borrowed DAI.

In this tutorial, after depositing WETH and borrowing Dai, it is time to repay back the funds we have deposited. To do this we will modify the deploy.py to completely do the 3 processes of swapping ETH for WETH, depositing WETH, borrowing DAI, and repaying the funds. As in the recent steps we hadn’t completely done the 4 tasks and as a result, we hadn’t repaid back the funds, we need to manually repay the remaining funds.

Modifying the deploy.py:

The final step of interacting with the Aave lending pool is to repay the DAI that we have borrowed. The complete steps of depositing, borrowing, and repaying back are included in this main function with the other functions we created in the recent parts:


def main():
	account = get_account()
	erc20_address = config["networks"][network.show_active()]["weth_token"]
	if network.show_active() in ["mainnet-fork"]:
		get_weth(account=account)
	lending_pool = get_lending_pool()
	approve_erc20(amount, lending_pool.address, erc20_address, account)
	print("Depositing...")
	lending_pool.deposit(erc20_address, amount, account.address, 0, {"from": account})
	print("Deposited!")
	borrowable_eth, total_debt_eth = get_borrowable_data(lending_pool, account)
	print(f"LETS BORROW IT ALL")
	erc20_eth_price = get_asset_price()
	amount_erc20_to_borrow = (1 / erc20_eth_price) * (borrowable_eth * 0.95)
	print(f"We are going to borrow {amount_erc20_to_borrow} DAI")
	borrow_erc20(lending_pool, amount_erc20_to_borrow, account)
	borrowable_eth, total_debt_eth = get_borrowable_data(lending_pool, account)



We continue the def main() with the following:



	repay_all(amount_erc20_to_borrow, lending_pool, account)
	get_borrowable_data(lending_pool, account)


And also, create a new function called repay_all:


def repay_all(amount, lending_pool, account):
	approve_erc20(Web3.toWei(amount, "ether"),
			lending_pool,
			config["networks"][network.show_active()]["aave_dai_token"],
			account,
			)
	tx = lending_pool.repay(
			config["networks"][network.show_active()]["aave_dai_token"],
			Web3.toWei(amount, "ether"),
			1,
			account.address,
			{"from": account},
			)
	tx.wait(1)
	print("Repaid!")



Deploying on the Kovan network:

Now, it is time to test the whole process:

brownie run scripts/aave_borrow.py --network kovan

Result:

Brownie v1.18.1 - Python development framework for Ethereum AaveBrownieProject is the active project. Running 'scripts/aave_borrow.py::main'... Approving ERC20... Transaction sent: 0x607f92b4795a1b344f27eb517b14b382b3af62060861bcb7e17fe56f2e49dbf4 Gas price: 2.500000007 gwei Gas limit: 50695 Nonce: 24 WETH9_.approve confirmed Block: 30944173 Gas used: 46087 (90.91%) WETH9_.approve confirmed Block: 30944173 Gas used: 46087 (90.91%) Approved! Depositing... Transaction sent: 0x8b19aae38d08356a19a336746530be83fb621e64c566d8777382f04dd97feb61 Fetching source of 0x2646FcF7F0AbB1ff279ED9845AdE04019C907EBE from api-kovan.etherscan.io... Gas price: 2.500000007 gwei Gas limit: 207906 Nonce: 25 Transaction confirmed Block: 30944175 Gas used: 176916 (85.09%) Deposited! You have 0.600462332371017026 worth of ETH deposited. You have 0.395594815443779104 worth of ETH borrowed. You can borrow 0.084775050453034517 worth of ETH. LETS BORROW IT ALL The DAI/ETH price is 0.00030417 We are going to borrow 264.7739682755788 DAI Transaction sent: 0x114bf44d1ee6ca2490dbb32276e9f7abb91078c2b157c42edede91fbc331024d Fetching source of 0x2646FcF7F0AbB1ff279ED9845AdE04019C907EBE from api-kovan.etherscan.io... Gas price: 2.500000007 gwei Gas limit: 330966 Nonce: 26 Transaction confirmed Block: 30944177 Gas used: 299518 (90.50%) This transaction already has 2 confirmations. Congratulations! We have just borrowed 264.7739682755788 You have 0.600462628542463152 worth of ETH deposited. You have 0.476131125542422592 worth of ETH borrowed. You can borrow 0.00423897729154793 worth of ETH. Approving ERC20... Transaction sent: 0x442a4ae4ee05339a7081fd4c91d231e71d9f30dfc489cc1ba511e3c64143587a Gas price: 2.500000007 gwei Gas limit: 31988 Nonce: 27 Transaction confirmed Block: 30944180 Gas used: 29080 (90.91%) Transaction confirmed Block: 30944180 Gas used: 29080 (90.91%) Approved! Transaction sent: 0x20529c85bc1852bce18341ed3404094ba1af3db58f81ade3145edea7afefb330 Fetching source of 0x2646FcF7F0AbB1ff279ED9845AdE04019C907EBE from api-kovan.etherscan.io... Gas price: 2.500000007 gwei Gas limit: 242655 Nonce: 28 Transaction confirmed Block: 30944181 Gas used: 212817 (87.70%) Transaction confirmed Block: 30944181 Gas used: 212817 (87.70%) Repaid! You have 0.60046299875677081 worth of ETH deposited. You have 0.395594844442992547 worth of ETH borrowed. You can borrow 0.084775554562424101 worth of ETH.

As you can see, we have successfully deposited WETH, borrowed some DAI, and repaid what we have borrowed. Now let’s check our Metamask wallet.

Repaying all funds:

You might wonder why we still have some DAI in it. The reason is that, in our recent code run, we didn’t have to repay. As a result, we still have some of the DAIs that we haven’t repaid back. To do so, change the main function to:


def main():
	account = get_account()
	erc20_address = config["networks"][network.show_active()]["weth_token"]
	if network.show_active() in ["mainnet-fork"]:
		get_weth(account=account)
	lending_pool = get_lending_pool()
	approve_erc20(amount, lending_pool.address, erc20_address, account)
	borrowable_eth, total_debt_eth = get_borrowable_data(lending_pool, account)
	amount_erc20_to_repay =  # enter the remaining to repay


We should determine the amount we want to repay in the amount_erc20_to_repay.


repay_all(amount_erc20_to_repay, lending_pool, account)
get_borrowable_data(lending_pool, account)


Or instead, you can repay manually using the Aave interface:

Click repay next to DAI and then select max under the DAI icon in the pop-up window, and click approve, after that confirm the Metamask pop-up:

Now, you can repay DAI:

Confirm Metamask pop-up again:

You can also withdraw by following the steps in the below photos. Notice that for both repay and withdraw you need to get approval from your Metamask wallet and click confirm and then repay or withdraw.

Conclusion

In this article, we have managed to borrow some Dai from the Aave lending pool. To do this, we have added some dependency contracts like LinkTokenInterface.sol and AggregatorV3Interface.sol. We have also modified the deploy.py file to do the borrowing function for us.
In this tutorial, we have managed to do the complete process of the Aave lending pool, meaning that we have swapped ETH for WETH, deposited WETH, borrowed Dai, and finally repaid borrowed Dai using Brownie python tools.

Download this Article in PDF format

3d animation

Check Out Our Services

In Arashtad, we’re working on 3D games, metaverses, and other types of WebGL and 3D applications with our 3D web development team. However, our services are not limited to these. Back-end developments, front-end developments, 3d modeling, and animations are in our arsenal too.

Arashtad Serivces
Drop us a message and tell us about your ideas.
Tell Us What You Need
Blockchain Development

Using Python Framework Brownie for Interacting with Aave Protocol

In this tutorial, regarding the Aave protocol, we are going to interact with the Aave protocol using Brownie. The first step is to swap ETH with WETH which is necessary when we want to deal with Aave. This lending and borrowing protocol, accept WETH instead of ETH. We add WETH (Wrapped Ethereum) to our Metamask wallet and also create the project folder for interacting with the Aave protocol. After the first (which was swapping ETH with WETH), it is now time to deposit our WETH. To do this, we will download some solidity scripts related to the Aave lending pool, and paste them into the contracts directory of the project folder. Finally, modify the deploy.py to also activate the depositing contracts.

Interacting with Aave Protocol by Using Brownie

This series of tutorials is the continuation of How to deploy a smart contract using python web3 tools in addition to solidity, smart contracts, and brownie python tools. As a result, it is highly recommended that before you start this tutorial, be familiar with the python web3 and smart contracts. In the last tutorial, we learned how to interact with the Aave protocol using the interface provided at this link. In this part, we are going to do the same interactions using python scripts.
These interactions include:

    1. Swapping ETH with WETH (Wrapped ETH).
    2. Depositing WETH into Aave.
    3 Using WETH collateral to borrow DAI
    4. Paying back the borrowed DAI

Creating the Project

So, like every project, we begin with: mkdir aave_brownie
cd aave_brownie
code .
Brownie init

The first thing you should do to interact with the Aave protocol is to deposit some ETH into Aave. When you do this, the actual thing that happens is that ETH is swapped into the ERC-20 version of Ethereum, called WETH ( Wrapped Ethereum). Because in our project we use the Kovan network, we should copy the address of the contract from this link and paste it into our config file (brownie-config.yaml).

using Brownie for Aave protocol

If you go to write the contract, you will see an actual interface of the WETH contract for deposit and withdrawal.

using Brownie for Aave protocol

Now, let’s write our scripts, we begin with creating a file called get_weth.py in the scripts folder:


from brownie import accounts, config, network, interface
from scripts.helpful_scripts import get_account

def main():
	get_weth()

def get_weth(account=None):
	account = get_account()
	weth = interface.Weth(
	config["networks"][network.show_active()]["weth_token"])
	tx = weth.deposit({"from": account, "value": 0.1 * 1e18})
	print("Received 0.1 WETH")
	return tx


Use the helpful_scripts.py to get the proper account in the scripts folder:


from brownie import network, config, accounts
def get_account():
	if network.show_active() in ["hardhat", "development", "mainnet-fork"]:
		return accounts[0]
	if network.show_active() in config["networks"]:
		account = accounts.add(config["wallets"]["from_key"])
	return account
	return None



pragma solidity ^ 0.4.19;

interface Weth {
	function allowance(address owner, address spender) external view returns (uint256 remaining);
	function approve(address spender, uint256 value) external returns (bool success);
	function balanceOf(address owner) external view returns (uint256 balance);
	function decimals() external view returns (uint8 decimalPlaces);
	function name() external view returns (string memory tokenName);
	function symbol() external view returns (string memory tokenSymbol);
	function totalSupply() external view returns (uint256 totalTokensIssued);
	function transfer(address to, uint256 value) external returns (bool success);
	function transferFrom(address from, address to, uint256 value) external returns(bool success);
	function deposit() external;
	function withdraw(uint wad) external;
}


And an interface named IWeth.sol in the interface folder (you can notice that it has the methods and events of an ERC20 token):


dotenv: .env
networks:
	mainnet-fork:
		weth_token: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"

	kovan:
		weth_token: "0xd0a1e359811322d97991e03f863a0c30c2cf029c"
wallets:
	from_key: ${PRIVATE_KEY}


Also, enter your private key and Infura ID in the .env file, in the main directory:


export WEB3_INFURA_PROJECT_ID=' your Infura ID'
export PRIVATE_KEY=' Your private key '


It is now time to run our project on the Kovan test network to swap ETH with WETH:

brownie run scripts/get_weth.py --network kovan

Result:

Brownie v1.18.1 - Python development framework for Ethereum AaveBrownieProject is the active project. Running 'scripts/get_weth.py::main'... Transaction sent: 0x96955d6833e1ceb11e8de71d8c64cffd4af84765ceb7c333e6fa666c2f2e21f5 Gas price: 2.500000007 gwei Gas limit: 49572 Nonce: 7 WETH9_.deposit confirmed Block: 30924833 Gas used: 45066 (90.91%) Received 0.1 WETH

To make sure we have actually received the desired WETH, let’s head over to our Metamask and import WETH token by pasting the address of WETH Kovan contract in the Token Contract Address box:

using Brownie for Aave protocol

And there we go! We have 0.1 WETH in our account in exchange for 0.1 ETH.

brownie

And there we go! We have 0.1 WETH in our account in exchange for 0.1 ETH.

brownie

After our first approach towards using Brownie for Aave protocol (which was swapping ETH with WETH), it is now time to deposit our WETH. To do this, we will download some solidity scripts related to the Aave lending pool, and paste them into the contacts directory of the project folder. And finally, modify the deploy.py to also activate the depositing contracts.

Depositing WETH

After having successfully swapped ETH with WETH, it is time to deposit our WETH to the lending pool, so let’s keep the scripts we added to our project, and add some more files to be able to do this task. First and foremost, we need to export our Etherscan API key in the .env file:


export ETHERSCAN='Your Etherscan API key'

The next thing we need to do is to add some interfaces to the interfaces folder from the Aave GitHub repository. These files include:

    1. ILendingPoolAddressesProvider.sol
    2. ILendingPool.sol

Notice that you should modify lines 5 and 6 in the code to be able to use it in your directory:


import {ILendingPoolAddressesProvider} from 	 
	'@aave/contracts/interfaces/IlendingPoolAddressesProvider.sol';
import {DataTypes} from '@aave/contracts/protocol/libraries/types/DataTypes.sol';


Besides, in the brownie-config.yaml, add these lines:


dependencies:
	- smartcontractkit/[email protected]
	- aave/[email protected]
compiler:
	solc:
		remappings:
			- "@chainlink=smartcontractkit/[email protected]"
			- "@aave=aave/[email protected]"


IERC20.sol

We also need to paste in ERC-20 smart contract in the contracts directory:


pragma solidity ^ 0.6.6;

interface IERC20 {
	function allowance(address owner, address spender) external view returns (uint256 remaining);
	function approve(address spender, uint256 value) external returns (bool success);
	function balanceOf(address owner) external view returns (uint256 balance);
	function decimals() external view returns (uint8 decimalPlaces);
	function decreaseApproval(address spender, uint256 addedValue) external returns (bool success);
	function increaseApproval(address spender, uint256 subtractedValue) external;
	function name() external view returns (string memory tokenName);
	function symbol() external view returns (string memory tokenSymbol);
	function totalSupply() external view returns (uint256 totalTokensIssued);
	function transfer(address to, uint256 value) external returns (bool success);
	function transferFrom(address from, address to, uint256 value) external returns (bool success);
}


brownie_config.yaml:

In the brownie-config.yaml file, we also need to add some specifications about the networks:


networks:
	default: mainnet-fork
	mainnet-fork:
		lending_pool_addresses_provider: "0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5"
		weth_token: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
	kovan:
		lending_pool_addresses_provider: "0x88757f2f99175387ab4c6a4b3067c77a695b0349"
		weth_token: "0xd0a1e359811322d97991e03f863a0c30c2cf029c"


aave_borrow.py

And finally we create a file called aave_borrow.py to start our depositing process:


from brownie import accounts, config, interface, network
from web3 import Web3
from scripts.get_weth import get_weth
from scripts.helpful_scripts import get_account

amount = Web3.toWei(0.1, "ether")


The above function will convert 0.1 ether into a Wei unit. We use this conversion to avoid writing many zeros after 1.


def main():
	account = get_account()
	erc20_address = config["networks"][network.show_active()]["weth_token"]
	if network.show_active() in ["mainnet-fork"]:
		get_weth(account=account)
	lending_pool = get_lending_pool()
	approve_erc20(amount, lending_pool.address, erc20_address, account)
	print("Depositing...")
	lending_pool.deposit(erc20_address, amount, account.address, 0, {"from": account})
	print("Deposited!")


The above function will get the account address depending on the active network and then gets the ERC-20 address of the WETH token according to the chosen network and then gets some WETH token if the Mainnet-fork network is active (remember in the last tutorial we got some WETH in the Kovan network but not in the Mainnet-fork). After that, we will get the lending pool using the function get_lending_pool(). We also approve the ERC-20 transaction using the approve_erc20 function and in the end, we deposit the WETH in the lending pool.


def get_lending_pool():
	lending_pool_addresses_provider = interface.ILendingPoolAddressesProvider(
		config["networks"][network.show_active()]["lending_pool_addresses_provider"])
	lending_pool_address = lend-ing_pool_addresses_provider.getLendingPool()
	lending_pool = interface.ILendingPool(lending_pool_address)
	return lending_pool


The above function uses the address of the lending pool according to the active network and then returns the lending pool according to the address. Notice that we use the function of the IlendingPoolAddressesProvider.sol file.


def approve_erc20(amount, lending_pool_address, erc20_address, account):
	print("Approving ERC20...")
	erc20 = interface.IERC20(erc20_address)
	tx_hash = erc20.approve(lending_pool_address, amount, {"from": account})
	tx_hash.wait(1)
	print("Approved!")
	return True


The above function will approve the ERC-20 using the IERC20.sol file.


if __name__ == "__main__":
	main()


Compiling and Running the Project

After having set all files up, it is time to compile our project:

brownie compile

Result:

Brownie v1.18.1 - Python development framework for Ethereum Generating interface ABIs... Project has been compiled. Build artifacts saved at /home/mohamad/AAVE_brownie/build/contracts

Finally, we deposit the WETH into the lending pool on the Kovan network.

brownie run scripts/aave_borrow.py --network kovan

Result:

Brownie v1.18.1 - Python development framework for Ethereum AaveBrownieProject is the active project. Running 'scripts/aave_borrow.py::main'... Approving ERC20... Transaction sent: 0x0ea015becac17bb9f7a40dd6f93fbe8dd2043c8c41f35baf2158e5c9d58ce73a Gas price: 2.500000007 gwei Gas limit: 28805 Nonce: 12 WETH9_.approve confirmed Block: 30939584 Gas used: 26187 (90.91%) WETH9_.approve confirmed Block: 30939584 Gas used: 26187 (90.91%) Approved! Depositing... Transaction sent: 0x83cbdafc218186ee61a02097cd14dff42cce5dc1df4f2d66518d1b6fc6ab6501 Fetching source of 0x2646FcF7F0AbB1ff279ED9845AdE04019C907EBE from api-kovan.etherscan.io... Gas price: 2.500000007 gwei Gas limit: 207906 Nonce: 13 Transaction confirmed Block: 30939586 Gas used: 176916 (85.09%) Deposited!

As you can see, we have successfully deposited our 0.1 WETH into the Aave lending pool. In the next part, we are going to borrow some DAI from the pool.

Conclusion:

In this tutorial, we have managed to interact with the Aave protocol using Brownie. The first thing we did was to swap ETH with WETH which was a necessary step when we want to deal with Aave. This lending and borrowing protocol accepts WETH instead of ETH. We added WETH (Wrapped Ethereum) to our Metamask wallet and also created the project folder for interacting with the Aave protocol.
In this tutorial, we have downloaded the lending pool smart contract from the Aave protocol GitHub account and pasted them into our directory. In addition to that, we have modified the deploy.py file to deploy the depositing smart contracts as well as adding the aave_borrow.py file, and eventually, we have deposited the WETH we have in the Aave lending pool.

Download this Article in PDF format

metaverse

What Do We Do in Arashtad?

3D websites, 3D games, metaverses, and other types of WebGL and 3D applications are what we develop in our 3D web development team.

Arashtad Serivces
Drop us a message and tell us about your ideas.
Request a Quote
ThreeJS Development

How to Interact with the Aave Protocol Directly + Testnet User Interface

In this tutorial, regarding the Aave protocol, we are going to connect our Metamask to the Aave website to be able to interact with the Aave protocol directly. Then, we will switch to the Testnet. We will also get some Kovan ETH from the Chainlink Kovan faucet. Using the test ETH we have got, we will deposit it, withdraw Dai, and then finally pay back all the funds.

What Is Aave Protocol?

Aave is a decentralized finance (DeFi) protocol that allows us to borrow and lend our crypto assets. It provides tools to deposit our money and get some yields or borrow some money and repay whenever we want with a certain APY.
Now, we are going to first interact manually with the user interface at the Aave web app and then get into the code interaction with the Aave protocol.

Connecting Metamask to Aave:

First of all, you should connect your Metamask wallet to the Aave web app.

Interact with Aave Protocol

Then from the top right settings button, switch to Testnet mode

Interact with Aave Protocol

Getting Kovan ETH:

Also, don’t forget to get some Kovan ETH from this link:

Interact with Aave Protocol

Interacting with Aave Protocol Directly

Once your wallet is connected and you have got some kovan ETH, you will be able to see that your available asset on kovan test network is displayed on the webpage:

Interact with Aave Protocol

Now, in order to deposit or supply some ETH, Click supply and choose the amount of ETH you want to supply or lend. And then click Supply ETH.

Interact with Aave Protocol

You will see the Metamask pop-up asking for confirmation:

Interact with Aave Protocol

And there we go! We have deposited 0.1 ETH.

Interact with Aave Protocol

You can activate and deactivate the collateral option, you can also withdraw the money any time you want, and if you balance you will notice that the amount increases little by little.

Interact with Aave Protocol

You can also borrow any other assets that you want. To do that, you should click borrow next to the token that you want to borrow and choose the borrow APY rate and check the health factor to avoid being liquidated.

Interact with Aave Protocol

Click borrow DAI (the token we have chosen to borrow) and confirm the Metamask pop-up.

Aave Protocol

Now, you can see the borrows and supplies (deposits) lists on top.

Aave Protocol

You can repay DAI whenever you want. To do that, click repay:

Aave Protocol

Notice that even if you choose the max amount, you cannot repay all of it because the amount of debt has increased with time because of APY. Click approve to continue and confirm the Metamask pop-up.

Aave Protocol

After it is successfully done, you will be able to see that your debt has decreased to a very small fraction of DAI.

Aave Protocol

In the next part, we are going to interact with these tools through python scripts.

Last Thought

In this tutorial, we have managed to connect our Metamask to the Aave website to be able to interact with the Aave protocol directly. Then, we switched to Testnet. We also got some Kovan ETH from the Chainlink Kovan faucet. Using the test ETH we have got, we have managed to deposit it, withdraw Dai, and pay it back.

Download this Article in PDF format

metaverse

We Are Working on Metaverses

Metaverses, 3d modeling, 3d application, 3d websites, and even animations are just some of our vase expertise.

Arashtad Serivces
Tell us about your ideas
Fill in the Form
Blockchain Development