code.vegaprotocol.io/vega@v0.79.0/core/datasource/spec/adaptors/openoracle.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package adaptors
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"code.vegaprotocol.io/vega/core/datasource/common"
    22  	"code.vegaprotocol.io/vega/core/datasource/external/openoracle"
    23  	"code.vegaprotocol.io/vega/libs/crypto"
    24  )
    25  
    26  // OpenOracleAdaptor is a specific oracle Adaptor for Open Oracle / Open Price Feed
    27  // standard.
    28  // Link: https://compound.finance/docs/prices
    29  type OpenOracleAdaptor struct{}
    30  
    31  // NewOpenOracleAdaptor creates a new OpenOracleAdaptor.
    32  func NewOpenOracleAdaptor() *OpenOracleAdaptor {
    33  	return &OpenOracleAdaptor{}
    34  }
    35  
    36  // Normalise normalises an Open Oracle / Open Price Feed payload into an signed.Data.
    37  // The public key from the transaction is not used, only those from the Open
    38  // Oracle data.
    39  func (a *OpenOracleAdaptor) Normalise(_ crypto.PublicKey, data []byte) (*common.Data, error) {
    40  	response, err := openoracle.Unmarshal(data)
    41  	if err != nil {
    42  		return nil, fmt.Errorf("couldn't unmarshal Open Oracle data: %w", err)
    43  	}
    44  
    45  	pubKeys, kvs, err := openoracle.Verify(*response)
    46  	if err != nil {
    47  		return nil, fmt.Errorf("invalid Open Oracle response: %w", err)
    48  	}
    49  
    50  	pubKeysSigners := make([]*common.Signer, len(pubKeys))
    51  	for i, pk := range pubKeys {
    52  		pubKeysSigners[i] = common.CreateSignerFromString(pk, common.SignerTypeEthAddress)
    53  	}
    54  	return &common.Data{
    55  		Signers: pubKeysSigners,
    56  		Data:    kvs,
    57  		MetaData: map[string]string{
    58  			"open-oracle-timestamp": response.Timestamp,
    59  		},
    60  	}, nil
    61  }