def _find_closest_event( join_events: List[Dict[str, Any]], exit_events: List[Dict[str, Any]], index: int, comparison: Callable, ) -> Optional[Dict[str, Any]]: """Given lists of events and index/comparisonop find the closest event Index and comparisonon depend on whether we are searching for the events backwards or forwards. """ found_event = None if len(join_events) != 0: found_event = join_events[index] if len(exit_events) != 0: if found_event: try: join_number = deserialize_blocknumber( found_event['blockNumber']) exit_number = deserialize_blocknumber( exit_events[index]['blockNumber']) except DeserializationError as e: msg = f'Error at reading DSR drip event block number. {str(e)}' raise ChiRetrievalError(msg) if comparison(exit_number, join_number): found_event = exit_events[index] else: found_event = exit_events[index] return found_event
def _get_comp_events( self, address: ChecksumEthAddress, from_ts: Timestamp, to_ts: Timestamp, ) -> List[CompoundEvent]: self.ethereum.etherscan.get_blocknumber_by_time(from_ts) from_block = max( COMP_DEPLOYED_BLOCK, self.ethereum.etherscan.get_blocknumber_by_time(from_ts), ) argument_filters = { 'from': COMPTROLLER_PROXY.address, 'to': address, } comp_events = self.ethereum.get_logs( contract_address=A_COMP.ethereum_address, abi=ERC20TOKEN_ABI, event_name='Transfer', argument_filters=argument_filters, from_block=from_block, to_block=self.ethereum.etherscan.get_blocknumber_by_time(to_ts), ) events = [] for event in comp_events: timestamp = self.ethereum.get_event_timestamp(event) amount = token_normalized_value(hex_or_bytes_to_int(event['data']), A_COMP) usd_price = query_usd_price_zero_if_error( asset=A_COMP, time=timestamp, location='comp_claim', msg_aggregator=self.msg_aggregator, ) value = Balance(amount, amount * usd_price) events.append( CompoundEvent( event_type='comp', address=address, block_number=deserialize_blocknumber(event['blockNumber']), timestamp=timestamp, asset=A_COMP, value=value, to_asset=None, to_value=None, realized_pnl=value, tx_hash=event['transactionHash'], log_index=deserialize_int_from_hex_or_int( event['logIndex'], 'comp log index'), )) return events
def _get_vault_withdraw_events( self, vault: YearnVault, address: ChecksumEthAddress, from_block: int, to_block: int, ) -> List[YearnVaultEvent]: """Get all withdraw events of the underlying token to the vault""" events: List[YearnVaultEvent] = [] argument_filters = {'from': vault.contract.address, 'to': address} withdraw_events = self.ethereum.get_logs( contract_address=vault.underlying_token.ethereum_address, abi=ERC20TOKEN_ABI, event_name='Transfer', argument_filters=argument_filters, from_block=from_block, to_block=to_block, ) for withdraw_event in withdraw_events: timestamp = self.ethereum.get_event_timestamp(withdraw_event) withdraw_amount = token_normalized_value( token_amount=hex_or_bytes_to_int(withdraw_event['data']), token=vault.token, ) tx_hash = withdraw_event['transactionHash'] tx_receipt = self.ethereum.get_transaction_receipt(tx_hash) withdraw_index = deserialize_int_from_hex_or_int( withdraw_event['logIndex'], 'yearn withdraw log index', ) burn_amount = None for log in tx_receipt['logs']: found_event = ( log['topics'][0] == '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' and # noqa: E501 log['topics'][1] == address_to_bytes32(address) and log['topics'][2] == address_to_bytes32(ZERO_ADDRESS)) if found_event: # found the burn log burn_amount = token_normalized_value( token_amount=hex_or_bytes_to_int(log['data']), token=vault.token, ) if burn_amount is None: self.msg_aggregator.add_error( f'Ignoring yearn withdraw event with tx_hash {tx_hash} and log index ' f'{withdraw_index} due to inability to find corresponding burn event', ) continue withdraw_usd_price = get_usd_price_zero_if_error( asset=vault.underlying_token, time=timestamp, location='yearn vault withdraw', msg_aggregator=self.msg_aggregator, ) burn_usd_price = get_usd_price_zero_if_error( asset=vault.token, time=timestamp, location='yearn vault withdraw', msg_aggregator=self.msg_aggregator, ) events.append( YearnVaultEvent( event_type='withdraw', block_number=deserialize_blocknumber( withdraw_event['blockNumber']), timestamp=timestamp, from_asset=vault.token, from_value=Balance( amount=burn_amount, usd_value=burn_amount * burn_usd_price, ), to_asset=vault.underlying_token, to_value=Balance( amount=withdraw_amount, usd_value=withdraw_amount * withdraw_usd_price, ), realized_pnl=None, tx_hash=tx_hash, log_index=withdraw_index, )) return events
def get_events_for_atoken_and_address( self, user_address: ChecksumEthAddress, atoken: EthereumToken, deposit_events: List[Dict[str, Any]], withdraw_events: List[Dict[str, Any]], from_block: int, to_block: int, ) -> List[AaveEvent]: """This function should be entered while holding the history_lock semaphore""" argument_filters = { 'from': ZERO_ADDRESS, 'to': user_address, } mint_events = self.ethereum.get_logs( contract_address=atoken.ethereum_address, abi=ATOKEN_ABI, event_name='Transfer', argument_filters=argument_filters, from_block=from_block, to_block=to_block, ) mint_data = set() mint_data_to_log_index = {} for event in mint_events: amount = hex_or_bytes_to_int(event['data']) if amount == 0: continue # first mint can be for 0. Ignore entry = ( deserialize_blocknumber(event['blockNumber']), amount, self.ethereum.get_event_timestamp(event), event['transactionHash'], ) mint_data.add(entry) mint_data_to_log_index[entry] = deserialize_int_from_hex_or_int( event['logIndex'], 'aave log index', ) reserve_asset = _atoken_to_reserve_asset(atoken) reserve_address, decimals = _get_reserve_address_decimals( reserve_asset.identifier) aave_events = [] for event in deposit_events: if hex_or_bytes_to_address(event['topics'][1]) == reserve_address: # first 32 bytes of the data are the amount deposit = hex_or_bytes_to_int(event['data'][:66]) block_number = deserialize_blocknumber(event['blockNumber']) timestamp = self.ethereum.get_event_timestamp(event) tx_hash = event['transactionHash'] log_index = deserialize_int_from_hex_or_int( event['logIndex'], 'aave log index') # If there is a corresponding deposit event remove the minting event data entry = (block_number, deposit, timestamp, tx_hash) if entry in mint_data: mint_data.remove(entry) del mint_data_to_log_index[entry] usd_price = query_usd_price_zero_if_error( asset=reserve_asset, time=timestamp, location='aave deposit', msg_aggregator=self.msg_aggregator, ) deposit_amount = deposit / (FVal(10)**FVal(decimals)) aave_events.append( AaveEvent( event_type='deposit', asset=reserve_asset, value=Balance( amount=deposit_amount, usd_value=deposit_amount * usd_price, ), block_number=block_number, timestamp=timestamp, tx_hash=tx_hash, log_index=log_index, )) for data in mint_data: usd_price = query_usd_price_zero_if_error( asset=atoken, time=data[2], location='aave interest profit', msg_aggregator=self.msg_aggregator, ) interest_amount = data[1] / (FVal(10)**FVal(decimals)) aave_events.append( AaveEvent( event_type='interest', asset=atoken, value=Balance( amount=interest_amount, usd_value=interest_amount * usd_price, ), block_number=data[0], timestamp=data[2], tx_hash=data[3], log_index=mint_data_to_log_index[data], )) for event in withdraw_events: if hex_or_bytes_to_address(event['topics'][1]) == reserve_address: # first 32 bytes of the data are the amount withdrawal = hex_or_bytes_to_int(event['data'][:66]) block_number = deserialize_blocknumber(event['blockNumber']) timestamp = self.ethereum.get_event_timestamp(event) tx_hash = event['transactionHash'] usd_price = query_usd_price_zero_if_error( asset=reserve_asset, time=timestamp, location='aave withdrawal', msg_aggregator=self.msg_aggregator, ) withdrawal_amount = withdrawal / (FVal(10)**FVal(decimals)) aave_events.append( AaveEvent( event_type='withdrawal', asset=reserve_asset, value=Balance( amount=withdrawal_amount, usd_value=withdrawal_amount * usd_price, ), block_number=block_number, timestamp=timestamp, tx_hash=tx_hash, log_index=deserialize_int_from_hex_or_int( event['logIndex'], 'aave log index'), )) return aave_events
def _try_get_chi_close_to(self, time: Timestamp) -> FVal: """Best effort attempt to get a chi value close to the given timestamp It can't be 100% accurate since we use the logs of join() or exit() in order to find the closest time chi was changed. It also may not work if for some reason there is no logs in the block range we are looking for. Better solution would have been an archive node's query. May raise: - RemoteError if there are problems with querying etherscan - ChiRetrievalError if we are unable to query chi at the given timestamp """ block_number = self.ethereum.etherscan.get_blocknumber_by_time(time) if self.ethereum.connected: latest_block = self.ethereum.web3.eth.blockNumber else: latest_block = self.ethereum.query_eth_highest_block() blocks_queried = 0 counter = 1 # Keep trying to find events that could reveal the chi to us. Go back # as far as MAX_BLOCKS_TO_QUERY and only then give up while blocks_queried < MAX_BLOCKS_TO_QUERY: back_from_block = max( POT_CREATION_BLOCK, block_number - counter * CHI_BLOCKS_SEARCH_DISTANCE, ) back_to_block = block_number - (counter - 1) * CHI_BLOCKS_SEARCH_DISTANCE forward_from_block = min( latest_block, block_number + (counter - 1) * CHI_BLOCKS_SEARCH_DISTANCE, ) forward_to_block = min( latest_block, block_number + CHI_BLOCKS_SEARCH_DISTANCE, ) back_joins, back_exits = self._get_join_exit_events( back_from_block, back_to_block) forward_joins, forward_exits = self._get_join_exit_events( from_block=forward_from_block, to_block=forward_to_block, ) no_results = all( len(x) == 0 for x in (back_joins, back_exits, forward_joins, forward_exits)) if latest_block == forward_to_block and no_results: # if our forward querying got us to the latest block and there is # still no other results, then take current chi return self.ethereum.call_contract( contract_address=MAKERDAO_POT_ADDRESS, abi=MAKERDAO_POT_ABI, method_name='chi', ) if not no_results: # got results! break blocks_queried += 2 * CHI_BLOCKS_SEARCH_DISTANCE counter += 1 if no_results: raise ChiRetrievalError( f'Found no DSR events around timestamp {time}. Cant query chi.', ) # Find the closest event to the to_block number, looking both at events # in the blocks before and in the blocks after block_number found_event = None back_event = _find_closest_event(back_joins, back_exits, -1, operator.gt) forward_event = _find_closest_event(forward_joins, forward_exits, 0, operator.lt) if back_event and not forward_event: found_event = back_event elif forward_event and not back_event: found_event = forward_event else: # We have both backward and forward events, get the one closer to block number try: back_block_number = deserialize_blocknumber( back_event['blockNumber']) # type: ignore # noqa: E501 forward_block_number = deserialize_blocknumber( forward_event['blockNumber']) # type: ignore # noqa: E501 except DeserializationError as e: msg = f'Error at reading DSR drip event block number. {str(e)}' raise ChiRetrievalError(msg) if block_number - back_block_number <= forward_block_number - block_number: found_event = back_event else: found_event = forward_event assert found_event, 'at this point found_event should be populated' # helps mypy event_block_number = deserialize_blocknumber( found_event['blockNumber']) first_topic = found_event['topics'][0] if isinstance(first_topic, bytes): first_topic = first_topic.hex() if first_topic.startswith('0x049878f3'): # join from_address = hex_or_bytes_to_address(found_event['topics'][1]) to_address = MAKERDAO_POT_ADDRESS else: from_address = MAKERDAO_POT_ADDRESS to_address = hex_or_bytes_to_address(found_event['topics'][1]) amount = self._get_vat_move_event_value( from_address=from_address, to_address=to_address, block_number=event_block_number, transaction_index=found_event['transactionIndex'], ) if not amount: raise ChiRetrievalError( f'Found no VAT.move events around timestamp {time}. Cant query chi.', ) wad_val = hex_or_bytes_to_int(found_event['topics'][2]) chi = FVal(amount) / FVal(wad_val) return chi
def _historical_dsr_for_account( self, account: ChecksumEthAddress, proxy: ChecksumEthAddress, ) -> DSRAccountReport: """Creates a historical DSR report for a single account May raise: - RemoteError if etherscan is used and there is a problem with reaching it or with the returned result. """ movements = [] join_normalized_balances = [] exit_normalized_balances = [] argument_filters = { 'sig': '0x049878f3', # join 'usr': proxy, } join_events = self.ethereum.get_logs( contract_address=MAKERDAO_POT_ADDRESS, abi=MAKERDAO_POT_ABI, event_name='LogNote', argument_filters=argument_filters, from_block=POT_CREATION_BLOCK, ) for join_event in join_events: try: wad_val = hex_or_bytes_to_int(join_event['topics'][2]) except ConversionError as e: msg = f'Error at reading DSR join event topics. {str(e)}. Skipping event...' self.msg_aggregator.add_error(msg) continue join_normalized_balances.append(wad_val) # and now get the deposit amount try: block_number = deserialize_blocknumber( join_event['blockNumber']) except DeserializationError as e: msg = f'Error at reading DSR join event block number. {str(e)}. Skipping event...' self.msg_aggregator.add_error(msg) continue dai_value = self._get_vat_move_event_value( from_address=proxy, to_address=MAKERDAO_POT_ADDRESS, block_number=block_number, transaction_index=join_event['transactionIndex'], ) if not dai_value: self.msg_aggregator.add_error( 'Did not find corresponding vat.move event for pot join. Skipping ...', ) continue movements.append( DSRMovement( movement_type='deposit', address=account, normalized_balance=wad_val, amount=dai_value, block_number=deserialize_blocknumber( join_event['blockNumber']), timestamp=self.ethereum.get_event_timestamp(join_event), ), ) argument_filters = { 'sig': '0x7f8661a1', # exit 'usr': proxy, } exit_events = self.ethereum.get_logs( contract_address=MAKERDAO_POT_ADDRESS, abi=MAKERDAO_POT_ABI, event_name='LogNote', argument_filters=argument_filters, from_block=POT_CREATION_BLOCK, ) for exit_event in exit_events: try: wad_val = hex_or_bytes_to_int(exit_event['topics'][2]) except ConversionError as e: msg = f'Error at reading DSR exit event topics. {str(e)}. Skipping event...' self.msg_aggregator.add_error(msg) continue exit_normalized_balances.append(wad_val) try: block_number = deserialize_blocknumber( exit_event['blockNumber']) except DeserializationError as e: msg = f'Error at reading DSR exit event block number. {str(e)}. Skipping event...' self.msg_aggregator.add_error(msg) continue # and now get the withdrawal amount dai_value = self._get_vat_move_event_value( from_address=MAKERDAO_POT_ADDRESS, to_address=proxy, block_number=block_number, transaction_index=exit_event['transactionIndex'], ) if not dai_value: self.msg_aggregator.add_error( 'Did not find corresponding vat.move event for pot exit. Skipping ...', ) continue movements.append( DSRMovement( movement_type='withdrawal', address=account, normalized_balance=wad_val, amount=dai_value, block_number=deserialize_blocknumber( exit_event['blockNumber']), timestamp=self.ethereum.get_event_timestamp(exit_event), ), ) normalized_balance = 0 amount_in_dsr = 0 movements.sort(key=lambda x: x.block_number) for m in movements: current_chi = FVal(m.amount) / FVal(m.normalized_balance) gain_so_far = normalized_balance * current_chi - amount_in_dsr m.gain_so_far = gain_so_far.to_int(exact=False) if m.movement_type == 'deposit': normalized_balance += m.normalized_balance amount_in_dsr += m.amount else: # withdrawal amount_in_dsr -= m.amount normalized_balance -= m.normalized_balance chi = self.ethereum.call_contract( contract_address=MAKERDAO_POT_ADDRESS, abi=MAKERDAO_POT_ABI, method_name='chi', ) normalized_balance = normalized_balance * chi amount_in_dsr = amount_in_dsr gain = normalized_balance - amount_in_dsr return DSRAccountReport(movements=movements, gain_so_far=gain)
def _try_get_chi_close_to(self, time: Timestamp) -> FVal: """Best effort attempt to get a chi value close to the given timestamp It can't be 100% accurate since we use the logs of join() or exit() in order to find the closest time chi was changed. It also may not work if for some reason there is no logs in the block range we are looking for. Better solution would have been an archive node's query. May raise: - RemoteError if there are problems with querying etherscan - ChiRetrievalError if we are unable to query chi at the given timestamp - BlockchainQueryError if an ethereum node is used and the contract call queries fail for some reason """ if time > 1584386100: # If the time is after 16/03/2020 19:15 GMT we know that # makerdao DSR was set to 0% we know chi has not changed # https://twitter.com/MakerDAO/status/1239270910810411008 return FVal('1018008449363110619399951035') block_number = self.ethereum.etherscan.get_blocknumber_by_time(time) if self.ethereum.web3 is not None: latest_block = self.ethereum.web3.eth.blockNumber else: latest_block = self.ethereum.query_eth_highest_block() blocks_queried = 0 counter = 1 # Keep trying to find events that could reveal the chi to us. Go back # as far as MAX_BLOCKS_TO_QUERY and only then give up while blocks_queried < MAX_BLOCKS_TO_QUERY: back_from_block = max( MAKERDAO_POT.deployed_block, block_number - counter * CHI_BLOCKS_SEARCH_DISTANCE, ) back_to_block = block_number - (counter - 1) * CHI_BLOCKS_SEARCH_DISTANCE forward_from_block = min( latest_block, block_number + (counter - 1) * CHI_BLOCKS_SEARCH_DISTANCE, ) forward_to_block = min( latest_block, block_number + CHI_BLOCKS_SEARCH_DISTANCE, ) back_joins, back_exits = self._get_join_exit_events(back_from_block, back_to_block) forward_joins, forward_exits = self._get_join_exit_events( from_block=forward_from_block, to_block=forward_to_block, ) no_results = all( len(x) == 0 for x in (back_joins, back_exits, forward_joins, forward_exits) ) if latest_block == forward_to_block and no_results: # if our forward querying got us to the latest block and there is # still no other results, then take current chi return self.ethereum.call_contract( contract_address=MAKERDAO_POT.address, abi=MAKERDAO_POT.abi, method_name='chi', ) if not no_results: # got results! break blocks_queried += 2 * CHI_BLOCKS_SEARCH_DISTANCE counter += 1 if no_results: raise ChiRetrievalError( f'Found no DSR events around timestamp {time}. Cant query chi.', ) # Find the closest event to the to_block number, looking both at events # in the blocks before and in the blocks after block_number found_event = None back_event = _find_closest_event(back_joins, back_exits, -1, operator.gt) forward_event = _find_closest_event(forward_joins, forward_exits, 0, operator.lt) if back_event and not forward_event: found_event = back_event elif forward_event and not back_event: found_event = forward_event else: # We have both backward and forward events, get the one closer to block number try: back_block_number = deserialize_blocknumber(back_event['blockNumber']) # type: ignore # noqa: E501 forward_block_number = deserialize_blocknumber(forward_event['blockNumber']) # type: ignore # noqa: E501 except DeserializationError as e: msg = f'Error at reading DSR drip event block number. {str(e)}' raise ChiRetrievalError(msg) if block_number - back_block_number <= forward_block_number - block_number: found_event = back_event else: found_event = forward_event assert found_event, 'at this point found_event should be populated' # helps mypy event_block_number = deserialize_blocknumber(found_event['blockNumber']) first_topic = hex_or_bytes_to_str(found_event['topics'][0]) amount = self._get_vat_join_exit_at_transaction( movement_type='join' if first_topic.startswith('0x049878f3') else 'exit', proxy_address=hex_or_bytes_to_address(found_event['topics'][1]), block_number=event_block_number, transaction_index=found_event['transactionIndex'], ) if amount is None: raise ChiRetrievalError( f'Found no VAT.move events around timestamp {time}. Cant query chi.', ) wad_val = hex_or_bytes_to_int(found_event['topics'][2]) chi = FVal(amount) / FVal(wad_val) return chi
def _historical_dsr_for_account( self, account: ChecksumEthAddress, proxy: ChecksumEthAddress, ) -> DSRAccountReport: """Creates a historical DSR report for a single account May raise: - RemoteError if etherscan is used and there is a problem with reaching it or with the returned result. - BlockchainQueryError if an ethereum node is used and the contract call queries fail for some reason """ movements = [] join_normalized_balances = [] exit_normalized_balances = [] argument_filters = { 'sig': '0x049878f3', # join 'usr': proxy, } join_events = self.ethereum.get_logs( contract_address=MAKERDAO_POT.address, abi=MAKERDAO_POT.abi, event_name='LogNote', argument_filters=argument_filters, from_block=MAKERDAO_POT.deployed_block, ) for join_event in join_events: try: wad_val = hex_or_bytes_to_int(join_event['topics'][2]) except ConversionError as e: msg = f'Error at reading DSR join event topics. {str(e)}. Skipping event...' self.msg_aggregator.add_error(msg) continue join_normalized_balances.append(wad_val) # and now get the deposit amount try: block_number = deserialize_blocknumber(join_event['blockNumber']) except DeserializationError as e: msg = f'Error at reading DSR join event block number. {str(e)}. Skipping event...' self.msg_aggregator.add_error(msg) continue dai_value = self._get_vat_join_exit_at_transaction( movement_type='join', proxy_address=proxy, block_number=block_number, transaction_index=join_event['transactionIndex'], ) if dai_value is None: self.msg_aggregator.add_error( 'Did not find corresponding vat.move event for pot join. Skipping ...', ) continue timestamp = self.ethereum.get_event_timestamp(join_event) usd_price = query_usd_price_or_use_default( asset=A_DAI, time=timestamp, default_value=FVal(1), location='DSR deposit', ) movements.append( DSRMovement( movement_type='deposit', address=account, normalized_balance=wad_val, amount=dai_value, amount_usd_value=_dsrdai_to_dai(dai_value) * usd_price, block_number=deserialize_blocknumber(join_event['blockNumber']), timestamp=timestamp, tx_hash=join_event['transactionHash'], ), ) argument_filters = { 'sig': '0x7f8661a1', # exit 'usr': proxy, } exit_events = self.ethereum.get_logs( contract_address=MAKERDAO_POT.address, abi=MAKERDAO_POT.abi, event_name='LogNote', argument_filters=argument_filters, from_block=MAKERDAO_POT.deployed_block, ) for exit_event in exit_events: try: wad_val = hex_or_bytes_to_int(exit_event['topics'][2]) except ConversionError as e: msg = f'Error at reading DSR exit event topics. {str(e)}. Skipping event...' self.msg_aggregator.add_error(msg) continue exit_normalized_balances.append(wad_val) try: block_number = deserialize_blocknumber(exit_event['blockNumber']) except DeserializationError as e: msg = f'Error at reading DSR exit event block number. {str(e)}. Skipping event...' self.msg_aggregator.add_error(msg) continue # and now get the withdrawal amount dai_value = self._get_vat_join_exit_at_transaction( movement_type='exit', proxy_address=proxy, block_number=block_number, transaction_index=exit_event['transactionIndex'], ) if dai_value is None: self.msg_aggregator.add_error( 'Did not find corresponding vat.move event for pot exit. Skipping ...', ) continue timestamp = self.ethereum.get_event_timestamp(exit_event) usd_price = query_usd_price_or_use_default( asset=A_DAI, time=timestamp, default_value=FVal(1), location='DSR withdrawal', ) movements.append( DSRMovement( movement_type='withdrawal', address=account, normalized_balance=wad_val, amount=dai_value, amount_usd_value=_dsrdai_to_dai(dai_value) * usd_price, block_number=deserialize_blocknumber(exit_event['blockNumber']), timestamp=timestamp, tx_hash=exit_event['transactionHash'], ), ) normalized_balance = 0 amount_in_dsr = 0 movements.sort(key=lambda x: x.block_number) for idx, m in enumerate(movements): if m.normalized_balance == 0: # skip 0 amount/balance movements. Consider last gain as last gain so far. if idx == 0: m.gain_so_far = 0 m.gain_so_far_usd_value = ZERO else: m.gain_so_far = movements[idx - 1].gain_so_far m.gain_so_far_usd_value = movements[idx - 1].gain_so_far_usd_value continue if normalized_balance == m.normalized_balance: m.gain_so_far = m.amount - amount_in_dsr else: current_chi = FVal(m.amount) / FVal(m.normalized_balance) gain_so_far = normalized_balance * current_chi - amount_in_dsr m.gain_so_far = gain_so_far.to_int(exact=False) usd_price = query_usd_price_or_use_default( asset=A_DAI, time=m.timestamp, default_value=FVal(1), location='DSR movement', ) m.gain_so_far_usd_value = _dsrdai_to_dai(m.gain_so_far) * usd_price if m.movement_type == 'deposit': normalized_balance += m.normalized_balance amount_in_dsr += m.amount else: # withdrawal amount_in_dsr -= m.amount normalized_balance -= m.normalized_balance chi = self.ethereum.call_contract( contract_address=MAKERDAO_POT.address, abi=MAKERDAO_POT.abi, method_name='chi', ) normalized_balance = normalized_balance * chi amount_in_dsr = amount_in_dsr gain = normalized_balance - amount_in_dsr try: current_dai_price = Inquirer().find_usd_price(A_DAI) except RemoteError: current_dai_price = Price(FVal(1)) # Calculate the total gain so far in USD unaccounted_gain = _dsrdai_to_dai(gain) last_usd_value = ZERO last_dai_gain = 0 if len(movements) != 0: last_usd_value = movements[-1].gain_so_far_usd_value last_dai_gain = movements[-1].gain_so_far unaccounted_gain = _dsrdai_to_dai(gain - last_dai_gain) gain_so_far_usd_value = unaccounted_gain * current_dai_price + last_usd_value return DSRAccountReport( movements=movements, gain_so_far=gain, gain_so_far_usd_value=gain_so_far_usd_value, )
def _try_get_chi_close_to(self, time: Timestamp) -> FVal: """Best effort attempt to get a chi value close to the given timestamp It can't be 100% accurate since we use the logs of join() or exit() in order to find the closest time chi was changed. It also may not work if for some reason there is no logs in the block range we are looking for. Better solution would have been an archive node's query. May raise: - RemoteError if there are problems with querying etherscan - ChiRetrievalError if we are unable to query chi at the given timestamp """ block_number = self.ethchain.etherscan.get_blocknumber_by_time(time) from_block = max(POT_CREATION_BLOCK, block_number - CHI_BLOCKS_SEARCH_DISTANCE) if self.ethchain.connected: latest_block = self.ethchain.web3.eth.blockNumber else: latest_block = self.ethchain.query_eth_highest_block() to_block = min(latest_block, block_number + CHI_BLOCKS_SEARCH_DISTANCE) join_events = self.ethchain.get_logs( contract_address=MAKERDAO_POT_ADDRESS, abi=MAKERDAO_POT_ABI, event_name='LogNote', argument_filters={'sig': '0x049878f3'}, # join from_block=from_block, to_block=to_block, ) exit_events = self.ethchain.get_logs( contract_address=MAKERDAO_POT_ADDRESS, abi=MAKERDAO_POT_ABI, event_name='LogNote', argument_filters={'sig': '0x7f8661a1'}, # exit from_block=from_block, to_block=to_block, ) events = join_events + exit_events # Find the closest event to the block number found_idx = -1 min_distance = 99999999 for idx, event in enumerate(events): try: event_block_number = deserialize_blocknumber( event['blockNumber']) except DeserializationError as e: msg = f'Error at reading DSR drip event block number. {str(e)}' raise ChiRetrievalError(msg) this_distance = abs(event_block_number - block_number) if this_distance < min_distance: min_distance = this_distance found_idx = idx if found_idx == -1: raise ChiRetrievalError( f'Found no DSR events around timestamp {time}. Cant query chi.', ) found_event = events[found_idx] event_block_number = deserialize_blocknumber( found_event['blockNumber']) first_topic = found_event['topics'][0] if isinstance(first_topic, bytes): first_topic = first_topic.hex() if first_topic.startswith('0x049878f3'): # join from_address = hex_or_bytes_to_address(found_event['topics'][1]) to_address = MAKERDAO_POT_ADDRESS else: from_address = MAKERDAO_POT_ADDRESS to_address = hex_or_bytes_to_address(found_event['topics'][1]) amount = self._get_vat_move_event_value( from_address=from_address, to_address=to_address, block_number=event_block_number, transaction_index=found_event['transactionIndex'], ) if not amount: raise ChiRetrievalError( f'Found no VAT.move events around timestamp {time}. Cant query chi.', ) wad_val = hex_or_bytes_to_int(found_event['topics'][2]) chi = FVal(amount) / FVal(wad_val) return chi