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

Aave fix for borrowing #1137

Merged
merged 3 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 44 additions & 5 deletions rotkehlchen/chain/ethereum/aave.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
}
ATOKENS_LIST = [EthereumToken(x) for x in ATOKEN_TO_DEPLOYED_BLOCK]

A_LEND = EthereumToken('LEND')


class AaveEvent(NamedTuple):
"""An event related to an Aave aToken
Expand Down Expand Up @@ -150,9 +152,11 @@ def get_balances(
if balance_entry.protocol.name != 'Aave':
continue

# aave only has one underlying balance per base balance which
# is what we will show. This is also the reserve asset
asset = balance_entry.underlying_balances[0]
# Depending on whether it's asset or debt we find what the reserve asset is
if balance_entry.balance_type == 'Asset':
asset = balance_entry.underlying_balances[0]
else:
asset = balance_entry.base_balance
reserve_address, _ = _get_reserve_address_decimals(asset.token_symbol)

reserve_data = reserve_cache.get(reserve_address, None)
Expand All @@ -173,8 +177,43 @@ def get_balances(
else: # 'Debt'
borrowing_map[asset.token_symbol] = AaveBorrowingBalance(
balance=asset.balance,
variable_apr=FVal(reserve_data[4] / RAY),
stable_apr=FVal(reserve_data[4] / RAY),
variable_apr=FVal(reserve_data[5] / RAY),
stable_apr=FVal(reserve_data[6] / RAY),
)

lend_reserve_data = reserve_cache.get(reserve_address, None)
if lend_reserve_data is None:
lend_reserve_data = self.ethereum.call_contract(
contract_address=AAVE_LENDING_POOL.address,
abi=AAVE_LENDING_POOL.abi,
method_name='getReserveData',
arguments=[A_LEND.ethereum_address],
)
reserve_cache['LEND'] = lend_reserve_data

user_lend_data = self.ethereum.call_contract(
contract_address=AAVE_LENDING_POOL.address,
abi=AAVE_LENDING_POOL.abi,
method_name='getUserReserveData',
arguments=[A_LEND.ethereum_address, account],
)
if user_lend_data[0] != 0 or user_lend_data[1] != 0:
lend_usd_price = Inquirer().find_usd_price(A_LEND)

if user_lend_data[0] != 0:
amount = FVal(user_lend_data[0] / (10 ** A_LEND.decimals))
balance = Balance(amount=amount, usd_value=amount * lend_usd_price)
lending_map['LEND'] = AaveLendingBalance(
balance=balance,
apy=FVal(lend_reserve_data[4] / RAY),
)
if user_lend_data[1] != 0:
amount = FVal(user_lend_data[1] / (10 ** A_LEND.decimals))
balance = Balance(amount=amount, usd_value=amount * lend_usd_price)
borrowing_map['LEND'] = AaveBorrowingBalance(
balance=balance,
variable_apr=FVal(lend_reserve_data[5] / RAY),
stable_apr=FVal(lend_reserve_data[6] / RAY),
)

if lending_map == {} and borrowing_map == {}:
Expand Down
6 changes: 6 additions & 0 deletions rotkehlchen/chain/ethereum/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ def _call_contract_etherscan(
to_address=contract_address,
input_data=input_data,
)
if result == '0x':
raise BlockchainQueryError(
f'Error doing call on contract {contract_address} via etherscan.'
f' Returned 0x result',
)

fn_abi = contract._find_matching_fn_abi(
fn_identifier=method_name,
args=arguments,
Expand Down