Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

connection.gno

1.17 Kb · 49 lines
 1package types
 2
 3import "gno.land/p/onbloc/encoding/abi"
 4
 5type ConnectionState uint8
 6
 7const (
 8	ConnectionStateUnknown ConnectionState = 0
 9	ConnectionStateInit    ConnectionState = 1
10	ConnectionStateTryOpen ConnectionState = 2
11	ConnectionStateOpen    ConnectionState = 3
12)
13
14type Connection struct {
15	State                    ConnectionState
16	ClientId                 ClientId
17	CounterpartyClientId     ClientId
18	CounterpartyConnectionId ConnectionId
19}
20
21func (c Connection) EncodeABI() ([]byte, error) {
22	schema := abi.Schema{Fields: []abi.Field{
23		{Type: abi.TypeUint8},
24		{Type: abi.TypeUint32},
25		{Type: abi.TypeUint32},
26		{Type: abi.TypeUint32},
27	}}
28
29	bz, err := abi.Encode(schema, []any{
30		uint8(c.State),
31		uint32(c.ClientId),
32		uint32(c.CounterpartyClientId),
33		uint32(c.CounterpartyConnectionId),
34	})
35	if err != nil {
36		return nil, err
37	}
38
39	return bz, nil
40}
41
42func NewConnection(state ConnectionState, clientId, counterpartyClientId ClientId, counterpartyConnectionId ConnectionId) Connection {
43	return Connection{
44		State:                    state,
45		ClientId:                 clientId,
46		CounterpartyClientId:     counterpartyClientId,
47		CounterpartyConnectionId: counterpartyConnectionId,
48	}
49}