Skip to content

Commit 65c0f25

Browse files
authored
Merge pull request #914 from crypto-com/feat/support-v1-cosmos-api
Feat: add cosmos api version param to cosmos api
2 parents f43001a + 590e461 commit 65c0f25

87 files changed

Lines changed: 356 additions & 146 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

appinterface/cosmosapp/client.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
)
1010

1111
type Client interface {
12-
Account(accountAddress string) (*Account, error)
13-
Balances(accountAddress string) (coin.Coins, error)
14-
BalanceByDenom(accountAddresss string, denom string) (coin.Coin, error)
15-
BondedBalance(accountAddress string) (coin.Coins, error)
16-
RedelegatingBalance(accountAddress string) (coin.Coins, error)
17-
UnbondingBalance(accountAddress string) (coin.Coins, error)
12+
Account(accountAddress string, cosmosAPIVersion string) (*Account, error)
13+
Balances(accountAddress string, cosmosAPIVersion string) (coin.Coins, error)
14+
BalanceByDenom(accountAddresss string, denom string, cosmosAPIVersion string) (coin.Coin, error)
15+
BondedBalance(accountAddress string, cosmosAPIVersion string) (coin.Coins, error)
16+
RedelegatingBalance(accountAddress string, cosmosAPIVersion string) (coin.Coins, error)
17+
UnbondingBalance(accountAddress string, cosmosAPIVersion string) (coin.Coins, error)
1818

19-
SupplyByDenom(denom string) (coin.Coin, error)
19+
SupplyByDenom(denom string, cosmosAPIVersion string) (coin.Coin, error)
2020

21-
TotalRewards(accountAddress string) (coin.DecCoins, error)
22-
Commission(validatorAddress string) (coin.DecCoins, error)
23-
CommunityPool() (coin.DecCoins, error)
21+
TotalRewards(accountAddress string, cosmosAPIVersion string) (coin.DecCoins, error)
22+
Commission(validatorAddress string, cosmosAPIVersion string) (coin.DecCoins, error)
23+
CommunityPool(cosmosAPIVersion string) (coin.DecCoins, error)
2424

25-
Validator(validatorAddress string) (*Validator, error)
26-
Delegation(delegator string, validator string) (*DelegationResponse, error)
27-
TotalBondedBalance() (coin.Coin, error)
28-
CommunityTax() (*big.Float, error)
25+
Validator(validatorAddress string, cosmosAPIVersion string) (*Validator, error)
26+
Delegation(delegator string, validator string, cosmosAPIVersion string) (*DelegationResponse, error)
27+
TotalBondedBalance(cosmosAPIVersion string) (coin.Coin, error)
28+
CommunityTax(cosmosAPIVersion string) (*big.Float, error)
2929

30-
AnnualProvisions() (coin.DecCoin, error)
30+
AnnualProvisions(cosmosAPIVersion string) (coin.DecCoin, error)
3131

32-
Proposals() ([]Proposal, error)
33-
ProposalById(id string) (Proposal, error)
34-
ProposalTally(id string) (Tally, error)
32+
Proposals(cosmosAPIVersion string) ([]Proposal, error)
33+
ProposalById(id string, cosmosAPIVersion string) (Proposal, error)
34+
ProposalTally(id string, cosmosAPIVersion string) (Tally, error)
3535

36-
Tx(txHash string) (*model.Tx, error)
36+
Tx(txHash string, cosmosAPIVersion string) (*model.Tx, error)
3737
}
3838

3939
var ErrAccountNotFound = errors.New("account not found")

appinterface/cosmosapp/mockclient.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,112 +16,112 @@ func NewMockClient() *MockClient {
1616
return &MockClient{}
1717
}
1818

19-
func (conn *MockClient) Account(accountAddress string) (*Account, error) {
20-
mockArgs := conn.Called(accountAddress)
19+
func (conn *MockClient) Account(accountAddress string, cosmosAPIVersion string) (*Account, error) {
20+
mockArgs := conn.Called(accountAddress, cosmosAPIVersion)
2121
result, _ := mockArgs.Get(0).(*Account)
2222
return result, mockArgs.Error(1)
2323
}
2424

25-
func (conn *MockClient) Balances(accountAddress string) (coin.Coins, error) {
26-
mockArgs := conn.Called(accountAddress)
25+
func (conn *MockClient) Balances(accountAddress string, cosmosAPIVersion string) (coin.Coins, error) {
26+
mockArgs := conn.Called(accountAddress, cosmosAPIVersion)
2727
result, _ := mockArgs.Get(0).(coin.Coins)
2828
return result, mockArgs.Error(1)
2929
}
3030

31-
func (conn *MockClient) BalanceByDenom(accountAddress string, denom string) (coin.Coin, error) {
32-
mockArgs := conn.Called(accountAddress, denom)
31+
func (conn *MockClient) BalanceByDenom(accountAddress string, denom string, cosmosAPIVersion string) (coin.Coin, error) {
32+
mockArgs := conn.Called(accountAddress, denom, cosmosAPIVersion)
3333
result, _ := mockArgs.Get(0).(coin.Coin)
3434
return result, mockArgs.Error(1)
3535
}
3636

37-
func (conn *MockClient) BondedBalance(accountAddress string) (coin.Coins, error) {
38-
mockArgs := conn.Called(accountAddress)
37+
func (conn *MockClient) BondedBalance(accountAddress string, cosmosAPIVersion string) (coin.Coins, error) {
38+
mockArgs := conn.Called(accountAddress, cosmosAPIVersion)
3939
result, _ := mockArgs.Get(0).(coin.Coins)
4040
return result, mockArgs.Error(1)
4141
}
4242

43-
func (conn *MockClient) RedelegatingBalance(accountAddress string) (coin.Coins, error) {
44-
mockArgs := conn.Called(accountAddress)
43+
func (conn *MockClient) RedelegatingBalance(accountAddress string, cosmosAPIVersion string) (coin.Coins, error) {
44+
mockArgs := conn.Called(accountAddress, cosmosAPIVersion)
4545
result, _ := mockArgs.Get(0).(coin.Coins)
4646
return result, mockArgs.Error(1)
4747
}
4848

49-
func (conn *MockClient) UnbondingBalance(accountAddress string) (coin.Coins, error) {
50-
mockArgs := conn.Called(accountAddress)
49+
func (conn *MockClient) UnbondingBalance(accountAddress string, cosmosAPIVersion string) (coin.Coins, error) {
50+
mockArgs := conn.Called(accountAddress, cosmosAPIVersion)
5151
result, _ := mockArgs.Get(0).(coin.Coins)
5252
return result, mockArgs.Error(1)
5353
}
5454

55-
func (conn *MockClient) SupplyByDenom(denom string) (coin.Coin, error) {
56-
mockArgs := conn.Called(denom)
55+
func (conn *MockClient) SupplyByDenom(denom string, cosmosAPIVersion string) (coin.Coin, error) {
56+
mockArgs := conn.Called(denom, cosmosAPIVersion)
5757
result, _ := mockArgs.Get(0).(coin.Coin)
5858
return result, mockArgs.Error(1)
5959
}
6060

61-
func (conn *MockClient) TotalRewards(accountAddress string) (coin.DecCoins, error) {
62-
mockArgs := conn.Called(accountAddress)
61+
func (conn *MockClient) TotalRewards(accountAddress string, cosmosAPIVersion string) (coin.DecCoins, error) {
62+
mockArgs := conn.Called(accountAddress, cosmosAPIVersion)
6363
result, _ := mockArgs.Get(0).(coin.DecCoins)
6464
return result, mockArgs.Error(1)
6565
}
6666

67-
func (conn *MockClient) Commission(validatorAddress string) (coin.DecCoins, error) {
68-
mockArgs := conn.Called(validatorAddress)
67+
func (conn *MockClient) Commission(validatorAddress string, cosmosAPIVersion string) (coin.DecCoins, error) {
68+
mockArgs := conn.Called(validatorAddress, cosmosAPIVersion)
6969
result, _ := mockArgs.Get(0).(coin.DecCoins)
7070
return result, mockArgs.Error(1)
7171
}
7272

73-
func (conn *MockClient) CommunityPool() (coin.DecCoins, error) {
74-
mockArgs := conn.Called()
73+
func (conn *MockClient) CommunityPool(cosmosAPIVersion string) (coin.DecCoins, error) {
74+
mockArgs := conn.Called(cosmosAPIVersion)
7575
result, _ := mockArgs.Get(0).(coin.DecCoins)
7676
return result, mockArgs.Error(1)
7777
}
7878

79-
func (conn *MockClient) Validator(validatorAddress string) (*Validator, error) {
80-
mockArgs := conn.Called(validatorAddress)
79+
func (conn *MockClient) Validator(validatorAddress string, cosmosAPIVersion string) (*Validator, error) {
80+
mockArgs := conn.Called(validatorAddress, cosmosAPIVersion)
8181
result, _ := mockArgs.Get(0).(*Validator)
8282
return result, mockArgs.Error(1)
8383
}
8484

85-
func (conn *MockClient) Delegation(delegator string, validator string) (*DelegationResponse, error) {
86-
mockArgs := conn.Called(delegator, validator)
85+
func (conn *MockClient) Delegation(delegator string, validator string, cosmosAPIVersion string) (*DelegationResponse, error) {
86+
mockArgs := conn.Called(delegator, validator, cosmosAPIVersion)
8787
result, _ := mockArgs.Get(0).(*DelegationResponse)
8888
return result, mockArgs.Error(1)
8989
}
9090

91-
func (conn *MockClient) TotalBondedBalance() (coin.Coin, error) {
92-
args := conn.Called()
91+
func (conn *MockClient) TotalBondedBalance(cosmosAPIVersion string) (coin.Coin, error) {
92+
args := conn.Called(cosmosAPIVersion)
9393
return args.Get(0).(coin.Coin), args.Error(1)
9494
}
9595

96-
func (conn *MockClient) CommunityTax() (*big.Float, error) {
97-
args := conn.Called()
96+
func (conn *MockClient) CommunityTax(cosmosAPIVersion string) (*big.Float, error) {
97+
args := conn.Called(cosmosAPIVersion)
9898
return args.Get(0).(*big.Float), args.Error(1)
9999
}
100100

101-
func (conn *MockClient) AnnualProvisions() (coin.DecCoin, error) {
102-
args := conn.Called()
101+
func (conn *MockClient) AnnualProvisions(cosmosAPIVersion string) (coin.DecCoin, error) {
102+
args := conn.Called(cosmosAPIVersion)
103103
return args.Get(0).(coin.DecCoin), args.Error(1)
104104
}
105105

106-
func (conn *MockClient) Proposals() ([]Proposal, error) {
107-
args := conn.Called()
106+
func (conn *MockClient) Proposals(cosmosAPIVersion string) ([]Proposal, error) {
107+
args := conn.Called(cosmosAPIVersion)
108108
return args.Get(0).([]Proposal), args.Error(1)
109109
}
110110

111-
func (conn *MockClient) ProposalById(id string) (Proposal, error) {
112-
mockArgs := conn.Called(id)
111+
func (conn *MockClient) ProposalById(id string, cosmosAPIVersion string) (Proposal, error) {
112+
mockArgs := conn.Called(id, cosmosAPIVersion)
113113
result, _ := mockArgs.Get(0).(Proposal)
114114
return result, mockArgs.Error(1)
115115
}
116116

117-
func (conn *MockClient) ProposalTally(id string) (Tally, error) {
118-
mockArgs := conn.Called(id)
117+
func (conn *MockClient) ProposalTally(id string, cosmosAPIVersion string) (Tally, error) {
118+
mockArgs := conn.Called(id, cosmosAPIVersion)
119119
result, _ := mockArgs.Get(0).(Tally)
120120
return result, mockArgs.Error(1)
121121
}
122122

123-
func (conn *MockClient) Tx(txHash string) (*model.Tx, error) {
124-
mockArgs := conn.Called(txHash)
123+
func (conn *MockClient) Tx(txHash string, cosmosAPIVersion string) (*model.Tx, error) {
124+
mockArgs := conn.Called(txHash, cosmosAPIVersion)
125125
result, _ := mockArgs.Get(0).(*model.Tx)
126126
return result, mockArgs.Error(1)
127127
}

appinterface/cosmosapp/proposal.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ type Tally struct {
4343
No string `json:"no"`
4444
NoWithVeto string `json:"no_with_veto"`
4545
}
46+
47+
type TallyV1 struct {
48+
YesCount string `json:"yes_count"`
49+
AbstainCount string `json:"abstain_count"`
50+
NoCount string `json:"no_count"`
51+
NoWithVetoCount string `json:"no_with_veto_count"`
52+
}

bootstrap/syncmanager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
eventhandler_interface "github.com/crypto-com/chain-indexing/appinterface/eventhandler"
1010
tendermint_interface "github.com/crypto-com/chain-indexing/appinterface/tendermint"
1111
"github.com/crypto-com/chain-indexing/external/ethereumtxinnermsgdecoder"
12+
"github.com/crypto-com/chain-indexing/external/tmcosmosutils"
1213
"github.com/crypto-com/chain-indexing/external/txdecoder"
1314
cosmosapp_infrastructure "github.com/crypto-com/chain-indexing/infrastructure/cosmosapp"
1415
"github.com/crypto-com/chain-indexing/usecase/model"
@@ -277,7 +278,7 @@ func (manager *SyncManager) syncBlockWorker(blockHeight int64) ([]command_entity
277278
decodedTx, err = manager.txDecoder.DecodeBase64(txHex)
278279
if err != nil {
279280
var resTx *model.Tx
280-
resTx, err = manager.cosmosClient.Tx(txHash)
281+
resTx, err = manager.cosmosClient.Tx(txHash, tmcosmosutils.DefaultCosmosAPIVersion)
281282
if err != nil {
282283
return nil, fmt.Errorf("error requesting chain txs (%s) at height %d: %v", txHex, blockHeight, err)
283284
}

entity/event/base.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Base struct {
1111
EventVersion int `json:"version"`
1212
BlockHeight int64 `json:"height"`
1313
EventUUID string `json:"uuid"`
14+
MsgVersion string `json:"msgVersion"`
1415
}
1516

1617
func NewBase(params BaseParams) Base {
@@ -19,6 +20,7 @@ func NewBase(params BaseParams) Base {
1920
EventVersion: params.Version,
2021
BlockHeight: params.BlockHeight,
2122
EventUUID: uuid.New().String(),
23+
MsgVersion: params.MsgVersion,
2224
}
2325
}
2426

@@ -38,8 +40,13 @@ func (event *Base) UUID() string {
3840
return event.EventUUID
3941
}
4042

43+
func (event *Base) MessageVersion() string {
44+
return event.MsgVersion
45+
}
46+
4147
type BaseParams struct {
4248
Name string
4349
Version int
4450
BlockHeight int64
51+
MsgVersion string
4552
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tmcosmosutils
2+
3+
import (
4+
"strings"
5+
)
6+
7+
const (
8+
DefaultCosmosAPIVersion = "v1beta1"
9+
CosmosAPIVersionV1 = "v1"
10+
)
11+
12+
var CosmosAPIVersionMap = map[string]bool{
13+
DefaultCosmosAPIVersion: true,
14+
CosmosAPIVersionV1: true,
15+
}
16+
17+
func GetMsgVersionFromMsgType(msgType string) string {
18+
msgVersion := DefaultCosmosAPIVersion
19+
parts := strings.Split(msgType, ".")
20+
if len(parts) >= 2 {
21+
msgVersion = parts[len(parts)-2]
22+
if _, exists := CosmosAPIVersionMap[msgVersion]; !exists {
23+
msgVersion = DefaultCosmosAPIVersion
24+
}
25+
}
26+
return msgVersion
27+
}

0 commit comments

Comments
 (0)