github.com/gagliardetto/solana-go@v1.11.0/programs/system/TransferWithSeed.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 system 16 17 import ( 18 "encoding/binary" 19 "errors" 20 "fmt" 21 22 ag_binary "github.com/gagliardetto/binary" 23 ag_solanago "github.com/gagliardetto/solana-go" 24 ag_format "github.com/gagliardetto/solana-go/text/format" 25 ag_treeout "github.com/gagliardetto/treeout" 26 ) 27 28 // Transfer lamports from a derived address 29 type TransferWithSeed struct { 30 // Amount to transfer 31 Lamports *uint64 32 33 // Seed to use to derive the funding account address 34 FromSeed *string 35 36 // Owner to use to derive the funding account address 37 FromOwner *ag_solanago.PublicKey 38 39 // [0] = [WRITE] FundingAccount 40 // ··········· Funding account 41 // 42 // [1] = [SIGNER] BaseForFundingAccount 43 // ··········· Base for funding account 44 // 45 // [2] = [WRITE] RecipientAccount 46 // ··········· Recipient account 47 ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"` 48 } 49 50 // NewTransferWithSeedInstructionBuilder creates a new `TransferWithSeed` instruction builder. 51 func NewTransferWithSeedInstructionBuilder() *TransferWithSeed { 52 nd := &TransferWithSeed{ 53 AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 3), 54 } 55 return nd 56 } 57 58 // Amount to transfer 59 func (inst *TransferWithSeed) SetLamports(lamports uint64) *TransferWithSeed { 60 inst.Lamports = &lamports 61 return inst 62 } 63 64 // Seed to use to derive the funding account address 65 func (inst *TransferWithSeed) SetFromSeed(from_seed string) *TransferWithSeed { 66 inst.FromSeed = &from_seed 67 return inst 68 } 69 70 // Owner to use to derive the funding account address 71 func (inst *TransferWithSeed) SetFromOwner(from_owner ag_solanago.PublicKey) *TransferWithSeed { 72 inst.FromOwner = &from_owner 73 return inst 74 } 75 76 // Funding account 77 func (inst *TransferWithSeed) SetFundingAccount(fundingAccount ag_solanago.PublicKey) *TransferWithSeed { 78 inst.AccountMetaSlice[0] = ag_solanago.Meta(fundingAccount).WRITE() 79 return inst 80 } 81 82 func (inst *TransferWithSeed) GetFundingAccount() *ag_solanago.AccountMeta { 83 return inst.AccountMetaSlice[0] 84 } 85 86 // Base for funding account 87 func (inst *TransferWithSeed) SetBaseForFundingAccount(baseForFundingAccount ag_solanago.PublicKey) *TransferWithSeed { 88 inst.AccountMetaSlice[1] = ag_solanago.Meta(baseForFundingAccount).SIGNER() 89 return inst 90 } 91 92 func (inst *TransferWithSeed) GetBaseForFundingAccount() *ag_solanago.AccountMeta { 93 return inst.AccountMetaSlice[1] 94 } 95 96 // Recipient account 97 func (inst *TransferWithSeed) SetRecipientAccount(recipientAccount ag_solanago.PublicKey) *TransferWithSeed { 98 inst.AccountMetaSlice[2] = ag_solanago.Meta(recipientAccount).WRITE() 99 return inst 100 } 101 102 func (inst *TransferWithSeed) GetRecipientAccount() *ag_solanago.AccountMeta { 103 return inst.AccountMetaSlice[2] 104 } 105 106 func (inst TransferWithSeed) Build() *Instruction { 107 return &Instruction{BaseVariant: ag_binary.BaseVariant{ 108 Impl: inst, 109 TypeID: ag_binary.TypeIDFromUint32(Instruction_TransferWithSeed, binary.LittleEndian), 110 }} 111 } 112 113 // ValidateAndBuild validates the instruction parameters and accounts; 114 // if there is a validation error, it returns the error. 115 // Otherwise, it builds and returns the instruction. 116 func (inst TransferWithSeed) ValidateAndBuild() (*Instruction, error) { 117 if err := inst.Validate(); err != nil { 118 return nil, err 119 } 120 return inst.Build(), nil 121 } 122 123 func (inst *TransferWithSeed) Validate() error { 124 // Check whether all (required) parameters are set: 125 { 126 if inst.Lamports == nil { 127 return errors.New("Lamports parameter is not set") 128 } 129 if inst.FromSeed == nil { 130 return errors.New("FromSeed parameter is not set") 131 } 132 if inst.FromOwner == nil { 133 return errors.New("FromOwner parameter is not set") 134 } 135 } 136 137 // Check whether all accounts are set: 138 for accIndex, acc := range inst.AccountMetaSlice { 139 if acc == nil { 140 return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex) 141 } 142 } 143 return nil 144 } 145 146 func (inst *TransferWithSeed) EncodeToTree(parent ag_treeout.Branches) { 147 parent.Child(ag_format.Program(ProgramName, ProgramID)). 148 // 149 ParentFunc(func(programBranch ag_treeout.Branches) { 150 programBranch.Child(ag_format.Instruction("TransferWithSeed")). 151 // 152 ParentFunc(func(instructionBranch ag_treeout.Branches) { 153 154 // Parameters of the instruction: 155 instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) { 156 paramsBranch.Child(ag_format.Param(" Lamports", *inst.Lamports)) 157 paramsBranch.Child(ag_format.Param(" FromSeed", *inst.FromSeed)) 158 paramsBranch.Child(ag_format.Param("FromOwner", *inst.FromOwner)) 159 }) 160 161 // Accounts of the instruction: 162 instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) { 163 accountsBranch.Child(ag_format.Meta(" Funding", inst.AccountMetaSlice[0])) 164 accountsBranch.Child(ag_format.Meta("BaseForFunding", inst.AccountMetaSlice[1])) 165 accountsBranch.Child(ag_format.Meta(" Recipient", inst.AccountMetaSlice[2])) 166 }) 167 }) 168 }) 169 } 170 171 func (inst TransferWithSeed) MarshalWithEncoder(encoder *ag_binary.Encoder) error { 172 // Serialize `Lamports` param: 173 { 174 err := encoder.Encode(*inst.Lamports) 175 if err != nil { 176 return err 177 } 178 } 179 // Serialize `FromSeed` param: 180 { 181 err := encoder.WriteRustString(*inst.FromSeed) 182 if err != nil { 183 return err 184 } 185 } 186 // Serialize `FromOwner` param: 187 { 188 err := encoder.Encode(*inst.FromOwner) 189 if err != nil { 190 return err 191 } 192 } 193 return nil 194 } 195 196 func (inst *TransferWithSeed) UnmarshalWithDecoder(decoder *ag_binary.Decoder) error { 197 // Deserialize `Lamports` param: 198 { 199 err := decoder.Decode(&inst.Lamports) 200 if err != nil { 201 return err 202 } 203 } 204 // Deserialize `FromSeed` param: 205 { 206 value, err := decoder.ReadRustString() 207 if err != nil { 208 return err 209 } 210 inst.FromSeed = &value 211 } 212 // Deserialize `FromOwner` param: 213 { 214 err := decoder.Decode(&inst.FromOwner) 215 if err != nil { 216 return err 217 } 218 } 219 return nil 220 } 221 222 // NewTransferWithSeedInstruction declares a new TransferWithSeed instruction with the provided parameters and accounts. 223 func NewTransferWithSeedInstruction( 224 // Parameters: 225 lamports uint64, 226 from_seed string, 227 from_owner ag_solanago.PublicKey, 228 // Accounts: 229 fundingAccount ag_solanago.PublicKey, 230 baseForFundingAccount ag_solanago.PublicKey, 231 recipientAccount ag_solanago.PublicKey) *TransferWithSeed { 232 return NewTransferWithSeedInstructionBuilder(). 233 SetLamports(lamports). 234 SetFromSeed(from_seed). 235 SetFromOwner(from_owner). 236 SetFundingAccount(fundingAccount). 237 SetBaseForFundingAccount(baseForFundingAccount). 238 SetRecipientAccount(recipientAccount) 239 }