Skip to content

Commit

Permalink
fix: web3 batching in chainlink.price-by-registry
Browse files Browse the repository at this point in the history
  • Loading branch information
v1nvn committed Jan 29, 2024
1 parent 8175a75 commit d202ddf
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions models/credmark/protocols/oracle/chainlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

from credmark.cmf.model import Model
from credmark.cmf.model.errors import ModelDataError, ModelEngineError, ModelRunError
from credmark.cmf.types import BlockNumberOutOfRangeError, Contract, Maybe, Network, Price, PriceWithQuote
from credmark.cmf.types import (
BlockNumberOutOfRangeError,
Contract,
Maybe,
Network,
Price,
PriceWithQuote,
)
from credmark.dto import DTO, DTOField
from ens import ENS
from web3.exceptions import ContractLogicError
Expand Down Expand Up @@ -169,20 +176,27 @@ def run(self, input: PriceInputWithRegistry) -> Price:
sys.tracebacklimit = 0
feed = registry.functions.getFeed(
base_address, quote_address).call()
[round_data, decimals, description, version, is_feed_enabled] = self.context.web3_batch.call([
registry.functions.latestRoundData(base_address, quote_address),
registry.functions.decimals(base_address, quote_address),
registry.functions.description(base_address, quote_address),
registry.functions.version(base_address, quote_address),
registry.functions.isFeedEnabled(feed)
], unwrap=True, require_success=True)

(_roundId, answer, _startedAt, _updatedAt, _answeredInRound) = cast(
tuple[int, int, int, int, int],
registry.functions.latestRoundData(base_address, quote_address).call())
decimals = cast(int, registry.functions.decimals(base_address, quote_address).call())
description = cast(str, registry.functions.description(
base_address, quote_address).call())
version = cast(int, registry.functions.version(base_address, quote_address).call())
isFeedEnabled = cast(bool, registry.functions.isFeedEnabled(feed).call())
round_data)
decimals = cast(int, decimals)
description = cast(str, description)
version = cast(int, version)
is_feed_enabled = cast(bool, is_feed_enabled)

time_diff = self.context.block_number.timestamp - _updatedAt
round_diff = _answeredInRound - _roundId
return PriceWithQuote(price=answer / (10 ** decimals),
src=(f'{self.slug}|{description}|{feed}|v{version}|'
f'{isFeedEnabled}|t:{time_diff}s|r:{round_diff}'),
f'{is_feed_enabled}|t:{time_diff}s|r:{round_diff}'),
quoteAddress=quote_address)
except ContractLogicError as err:
if 'Feed not found' in str(err):
Expand Down

0 comments on commit d202ddf

Please sign in to comment.