Skip to content

Commit

Permalink
fix events response
Browse files Browse the repository at this point in the history
  • Loading branch information
klim0v committed Oct 17, 2019
1 parent faa5c38 commit 748282d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (a *Api) Events(height int) (*EventsResult, error) {...}
```go
response, err := minterClient.Events(12)
// &{Events:[{Type:minter/RewardEvent Value:{Role:DAO Address:Mx18467bbb64a8edf890201d526c35957d82be3d95 Amount:366300000000000000000 ValidatorPubKey:Mp0d29a83e54653a1d5f34e561e0135f1e81cbcae152f1f327ab36857a7e32de4c}} {Type:minter/RewardEvent Value:{Role:Developers Address:Mx04bea23efb744dc93b4fda4c20bf4a21c6e195f1 Amount:366300000000000000000 ValidatorPubKey:Mp0d29a83e54653a1d5f34e561e0135f1e81cbcae152f1f327ab36857a7e32de4c}} {Type:minter/RewardEvent Value:{Role:Validator Address:Mxee81347211c72524338f9680072af90744333146 Amount:2930400000000000000000 ValidatorPubKey:Mp0d29a83e54653a1d5f34e561e0135f1e81cbcae152f1f327ab36857a7e32de4c}}]}
// &{Events:[{Type:minter/RewardEvent Value:map[address:Mx18467bbb64a8edf890201d526c35957d82be3d95 amount:111497225000000000000 role:DAO validator_pub_key:Mp4ae1ee73e6136c85b0ca933a9a1347758a334885f10b3238398a67ac2eb153b8]} {Type:minter/RewardEvent Value:map[address:Mx04bea23efb744dc93b4fda4c20bf4a21c6e195f1 amount:111497225000000000000 role:Developers validator_pub_key:Mp4ae1ee73e6136c85b0ca933a9a1347758a334885f10b3238398a67ac2eb153b8]} {Type:minter/RewardEvent Value:map[address:Mx18467bbb64a8edf890201d526c35957d82be3d95 amount:891977800000000000000 role:Validator validator_pub_key:Mp4ae1ee73e6136c85b0ca933a9a1347758a334885f10b3238398a67ac2eb153b8]} {Type:minter/RewardEvent Value:map[address:Mx18467bbb64a8edf890201d526c35957d82be3d95 amount:111497225000000000000 role:DAO validator_pub_key:Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd1c]} {Type:minter/RewardEvent Value:map[address:Mx04bea23efb744dc93b4fda4c20bf4a21c6e195f1 amount:111497225000000000000 role:Developers validator_pub_key:Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd1c]} {Type:minter/RewardEvent Value:map[address:Mx18467bbb64a8edf890201d526c35957d82be3d95 amount:891977800000000000000 role:Validator validator_pub_key:Mp738da41ba6a7b7d69b7294afa158b89c5a1b410cbf0c2443c85c5fe24ad1dd1c]} {Type:minter/RewardEvent Value:map[address:Mx18467bbb64a8edf890201d526c35957d82be3d95 amount:111497225000000000000 role:DAO validator_pub_key:Mp6f16c1ff21a6fb946aaed0f4c1fcca272b72fd904988f91d3883282b8ae31ba2]} {Type:minter/RewardEvent Value:map[address:Mx04bea23efb744dc93b4fda4c20bf4a21c6e195f1 amount:111497225000000000000 role:Developers validator_pub_key:Mp6f16c1ff21a6fb946aaed0f4c1fcca272b72fd904988f91d3883282b8ae31ba2]} {Type:minter/RewardEvent Value:map[address:Mx18467bbb64a8edf890201d526c35957d82be3d95 amount:891977800000000000000 role:Validator validator_pub_key:Mp6f16c1ff21a6fb946aaed0f4c1fcca272b72fd904988f91d3883282b8ae31ba2]} {Type:minter/RewardEvent Value:map[address:Mx18467bbb64a8edf890201d526c35957d82be3d95 amount:111497225000000000000 role:DAO validator_pub_key:Mp9e13f2f5468dd782b316444fbd66595e13dba7d7bd3efa1becd50b42045f58c6]} {Type:minter/RewardEvent Value:map[address:Mx04bea23efb744dc93b4fda4c20bf4a21c6e195f1 amount:111497225000000000000 role:Developers validator_pub_key:Mp9e13f2f5468dd782b316444fbd66595e13dba7d7bd3efa1becd50b42045f58c6]} {Type:minter/RewardEvent Value:map[address:Mx18467bbb64a8edf890201d526c35957d82be3d95 amount:891977800000000000000 role:Validator validator_pub_key:Mp9e13f2f5468dd782b316444fbd66595e13dba7d7bd3efa1becd50b42045f58c6]}]}
```

### MaxGas
Expand Down
70 changes: 61 additions & 9 deletions api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"encoding/json"
"errors"
"strconv"
)

Expand All @@ -13,15 +14,66 @@ type EventsResponse struct {
}

type EventsResult struct {
Events []struct {
Type string `json:"type"`
Value struct {
Role string `json:"role"`
Address string `json:"address"`
Amount string `json:"amount"`
ValidatorPubKey string `json:"validator_pub_key"`
} `json:"value"`
} `json:"events"`
Events []Event `json:"events"`
}

type Event struct {
Type string `json:"type"`
Value map[string]string `json:"value"`
}

// Converting event map data to the structure interface regarding event type
func (e *Event) ValueStruct() (interface{}, error) {
bytes, err := json.Marshal(e.Value)
if err != nil {
return nil, err
}

var value interface{}
switch e.Type {
case "minter/RewardEvent":
value = &RewardEventValue{}
case "minter/SlashEvent":
value = &SlashEventValue{}
case "minter/UnbondEvent":
value = &UnbondEventValue{}
case "minter/CoinLiquidationEvent":
value = &CoinLiquidationEventValue{}
default:
return nil, errors.New("unknown event type")
}

err = json.Unmarshal(bytes, value)
if err != nil {
return nil, err
}

return value, nil
}

type RewardEventValue struct {
Role string `json:"role"`
Address string `json:"address"`
Amount string `json:"amount"`
ValidatorPubKey string `json:"validator_pub_key"`
}

type SlashEventValue struct {
Address string `json:"address"`
Amount string `json:"amount"`
Coin string `json:"coin"`
ValidatorPubKey string `json:"validator_pub_key"`
}

type UnbondEventValue struct {
Address string `json:"address"`
Amount string `json:"amount"`
Coin string `json:"coin"`
ValidatorPubKey string `json:"validator_pub_key"`
}

type CoinLiquidationEventValue struct {
Coin string `json:"coin"`
}

// Returns events at given height.
Expand Down
28 changes: 27 additions & 1 deletion api/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,38 @@

package api

import "testing"
import (
"testing"
)

func TestApi_Events(t *testing.T) {
response, err := testApi.Events(12)
if err != nil {
t.Fatal(err)
}
for _, v := range response.Events {
t.Run(v.Type, func(t *testing.T) {
data, err := v.ValueStruct()
if err != nil {
t.Error(err)
}
var ok bool
switch v.Type {
case "minter/RewardEvent":
_, ok = data.(*RewardEventValue)
case "minter/SlashEvent":
_, ok = data.(*SlashEventValue)
case "minter/UnbondEvent":
_, ok = data.(*UnbondEventValue)
case "minter/CoinLiquidationEvent":
_, ok = data.(*CoinLiquidationEventValue)
default:
t.Fatal("not found interface by type")
}
if !ok {
t.Fatalf("interface conversion: interface {} is %T", data)
}
})
}
t.Logf("%+v", response)
}

0 comments on commit 748282d

Please sign in to comment.