code.vegaprotocol.io/vega@v0.79.0/core/datasource/spec/adaptors/openoracle_test.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_test 17 18 import ( 19 "encoding/hex" 20 "encoding/json" 21 "testing" 22 23 "code.vegaprotocol.io/vega/core/datasource/common" 24 "code.vegaprotocol.io/vega/core/datasource/external/openoracle" 25 "code.vegaprotocol.io/vega/core/datasource/spec/adaptors" 26 "code.vegaprotocol.io/vega/libs/crypto" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30 ) 31 32 func TestOpenOracleAdaptor(t *testing.T) { 33 t.Run("Normalising incompatible data fails", testOpenOracleAdaptorNormalisingIncompatibleDataFails) 34 t.Run("Normalising compatible but invalid data fails", testOpenOracleAdaptorNormalisingCompatibleButInvalidDataFails) 35 t.Run("Normalising compatible and valid data succeeds", testOpenOracleAdaptorNormalisingCompatibleAndValidDataSucceeds) 36 } 37 38 func testOpenOracleAdaptorNormalisingIncompatibleDataFails(t *testing.T) { 39 // given 40 pubKeyB := []byte("0xdeadbeef") 41 pubKey := crypto.NewPublicKey(hex.EncodeToString(pubKeyB), pubKeyB) 42 rawData, _ := json.Marshal(struct { 43 Prices string 44 MarketNumber uint 45 }{ 46 Prices: "42", 47 MarketNumber: 1337, 48 }) 49 50 // when 51 normalisedData, err := adaptors.NewOpenOracleAdaptor().Normalise(pubKey, rawData) 52 53 // then 54 assert.Error(t, err) 55 assert.Nil(t, normalisedData) 56 } 57 58 func testOpenOracleAdaptorNormalisingCompatibleButInvalidDataFails(t *testing.T) { 59 // given 60 pubKeyB := []byte("0xdeadbeef") 61 pubKey := crypto.NewPublicKey(hex.EncodeToString(pubKeyB), pubKeyB) 62 rawData, _ := json.Marshal(openoracle.OracleResponse{ 63 Timestamp: "1611924180", 64 Messages: []string{ 65 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000008b38744c80000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034254430000000000000000000000000000000000000000000000000000000000", 66 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005433a2300000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034554480000000000000000000000000000000000000000000000000000000000", 67 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000002d1ff400000000000000000000000000000000000000000000000000000000000000067072696365730000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000358545a0000000000000000000000000000000000000000000000000000000000", 68 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601401e400000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000f4b2e0000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034441490000000000000000000000000000000000000000000000000000000000", 69 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601401a800000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000013461500000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035245500000000000000000000000000000000000000000000000000000000000", 70 "0x0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006014029800000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000009185b0000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035a52580000000000000000000000000000000000000000000000000000000000", 71 "0x0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006014029800000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000004c3330000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034241540000000000000000000000000000000000000000000000000000000000", 72 "0x0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006014025c00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000013acb80000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034b4e430000000000000000000000000000000000000000000000000000000000", 73 "0x0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006014029800000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000016826340000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044c494e4b00000000000000000000000000000000000000000000000000000000", 74 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000edfada0000000000000000000000000000000000000000000000000000000000000000670726963657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004434f4d5000000000000000000000000000000000000000000000000000000000", 75 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000df2c6c000000000000000000000000000000000000000000000000000000000000000670726963657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003554e490000000000000000000000000000000000000000000000000000000000", 76 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000865ce0000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034752540000000000000000000000000000000000000000000000000000000000", 77 }, 78 Signatures: []string{ // Broken signatures 79 "0x007c80118c4e9c871a2342f5ee6e182d1aa8a463c7487b9292d728fc867c188b65838a1a721cc80795b93d40635525f1714d2f272b198be6f379d36617ed1966000000000000000000000000000000000000000000000000000000000000001b", 80 "0x00f40ce29496af004c77b780032b28715a38454d610e9641f0c7aca5bfd494c5cabdbb70340cdf9d0145b9ca8ba3627688013c5999ff9b00690c76ab4102c8d3000000000000000000000000000000000000000000000000000000000000001c", 81 "0x008aaa6bc4c04e0039c5531119c68e5f74b8e78aba57397c2f3c7b74787202faa73b7fb0817924ec0bdb9b8761bf2c94be0249160a4b99bc25a39a8431271763000000000000000000000000000000000000000000000000000000000000001c", 82 "0x0085ec34b36eef7000a878c147328872168b7fdb1a822256857cf5e015cb9b9a23ec5bbcdaf9e18edd6b6c088f8e4e56bcb74f7f8b2ad990883b51642438cd64000000000000000000000000000000000000000000000000000000000000001c", 83 "0x005e8a60b777296f9fed37df5af51d13e1303882bb03b084d548ca77e0aba842de1d5beafecdaf3dada91da21e3478164aadaf489d9d3223f50cd92cab778f6c000000000000000000000000000000000000000000000000000000000000001b", 84 "0x00cf582b040ed849d09813aa7bf7f6b68ab53a3c459987f5a846d984aaacd11fa7ace4dc1b1dc6b338c1d9a25bd20829f431e4571a0889bba878a40fb78d7b80000000000000000000000000000000000000000000000000000000000000001b", 85 "0x0039118be2546aa6493fa5a5eb4477d098aef0537db83b520781592bd01c7606ef390d0e332277b1ed55acf0ef8922e2feddf05c79d780dbc25431495ecd85ad000000000000000000000000000000000000000000000000000000000000001b", 86 "0x00f90e238defaf70ae8e8cc0fe9aae7f27dc8b3624c2ef023f66e7a6de90f6c4b22f9f603d7d049d17f605827fd1bf08d4f1fa7778a81bbe447c4c20b82cc274000000000000000000000000000000000000000000000000000000000000001b", 87 "0x004e0c8ac16591bf5cb57cb96536910384b5cd7a3ee478f968fa179947e595e8d6137a1d8d38815df25b6a8b96055e896b1293b19c0cf9f06cd03fc78630576e000000000000000000000000000000000000000000000000000000000000001c", 88 "0x00245a1b11e3742fda4b8d1611b636625e10a2749e691d717aa75f30970a85c1d8d72541bf664fea528c50d444665db6071bae4c4013f97d30556f6bc4f521c7000000000000000000000000000000000000000000000000000000000000001c", 89 "0x008b8d87bafc2ba13ac1e51a12353e66fc08a8aadd2741465cdffa97727d0143b2bbc324b16242e0138b523b29329fe9390efa2fdd360a3617742dc3eb02c461000000000000000000000000000000000000000000000000000000000000001b", 90 "0x00d0e50d507e7caca0ec3185d7bc55583f62faa7c614d65e244d577184ce1e3a6e00c7290af35116792f35a61a3cf9baa4dc381c90df1d57dae64e6ac0993aa4000000000000000000000000000000000000000000000000000000000000001b", 91 }, 92 Prices: map[string]string{ 93 "BTC": "37371.725", 94 "ETH": "1412.67", 95 "XTZ": "2.9573", 96 "DAI": "1.0022864999999999", 97 "REP": "20.21", 98 "ZRX": "0.5960595", 99 "BAT": "0.312115", 100 "KNC": "1.2894", 101 "LINK": "23.60274", 102 "COMP": "249.54", 103 "UNI": "14.625900000000001", 104 "GRT": "0.55035", 105 }, 106 }) 107 108 // when 109 normalisedData, err := adaptors.NewOpenOracleAdaptor().Normalise(pubKey, rawData) 110 111 // then 112 assert.Error(t, err) 113 assert.Nil(t, normalisedData) 114 } 115 116 func testOpenOracleAdaptorNormalisingCompatibleAndValidDataSucceeds(t *testing.T) { 117 // given 118 pubKeyB := []byte("0xdeadbeef") 119 pubKey := crypto.NewPublicKey(hex.EncodeToString(pubKeyB), pubKeyB) 120 rawData, _ := json.Marshal(openoracle.OracleResponse{ 121 Timestamp: "1611924180", 122 Messages: []string{ 123 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000008b38744c80000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034254430000000000000000000000000000000000000000000000000000000000", 124 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005433a2300000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034554480000000000000000000000000000000000000000000000000000000000", 125 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000002d1ff400000000000000000000000000000000000000000000000000000000000000067072696365730000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000358545a0000000000000000000000000000000000000000000000000000000000", 126 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601401e400000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000f4b2e0000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034441490000000000000000000000000000000000000000000000000000000000", 127 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601401a800000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000013461500000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035245500000000000000000000000000000000000000000000000000000000000", 128 "0x0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006014029800000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000009185b0000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035a52580000000000000000000000000000000000000000000000000000000000", 129 "0x0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006014029800000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000004c3330000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034241540000000000000000000000000000000000000000000000000000000000", 130 "0x0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006014025c00000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000013acb80000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034b4e430000000000000000000000000000000000000000000000000000000000", 131 "0x0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006014029800000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000016826340000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044c494e4b00000000000000000000000000000000000000000000000000000000", 132 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000edfada0000000000000000000000000000000000000000000000000000000000000000670726963657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004434f4d5000000000000000000000000000000000000000000000000000000000", 133 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000df2c6c000000000000000000000000000000000000000000000000000000000000000670726963657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003554e490000000000000000000000000000000000000000000000000000000000", 134 "0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000601402d400000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000865ce0000000000000000000000000000000000000000000000000000000000000006707269636573000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034752540000000000000000000000000000000000000000000000000000000000", 135 }, 136 Signatures: []string{ 137 "0x137c80118c4e9c871a2342f5ee6e182d1aa8a463c7487b9292d728fc867c188b65838a1a721cc80795b93d40635525f1714d2f272b198be6f379d36617ed1966000000000000000000000000000000000000000000000000000000000000001b", 138 "0x7af40ce29496af004c77b780032b28715a38454d610e9641f0c7aca5bfd494c5cabdbb70340cdf9d0145b9ca8ba3627688013c5999ff9b00690c76ab4102c8d3000000000000000000000000000000000000000000000000000000000000001c", 139 "0x1f8aaa6bc4c04e0039c5531119c68e5f74b8e78aba57397c2f3c7b74787202faa73b7fb0817924ec0bdb9b8761bf2c94be0249160a4b99bc25a39a8431271763000000000000000000000000000000000000000000000000000000000000001c", 140 "0x4c85ec34b36eef7000a878c147328872168b7fdb1a822256857cf5e015cb9b9a23ec5bbcdaf9e18edd6b6c088f8e4e56bcb74f7f8b2ad990883b51642438cd64000000000000000000000000000000000000000000000000000000000000001c", 141 "0x275e8a60b777296f9fed37df5af51d13e1303882bb03b084d548ca77e0aba842de1d5beafecdaf3dada91da21e3478164aadaf489d9d3223f50cd92cab778f6c000000000000000000000000000000000000000000000000000000000000001b", 142 "0x60cf582b040ed849d09813aa7bf7f6b68ab53a3c459987f5a846d984aaacd11fa7ace4dc1b1dc6b338c1d9a25bd20829f431e4571a0889bba878a40fb78d7b80000000000000000000000000000000000000000000000000000000000000001b", 143 "0x4c39118be2546aa6493fa5a5eb4477d098aef0537db83b520781592bd01c7606ef390d0e332277b1ed55acf0ef8922e2feddf05c79d780dbc25431495ecd85ad000000000000000000000000000000000000000000000000000000000000001b", 144 "0x82f90e238defaf70ae8e8cc0fe9aae7f27dc8b3624c2ef023f66e7a6de90f6c4b22f9f603d7d049d17f605827fd1bf08d4f1fa7778a81bbe447c4c20b82cc274000000000000000000000000000000000000000000000000000000000000001b", 145 "0xc14e0c8ac16591bf5cb57cb96536910384b5cd7a3ee478f968fa179947e595e8d6137a1d8d38815df25b6a8b96055e896b1293b19c0cf9f06cd03fc78630576e000000000000000000000000000000000000000000000000000000000000001c", 146 "0x74245a1b11e3742fda4b8d1611b636625e10a2749e691d717aa75f30970a85c1d8d72541bf664fea528c50d444665db6071bae4c4013f97d30556f6bc4f521c7000000000000000000000000000000000000000000000000000000000000001c", 147 "0x288b8d87bafc2ba13ac1e51a12353e66fc08a8aadd2741465cdffa97727d0143b2bbc324b16242e0138b523b29329fe9390efa2fdd360a3617742dc3eb02c461000000000000000000000000000000000000000000000000000000000000001b", 148 "0x9dd0e50d507e7caca0ec3185d7bc55583f62faa7c614d65e244d577184ce1e3a6e00c7290af35116792f35a61a3cf9baa4dc381c90df1d57dae64e6ac0993aa4000000000000000000000000000000000000000000000000000000000000001b", 149 }, 150 Prices: map[string]string{ 151 "BTC": "37371.725", 152 "ETH": "1412.67", 153 "XTZ": "2.9573", 154 "DAI": "1.0022864999999999", 155 "REP": "20.21", 156 "ZRX": "0.5960595", 157 "BAT": "0.312115", 158 "KNC": "1.2894", 159 "LINK": "23.60274", 160 "COMP": "249.54", 161 "UNI": "14.625900000000001", 162 "GRT": "0.55035", 163 }, 164 }) 165 166 // when 167 normalisedData, err := adaptors.NewOpenOracleAdaptor().Normalise(pubKey, rawData) 168 169 // then 170 expectedData := common.Data{ 171 Data: map[string]string{ 172 "prices.BTC.value": "37371725000", 173 "prices.BTC.timestamp": "1611924180", 174 "prices.ETH.value": "1412670000", 175 "prices.ETH.timestamp": "1611924180", 176 "prices.XTZ.value": "2957300", 177 "prices.XTZ.timestamp": "1611924180", 178 "prices.DAI.value": "1002286", 179 "prices.DAI.timestamp": "1611923940", 180 "prices.REP.value": "20210000", 181 "prices.REP.timestamp": "1611923880", 182 "prices.ZRX.value": "596059", 183 "prices.ZRX.timestamp": "1611924120", 184 "prices.BAT.value": "312115", 185 "prices.BAT.timestamp": "1611924120", 186 "prices.KNC.value": "1289400", 187 "prices.KNC.timestamp": "1611924060", 188 "prices.LINK.value": "23602740", 189 "prices.LINK.timestamp": "1611924120", 190 "prices.COMP.value": "249540000", 191 "prices.COMP.timestamp": "1611924180", 192 "prices.UNI.value": "14625900", 193 "prices.UNI.timestamp": "1611924180", 194 "prices.GRT.value": "550350", 195 "prices.GRT.timestamp": "1611924180", 196 }, 197 MetaData: map[string]string{ 198 "open-oracle-timestamp": "1611924180", 199 }, 200 Signers: []*common.Signer{ 201 common.CreateSignerFromString("0xfCEAdAFab14d46e20144F48824d0C09B1a03F2BC", common.SignerTypeEthAddress), 202 }, 203 } 204 205 require.NoError(t, err) 206 assert.Equal(t, &expectedData, normalisedData) 207 }