Skip to content

Commit

Permalink
Examples for more schema types
Browse files Browse the repository at this point in the history
  • Loading branch information
samsondav committed Dec 4, 2024
1 parent d074b92 commit ec1c464
Showing 1 changed file with 160 additions and 6 deletions.
166 changes: 160 additions & 6 deletions core/services/llo/evm/report_codec_evm_abi_encode_unpacked_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func TestReportCodecEVMABIEncodeUnpacked_Encode_properties(t *testing.T) {
baseMarketDepthStreamID := llotypes.StreamID(rand.Uint32())

Check failure on line 84 in core/services/llo/evm/report_codec_evm_abi_encode_unpacked_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
quoteMarketDepthStreamID := llotypes.StreamID(rand.Uint32())
marketStatusStreamID := llotypes.StreamID(rand.Uint32())
binanceFundingRateStreamID := llotypes.StreamID(rand.Uint32())
binanceFundingTimeStreamID := llotypes.StreamID(rand.Uint32())
binanceFundingIntervalHoursStreamID := llotypes.StreamID(rand.Uint32())

Check failure on line 89 in core/services/llo/evm/report_codec_evm_abi_encode_unpacked_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
deribitFundingRateStreamID := llotypes.StreamID(rand.Uint32())
deribitFundingTimeStreamID := llotypes.StreamID(rand.Uint32())
deribitFundingIntervalHoursStreamID := llotypes.StreamID(rand.Uint32())

t.Run("DEX-based asset schema example", func(t *testing.T) {
expectedDEXBasedAssetSchema := abi.Arguments([]abi.Argument{
Expand Down Expand Up @@ -239,12 +245,6 @@ func TestReportCodecEVMABIEncodeUnpacked_Encode_properties(t *testing.T) {
Specimen: false,
}

linkQuoteStreamID := llotypes.StreamID(rand.Uint32())
ethQuoteStreamID := llotypes.StreamID(rand.Uint32())
dexBasedAssetDecimalStreamID := llotypes.StreamID(rand.Uint32())
baseMarketDepthStreamID := llotypes.StreamID(rand.Uint32())
quoteMarketDepthStreamID := llotypes.StreamID(rand.Uint32())

opts := ReportFormatEVMABIEncodeOpts{
BaseUSDFee: sampleBaseUSDFee,
ExpirationWindow: sampleExpirationWindow,
Expand Down Expand Up @@ -324,6 +324,143 @@ func TestReportCodecEVMABIEncodeUnpacked_Encode_properties(t *testing.T) {

properties.TestingRun(t)
})

t.Run("benchmark price schema example", func(t *testing.T) {
// TODO
})

t.Run("funding rate schema example", func(t *testing.T) {
expectedFundingRateSchema := abi.Arguments([]abi.Argument{
{Name: "feedId", Type: mustNewABIType("bytes32")},
{Name: "validFromTimestamp", Type: mustNewABIType("uint32")},
{Name: "observationsTimestamp", Type: mustNewABIType("uint32")},
{Name: "nativeFee", Type: mustNewABIType("uint192")},
{Name: "linkFee", Type: mustNewABIType("uint192")},
{Name: "expiresAt", Type: mustNewABIType("uint32")},
{Name: "binanceFundingRate", Type: mustNewABIType("int192")},
{Name: "binanceFundingTime", Type: mustNewABIType("uint32")},
{Name: "binanceFundingIntervalHours", Type: mustNewABIType("uint32")},
{Name: "deribitFundingRate", Type: mustNewABIType("int192")},
{Name: "deribitFundingTime", Type: mustNewABIType("uint32")},
{Name: "deribitFundingIntervalHours", Type: mustNewABIType("uint32")},
})

runTest := func(sampleFeedID common.Hash, sampleObservationsTimestamp, sampleValidAfterSeconds, sampleExpirationWindow uint32, sampleBaseUSDFee, sampleLinkBenchmarkPrice, sampleNativeBenchmarkPrice, sampleBinanceFundingRate, sampleBinanceFundingTime, sampleBinanceFundingIntervalHours, sampleDeribitFundingRate, sampleDeribitFundingTime, sampleDeribitFundingIntervalHours decimal.Decimal) bool {
report := llo.Report{
ConfigDigest: types.ConfigDigest{0x01},
SeqNr: 0x02,
ChannelID: llotypes.ChannelID(0x03),
ValidAfterSeconds: sampleValidAfterSeconds,
ObservationTimestampSeconds: sampleObservationsTimestamp,
Values: []llo.StreamValue{
&llo.Quote{Bid: decimal.NewFromFloat(6.1), Benchmark: sampleLinkBenchmarkPrice, Ask: decimal.NewFromFloat(8.2332)}, // Link price
&llo.Quote{Bid: decimal.NewFromFloat(9.4), Benchmark: sampleNativeBenchmarkPrice, Ask: decimal.NewFromFloat(11.33)}, // Native price
llo.ToDecimal(sampleBinanceFundingRate), // Binance funding rate
llo.ToDecimal(sampleBinanceFundingTime), // Binance funding time
llo.ToDecimal(sampleBinanceFundingIntervalHours), // Binance funding interval hours
llo.ToDecimal(sampleDeribitFundingRate), // Deribit funding rate
llo.ToDecimal(sampleDeribitFundingTime), // Deribit funding time
llo.ToDecimal(sampleDeribitFundingIntervalHours), // Deribit funding interval hours
},
Specimen: false,
}

opts := ReportFormatEVMABIEncodeOpts{
BaseUSDFee: sampleBaseUSDFee,
ExpirationWindow: sampleExpirationWindow,
FeedID: sampleFeedID,
ABI: []ABIEncoder{
ABIEncoder{StreamID: binanceFundingRateStreamID, Type: "int192"},
ABIEncoder{StreamID: binanceFundingTimeStreamID, Type: "uint32"},
ABIEncoder{StreamID: binanceFundingIntervalHoursStreamID, Type: "uint32"},
ABIEncoder{StreamID: deribitFundingRateStreamID, Type: "int192"},
ABIEncoder{StreamID: deribitFundingTimeStreamID, Type: "uint32"},
ABIEncoder{StreamID: deribitFundingIntervalHoursStreamID, Type: "uint32"},
},
}
serializedOpts, err := opts.Encode()
require.NoError(t, err)

cd := llotypes.ChannelDefinition{
// ReportFormat: llotypes.ReportFormatEVMABIEncodeUnpacked,
ReportFormat: llotypes.ReportFormat(4), // FIXME: When chainlink-common is fixed
Streams: []llotypes.Stream{
{
StreamID: linkQuoteStreamID,
Aggregator: llotypes.AggregatorMedian,
},
{
StreamID: ethQuoteStreamID,
Aggregator: llotypes.AggregatorMedian,
},
{
StreamID: dexBasedAssetDecimalStreamID,
Aggregator: llotypes.AggregatorQuote,
},
{
StreamID: baseMarketDepthStreamID,
Aggregator: llotypes.AggregatorMedian,
},
{
StreamID: quoteMarketDepthStreamID,
Aggregator: llotypes.AggregatorMedian,
},
},
Opts: serializedOpts,
}

encoded, err := codec.Encode(ctx, report, cd)
require.NoError(t, err)

values, err := expectedFundingRateSchema.Unpack(encoded)
require.NoError(t, err)

require.Len(t, values, len(expectedFundingRateSchema))

expectedLinkFee := CalculateFee(sampleLinkBenchmarkPrice, sampleBaseUSDFee)
expectedNativeFee := CalculateFee(sampleNativeBenchmarkPrice, sampleBaseUSDFee)

return AllTrue([]bool{
assert.Equal(t, sampleFeedID, (common.Hash)(values[0].([32]byte))), // feedId
assert.Equal(t, uint32(sampleValidAfterSeconds+1), values[1].(uint32)), // validFromTimestamp
assert.Equal(t, sampleObservationsTimestamp, values[2].(uint32)), // observationsTimestamp
assert.Equal(t, expectedLinkFee.String(), values[3].(*big.Int).String()), // linkFee
assert.Equal(t, expectedNativeFee.String(), values[4].(*big.Int).String()), // nativeFee
assert.Equal(t, uint32(sampleObservationsTimestamp+sampleExpirationWindow), values[5].(uint32)), // expiresAt
assert.Equal(t, sampleBinanceFundingRate.BigInt().String(), values[6].(*big.Int).String()), // binanceFundingRate
assert.Equal(t, uint32(sampleBinanceFundingTime.BigInt().Int64()), values[7].(uint32)), // binanceFundingTime

Check failure on line 431 in core/services/llo/evm/report_codec_evm_abi_encode_unpacked_test.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion int64 -> uint32 (gosec)
assert.Equal(t, uint32(sampleBinanceFundingIntervalHours.BigInt().Int64()), values[8].(uint32)), // binanceFundingIntervalHours

Check failure on line 432 in core/services/llo/evm/report_codec_evm_abi_encode_unpacked_test.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion int64 -> uint32 (gosec)
assert.Equal(t, sampleDeribitFundingRate.BigInt().String(), values[9].(*big.Int).String()), // deribitFundingRate
assert.Equal(t, uint32(sampleDeribitFundingTime.BigInt().Int64()), values[10].(uint32)), // deribitFundingTime
assert.Equal(t, uint32(sampleDeribitFundingIntervalHours.BigInt().Int64()), values[11].(uint32)), // deribitFundingIntervalHours
})
}

properties.Property("Encodes values", prop.ForAll(
runTest,
genFeedID(),
genObservationsTimestamp(),
genValidAfterSeconds(),
genExpirationWindow(),
genBaseUSDFee(),
genLinkBenchmarkPrice(),
genNativeBenchmarkPrice(),
genFundingRate(),
genFundingTime(),
genFundingIntervalHours(),
genFundingRate(),
genFundingTime(),
genFundingIntervalHours(),
))

properties.TestingRun(t)
})
}

func TestReportCodecEVMABIEncodeUnpacked_Encode(t *testing.T) {
t.Run("ABI and values length mismatch error", func(t *testing.T) {
// TODO
})
}

func genFeedID() gopter.Gen {
Expand Down Expand Up @@ -394,6 +531,23 @@ func genMarketStatus() gopter.Gen {
})
}

func genFundingRate() gopter.Gen {
return genDecimal()
}

func genFundingTime() gopter.Gen {
// Unix epochs
return gen.UInt32Range(1500000000, 2000000000).Map(func(i uint32) decimal.Decimal {
return decimal.NewFromInt(int64(i))
})
}

func genFundingIntervalHours() gopter.Gen {
return gen.UInt32().Map(func(i uint32) decimal.Decimal {
return decimal.NewFromInt(int64(i))
})
}

func mustNewABIType(t string) abi.Type {
result, err := abi.NewType(t, "", []abi.ArgumentMarshaling{})
if err != nil {
Expand Down

0 comments on commit ec1c464

Please sign in to comment.