github.com/smartcontractkit/chainlink-terra@v0.1.4/tests/e2e/ocr2_proxy.go (about)

     1  package e2e
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"strconv"
     7  
     8  	"github.com/rs/zerolog/log"
     9  	"github.com/smartcontractkit/chainlink-terra/tests/e2e/ocr2proxytypes"
    10  	"github.com/smartcontractkit/chainlink-terra/tests/e2e/ocr2types"
    11  	terraClient "github.com/smartcontractkit/terra.go/client"
    12  	"github.com/smartcontractkit/terra.go/msg"
    13  )
    14  
    15  type OCRv2Proxy struct {
    16  	client  *TerraLCDClient
    17  	address msg.AccAddress
    18  }
    19  
    20  func (m *OCRv2Proxy) Address() string {
    21  	return m.address.String()
    22  }
    23  
    24  func (m *OCRv2Proxy) ProposeContract(addr string) error {
    25  	executeMsg := ocr2proxytypes.ProposeContractMsg{
    26  		ContractAddress: addr,
    27  	}
    28  	executeMsgBytes, err := json.Marshal(executeMsg)
    29  	if err != nil {
    30  		return err
    31  	}
    32  	return m.send(executeMsgBytes)
    33  }
    34  
    35  func (m *OCRv2Proxy) ConfirmContract(addr string) error {
    36  	executeMsg := ocr2proxytypes.ConfirmContractMsg{
    37  		ContractAddress: addr,
    38  	}
    39  	executeMsgBytes, err := json.Marshal(executeMsg)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	return m.send(executeMsgBytes)
    44  }
    45  
    46  func (m *OCRv2Proxy) TransferOwnership(to string) error {
    47  	executeMsg := ocr2proxytypes.TransferOwnershipMsg{
    48  		ToAddress: to,
    49  	}
    50  	executeMsgBytes, err := json.Marshal(executeMsg)
    51  	if err != nil {
    52  		return err
    53  	}
    54  	return m.send(executeMsgBytes)
    55  }
    56  
    57  func (m *OCRv2Proxy) send(executeMsgBytes []byte) error {
    58  	sender := m.client.DefaultWallet.AccAddress
    59  	_, err := m.client.SendTX(terraClient.CreateTxOptions{
    60  		Msgs: []msg.Msg{
    61  			msg.NewMsgExecuteContract(
    62  				sender,
    63  				m.address,
    64  				executeMsgBytes,
    65  				msg.NewCoins(),
    66  			),
    67  		},
    68  	}, true)
    69  	return err
    70  }
    71  
    72  func (m *OCRv2Proxy) GetLatestRoundData() (uint64, uint64, uint64, error) {
    73  	resp := ocr2types.QueryLatestRoundDataResponse{}
    74  	log.Warn().Interface("Addr", m.address)
    75  	if err := m.client.QuerySmart(context.Background(), m.address, ocr2types.QueryLatestRoundData, &resp); err != nil {
    76  		return 0, 0, 0, err
    77  	}
    78  	answer, _ := strconv.Atoi(resp.QueryResult.Answer)
    79  	return uint64(answer), resp.QueryResult.TransmissionTimestamp, resp.QueryResult.RoundID, nil
    80  }
    81  
    82  func (m *OCRv2Proxy) GetRoundData(roundID uint32) (map[string]interface{}, error) {
    83  	resp := make(map[string]interface{})
    84  	if err := m.client.QuerySmart(
    85  		context.Background(),
    86  		m.address,
    87  		ocr2types.QueryRoundDataMsg{
    88  			RoundData: ocr2types.QueryRoundDataTypeMsg{
    89  				RoundID: roundID,
    90  			},
    91  		},
    92  		&resp,
    93  	); err != nil {
    94  		return nil, err
    95  	}
    96  	return resp, nil
    97  }
    98  
    99  func (m *OCRv2Proxy) GetDecimals() (int, error) {
   100  	resp := make(map[string]int)
   101  	if err := m.client.QuerySmart(
   102  		context.Background(),
   103  		m.address,
   104  		ocr2types.QueryDecimals,
   105  		&resp,
   106  	); err != nil {
   107  		return 0, err
   108  	}
   109  	log.Info().Interface("Decimals response", resp).Msg("The decimals from the proxy")
   110  	return resp["query_result"], nil
   111  }
   112  
   113  func (m *OCRv2Proxy) GetDescription() (string, error) {
   114  	resp := make(map[string]string)
   115  	if err := m.client.QuerySmart(
   116  		context.Background(),
   117  		m.address,
   118  		ocr2types.QueryDescription,
   119  		&resp,
   120  	); err != nil {
   121  		return "", err
   122  	}
   123  	log.Info().Interface("Description response", resp).Msg("The description from the proxy")
   124  	return resp["query_result"], nil
   125  }