Skip to content

Commit

Permalink
Refactor GetLatestValue to be more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Nov 13, 2023
1 parent 62fe5e4 commit e7228ae
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions core/services/relay/evm/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ func (cr *chainReader) GetMaxDecodingSize(ctx context.Context, n int, itemType s
return 0, fmt.Errorf("Unimplemented method GetMaxDecodingSize called %w", relaytypes.ErrorChainReaderUnsupported{})
}

// GetLatestValue retrieves latest value from contract.
func (cr *chainReader) GetLatestValue(ctx context.Context, bc relaytypes.BoundContract, method string, params any, returnVal any) (err error) {
// GetLatestValue retrieves latest value from contract.
func (cr *chainReader) GetLatestValue(ctx context.Context, bc relaytypes.BoundContract, method string, params any, returnVal any) error {
var response []byte
chainContractReader, exists := cr.chainContractReaders[bc.Name]
if !exists {
Expand All @@ -232,13 +232,13 @@ func (cr *chainReader) GetLatestValue(ctx context.Context, bc relaytypes.BoundCo
contractAddr := common.HexToAddress(bc.Address)

if chainReadingDefinition.ReadType == types.Method {
var callData []byte
if params != nil {
callData, err = chainContractReader.ParsedContractABI.Pack(chainSpecificName, params)
if err != nil {
return err
}
} else {
callData, err := chainContractReader.ParsedContractABI.Pack(chainSpecificName, params)
if err != nil {
return err
}

// if params are nil, repack callData because nil != empty
if params == nil {
callData, err = chainContractReader.ParsedContractABI.Pack(chainSpecificName)
if err != nil {
return err
Expand All @@ -255,15 +255,14 @@ func (cr *chainReader) GetLatestValue(ctx context.Context, bc relaytypes.BoundCo
if err != nil {
return err
}
return chainContractReader.ParsedContractABI.UnpackIntoInterface(returnVal, chainSpecificName, response)
}

event := chainContractReader.ParsedContractABI.Events[chainSpecificName]
log, err := cr.lp.LatestLogByEventSigWithConfs(event.ID, contractAddr, logpoller.Finalized)
if err != nil {
return err
} else if chainReadingDefinition.ReadType == types.Event {
event := chainContractReader.ParsedContractABI.Events[chainSpecificName]
log, err := cr.lp.LatestLogByEventSigWithConfs(event.ID, contractAddr, logpoller.Finalized)
if err != nil {
return err
}
response = log.Data
}
response = log.Data

return chainContractReader.ParsedContractABI.UnpackIntoInterface(returnVal, chainSpecificName, response)
}
Expand Down

0 comments on commit e7228ae

Please sign in to comment.