Velocity.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using System.Threading.Tasks;
  6. using Solana.Unity;
  7. using Solana.Unity.Programs.Abstract;
  8. using Solana.Unity.Programs.Utilities;
  9. using Solana.Unity.Rpc;
  10. using Solana.Unity.Rpc.Builders;
  11. using Solana.Unity.Rpc.Core.Http;
  12. using Solana.Unity.Rpc.Core.Sockets;
  13. using Solana.Unity.Rpc.Types;
  14. using Solana.Unity.Wallet;
  15. using Velocity;
  16. using Velocity.Program;
  17. using Velocity.Errors;
  18. using Velocity.Accounts;
  19. using Velocity.Types;
  20. namespace Velocity
  21. {
  22. namespace Accounts
  23. {
  24. public partial class Entity
  25. {
  26. public static ulong ACCOUNT_DISCRIMINATOR => 1751670451238706478UL;
  27. public static ReadOnlySpan<byte> ACCOUNT_DISCRIMINATOR_BYTES => new byte[]{46, 157, 161, 161, 254, 46, 79, 24};
  28. public static string ACCOUNT_DISCRIMINATOR_B58 => "8oEQa6zH67R";
  29. public ulong Id { get; set; }
  30. public static Entity Deserialize(ReadOnlySpan<byte> _data)
  31. {
  32. int offset = 0;
  33. ulong accountHashValue = _data.GetU64(offset);
  34. offset += 8;
  35. if (accountHashValue != ACCOUNT_DISCRIMINATOR)
  36. {
  37. return null;
  38. }
  39. Entity result = new Entity();
  40. result.Id = _data.GetU64(offset);
  41. offset += 8;
  42. return result;
  43. }
  44. }
  45. public partial class SessionToken
  46. {
  47. public static ulong ACCOUNT_DISCRIMINATOR => 1081168673100727529UL;
  48. public static ReadOnlySpan<byte> ACCOUNT_DISCRIMINATOR_BYTES => new byte[]{233, 4, 115, 14, 46, 21, 1, 15};
  49. public static string ACCOUNT_DISCRIMINATOR_B58 => "fyZWTdUu1pS";
  50. public PublicKey Authority { get; set; }
  51. public PublicKey TargetProgram { get; set; }
  52. public PublicKey SessionSigner { get; set; }
  53. public long ValidUntil { get; set; }
  54. public static SessionToken Deserialize(ReadOnlySpan<byte> _data)
  55. {
  56. int offset = 0;
  57. ulong accountHashValue = _data.GetU64(offset);
  58. offset += 8;
  59. if (accountHashValue != ACCOUNT_DISCRIMINATOR)
  60. {
  61. return null;
  62. }
  63. SessionToken result = new SessionToken();
  64. result.Authority = _data.GetPubKey(offset);
  65. offset += 32;
  66. result.TargetProgram = _data.GetPubKey(offset);
  67. offset += 32;
  68. result.SessionSigner = _data.GetPubKey(offset);
  69. offset += 32;
  70. result.ValidUntil = _data.GetS64(offset);
  71. offset += 8;
  72. return result;
  73. }
  74. }
  75. public partial class Velocity
  76. {
  77. public static ulong ACCOUNT_DISCRIMINATOR => 17932774833452681865UL;
  78. public static ReadOnlySpan<byte> ACCOUNT_DISCRIMINATOR_BYTES => new byte[]{137, 10, 3, 249, 178, 3, 222, 248};
  79. public static string ACCOUNT_DISCRIMINATOR_B58 => "PvTAh7RLish";
  80. public long X { get; set; }
  81. public long Y { get; set; }
  82. public long Z { get; set; }
  83. public long LastApplied { get; set; }
  84. public string Description { get; set; }
  85. public BoltMetadata BoltMetadata { get; set; }
  86. public static Velocity Deserialize(ReadOnlySpan<byte> _data)
  87. {
  88. int offset = 0;
  89. ulong accountHashValue = _data.GetU64(offset);
  90. offset += 8;
  91. if (accountHashValue != ACCOUNT_DISCRIMINATOR)
  92. {
  93. return null;
  94. }
  95. Velocity result = new Velocity();
  96. result.X = _data.GetS64(offset);
  97. offset += 8;
  98. result.Y = _data.GetS64(offset);
  99. offset += 8;
  100. result.Z = _data.GetS64(offset);
  101. offset += 8;
  102. result.LastApplied = _data.GetS64(offset);
  103. offset += 8;
  104. offset += _data.GetBorshString(offset, out var resultDescription);
  105. result.Description = resultDescription;
  106. offset += BoltMetadata.Deserialize(_data, offset, out var resultBoltMetadata);
  107. result.BoltMetadata = resultBoltMetadata;
  108. return result;
  109. }
  110. }
  111. }
  112. namespace Errors
  113. {
  114. public enum VelocityErrorKind : uint
  115. {
  116. }
  117. }
  118. namespace Types
  119. {
  120. public partial class BoltMetadata
  121. {
  122. public PublicKey Authority { get; set; }
  123. public int Serialize(byte[] _data, int initialOffset)
  124. {
  125. int offset = initialOffset;
  126. _data.WritePubKey(Authority, offset);
  127. offset += 32;
  128. return offset - initialOffset;
  129. }
  130. public static int Deserialize(ReadOnlySpan<byte> _data, int initialOffset, out BoltMetadata result)
  131. {
  132. int offset = initialOffset;
  133. result = new BoltMetadata();
  134. result.Authority = _data.GetPubKey(offset);
  135. offset += 32;
  136. return offset - initialOffset;
  137. }
  138. }
  139. }
  140. public partial class VelocityClient : TransactionalBaseClient<VelocityErrorKind>
  141. {
  142. public VelocityClient(IRpcClient rpcClient, IStreamingRpcClient streamingRpcClient, PublicKey programId = null) : base(rpcClient, streamingRpcClient, programId ?? new PublicKey(VelocityProgram.ID))
  143. {
  144. }
  145. public async Task<Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<Entity>>> GetEntitysAsync(string programAddress = VelocityProgram.ID, Commitment commitment = Commitment.Confirmed)
  146. {
  147. var list = new List<Solana.Unity.Rpc.Models.MemCmp>{new Solana.Unity.Rpc.Models.MemCmp{Bytes = Entity.ACCOUNT_DISCRIMINATOR_B58, Offset = 0}};
  148. var res = await RpcClient.GetProgramAccountsAsync(programAddress, commitment, memCmpList: list);
  149. if (!res.WasSuccessful || !(res.Result?.Count > 0))
  150. return new Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<Entity>>(res);
  151. List<Entity> resultingAccounts = new List<Entity>(res.Result.Count);
  152. resultingAccounts.AddRange(res.Result.Select(result => Entity.Deserialize(Convert.FromBase64String(result.Account.Data[0]))));
  153. return new Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<Entity>>(res, resultingAccounts);
  154. }
  155. public async Task<Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<SessionToken>>> GetSessionTokensAsync(string programAddress = VelocityProgram.ID, Commitment commitment = Commitment.Confirmed)
  156. {
  157. var list = new List<Solana.Unity.Rpc.Models.MemCmp>{new Solana.Unity.Rpc.Models.MemCmp{Bytes = SessionToken.ACCOUNT_DISCRIMINATOR_B58, Offset = 0}};
  158. var res = await RpcClient.GetProgramAccountsAsync(programAddress, commitment, memCmpList: list);
  159. if (!res.WasSuccessful || !(res.Result?.Count > 0))
  160. return new Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<SessionToken>>(res);
  161. List<SessionToken> resultingAccounts = new List<SessionToken>(res.Result.Count);
  162. resultingAccounts.AddRange(res.Result.Select(result => SessionToken.Deserialize(Convert.FromBase64String(result.Account.Data[0]))));
  163. return new Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<SessionToken>>(res, resultingAccounts);
  164. }
  165. public async Task<Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<Velocity.Accounts.Velocity>>> GetVelocitysAsync(string programAddress = VelocityProgram.ID, Commitment commitment = Commitment.Confirmed)
  166. {
  167. var list = new List<Solana.Unity.Rpc.Models.MemCmp>{new Solana.Unity.Rpc.Models.MemCmp{Bytes = Velocity.Accounts.Velocity.ACCOUNT_DISCRIMINATOR_B58, Offset = 0}};
  168. var res = await RpcClient.GetProgramAccountsAsync(programAddress, commitment, memCmpList: list);
  169. if (!res.WasSuccessful || !(res.Result?.Count > 0))
  170. return new Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<Velocity.Accounts.Velocity>>(res);
  171. List<Velocity.Accounts.Velocity> resultingAccounts = new List<Velocity.Accounts.Velocity>(res.Result.Count);
  172. resultingAccounts.AddRange(res.Result.Select(result => Velocity.Accounts.Velocity.Deserialize(Convert.FromBase64String(result.Account.Data[0]))));
  173. return new Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<Velocity.Accounts.Velocity>>(res, resultingAccounts);
  174. }
  175. public async Task<Solana.Unity.Programs.Models.AccountResultWrapper<Entity>> GetEntityAsync(string accountAddress, Commitment commitment = Commitment.Finalized)
  176. {
  177. var res = await RpcClient.GetAccountInfoAsync(accountAddress, commitment);
  178. if (!res.WasSuccessful)
  179. return new Solana.Unity.Programs.Models.AccountResultWrapper<Entity>(res);
  180. var resultingAccount = Entity.Deserialize(Convert.FromBase64String(res.Result.Value.Data[0]));
  181. return new Solana.Unity.Programs.Models.AccountResultWrapper<Entity>(res, resultingAccount);
  182. }
  183. public async Task<Solana.Unity.Programs.Models.AccountResultWrapper<SessionToken>> GetSessionTokenAsync(string accountAddress, Commitment commitment = Commitment.Finalized)
  184. {
  185. var res = await RpcClient.GetAccountInfoAsync(accountAddress, commitment);
  186. if (!res.WasSuccessful)
  187. return new Solana.Unity.Programs.Models.AccountResultWrapper<SessionToken>(res);
  188. var resultingAccount = SessionToken.Deserialize(Convert.FromBase64String(res.Result.Value.Data[0]));
  189. return new Solana.Unity.Programs.Models.AccountResultWrapper<SessionToken>(res, resultingAccount);
  190. }
  191. public async Task<Solana.Unity.Programs.Models.AccountResultWrapper<Velocity.Accounts.Velocity>> GetVelocityAsync(string accountAddress, Commitment commitment = Commitment.Finalized)
  192. {
  193. var res = await RpcClient.GetAccountInfoAsync(accountAddress, commitment);
  194. if (!res.WasSuccessful)
  195. return new Solana.Unity.Programs.Models.AccountResultWrapper<Velocity.Accounts.Velocity>(res);
  196. var resultingAccount = Velocity.Accounts.Velocity.Deserialize(Convert.FromBase64String(res.Result.Value.Data[0]));
  197. return new Solana.Unity.Programs.Models.AccountResultWrapper<Velocity.Accounts.Velocity>(res, resultingAccount);
  198. }
  199. public async Task<SubscriptionState> SubscribeEntityAsync(string accountAddress, Action<SubscriptionState, Solana.Unity.Rpc.Messages.ResponseValue<Solana.Unity.Rpc.Models.AccountInfo>, Entity> callback, Commitment commitment = Commitment.Finalized)
  200. {
  201. SubscriptionState res = await StreamingRpcClient.SubscribeAccountInfoAsync(accountAddress, (s, e) =>
  202. {
  203. Entity parsingResult = null;
  204. if (e.Value?.Data?.Count > 0)
  205. parsingResult = Entity.Deserialize(Convert.FromBase64String(e.Value.Data[0]));
  206. callback(s, e, parsingResult);
  207. }, commitment);
  208. return res;
  209. }
  210. public async Task<SubscriptionState> SubscribeSessionTokenAsync(string accountAddress, Action<SubscriptionState, Solana.Unity.Rpc.Messages.ResponseValue<Solana.Unity.Rpc.Models.AccountInfo>, SessionToken> callback, Commitment commitment = Commitment.Finalized)
  211. {
  212. SubscriptionState res = await StreamingRpcClient.SubscribeAccountInfoAsync(accountAddress, (s, e) =>
  213. {
  214. SessionToken parsingResult = null;
  215. if (e.Value?.Data?.Count > 0)
  216. parsingResult = SessionToken.Deserialize(Convert.FromBase64String(e.Value.Data[0]));
  217. callback(s, e, parsingResult);
  218. }, commitment);
  219. return res;
  220. }
  221. public async Task<SubscriptionState> SubscribeVelocityAsync(string accountAddress, Action<SubscriptionState, Solana.Unity.Rpc.Messages.ResponseValue<Solana.Unity.Rpc.Models.AccountInfo>, Velocity.Accounts.Velocity> callback, Commitment commitment = Commitment.Finalized)
  222. {
  223. SubscriptionState res = await StreamingRpcClient.SubscribeAccountInfoAsync(accountAddress, (s, e) =>
  224. {
  225. Velocity.Accounts.Velocity parsingResult = null;
  226. if (e.Value?.Data?.Count > 0)
  227. parsingResult = Velocity.Accounts.Velocity.Deserialize(Convert.FromBase64String(e.Value.Data[0]));
  228. callback(s, e, parsingResult);
  229. }, commitment);
  230. return res;
  231. }
  232. protected override Dictionary<uint, ProgramError<VelocityErrorKind>> BuildErrorsDictionary()
  233. {
  234. return new Dictionary<uint, ProgramError<VelocityErrorKind>>{};
  235. }
  236. }
  237. namespace Program
  238. {
  239. public class InitializeAccounts
  240. {
  241. public PublicKey Payer { get; set; }
  242. public PublicKey Data { get; set; }
  243. public PublicKey Entity { get; set; }
  244. public PublicKey Authority { get; set; }
  245. public PublicKey InstructionSysvarAccount { get; set; } = new PublicKey("Sysvar1nstructions1111111111111111111111111");
  246. public PublicKey SystemProgram { get; set; } = new PublicKey("11111111111111111111111111111111");
  247. }
  248. public class UpdateAccounts
  249. {
  250. public PublicKey BoltComponent { get; set; }
  251. public PublicKey Authority { get; set; }
  252. public PublicKey InstructionSysvarAccount { get; set; } = new PublicKey("Sysvar1nstructions1111111111111111111111111");
  253. public PublicKey SessionToken { get; set; }
  254. }
  255. public static class VelocityProgram
  256. {
  257. public const string ID = "CbHEFbSQdRN4Wnoby9r16umnJ1zWbULBHg4yqzGQonU1";
  258. public static Solana.Unity.Rpc.Models.TransactionInstruction Initialize(InitializeAccounts accounts, PublicKey programId = null)
  259. {
  260. programId ??= new(ID);
  261. List<Solana.Unity.Rpc.Models.AccountMeta> keys = new()
  262. {Solana.Unity.Rpc.Models.AccountMeta.Writable(accounts.Payer, true), Solana.Unity.Rpc.Models.AccountMeta.Writable(accounts.Data, false), Solana.Unity.Rpc.Models.AccountMeta.ReadOnly(accounts.Entity, false), Solana.Unity.Rpc.Models.AccountMeta.ReadOnly(accounts.Authority, false), Solana.Unity.Rpc.Models.AccountMeta.ReadOnly(accounts.InstructionSysvarAccount, false), Solana.Unity.Rpc.Models.AccountMeta.ReadOnly(accounts.SystemProgram, false)};
  263. byte[] _data = new byte[1200];
  264. int offset = 0;
  265. _data.WriteU64(17121445590508351407UL, offset);
  266. offset += 8;
  267. byte[] resultData = new byte[offset];
  268. Array.Copy(_data, resultData, offset);
  269. return new Solana.Unity.Rpc.Models.TransactionInstruction{Keys = keys, ProgramId = programId.KeyBytes, Data = resultData};
  270. }
  271. public static Solana.Unity.Rpc.Models.TransactionInstruction Update(UpdateAccounts accounts, byte[] data, PublicKey programId = null)
  272. {
  273. programId ??= new(ID);
  274. List<Solana.Unity.Rpc.Models.AccountMeta> keys = new()
  275. {Solana.Unity.Rpc.Models.AccountMeta.Writable(accounts.BoltComponent, false), Solana.Unity.Rpc.Models.AccountMeta.ReadOnly(accounts.Authority, true), Solana.Unity.Rpc.Models.AccountMeta.ReadOnly(accounts.InstructionSysvarAccount, false), Solana.Unity.Rpc.Models.AccountMeta.ReadOnly(accounts.SessionToken == null ? programId : accounts.SessionToken, false)};
  276. byte[] _data = new byte[1200];
  277. int offset = 0;
  278. _data.WriteU64(9222597562720635099UL, offset);
  279. offset += 8;
  280. _data.WriteS32(data.Length, offset);
  281. offset += 4;
  282. _data.WriteSpan(data, offset);
  283. offset += data.Length;
  284. byte[] resultData = new byte[offset];
  285. Array.Copy(_data, resultData, offset);
  286. return new Solana.Unity.Rpc.Models.TransactionInstruction{Keys = keys, ProgramId = programId.KeyBytes, Data = resultData};
  287. }
  288. }
  289. }
  290. }