From 21fdbcee0e90d59c8eef5391fe72f2546791211a Mon Sep 17 00:00:00 2001 From: Nathan Martell Date: Tue, 25 Apr 2023 14:13:02 -0400 Subject: [PATCH] Added ComputeBudgetProgram to enable priority fees for transactions --- src/Solnet.Programs/ComputeBudgetProgram.cs | 97 +++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/Solnet.Programs/ComputeBudgetProgram.cs diff --git a/src/Solnet.Programs/ComputeBudgetProgram.cs b/src/Solnet.Programs/ComputeBudgetProgram.cs new file mode 100644 index 00000000..ca355661 --- /dev/null +++ b/src/Solnet.Programs/ComputeBudgetProgram.cs @@ -0,0 +1,97 @@ +using Solnet.Programs.Utilities; +using Solnet.Rpc.Models; +using Solnet.Wallet; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Solnet.Programs +{ + /// + /// Implements the ComputeBudget Program methods. + /// + /// For more information see: https://spl.solana.com/memo + /// + /// + + public class ComputeBudgetProgram + { + + /// + /// The public key of the ComputeBudget Program. + /// + public static readonly PublicKey ProgramIdKey = new("ComputeBudget111111111111111111111111111111"); + + + /// + /// The program's name. + /// + private const string ProgramName = "Compute Budget Program"; + + + + /// + /// Request HeapFrame Instruction related to Priority Fees + /// + /// + /// + public static TransactionInstruction RequestHeapFrame(uint bytes) + { + List keys = new(); + + byte[] instructionBytes = new byte[5]; + instructionBytes.WriteU8(1, 0); + instructionBytes.WriteU32(bytes, 1); + + return new TransactionInstruction + { + ProgramId = ProgramIdKey.KeyBytes, + Keys = keys, + Data = instructionBytes + }; + } + /// + /// Set Compute Unit Limit Instruction for Priority Fees + /// + /// + /// + public static TransactionInstruction SetComputeUnitLimit(uint units) + { + List keys = new(); + + byte[] instructionBytes = new byte[5]; + instructionBytes.WriteU8(2, 0); + instructionBytes.WriteU64(units, 1); + + return new TransactionInstruction + { + ProgramId = ProgramIdKey.KeyBytes, + Keys = keys, + Data = instructionBytes + }; + } + /// + /// Set Compute Unit Price Instruction for Priority Fees + /// + /// + /// + public static TransactionInstruction SetComputeUnitPrice(ulong priority_rate) + { + List keys = new(); + + byte[] instructionBytes = new byte[9]; + instructionBytes.WriteU8(3, 0); + instructionBytes.WriteU64(priority_rate, 1); + + return new TransactionInstruction + { + ProgramId = ProgramIdKey.KeyBytes, + Keys = keys, + Data = instructionBytes + }; + } + + } +}