Skip to content

Commit

Permalink
updating to use LatestRound CR + lint
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhuie19 committed Sep 16, 2024
1 parent 2e31c05 commit 87adf08
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
6 changes: 3 additions & 3 deletions core/services/ocr2/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1651,9 +1651,9 @@ func (d *Delegate) newServicesCCIPCommit(ctx context.Context, lggr logger.Sugare

// TRYING TO FIGURE OUT CONTRACT READER
relayID := types.RelayID{Network: spec.Relay, ChainID: strconv.FormatUint(chainID, 10)}
relay, err := d.RelayGetter.Get(relayID)
if err != nil {
return nil, err
relay, rerr := d.RelayGetter.Get(relayID)
if rerr != nil {
return nil, rerr
}

contractReaderConfig := evmrelaytypes.ChainReaderConfig{
Expand Down
39 changes: 18 additions & 21 deletions core/services/ocr2/plugins/ccip/internal/pricegetter/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,6 @@ const decimalsMethodName = "decimals"
const latestRoundDataMethodName = "latestRoundData"
const OFFCHAIN_AGGREGATOR = "OffchainAggregator"

//nolint:lll
type latestRoundDataConfig struct {
// function latestRoundData() external view
// returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
RoundID *big.Int
Answer *big.Int
StartedAt *big.Int
UpdatedAt *big.Int
AnsweredInRound *big.Int
}

func init() {
// Ensure existence of latestRoundData method on the Aggregator contract.
aggregatorABI, err := abi.JSON(strings.NewReader(offchainaggregator.OffchainAggregatorABI))
Expand Down Expand Up @@ -210,7 +199,7 @@ func (d *DynamicPriceGetter) performBatchCall(ctx context.Context, chainID uint6
ReturnVal: &latestRoundData,
})
} else {
fmt.Errorf("unexpected method name in batchCalls: %v", call.MethodName())
return fmt.Errorf("unexpected method name in batchCalls: %v", call.MethodName())
}
}
result, err2 := contractReader.BatchGetLatestValues(ctx, batchGetLatestValuesRequest)
Expand Down Expand Up @@ -269,12 +258,6 @@ func (d *DynamicPriceGetter) performBatchCall(ctx context.Context, chainID uint6
decimals = append(decimals, v)
}

for i := range nbDecimalCalls {
if decimals[i] != decimalsCR[i] {
panic(fmt.Sprintf("decimalsCR != decimals, at index %v", i))
}
}

for i, res := range results[nbDecimalCalls : nbDecimalCalls+nbLatestRoundDataCalls] {
// latestRoundData function has multiple outputs (roundId,answer,startedAt,updatedAt,answeredInRound).
// we want the second one (answer, at idx=1).
Expand All @@ -286,15 +269,29 @@ func (d *DynamicPriceGetter) performBatchCall(ctx context.Context, chainID uint6
latestRounds = append(latestRounds, v)
}

for i := range nbDecimalCalls {
if decimals[i] != decimalsCR[i] {
panic(fmt.Sprintf("decimalsCR != decimals, at index %v", i))
}
}

latestRoundAnswerCR := make([]*big.Int, 0, nbLatestRoundDataCalls)
for i := range nbLatestRoundDataCalls {
if latestRounds[i] != latestRoundCR[i].Answer {
panic(fmt.Sprintf("latestRoundCR != latestRounds, at index %v", i))
}
latestRoundAnswerCR = append(latestRoundAnswerCR, latestRoundCR[i].Answer)
}

// Normalize and store prices.
for i := range batchCalls.tokenOrder {
// Normalize to 1e18.
if decimalsCR[i] < 18 {
latestRounds[i].Mul(latestRounds[i], big.NewInt(0).Exp(big.NewInt(10), big.NewInt(18-int64(decimalsCR[i])), nil))
latestRoundAnswerCR[i].Mul(latestRoundAnswerCR[i], big.NewInt(0).Exp(big.NewInt(10), big.NewInt(18-int64(decimalsCR[i])), nil))
} else if decimalsCR[i] > 18 {
latestRounds[i].Div(latestRounds[i], big.NewInt(0).Exp(big.NewInt(10), big.NewInt(int64(decimalsCR[i])-18), nil))
latestRoundAnswerCR[i].Div(latestRoundAnswerCR[i], big.NewInt(0).Exp(big.NewInt(10), big.NewInt(int64(decimalsCR[i])-18), nil))
}
prices[ccipcalc.EvmAddrToGeneric(batchCalls.tokenOrder[i])] = latestRounds[i]
prices[ccipcalc.EvmAddrToGeneric(batchCalls.tokenOrder[i])] = latestRoundAnswerCR[i]
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,7 @@ func mockCR(t *testing.T, decimals []uint8, rounds []aggregator_v3_interface.Lat

// Mock batch calls per chain: all decimals calls then all latestRoundData calls.
// bGLVR = batchGetLatestValueResult
var bGLVR types.BatchGetLatestValuesResult
bGLVR = make(map[string]types.ContractBatchResults, 1)
bGLVR := make(map[string]types.ContractBatchResults, 1)

bGLVR["OffchainAggregator"] = make([]types.BatchReadResult, 0, len(decimals)+len(rounds))
for _, d := range decimals {
Expand Down

0 comments on commit 87adf08

Please sign in to comment.