|
| 1 | +package v10 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/big" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/accounts/abi" |
| 8 | + "github.com/smartcontractkit/data-streams-sdk/go/feed" |
| 9 | +) |
| 10 | + |
| 11 | +var schema = Schema() |
| 12 | + |
| 13 | +// Schema returns this data version schema |
| 14 | +func Schema() abi.Arguments { |
| 15 | + mustNewType := func(t string) abi.Type { |
| 16 | + result, err := abi.NewType(t, "", []abi.ArgumentMarshaling{}) |
| 17 | + if err != nil { |
| 18 | + panic(fmt.Sprintf("Unexpected error during abi.NewType: %s", err)) |
| 19 | + } |
| 20 | + return result |
| 21 | + } |
| 22 | + return abi.Arguments([]abi.Argument{ |
| 23 | + {Name: "feedId", Type: mustNewType("bytes32")}, |
| 24 | + {Name: "validFromTimestamp", Type: mustNewType("uint32")}, |
| 25 | + {Name: "observationsTimestamp", Type: mustNewType("uint32")}, |
| 26 | + {Name: "nativeFee", Type: mustNewType("uint192")}, |
| 27 | + {Name: "linkFee", Type: mustNewType("uint192")}, |
| 28 | + {Name: "expiresAt", Type: mustNewType("uint32")}, |
| 29 | + {Name: "lastUpdateTimestamp", Type: mustNewType("uint64")}, |
| 30 | + {Name: "price", Type: mustNewType("int192")}, |
| 31 | + {Name: "marketStatus", Type: mustNewType("uint32")}, |
| 32 | + {Name: "currentMultiplier", Type: mustNewType("int192")}, |
| 33 | + {Name: "newMultiplier", Type: mustNewType("int192")}, |
| 34 | + {Name: "activationDateTime", Type: mustNewType("uint32")}, |
| 35 | + {Name: "tokenizedPrice", Type: mustNewType("int192")}, |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +// Data is the container for this schema attributes |
| 40 | +type Data struct { |
| 41 | + FeedID feed.ID `abi:"feedId"` |
| 42 | + ObservationsTimestamp uint32 |
| 43 | + ValidFromTimestamp uint32 |
| 44 | + ExpiresAt uint32 |
| 45 | + LinkFee *big.Int |
| 46 | + NativeFee *big.Int |
| 47 | + LastUpdateTimestamp uint64 |
| 48 | + Price *big.Int |
| 49 | + MarketStatus uint32 |
| 50 | + CurrentMultiplier *big.Int |
| 51 | + NewMultiplier *big.Int |
| 52 | + ActivationDateTime uint32 |
| 53 | + TokenizedPrice *big.Int |
| 54 | +} |
| 55 | + |
| 56 | +// Schema returns this data version schema |
| 57 | +func (Data) Schema() abi.Arguments { |
| 58 | + return Schema() |
| 59 | +} |
| 60 | + |
| 61 | +// Decode decodes the serialized data bytes |
| 62 | +func Decode(data []byte) (*Data, error) { |
| 63 | + values, err := schema.Unpack(data) |
| 64 | + if err != nil { |
| 65 | + return nil, fmt.Errorf("failed to decode report: %w", err) |
| 66 | + } |
| 67 | + decoded := new(Data) |
| 68 | + if err = schema.Copy(decoded, values); err != nil { |
| 69 | + return nil, fmt.Errorf("failed to copy report values to struct: %w", err) |
| 70 | + } |
| 71 | + return decoded, nil |
| 72 | +} |
0 commit comments