|
| 1 | +decoded, err := hex.DecodeString(strings.TrimPrefix(strings.ToLower(tx.CallData), "0x")) |
| 2 | +contractAddress := "0x" + strings.ToLower(strings.TrimLeft(hex.EncodeToString(decoded[4:36]), "0")) |
| 3 | + |
| 4 | +ff := strings.ToLower(strings.TrimLeft(hex.EncodeToString(decoded[100:104]), "0")) |
| 5 | +if strings.Compare(ff, "ba0cb29e") != 0 { |
| 6 | +return nil, fmt.Errorf("event doesn't invoke svr method: ba0cb29e, instead: %s", ff) |
| 7 | +} |
| 8 | + |
| 9 | +transmitSecondaryElems := make(map[string]interface{}) |
| 10 | +err := getTransmissionTypes().UnpackIntoMap(transmitSecondaryElems, decoded[104:]) |
| 11 | + |
| 12 | +report, exists := transmitSecondaryElems["report"] |
| 13 | +return MedianFromReport(report.([]byte)) |
| 14 | + |
| 15 | +func MedianFromReport(report []byte) (*big.Int, error) { |
| 16 | + reportElems := map[string]interface{}{} |
| 17 | + err := getReportTypes().UnpackIntoMap(reportElems, report) |
| 18 | + observationsIface, ok := reportElems["observations"] |
| 19 | + observations, ok := observationsIface.([]*big.Int) |
| 20 | + median := observations[len(observations)/2] |
| 21 | + return median, nil |
| 22 | +} |
| 23 | + |
| 24 | +func getReportTypes() abi.Arguments { |
| 25 | + return abi.Arguments([]abi.Argument{ |
| 26 | + {Name: "observationsTimestamp", Type: mustNewType("uint32")}, |
| 27 | + {Name: "rawObservers", Type: mustNewType("bytes32")}, |
| 28 | + {Name: "observations", Type: mustNewType("int192[]")}, |
| 29 | + {Name: "juelsPerFeeCoin", Type: mustNewType("int192")}, |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func getTransmissionTypes() abi.Arguments { |
| 34 | + return abi.Arguments([]abi.Argument{ |
| 35 | + {Name: "reportContext", Type: mustNewType("bytes32[3]")}, |
| 36 | + {Name: "report", Type: mustNewType("bytes")}, |
| 37 | + {Name: "rs", Type: mustNewType("bytes32[]")}, |
| 38 | + {Name: "ss", Type: mustNewType("bytes32[]")}, |
| 39 | + {Name: "rawVs", Type: mustNewType("bytes32")}, |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func mustNewType(t string) abi.Type { |
| 44 | + result, err := abi.NewType(t, "", nil) |
| 45 | + return result |
| 46 | +} |
0 commit comments