-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathMetaplexHelpers.cs
75 lines (63 loc) · 2.47 KB
/
MetaplexHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Solnet.Programs.Utilities;
using Solnet.Rpc;
using Solnet.Rpc.Models;
using Solnet.Rpc.Utilities;
using Solnet.Wallet;
namespace Observer
{
/// <summary>
/// Implements helpers for calculations related to Metaplex protocol.
/// </summary>
public static class MetaplexHelpers
{
/// <summary>
/// The prefix bytes for the metadata account PDA.
/// </summary>
private static readonly byte[] PrefixBytes = Encoding.UTF8.GetBytes("PREFIX");
/// <summary>
/// The Metaplex program.
/// </summary>
public static PublicKey ProgramId = new ("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s");
/// <summary>
/// Number of lamports per sol, used to calculate SOL values.
/// </summary>
public const int LamportsPerSol = 1_000_000_000;
/// <summary>
/// Gets price value from instruction data for SMB listings.
/// </summary>
/// <param name="data">The instruction data.</param>
/// <returns></returns>
public static ulong GetPriceFromData(ReadOnlySpan<byte> data)
{
var price = data.GetU64(1);
return price;
}
public static async Task<AccountInfo> GetAccountInfo(IRpcClient client, PublicKey address)
{
while (true)
{
var tokenAccountInfo = await client.GetAccountInfoAsync(address);
if (tokenAccountInfo.WasRequestSuccessfullyHandled) return tokenAccountInfo.Result.Value;
await Task.Delay(250);
}
}
public static (PublicKey mint, PublicKey owner) GetTokenAccountMintAndOwner(AccountInfo tokenAccountInfo)
{
var accountData = (ReadOnlySpan<byte>) Convert.FromBase64String(tokenAccountInfo.Data[0]).AsSpan();
var nftMint = accountData.GetPubKey(0);
var actualOwner = accountData.GetPubKey(32);
return (nftMint, actualOwner);
}
public static PublicKey DeriveMetadataAccount(PublicKey mint)
{
var success = AddressExtensions.TryFindProgramAddress(
new List<byte[]>() { PrefixBytes, ProgramId, mint }, ProgramId,
out var address, out _);
return success ? new PublicKey(address) : null;
}
}
}