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

channel.gno

1.40 Kb · 70 lines
 1package types
 2
 3import (
 4	"strconv"
 5
 6	"gno.land/p/onbloc/encoding/abi"
 7)
 8
 9type ChannelId uint32
10
11func (id ChannelId) String() string {
12	return strconv.FormatUint(uint64(id), 10)
13}
14
15type ChannelState uint8
16
17const (
18	ChannelStateUnknown ChannelState = 0
19	ChannelStateInit    ChannelState = 1
20	ChannelStateTryOpen ChannelState = 2
21	ChannelStateOpen    ChannelState = 3
22	ChannelStateClosed  ChannelState = 4
23)
24
25type Channel struct {
26	State                 ChannelState
27	ConnectionId          ConnectionId
28	CounterpartyChannelId ChannelId
29	CounterpartyPortId    Bytes
30	Version               string
31}
32
33func (c Channel) EncodeABI() ([]byte, error) {
34	schema := abi.Schema{Fields: []abi.Field{
35		{Type: abi.TypeUint8},
36		{Type: abi.TypeUint32},
37		{Type: abi.TypeUint32},
38		{Type: abi.TypeBytes},
39		{Type: abi.TypeString},
40	}}
41
42	bz, err := abi.Encode(schema, []any{
43		uint8(c.State),
44		uint32(c.ConnectionId),
45		uint32(c.CounterpartyChannelId),
46		c.CounterpartyPortId.Bytes(),
47		c.Version,
48	})
49	if err != nil {
50		return nil, err
51	}
52
53	return wrapABIEncode(bz), nil
54}
55
56func NewChannel(
57	state ChannelState,
58	connectionId ConnectionId,
59	counterpartyChannelId ChannelId,
60	counterpartyPortId Bytes,
61	version string,
62) Channel {
63	return Channel{
64		State:                 state,
65		ConnectionId:          connectionId,
66		CounterpartyChannelId: counterpartyChannelId,
67		CounterpartyPortId:    counterpartyPortId,
68		Version:               version,
69	}
70}