forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vote.go
116 lines (107 loc) · 3.35 KB
/
Vote.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
// Copyright 2021 github.com/gagliardetto
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vote
import (
"fmt"
"time"
"github.com/dojimanetwork/solana-go/v2"
"github.com/dojimanetwork/solana-go/v2/text/format"
bin "github.com/gagliardetto/binary"
"github.com/gagliardetto/treeout"
)
type Vote struct {
Slots []uint64
Hash solana.Hash
Timestamp *int64
// [0] = [WRITE] VoteAccount
// ··········· Vote account to vote with
//
// [1] = [] SysVarSlotHashes
// ··········· Slot hashes sysvar
//
// [2] = [] SysVarClock
// ··········· Clock sysvar
//
// [3] = [SIGNER] VoteAuthority
// ··········· Vote authority
solana.AccountMetaSlice `bin:"-" borsh_skip:"true"`
}
func (v *Vote) UnmarshalWithDecoder(dec *bin.Decoder) error {
v.Slots = nil
var numSlots uint64
if err := dec.Decode(&numSlots); err != nil {
return err
}
for i := uint64(0); i < numSlots; i++ {
var slot uint64
if err := dec.Decode(&slot); err != nil {
return err
}
v.Slots = append(v.Slots, slot)
}
if err := dec.Decode(&v.Hash); err != nil {
return err
}
var timestampVariant uint8
if err := dec.Decode(×tampVariant); err != nil {
return err
}
switch timestampVariant {
case 0:
break
case 1:
var ts int64
if err := dec.Decode(&ts); err != nil {
return err
}
v.Timestamp = &ts
default:
return fmt.Errorf("invalid vote timestamp variant %#08x", timestampVariant)
}
return nil
}
func (inst *Vote) Validate() error {
// Check whether all accounts are set:
for accIndex, acc := range inst.AccountMetaSlice {
if acc == nil {
return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex)
}
}
return nil
}
func (inst *Vote) EncodeToTree(parent treeout.Branches) {
parent.Child(format.Program(ProgramName, ProgramID)).
ParentFunc(func(programBranch treeout.Branches) {
programBranch.Child(format.Instruction("Vote")).
ParentFunc(func(instructionBranch treeout.Branches) {
// Parameters of the instruction:
instructionBranch.Child("Params").ParentFunc(func(paramsBranch treeout.Branches) {
paramsBranch.Child(format.Param("Slots", inst.Slots))
paramsBranch.Child(format.Param("Hash", inst.Hash))
var ts time.Time
if inst.Timestamp != nil {
ts = time.Unix(*inst.Timestamp, 0).UTC()
}
paramsBranch.Child(format.Param("Timestamp", ts))
})
// Accounts of the instruction:
instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch treeout.Branches) {
accountsBranch.Child(format.Meta("Vote Account ", inst.AccountMetaSlice[0]))
accountsBranch.Child(format.Meta("Slot Hashes Sysvar", inst.AccountMetaSlice[1]))
accountsBranch.Child(format.Meta("Clock Sysvar ", inst.AccountMetaSlice[2]))
accountsBranch.Child(format.Meta("Vote Authority ", inst.AccountMetaSlice[3]))
})
})
})
}