-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added extension methods to facilitate program data encoding and decoding and abstract class for bit masks.
- Loading branch information
1 parent
d571c80
commit c24a32a
Showing
16 changed files
with
1,160 additions
and
183 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
namespace Solnet.Programs.Abstract | ||
{ | ||
/// <summary> | ||
/// Represents a flag using a byte for masking. | ||
/// </summary> | ||
public class ByteFlag : Flag<byte> | ||
{ | ||
/// <summary> | ||
/// Check if the 1st bit is set. | ||
/// </summary> | ||
public bool Bit0 => (Value & 0x01) != 0; | ||
|
||
/// <summary> | ||
/// Check if the 2nd bit is set. | ||
/// </summary> | ||
public bool Bit1 => (Value & 0x02) != 0; | ||
|
||
/// <summary> | ||
/// Check if the 3rc bit is set. | ||
/// </summary> | ||
public bool Bit2 => (Value & 0x04) != 0; | ||
|
||
/// <summary> | ||
/// Check if the 4th bit is set. | ||
/// </summary> | ||
public bool Bit3 => (Value & 0x08) != 0; | ||
|
||
/// <summary> | ||
/// Check if the 5th bit is set. | ||
/// </summary> | ||
public bool Bit4 => (Value & 0x10) != 0; | ||
|
||
/// <summary> | ||
/// Check if the 6th bit is set. | ||
/// </summary> | ||
public bool Bit5 => (Value & 0x20) != 0; | ||
|
||
/// <summary> | ||
/// Check if the 7th bit is set. | ||
/// </summary> | ||
public bool Bit6 => (Value & 0x40) != 0; | ||
|
||
/// <summary> | ||
/// Check if the 8th bit is set. | ||
/// </summary> | ||
public bool Bit7 => (Value & 0x80) != 0; | ||
|
||
/// <summary> | ||
/// Initialize the flags with the given byte. | ||
/// </summary> | ||
/// <param name="mask">The byte to use.</param> | ||
public ByteFlag(byte mask) : base(mask) { } | ||
} | ||
} |
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,22 @@ | ||
namespace Solnet.Programs.Abstract | ||
{ | ||
/// <summary> | ||
/// Represents bitmask flags for various types of accounts within Solana Programs. | ||
/// </summary> | ||
public abstract class Flag<T> | ||
{ | ||
/// <summary> | ||
/// The mask for the account flags. | ||
/// </summary> | ||
public T Value { get; } | ||
|
||
/// <summary> | ||
/// Initialize the flags with the given mask. | ||
/// </summary> | ||
/// <param name="mask">The mask to use.</param> | ||
protected Flag(T mask) | ||
{ | ||
Value = mask; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,53 +1,54 @@ | ||
using Solnet.Rpc.Models; | ||
using Solnet.Wallet; | ||
using Solnet.Wallet.Utilities; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Solnet.Programs | ||
{ | ||
/// <summary> | ||
/// Helper class for the Shared Memory Program. | ||
/// <remarks> | ||
/// Used to write data to a given account data. | ||
/// Note: this programm, as of writting this, was inactive in some clusters. | ||
/// </remarks> | ||
/// </summary> | ||
public static class SharedMemoryProgram | ||
{ | ||
/// <summary> | ||
/// The address of the Shared Memory Program. | ||
/// </summary> | ||
public static readonly PublicKey ProgramId = new("shmem4EWT2sPdVGvTZCzXXRAURL9G5vpPxNwSeKhHUL"); | ||
|
||
/// <summary> | ||
/// Creates an instruction used to interact with the Shared memory program. | ||
/// This instruction writes data to a given program starting at a specific offset. | ||
/// </summary> | ||
/// <param name="dest">The account where the data is to be written.</param> | ||
/// <param name="payload">The data to be written.</param> | ||
/// <param name="offset">The offset of the account data to write to.</param> | ||
/// <returns>The <see cref="TransactionInstruction"/> encoded that interacts with the shared memory program..</returns> | ||
public static TransactionInstruction Write(PublicKey dest, byte[] payload, ulong offset) | ||
{ | ||
var keys = new List<AccountMeta> | ||
{ | ||
new (dest, true) | ||
}; | ||
|
||
var transactionData = new byte[payload.Length + 8]; | ||
|
||
Utils.Int64ToByteArrayLe(offset, transactionData, 0); | ||
|
||
payload.CopyTo(transactionData, 8); | ||
|
||
return new TransactionInstruction | ||
{ | ||
ProgramId = ProgramId.KeyBytes, | ||
Keys = keys, | ||
Data = transactionData | ||
}; | ||
} | ||
|
||
} | ||
using Solnet.Programs.Utilities; | ||
using Solnet.Rpc.Models; | ||
using Solnet.Wallet; | ||
using Solnet.Wallet.Utilities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Solnet.Programs | ||
{ | ||
/// <summary> | ||
/// Helper class for the Shared Memory Program. | ||
/// <remarks> | ||
/// Used to write data to a given account data. | ||
/// Note: this program, as of writing this, was inactive in some clusters. | ||
/// </remarks> | ||
/// </summary> | ||
public static class SharedMemoryProgram | ||
{ | ||
/// <summary> | ||
/// The address of the Shared Memory Program. | ||
/// </summary> | ||
public static readonly PublicKey ProgramId = new("shmem4EWT2sPdVGvTZCzXXRAURL9G5vpPxNwSeKhHUL"); | ||
|
||
/// <summary> | ||
/// Creates an instruction used to interact with the Shared memory program. | ||
/// This instruction writes data to a given program starting at a specific offset. | ||
/// </summary> | ||
/// <param name="dest">The account where the data is to be written.</param> | ||
/// <param name="payload">The data to be written.</param> | ||
/// <param name="offset">The offset of the account data to write to.</param> | ||
/// <returns>The <see cref="TransactionInstruction"/> encoded that interacts with the shared memory program..</returns> | ||
public static TransactionInstruction Write(PublicKey dest, ReadOnlySpan<byte> payload, ulong offset) | ||
{ | ||
List<AccountMeta> keys = new () | ||
{ | ||
new AccountMeta(dest, true) | ||
}; | ||
|
||
byte[] transactionData = new byte[payload.Length + 8]; | ||
|
||
transactionData.WriteU64(offset, 0); | ||
transactionData.WriteSpan(payload, 8); | ||
|
||
return new TransactionInstruction | ||
{ | ||
ProgramId = ProgramId.KeyBytes, | ||
Keys = keys, | ||
Data = transactionData | ||
}; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.