Skip to content

Commit

Permalink
msg gen changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dixitaniket committed Jul 28, 2023
1 parent 155d28b commit d7b69c3
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 38 deletions.
40 changes: 20 additions & 20 deletions cw-relayer/relayer/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,42 +121,42 @@ func generateContractRelayMsg(forceRelay bool, msgType MsgType, requestID uint64
RequestID: requestID,
}

if msgType != RelayHistoricalMedian {
if msgType == RelayRate {
for _, rate := range rates {
msg.SymbolRates = append(msg.SymbolRates, [2]interface{}{rate.Denom, rate.Amount.Mul(RateFactor).TruncateInt().String()})
}
}

switch msgType {
case RelayRate:
if forceRelay {
msgData, err = json.Marshal(MsgForceRelay{Relay: msg})
} else {
msgData, err = json.Marshal(MsgRelay{Relay: msg})
}

case RelayHistoricalMedian:
// collect denom's medians
medianRates := map[string][]string{}
return
} else {
symbolRates := map[string][]string{}
for _, rate := range rates {
medianRates[rate.Denom] = append(medianRates[rate.Denom], rate.Amount.Mul(RateFactor).TruncateInt().String())
symbolRates[rate.Denom] = append(symbolRates[rate.Denom], rate.Amount.Mul(RateFactor).TruncateInt().String())
}

for denom, medians := range medianRates {
for denom, medians := range symbolRates {
msg.SymbolRates = append(msg.SymbolRates, [2]interface{}{denom, medians})
}

if forceRelay {
msgData, err = json.Marshal(MsgForceRelayHistoricalMedian{Relay: msg})
} else {
msgData, err = json.Marshal(MsgRelayHistoricalMedian{Relay: msg})
}

case RelayHistoricalDeviation:
if forceRelay {
msgData, err = json.Marshal(MsgForceRelayHistoricalDeviation{Relay: msg})
} else {
msgData, err = json.Marshal(MsgRelayHistoricalDeviation{Relay: msg})
switch msgType {
case RelayHistoricalMedian:
if forceRelay {
msgData, err = json.Marshal(MsgForceRelayHistoricalMedian{Relay: msg})
} else {
msgData, err = json.Marshal(MsgRelayHistoricalMedian{Relay: msg})
}

case RelayHistoricalDeviation:
if forceRelay {
msgData, err = json.Marshal(MsgForceRelayHistoricalDeviation{Relay: msg})
} else {
msgData, err = json.Marshal(MsgRelayHistoricalDeviation{Relay: msg})
}
}
}

Expand Down
129 changes: 111 additions & 18 deletions cw-relayer/relayer/relayer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package relayer

import (
"encoding/json"
"fmt"
"math/rand"
"strconv"
"testing"

"github.com/cosmos/cosmos-sdk/types"
Expand All @@ -19,31 +23,120 @@ func TestServiceTestSuite(t *testing.T) {

func (rts *RelayerTestSuite) TestStop() {}

func (rts *RelayerTestSuite) Test_generateContractMsg() {
func (rts *RelayerTestSuite) Test_generateRelayMsg() {
exchangeRates := types.DecCoins{
types.NewDecCoinFromDec("atom", types.MustNewDecFromStr("1.23456789")),
types.NewDecCoinFromDec("atom", types.MustNewDecFromStr("1.13456789")),
types.NewDecCoinFromDec("umee", types.MustNewDecFromStr("1.23456789")),
types.NewDecCoinFromDec("juno", types.MustNewDecFromStr("1.23456789")),
types.NewDecCoinFromDec("juno", types.MustNewDecFromStr("1.33456789")),
}

rts.Run("Relay msg", func() {
msg, err := generateContractRelayMsg(false, 1, 1, 1, exchangeRates)
rts.Require().NoError(err)
testCases := []struct {
tc string
forceRelay bool
msgType MsgType
}{
{
tc: "Relay msg",
forceRelay: false,
msgType: RelayRate,
},
{
tc: "Force Relay msg",
forceRelay: true,
msgType: RelayRate,
},
}

for _, tc := range testCases {
rts.Run(
tc.tc, func() {
msg, err := generateContractRelayMsg(tc.forceRelay, tc.msgType, 0, 0, exchangeRates)
rts.Require().NoError(err)

// price * 10**9 (USD factor in contract)
expectedRes := "{\"relay\":{\"symbol_rates\":[[\"atom\",\"1234567890\"],[\"umee\",\"1234567890\"],[\"juno\",\"1234567890\"]],\"resolve_time\":\"1\",\"request_id\":\"1\"}}"
msgStr := string(msg)
var expectedMsg map[string]Msg
err = json.Unmarshal(msg, &expectedMsg)
rts.Require().NoError(err)

rts.Require().Equal(expectedRes, msgStr)
})
msgKey := tc.msgType.String()
if tc.forceRelay {
msgKey = fmt.Sprintf("force_%s", tc.msgType.String())
}

rts.Run("Force Relay msg", func() {
msg, err := generateContractRelayMsg(true, 1, 1, 1, exchangeRates)
rts.Require().NoError(err)
rates := expectedMsg[msgKey].SymbolRates
rts.Require().NotZero(len(rates))
for i, rate := range rates {
rts.Require().Equal(rate[0], exchangeRates[i].Denom)
rts.Require().Equal(rate[1], exchangeRates[i].Amount.Mul(RateFactor).TruncateInt().String())
}
},
)
}
}

expectedRes := "{\"force_relay\":{\"symbol_rates\":[[\"atom\",\"1234567890\"],[\"umee\",\"1234567890\"],[\"juno\",\"1234567890\"]],\"resolve_time\":\"1\",\"request_id\":\"1\"}}"
msgStr := string(msg)
func (rts *RelayerTestSuite) Test_generateMedianRelayMsg() {
var exchangeRates types.DecCoins
rateMap := map[string][]interface{}{}
for _, denom := range []string{"atom", "umee", "juno"} {
for i := 0; i < 10; i++ {
price := rand.Float64()
if i%2 == 1 {
// to have prices above 1
price = price * 100000
}

rts.Require().Equal(expectedRes, msgStr)
})
priceDec := types.MustNewDecFromStr(strconv.FormatFloat(price, 'f', 9, 64))
exchangeRates = append(exchangeRates, types.NewDecCoinFromDec(denom, priceDec))
rateMap[denom] = append(rateMap[denom], priceDec.Mul(RateFactor).TruncateInt().String())
}
}

testCases := []struct {
tc string
forceRelay bool
msgType MsgType
}{
{
tc: "Median Relay msg",
forceRelay: false,
msgType: RelayHistoricalMedian,
},
{
tc: "Median Force Relay msg",
forceRelay: true,
msgType: RelayHistoricalMedian,
},
{
tc: "Deviation Relay msg",
forceRelay: false,
msgType: RelayHistoricalDeviation,
},
{
tc: "Deviation Force Relay msg",
forceRelay: false,
msgType: RelayHistoricalDeviation,
},
}

for _, tc := range testCases {
rts.Run(tc.tc, func() {
msg, err := generateContractRelayMsg(tc.forceRelay, tc.msgType, 0, 0, exchangeRates)
rts.Require().NoError(err)

var expectedMsg map[string]Msg
err = json.Unmarshal(msg, &expectedMsg)
rts.Require().NoError(err)

key := tc.msgType.String()
if tc.forceRelay {
key = fmt.Sprintf("force_%s", key)
}

rates := expectedMsg[key].SymbolRates
rts.Require().Len(rates, 3)

for _, rate := range rates {
rts.Require().Equal(rate[1], rateMap[rate[0].(string)])
}
})
}
}

0 comments on commit d7b69c3

Please sign in to comment.