Velocity.cs 16 KB

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