Пример #1
0
        internal async Task <string> GetExtrinsicParametersAsync(GenericExtrinsicCall callArguments, Account account, uint tip, uint lifeTime, CancellationToken token)
        {
            Method method = GetMethod(callArguments);

            uint nonce = await System.AccountNextIndexAsync(account.Address, token);

            Era  era;
            Hash startEra;

            if (lifeTime == 0)
            {
                era      = Era.Create(0, 0);
                startEra = GenesisHash;
            }
            else
            {
                startEra = await Chain.GetFinalizedHeadAsync(token);

                Header finalizedHeader = await Chain.GetHeaderAsync(startEra, token);

                era = Era.Create(lifeTime, finalizedHeader.Number);
            }

            var uncheckedExtrinsic = RequestGenerator.SubmitExtrinsic(true, account, method, era, nonce, tip, GenesisHash, startEra);

            return(Utils.Bytes2HexString(uncheckedExtrinsic.Encode(), Utils.HexStringFormat.PREFIXED));
        }
Пример #2
0
        /// <summary>
        /// Get an unchecked extrinsic.
        /// </summary>
        /// <param name="callArguments"></param>
        /// <param name="account"></param>
        /// <param name="tip"></param>
        /// <param name="lifeTime"></param>
        /// <param name="signed"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <UnCheckedExtrinsic> GetExtrinsicParametersAsync(GenericExtrinsicCall callArguments, Account account, uint tip, uint lifeTime, bool signed, CancellationToken token)
        {
            var method = GetMethod(callArguments);

            var nonce = await System.AccountNextIndexAsync(account.Value, token);

            Era  era;
            Hash startEra;

            if (lifeTime == 0)
            {
                era      = Era.Create(0, 0);
                startEra = GenesisHash;
            }
            else
            {
                startEra = await Chain.GetFinalizedHeadAsync(token);

                var finalizedHeader = await Chain.GetHeaderAsync(startEra, token);

                era = Era.Create(lifeTime, finalizedHeader.Number.Value);
            }

            return(RequestGenerator.SubmitExtrinsic(signed, account, method, era, nonce, tip, GenesisHash, startEra, RuntimeVersion));
        }
Пример #3
0
        /// <summary>
        /// Subscribe Storage Key Async
        /// </summary>
        /// <param name="moduleName"></param>
        /// <param name="itemName"></param>
        /// <param name="parameter"></param>
        /// <param name="callback"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <string> SubscribeStorageKeyAsync(string moduleName, string itemName, string[] parameter,
                                                            Action <string, StorageChangeSet> callback, CancellationToken token)
        {
            if (_socket?.State != WebSocketState.Open)
            {
                throw new ClientNotConnectedException($"WebSocketState is not open! Currently {_socket?.State}!");
            }

            if (!MetaData.TryGetModuleByName(moduleName, out var module) ||
                !module.TryGetStorageItemByName(itemName, out var item))
            {
                throw new MissingModuleOrItemException(
                          $"Module '{moduleName}' or Item '{itemName}' missing in metadata of '{MetaData.Origin}'!");
            }

            if (item.Function?.Key1 != null && (parameter == null || parameter.Length == 0))
            {
                throw new MissingParameterException(
                          $"{moduleName}.{itemName} needs a parameter of type '{item.Function?.Key1}'!");
            }

            var parameters = RequestGenerator.GetStorage(module, item, parameter);

            var subscriptionId =
                await InvokeAsync <string>("state_subscribeStorage", new object[] { new JArray {
                                                                                        parameters
                                                                                    } }, token);

            Listener.RegisterCallBackHandler(subscriptionId, callback);
            return(subscriptionId);
        }
Пример #4
0
        /// <summary>
        /// Get Storage Keys Async
        /// </summary>
        /// <param name="moduleName"></param>
        /// <param name="itemName"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <JArray> GetStorageKeysAsync(string moduleName, string itemName, CancellationToken token)
        {
            if (_socket?.State != WebSocketState.Open)
            {
                throw new ClientNotConnectedException($"WebSocketState is not open! Currently {_socket?.State}!");
            }

            if (!MetaData.TryGetModuleByName(moduleName, out var module) ||
                !module.TryGetStorageItemByName(itemName, out var item))
            {
                throw new MissingModuleOrItemException(
                          $"Module '{moduleName}' or Item '{itemName}' missing in metadata of '{MetaData.Origin}'!");
            }

            var parameters = Utils.Bytes2HexString(RequestGenerator.GetStorageKeyBytesHash(module, item));

            return(await InvokeAsync <JArray>("state_getKeys", new object[] { parameters }, token));
        }
Пример #5
0
        /// <summary> Gets storage asynchronous. </summary>
        /// <remarks> 19.09.2020. </remarks>
        /// <exception cref="ClientNotConnectedException">  Thrown when a Client Not Connected error
        ///                                                 condition occurs. </exception>
        /// <exception cref="MissingModuleOrItemException"> Thrown when a Missing Module Or Item error
        ///                                                 condition occurs. </exception>
        /// <exception cref="MissingParameterException">    Thrown when a Missing Parameter error
        ///                                                 condition occurs. </exception>
        /// <exception cref="MissingConverterException">    Thrown when a Missing Converter error
        ///                                                 condition occurs. </exception>
        /// <param name="moduleName"> Name of the module. </param>
        /// <param name="itemName">   Name of the item. </param>
        /// <param name="parameter">  The parameter. </param>
        /// <param name="token">      A token that allows processing to be cancelled. </param>
        /// <returns> The storage. </returns>
        public async Task <object> GetStorageAsync(string moduleName, string itemName, string parameter, CancellationToken token)
        {
            if (_socket?.State != WebSocketState.Open)
            {
                throw new ClientNotConnectedException($"WebSocketState is not open! Currently {_socket?.State}!");
            }

            if (!MetaData.TryGetModuleByName(moduleName, out Module module) || !module.TryGetStorageItemByName(itemName, out Item item))
            {
                throw new MissingModuleOrItemException($"Module '{moduleName}' or Item '{itemName}' missing in metadata of '{MetaData.Origin}'!");
            }

            string method = "state_getStorage";

            if (item.Function?.Key1 != null && parameter == null)
            {
                throw new MissingParameterException($"{moduleName}.{itemName} needs a parameter of type '{item.Function?.Key1}'!");
            }

            string parameters;

            if (item.Function?.Key1 != null)
            {
                byte[] key1Bytes = Utils.KeyTypeToBytes(item.Function.Key1, parameter);
                parameters = "0x" + RequestGenerator.GetStorage(module, item, key1Bytes);
            }
            else
            {
                parameters = "0x" + RequestGenerator.GetStorage(module, item);
            }

            var resultString = await InvokeAsync <string>(method, new object[] { parameters }, token);

            string returnType = item.Function?.Value;

            if (!_typeConverters.ContainsKey(returnType))
            {
                throw new MissingConverterException($"Unknown type '{returnType}' for result '{resultString}'!");
            }

            return(_typeConverters[returnType].Create(resultString));
        }
Пример #6
0
        /// <summary>Gets the storage asynchronous.</summary>
        /// <param name="moduleName">Name of the module.</param>
        /// <param name="itemName">Name of the item.</param>
        /// <param name="key1Param">The key1 parameter.</param>
        /// <param name="key2Param">The key2 parameter.</param>
        /// <param name="token">The token.</param>
        /// <returns> Returns storage object of ITypeConverter result.</returns>
        /// <exception cref="ClientNotConnectedException">WebSocketState is not open! Currently {_socket?.State}!</exception>
        /// <exception cref="MissingModuleOrItemException">Module '{moduleName}' or Item '{itemName}' missing in metadata of '{MetaData.Origin}'!</exception>
        /// <exception cref="MissingParameterException"></exception>
        /// <exception cref="MissingConverterException">Unknown type '{returnType}' for result '{resultString}'!</exception>
        public async Task <object> GetStorageAsync(string moduleName, string itemName, string[] key1Param, string[] key2Param,
                                                   CancellationToken token)
        {
            if (_socket?.State != WebSocketState.Open)
            {
                throw new ClientNotConnectedException($"WebSocketState is not open! Currently {_socket?.State}!");
            }

            if (!MetaData.TryGetModuleByName(moduleName, out var module) ||
                !module.TryGetStorageItemByName(itemName, out var item))
            {
                throw new MissingModuleOrItemException(
                          $"Module '{moduleName}' or Item '{itemName}' missing in metadata of '{MetaData.Origin}'!");
            }

            // map
            if (item.Function?.Key1 != null && (key1Param == null || key1Param.Length == 0))
            {
                throw new MissingParameterException(
                          $"{moduleName}.{itemName} needs a parameter of type '{item.Function?.Key1}'!");
            }

            // double map
            if (item.Function?.Key2 != null && (key2Param == null || key2Param.Length == 0))
            {
                throw new MissingParameterException(
                          $"{moduleName}.{itemName} needs a parameter of type '{item.Function?.Key2}'!");
            }

            var parameters = RequestGenerator.GetStorage(module, item, key1Param, key2Param);

            var resultString = await InvokeAsync <string>("state_getStorage", new object[] { parameters }, token);

            var returnType = item.Function?.Value;

            if (!_typeConverters.ContainsKey(returnType))
            {
                throw new MissingConverterException($"Unknown type '{returnType}' for result '{resultString}'!");
            }

            return(_typeConverters[returnType].Create(resultString));
        }