|
| 1 | +using Solnet.Rpc.Models; |
| 2 | +using Solnet.Wallet; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace Solnet.Programs |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Implements the BPF Loader Program methods. |
| 10 | + /// <remarks> |
| 11 | + /// For more information see: |
| 12 | + /// https://docs.rs/solana-sdk/1.9.13/solana_sdk/loader_upgradeable_instruction/enum.UpgradeableLoaderInstruction.html |
| 13 | + /// </remarks> |
| 14 | + /// </summary> |
| 15 | + public static class BPFLoaderProgram |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// The public key of the BPF Loader Program. |
| 19 | + /// </summary> |
| 20 | + public static readonly PublicKey ProgramIdKey = new("BPFLoaderUpgradeab1e11111111111111111111111"); |
| 21 | + /// <summary> |
| 22 | + /// The program's name. |
| 23 | + /// </summary> |
| 24 | + private const string ProgramName = "BPF Loader Program"; |
| 25 | + /// <summary> |
| 26 | + /// Initialize a Buffer account. |
| 27 | + /// A Buffer account is an intermediary that once fully populated is used with the DeployWithMaxDataLen instruction to populate the program’s ProgramData account. |
| 28 | + /// The InitializeBuffer instruction requires no signers and MUST be included within the same Transaction as the system program’s CreateAccount instruction that creates the account being initialized. Otherwise another party may initialize the account. |
| 29 | + /// </summary> |
| 30 | + /// <param name="sourceAccount">public key of the account to init</param> |
| 31 | + /// <param name="authority">public key of the authority over the account</param> |
| 32 | + /// <returns>The transaction instruction</returns> |
| 33 | + public static TransactionInstruction InitializeBuffer(PublicKey sourceAccount , PublicKey authority = null) |
| 34 | + { |
| 35 | + var keys = new List<AccountMeta> () |
| 36 | + { |
| 37 | + AccountMeta.Writable(sourceAccount, false), |
| 38 | + }; |
| 39 | + if (authority !=null) keys.Add( AccountMeta.ReadOnly(authority,false)); |
| 40 | + return new TransactionInstruction |
| 41 | + { |
| 42 | + ProgramId = ProgramIdKey.KeyBytes, |
| 43 | + Keys = keys, |
| 44 | + Data = BPFLoaderProgramData.EncodeInitializeBuffer().ToArray() |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Write program data into a Buffer account. |
| 50 | + /// </summary> |
| 51 | + /// <param name="bufferAccount">the public key of the buffer account</param> |
| 52 | + /// <param name="bufferAuthority">the public key of the authority over the buffer account</param> |
| 53 | + /// <param name="data">data to write to the buffer account (Serialized program data)</param> |
| 54 | + /// <param name="offset">offset at which to write the given data.</param> |
| 55 | + /// <returns>The transaction instruction</returns> |
| 56 | + public static TransactionInstruction Write(PublicKey bufferAccount, PublicKey bufferAuthority, Span<byte> data, uint offset) |
| 57 | + { |
| 58 | + var keys = new List<AccountMeta> () |
| 59 | + { |
| 60 | + AccountMeta.Writable(bufferAccount, false), |
| 61 | + AccountMeta.ReadOnly(bufferAuthority, true), |
| 62 | + }; |
| 63 | + return new TransactionInstruction |
| 64 | + { |
| 65 | + ProgramId = ProgramIdKey.KeyBytes, |
| 66 | + Keys = keys, |
| 67 | + Data = BPFLoaderProgramData.EncodeWrite(offset, data).ToArray() |
| 68 | + }; |
| 69 | + } |
| 70 | + /// <summary> |
| 71 | + ///A program consists of a Program and ProgramData account pair |
| 72 | + /// The Program account’s address will serve as the program id for any instructions that execute this program. |
| 73 | + /// The ProgramData account will remain mutable by the loader only and holds the program data and authority information. The ProgramData account’s address is derived from the Program account’s address and created by the DeployWithMaxDataLen instruction |
| 74 | + /// </summary> |
| 75 | + /// <param name="payer">The payer account that will pay to create the ProgramData account</param> |
| 76 | + /// <param name="programDataAccount">The uninitialized ProgramData account</param> |
| 77 | + /// <param name="programAccount">The uninitialized Program account</param> |
| 78 | + /// <param name="bufferAccount"> The Buffer account where the program data has been written. The buffer account’s authority must match the program’s authority</param> |
| 79 | + /// <param name="authority">the public key of the authority</param> |
| 80 | + /// <param name="maxDataLenght">Maximum length that the program can be upgraded to</param> |
| 81 | + /// <returns>The transaction instruction</returns> |
| 82 | + public static TransactionInstruction DeployWithMaxDataLen(PublicKey payer, PublicKey programDataAccount, |
| 83 | + PublicKey programAccount, PublicKey bufferAccount, PublicKey authority, ulong maxDataLenght) |
| 84 | + { |
| 85 | + var keys = new List<AccountMeta> () |
| 86 | + { |
| 87 | + AccountMeta.ReadOnly(payer, true), |
| 88 | + AccountMeta.Writable(programDataAccount, false), |
| 89 | + AccountMeta.Writable(programAccount, false), |
| 90 | + AccountMeta.Writable(bufferAccount, false), |
| 91 | + AccountMeta.ReadOnly(SysVars.RentKey, false), |
| 92 | + AccountMeta.ReadOnly(SysVars.ClockKey, false), |
| 93 | + AccountMeta.ReadOnly(SystemProgram.ProgramIdKey, false), |
| 94 | + AccountMeta.ReadOnly(authority, true), |
| 95 | + }; |
| 96 | + return new TransactionInstruction |
| 97 | + { |
| 98 | + ProgramId = ProgramIdKey.KeyBytes, |
| 99 | + Keys = keys, |
| 100 | + Data = BPFLoaderProgramData.EncodeDeployWithMaxDataLen(maxDataLenght).ToArray() |
| 101 | + }; |
| 102 | + } |
| 103 | + /// <summary> |
| 104 | + /// Upgrade a program |
| 105 | + ///A program can be updated as long as the program’s authority has not been set to None. |
| 106 | + /// The Buffer account must contain sufficient lamports to fund the ProgramData account to be rent-exempt, any additional lamports left over will be transferred to the spill account, leaving the Buffer account balance at zero. |
| 107 | + /// </summary> |
| 108 | + /// <param name="programDataAccount">The ProgramData account.</param> |
| 109 | + /// <param name="programAccount"></param> |
| 110 | + /// <param name="bufferAccount"></param> |
| 111 | + /// <param name="spillAccount"></param> |
| 112 | + /// <param name="authority"></param> |
| 113 | + /// <returns>The transaction instruction</returns> |
| 114 | + public static TransactionInstruction Upgrade(PublicKey programDataAccount, PublicKey programAccount, |
| 115 | + PublicKey bufferAccount, PublicKey spillAccount, PublicKey authority) |
| 116 | + { |
| 117 | + var keys = new List<AccountMeta> () |
| 118 | + { |
| 119 | + AccountMeta.Writable(programDataAccount, false), |
| 120 | + AccountMeta.Writable(programAccount, false), |
| 121 | + AccountMeta.Writable(bufferAccount, false), |
| 122 | + AccountMeta.Writable(spillAccount, false), |
| 123 | + AccountMeta.ReadOnly(SysVars.RentKey, false), |
| 124 | + AccountMeta.ReadOnly(SysVars.ClockKey, false), |
| 125 | + AccountMeta.ReadOnly(authority, true), |
| 126 | + }; |
| 127 | + return new TransactionInstruction |
| 128 | + { |
| 129 | + ProgramId = ProgramIdKey.KeyBytes, |
| 130 | + Keys = keys, |
| 131 | + Data = BPFLoaderProgramData.EncodeUpgrade().ToArray() |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + ///Set a new authority that is allowed to write the buffer or upgrade the program. To permanently make the buffer immutable or disable program updates omit the new authority. |
| 137 | + /// </summary> |
| 138 | + /// <param name="bufferOrProgramDataaccount"></param> |
| 139 | + /// <param name="authority"></param> |
| 140 | + /// <param name="newAuthority"></param> |
| 141 | + /// <returns>The transaction instruction</returns> |
| 142 | + public static TransactionInstruction SetAuthority(PublicKey bufferOrProgramDataaccount, PublicKey authority, |
| 143 | + PublicKey newAuthority = null) |
| 144 | + { |
| 145 | + var keys = new List<AccountMeta> () |
| 146 | + { |
| 147 | + AccountMeta.Writable(bufferOrProgramDataaccount, false), |
| 148 | + AccountMeta.ReadOnly(authority, true), |
| 149 | + }; |
| 150 | + if (newAuthority != null) keys.Add(AccountMeta.ReadOnly(newAuthority,false)); |
| 151 | + return new TransactionInstruction |
| 152 | + { |
| 153 | + ProgramId = ProgramIdKey.KeyBytes, |
| 154 | + Keys = keys, |
| 155 | + Data = BPFLoaderProgramData.EncodeSetAuthority().ToArray() |
| 156 | + }; |
| 157 | + } |
| 158 | + /// <summary> |
| 159 | + /// Closes an account owned by the upgradeable loader of all lamports and withdraws all the lamports |
| 160 | + /// </summary> |
| 161 | + /// <param name="accountToClose">public key of the account to close</param> |
| 162 | + /// <param name="depositAccount">public key of the account to transfer remaining lamports to </param> |
| 163 | + /// <param name="associatedProgramAccount">public key of the associated program account</param> |
| 164 | + /// <param name="authority">public key of the authority for the account to close</param> |
| 165 | + /// <returns>The transaction instruction</returns> |
| 166 | + public static TransactionInstruction Close(PublicKey accountToClose, PublicKey depositAccount, |
| 167 | + PublicKey associatedProgramAccount = null, PublicKey authority = null) |
| 168 | + { |
| 169 | + var keys = new List<AccountMeta> () |
| 170 | + { |
| 171 | + AccountMeta.Writable(accountToClose, false), |
| 172 | + AccountMeta.Writable(depositAccount, false), |
| 173 | + }; |
| 174 | + if (authority != null) keys.Add(AccountMeta.ReadOnly(authority, true)); |
| 175 | + if (associatedProgramAccount != null) keys.Add(AccountMeta.Writable(associatedProgramAccount,false)); |
| 176 | + return new TransactionInstruction |
| 177 | + { |
| 178 | + ProgramId = ProgramIdKey.KeyBytes, |
| 179 | + Keys = keys, |
| 180 | + Data = BPFLoaderProgramData.EncodeClose().ToArray() |
| 181 | + }; |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
0 commit comments