Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: PriceData struct field MedianData should not be array of MedianData #321

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions x/gmp/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func (k Keeper) BuildGmpRequest(
denom,
k.oracleKeeper.MaximumMedianStamps(ctx),
)
// convert them to a medianData slice
medianData, err := types.NewMediansSlice(medians, deviations)
// convert them to a MedianData struct
medianData, err := types.NewMedianData(medians, deviations)
if err != nil {
return &ibctransfertypes.MsgTransfer{}, err
}
Expand Down
38 changes: 17 additions & 21 deletions x/gmp/types/abi_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type PriceData struct {
AssetName [32]byte
Price *big.Int
ResolveTime *big.Int
MedianData []MedianData
MedianData MedianData
}

// encoderSpec is the ABI specification for the GMP data.
Expand Down Expand Up @@ -103,7 +103,7 @@ func NewPriceData(
assetName string,
price sdk.Dec,
resolveTime *big.Int,
medianData []MedianData,
medianData MedianData,
) (PriceData, error) {
assetSlice := []byte(assetName)
if len(assetSlice) > 32 {
Expand All @@ -128,27 +128,23 @@ func decToInt(amount sdk.Dec) *big.Int {

var rateFactor = sdk.NewDec(10).Power(9)

// NewMediansSlice creates a slice of MedianData from slices of medians and deviations.
func NewMediansSlice(medians oracletypes.PriceStamps, deviations oracletypes.PriceStamps) ([]MedianData, error) {
// NewMedianData creates a MedianData object of medians and deviations and their block numbers.
func NewMedianData(medians oracletypes.PriceStamps, deviations oracletypes.PriceStamps) (MedianData, error) {
if len(medians) != len(deviations) {
return nil, fmt.Errorf("length of medians and deviations must be equal")
return MedianData{}, fmt.Errorf("length of medians and deviations must be equal")
}
// First, sort them so we'll be able to find matching blockNums.
sortedMedians := *medians.Sort()
sortedDeviations := *deviations.Sort()

// Then, create the MedianData slice.
medianData := make([]MedianData, 0, len(medians))
for i, median := range sortedMedians {
// If the median and deviation are not from the same block, skip.
if median.BlockNum != sortedDeviations[i].BlockNum {
continue
}
medianData = append(medianData, MedianData{
BlockNums: []*big.Int{big.NewInt(int64(median.BlockNum))},
Medians: []*big.Int{decToInt(median.ExchangeRate.Amount)},
Deviations: []*big.Int{decToInt(sortedDeviations[i].ExchangeRate.Amount)},
})

medianData := MedianData{
adamewozniak marked this conversation as resolved.
Show resolved Hide resolved
BlockNums: make([]*big.Int, 0, len(medians)),
Medians: make([]*big.Int, 0, len(medians)),
Deviations: make([]*big.Int, 0, len(medians)),
}

for i, median := range medians {
medianData.BlockNums = append(medianData.BlockNums, big.NewInt(int64(median.BlockNum)))
medianData.Medians = append(medianData.Medians, decToInt(median.ExchangeRate.Amount))
medianData.Deviations = append(medianData.Deviations, decToInt(deviations[i].ExchangeRate.Amount))
}

return medianData, nil
}
10 changes: 4 additions & 6 deletions x/gmp/types/abi_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ func TestEncode(t *testing.T) {
AssetName: [32]byte{},
Price: big.NewInt(1),
ResolveTime: big.NewInt(1),
MedianData: []MedianData{
{
BlockNums: []*big.Int{big.NewInt(1)},
Medians: []*big.Int{big.NewInt(1)},
Deviations: []*big.Int{big.NewInt(1)},
},
MedianData: MedianData{
BlockNums: []*big.Int{big.NewInt(1)},
Medians: []*big.Int{big.NewInt(1)},
Deviations: []*big.Int{big.NewInt(1)},
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions x/gmp/types/abi_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ var (
timestampType, _ = abi.NewType("uint256", "uint256", nil)

// priceDataType is the ABI specification for the PriceData tuple in Solidity.
// It is a tuple of (bytes32, uint256, uint256, tuple[]).
// It is a tuple of (bytes32, uint256, uint256, tuple).
// It includes MedianData, another tuple of (uint256[], uint256[], uint256[]).
priceDataType, _ = abi.NewType("tuple[]", "",
[]abi.ArgumentMarshaling{
{Name: "AssetName", Type: "bytes32"},
{Name: "Price", Type: "uint256"},
{Name: "ResolveTime", Type: "uint256"},
{
Name: "MedianData", Type: "tuple[]", Components: []abi.ArgumentMarshaling{
Name: "MedianData", Type: "tuple", Components: []abi.ArgumentMarshaling{
{Name: "BlockNums", Type: "uint256[]"},
{Name: "Medians", Type: "uint256[]"},
{Name: "Deviations", Type: "uint256[]"},
Expand Down