-
Notifications
You must be signed in to change notification settings - Fork 28
/
ibc_connections.go
116 lines (99 loc) · 3.4 KB
/
ibc_connections.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package view
import (
"errors"
"fmt"
"github.com/crypto-com/chain-indexing/appinterface/rdb"
)
type IBCConnections interface {
Insert(*IBCConnectionRow) error
Update(*IBCConnectionRow) error
FindCounterpartyChainIDBy(string) (string, error)
}
type IBCConnectionsView struct {
rdb *rdb.Handle
}
func NewIBCConnectionsView(handle *rdb.Handle) IBCConnections {
return &IBCConnectionsView{
handle,
}
}
func (ibcConnectionsView *IBCConnectionsView) Insert(ibcConnection *IBCConnectionRow) error {
sql, sqlArgs, err := ibcConnectionsView.rdb.StmtBuilder.
Insert("view_ibc_connections").
Columns(
"connection_id",
"client_id",
"counterparty_connection_id",
"counterparty_client_id",
"counterparty_chain_id",
).
Values(
ibcConnection.ConnectionID,
ibcConnection.ClientID,
ibcConnection.CounterpartyConnectionID,
ibcConnection.CounterpartyClientID,
ibcConnection.CounterpartyChainID,
).
ToSql()
if err != nil {
return fmt.Errorf("error building ibc_connection insertion sql: %v: %w", err, rdb.ErrBuildSQLStmt)
}
result, err := ibcConnectionsView.rdb.Exec(sql, sqlArgs...)
if err != nil {
return fmt.Errorf("error inserting ibc_connection into the table: %v: %w", err, rdb.ErrWrite)
}
if result.RowsAffected() != 1 {
return fmt.Errorf("error inserting ibc_connection into the table: no row inserted: %w", rdb.ErrWrite)
}
return nil
}
func (ibcConnectionsView *IBCConnectionsView) Update(ibcConnection *IBCConnectionRow) error {
sql, sqlArgs, err := ibcConnectionsView.rdb.StmtBuilder.
Update("view_ibc_connections").
SetMap(map[string]interface{}{
"client_id": ibcConnection.CounterpartyClientID,
"counterparty_connection_id": ibcConnection.CounterpartyConnectionID,
"counterparty_client_id": ibcConnection.CounterpartyClientID,
"counterparty_chain_id": ibcConnection.CounterpartyChainID,
}).
Where(
"connection_id = ?", ibcConnection.ConnectionID,
).
ToSql()
if err != nil {
return fmt.Errorf("error building ibc_connection update sql: %v: %w", err, rdb.ErrBuildSQLStmt)
}
result, err := ibcConnectionsView.rdb.Exec(sql, sqlArgs...)
if err != nil {
return fmt.Errorf("error updating ibc_connection on the table: %v: %w", err, rdb.ErrWrite)
}
if result.RowsAffected() != 1 {
return fmt.Errorf("error updating ibc_connection on the table: no row inserted: %w", rdb.ErrWrite)
}
return nil
}
func (ibcConnectionsView *IBCConnectionsView) FindCounterpartyChainIDBy(connectionID string) (string, error) {
sql, sqlArgs, err := ibcConnectionsView.rdb.StmtBuilder.
Select("counterparty_chain_id").
From("view_ibc_connections").
Where("connection_id = ?", connectionID).
ToSql()
if err != nil {
return "", fmt.Errorf("error building selection sql: %v: %w", err, rdb.ErrPrepare)
}
var counterpartyChainID string
if err = ibcConnectionsView.rdb.QueryRow(sql, sqlArgs...).Scan(&counterpartyChainID); err != nil {
if errors.Is(err, rdb.ErrNoRows) {
return "", rdb.ErrNoRows
}
return "", fmt.Errorf("error scanning ibc_connection row: %v: %w", err, rdb.ErrQuery)
}
return counterpartyChainID, nil
}
type IBCConnectionRow struct {
ConnectionID string `json:"connectionId"`
ClientID string `json:"clientId"`
CounterpartyConnectionID string `json:"counterpartyConnectionId"`
CounterpartyClientID string `json:"counterpartyClientId"`
CounterpartyChainID string `json:"counterpartyChainId"`
}