-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #419 from BifrostTitan/master
Added compute budget program, priority fees and versioned transactions. Bug fixes and more
- Loading branch information
Showing
33 changed files
with
763 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// Implements the ComputeBudget Program methods. | ||
/// <remarks> | ||
/// For more information see: https://spl.solana.com/memo | ||
/// </remarks> | ||
/// </summary> | ||
|
||
public class ComputeBudgetProgram | ||
{ | ||
|
||
/// <summary> | ||
/// The public key of the ComputeBudget Program. | ||
/// </summary> | ||
public static readonly PublicKey ProgramIdKey = new("ComputeBudget111111111111111111111111111111"); | ||
|
||
|
||
/// <summary> | ||
/// The program's name. | ||
/// </summary> | ||
private const string ProgramName = "Compute Budget Program"; | ||
|
||
|
||
|
||
/// <summary> | ||
/// Request HeapFrame Instruction related to Priority Fees | ||
/// </summary> | ||
/// <param name="bytes"></param> | ||
/// <returns></returns> | ||
public static TransactionInstruction RequestHeapFrame(uint bytes) | ||
{ | ||
List<AccountMeta> keys = new(); | ||
|
||
byte[] instructionBytes = new byte[17]; | ||
instructionBytes.WriteU8(1, 0); | ||
instructionBytes.WriteU32(bytes, 1); | ||
|
||
return new TransactionInstruction | ||
{ | ||
ProgramId = ProgramIdKey.KeyBytes, | ||
Keys = keys, | ||
Data = instructionBytes | ||
}; | ||
} | ||
/// <summary> | ||
/// Set Compute Unit Limit Instruction for Priority Fees | ||
/// </summary> | ||
/// <param name="units"></param> | ||
/// <returns></returns> | ||
public static TransactionInstruction SetComputeUnitLimit(uint units) | ||
{ | ||
List<AccountMeta> keys = new(); | ||
|
||
byte[] instructionBytes = new byte[9]; | ||
instructionBytes.WriteU8(2, 0); | ||
instructionBytes.WriteU64(units, 1); | ||
|
||
return new TransactionInstruction | ||
{ | ||
ProgramId = ProgramIdKey.KeyBytes, | ||
Keys = keys, | ||
Data = instructionBytes | ||
}; | ||
} | ||
/// <summary> | ||
/// Set Compute Unit Price Instruction for Priority Fees | ||
/// </summary> | ||
/// <param name="priority_rate"></param> | ||
/// <returns></returns> | ||
public static TransactionInstruction SetComputeUnitPrice(ulong priority_rate) | ||
{ | ||
List<AccountMeta> 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 | ||
}; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
using Solnet.Rpc.Models; | ||
using Solnet.Rpc.Utilities; | ||
using Solnet.Wallet; | ||
using Solnet.Wallet.Utilities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using static Solnet.Rpc.Models.Message; | ||
|
||
namespace Solnet.Rpc.Builders | ||
{ | ||
/// <summary> | ||
/// A compiled instruction within the message. | ||
/// </summary> | ||
public class VersionedMessageBuilder : MessageBuilder | ||
{ | ||
|
||
/// <summary> | ||
/// Address Table Lookups | ||
/// </summary> | ||
public List<MessageAddressTableLookup> AddressTableLookups { get; set; } | ||
public IList<PublicKey> AccountKeys { get; internal set; } | ||
|
||
/// <summary> | ||
/// Builds the message into the wire format. | ||
/// </summary> | ||
/// <returns>The encoded message.</returns> | ||
internal override byte[] Build() | ||
{ | ||
if (RecentBlockHash == null && NonceInformation == null) | ||
throw new Exception("recent block hash or nonce information is required"); | ||
if (Instructions == null) | ||
throw new Exception("no instructions provided in the transaction"); | ||
|
||
// In case the user specified nonce information, we'll use it. | ||
if (NonceInformation != null) | ||
{ | ||
RecentBlockHash = NonceInformation.Nonce; | ||
_accountKeysList.Add(NonceInformation.Instruction.Keys); | ||
_accountKeysList.Add(AccountMeta.ReadOnly(new PublicKey(NonceInformation.Instruction.ProgramId), | ||
false)); | ||
List<TransactionInstruction> newInstructions = new() { NonceInformation.Instruction }; | ||
newInstructions.AddRange(Instructions); | ||
Instructions = newInstructions; | ||
} | ||
|
||
_messageHeader = new MessageHeader(); | ||
|
||
List<AccountMeta> keysList = GetAccountKeys(); | ||
byte[] accountAddressesLength = ShortVectorEncoding.EncodeLength(keysList.Count); | ||
int compiledInstructionsLength = 0; | ||
List<CompiledInstruction> compiledInstructions = new(); | ||
|
||
foreach (TransactionInstruction instruction in Instructions) | ||
{ | ||
int keyCount = instruction.Keys.Count; | ||
byte[] keyIndices = new byte[keyCount]; | ||
|
||
if (instruction.GetType() == typeof(VersionedTransactionInstruction)) | ||
{ | ||
keyIndices = ((VersionedTransactionInstruction)instruction).KeyIndices; | ||
} | ||
else | ||
{ | ||
for (int i = 0; i < keyCount; i++) | ||
{ | ||
keyIndices[i] = FindAccountIndex(keysList, instruction.Keys[i].PublicKey); | ||
} | ||
} | ||
|
||
CompiledInstruction compiledInstruction = new() | ||
{ | ||
ProgramIdIndex = FindAccountIndex(keysList, instruction.ProgramId), | ||
KeyIndicesCount = ShortVectorEncoding.EncodeLength(keyIndices.Length), | ||
KeyIndices = keyIndices, | ||
DataLength = ShortVectorEncoding.EncodeLength(instruction.Data.Length), | ||
Data = instruction.Data | ||
}; | ||
compiledInstructions.Add(compiledInstruction); | ||
compiledInstructionsLength += compiledInstruction.Length(); | ||
} | ||
|
||
int accountKeysBufferSize = _accountKeysList.AccountList.Count * 32; | ||
MemoryStream accountKeysBuffer = new MemoryStream(accountKeysBufferSize); | ||
byte[] instructionsLength = ShortVectorEncoding.EncodeLength(compiledInstructions.Count); | ||
|
||
foreach (AccountMeta accountMeta in keysList) | ||
{ | ||
accountKeysBuffer.Write(accountMeta.PublicKeyBytes, 0, accountMeta.PublicKeyBytes.Length); | ||
if (accountMeta.IsSigner) | ||
{ | ||
_messageHeader.RequiredSignatures += 1; | ||
if (!accountMeta.IsWritable) | ||
_messageHeader.ReadOnlySignedAccounts += 1; | ||
} | ||
else | ||
{ | ||
if (!accountMeta.IsWritable) | ||
_messageHeader.ReadOnlyUnsignedAccounts += 1; | ||
} | ||
} | ||
|
||
#region Build Message Body | ||
|
||
int messageBufferSize = MessageHeader.Layout.HeaderLength + BlockHashLength + | ||
accountAddressesLength.Length + | ||
+instructionsLength.Length + compiledInstructionsLength + accountKeysBufferSize; | ||
MemoryStream buffer = new MemoryStream(messageBufferSize); | ||
byte[] messageHeaderBytes = _messageHeader.ToBytes(); | ||
|
||
buffer.Write(new byte[] { 128 }, 0, 1); | ||
buffer.Write(messageHeaderBytes, 0, messageHeaderBytes.Length); | ||
buffer.Write(accountAddressesLength, 0, accountAddressesLength.Length); | ||
buffer.Write(accountKeysBuffer.ToArray(), 0, accountKeysBuffer.ToArray().Length); | ||
var encodedRecentBlockHash = Encoders.Base58.DecodeData(RecentBlockHash); | ||
buffer.Write(encodedRecentBlockHash, 0, encodedRecentBlockHash.Length); | ||
buffer.Write(instructionsLength, 0, instructionsLength.Length); | ||
|
||
foreach (CompiledInstruction compiledInstruction in compiledInstructions) | ||
{ | ||
buffer.WriteByte(compiledInstruction.ProgramIdIndex); | ||
buffer.Write(compiledInstruction.KeyIndicesCount, 0, compiledInstruction.KeyIndicesCount.Length); | ||
buffer.Write(compiledInstruction.KeyIndices, 0, compiledInstruction.KeyIndices.Length); | ||
buffer.Write(compiledInstruction.DataLength, 0, compiledInstruction.DataLength.Length); | ||
buffer.Write(compiledInstruction.Data, 0, compiledInstruction.Data.Length); | ||
} | ||
|
||
#endregion | ||
|
||
var serializeAddressTableLookups = AddressTableLookupUtils.SerializeAddressTableLookups(AddressTableLookups); | ||
buffer.Write(serializeAddressTableLookups, 0, serializeAddressTableLookups.Length); | ||
|
||
return buffer.ToArray(); | ||
} | ||
} | ||
} |
Oops, something went wrong.