github.com/gagliardetto/solana-go@v1.11.0/programs/vote/Vote.go (about) 1 // Copyright 2021 github.com/gagliardetto 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package vote 16 17 import ( 18 "fmt" 19 "time" 20 21 bin "github.com/gagliardetto/binary" 22 "github.com/gagliardetto/solana-go" 23 "github.com/gagliardetto/solana-go/text/format" 24 "github.com/gagliardetto/treeout" 25 ) 26 27 type Vote struct { 28 Slots []uint64 29 Hash solana.Hash 30 Timestamp *int64 31 32 // [0] = [WRITE] VoteAccount 33 // ··········· Vote account to vote with 34 // 35 // [1] = [] SysVarSlotHashes 36 // ··········· Slot hashes sysvar 37 // 38 // [2] = [] SysVarClock 39 // ··········· Clock sysvar 40 // 41 // [3] = [SIGNER] VoteAuthority 42 // ··········· Vote authority 43 solana.AccountMetaSlice `bin:"-" borsh_skip:"true"` 44 } 45 46 func (v *Vote) UnmarshalWithDecoder(dec *bin.Decoder) error { 47 v.Slots = nil 48 var numSlots uint64 49 if err := dec.Decode(&numSlots); err != nil { 50 return err 51 } 52 for i := uint64(0); i < numSlots; i++ { 53 var slot uint64 54 if err := dec.Decode(&slot); err != nil { 55 return err 56 } 57 v.Slots = append(v.Slots, slot) 58 } 59 if err := dec.Decode(&v.Hash); err != nil { 60 return err 61 } 62 var timestampVariant uint8 63 if err := dec.Decode(×tampVariant); err != nil { 64 return err 65 } 66 switch timestampVariant { 67 case 0: 68 break 69 case 1: 70 var ts int64 71 if err := dec.Decode(&ts); err != nil { 72 return err 73 } 74 v.Timestamp = &ts 75 default: 76 return fmt.Errorf("invalid vote timestamp variant %#08x", timestampVariant) 77 } 78 return nil 79 } 80 81 func (inst *Vote) Validate() error { 82 // Check whether all accounts are set: 83 for accIndex, acc := range inst.AccountMetaSlice { 84 if acc == nil { 85 return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex) 86 } 87 } 88 return nil 89 } 90 91 func (inst *Vote) EncodeToTree(parent treeout.Branches) { 92 parent.Child(format.Program(ProgramName, ProgramID)). 93 ParentFunc(func(programBranch treeout.Branches) { 94 programBranch.Child(format.Instruction("Vote")). 95 ParentFunc(func(instructionBranch treeout.Branches) { 96 // Parameters of the instruction: 97 instructionBranch.Child("Params").ParentFunc(func(paramsBranch treeout.Branches) { 98 paramsBranch.Child(format.Param("Slots", inst.Slots)) 99 paramsBranch.Child(format.Param("Hash", inst.Hash)) 100 var ts time.Time 101 if inst.Timestamp != nil { 102 ts = time.Unix(*inst.Timestamp, 0).UTC() 103 } 104 paramsBranch.Child(format.Param("Timestamp", ts)) 105 }) 106 107 // Accounts of the instruction: 108 instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch treeout.Branches) { 109 accountsBranch.Child(format.Meta("Vote Account ", inst.AccountMetaSlice[0])) 110 accountsBranch.Child(format.Meta("Slot Hashes Sysvar", inst.AccountMetaSlice[1])) 111 accountsBranch.Child(format.Meta("Clock Sysvar ", inst.AccountMetaSlice[2])) 112 accountsBranch.Child(format.Meta("Vote Authority ", inst.AccountMetaSlice[3])) 113 }) 114 }) 115 }) 116 }