Ejemplo n.º 1
0
        protected override async Task <bool> IsTriggered(ITrigger trigger, RecipeTrigger recipeTrigger,
                                                         NBXplorerNewTransactionTriggerData triggerData,
                                                         NBXplorerNewTransactionTriggerParameters parameters)
        {
            var walletService = new NBXplorerWalletService(recipeTrigger.ExternalService,
                                                           _nbXplorerPublicWalletProvider, _derivationSchemeParser, _derivationStrategyFactoryProvider,
                                                           _nbXplorerClientProvider);
            var trackedSource = await walletService.ConstructTrackedSource();

            var walletData = walletService.GetData();

            if (!triggerData.CryptoCode.Equals(walletData.CryptoCode,
                                               StringComparison.InvariantCultureIgnoreCase))
            {
                return(false);
            }

            if (triggerData.Event != null && triggerData.Event.TrackedSource == trackedSource)
            {
                return(await UpdateTxToRecipeTrigger(triggerData.Event.TransactionData, recipeTrigger,
                                                     parameters));
            }

            return(false);
        }
        protected override async Task <TypedActionHandlerResult <PSBT> > Execute(Dictionary <string, object> data, RecipeAction recipeAction,
                                                                                 NBXplorerSignPSBTData actionData)
        {
            var externalService = await recipeAction.GetExternalService();

            var walletService = new NBXplorerWalletService(externalService, _nbXplorerPublicWalletProvider,
                                                           _derivationSchemeParser, _derivationStrategyFactoryProvider, _nbXplorerClientProvider);
            var walletData = walletService.GetData();
            var wallet     = await walletService.ConstructClient();

            var psbt     = PSBT.Parse(actionData.PSBT);
            var tx       = psbt.ExtractTX();
            var tBuilder = await wallet.CreateTransactionBuilder();

            tBuilder = tBuilder.ContinueToBuild(tx);
            foreach (var privateKeyDetails in walletData.PrivateKeys)
            {
                await wallet.SignTransaction(tBuilder, privateKeyDetails);
            }

            psbt = PSBT.FromTransaction(
                tBuilder.BuildTransaction(true));

            return(new NBXplorerActionHandlerResult <PSBT>(_nbXplorerClientProvider.GetClient(walletData.CryptoCode))
            {
                Executed = true,
                TypedData = psbt,
                Result = $"signed psbt: {psbt}"
            });
        }
Ejemplo n.º 3
0
        protected override async Task <TypedActionHandlerResult <BroadcastResult> > Execute(
            Dictionary <string, object> data, RecipeAction recipeAction,
            SendTransactionData actionData)
        {
            var externalService = await recipeAction.GetExternalService();

            var walletService = new NBXplorerWalletService(externalService, _nbXplorerPublicWalletProvider,
                                                           _derivationSchemeParser, _derivationStrategyFactoryProvider, _nbXplorerClientProvider);

            var wallet = await walletService.ConstructClient();

            var walletData     = walletService.GetData();
            var explorerClient = _nbXplorerClientProvider.GetClient(walletData.CryptoCode);
            var tx             = await wallet.BuildTransaction(actionData.Outputs.Select(output =>
                                                                                         (Money.Parse(InterpolateString(output.Amount, data)),
                                                                                          (IDestination)BitcoinAddress.Create(InterpolateString(output.DestinationAddress, data),
                                                                                                                              explorerClient.Network.NBitcoinNetwork), output.SubtractFeesFromOutput)),
                                                               walletData.PrivateKeys,
                                                               actionData.FeeSatoshiPerByte != null
                                                               ?new FeeRate(Money.Satoshis(actionData.FeeSatoshiPerByte.Value), 1)
                                                               : null, actionData.Fee.HasValue?new Money(actionData.Fee.Value, MoneyUnit.BTC) : null);

            var result = await wallet.BroadcastTransaction(tx);

            return(new NBXplorerActionHandlerResult <BroadcastResult>(
                       _nbXplorerClientProvider.GetClient(walletData.CryptoCode))
            {
                Executed = result.Success,
                TypedData = result,
                Result =
                    $"Tx broadcasted, {(result.Success ? "Successful" : "Unsuccessful")}, {result.RPCMessage}, {tx}"
            });
        }
        protected override async Task <TypedActionHandlerResult <PSBT> > Execute(
            Dictionary <string, object> data, RecipeAction recipeAction,
            NBXplorerCreatePSBTData actionData)
        {
            var externalService = await recipeAction.GetExternalService();

            var walletService = new NBXplorerWalletService(externalService, _nbXplorerPublicWalletProvider,
                                                           _derivationSchemeParser, _derivationStrategyFactoryProvider, _nbXplorerClientProvider);

            var wallet = await walletService.ConstructClient();

            var walletData     = walletService.GetData();
            var explorerClient = _nbXplorerClientProvider.GetClient(walletData.CryptoCode);
            var tx             = await wallet.BuildTransaction(actionData.Outputs.Select(output =>
                                                                                         (Money.Parse(InterpolateString(output.Amount, data)),
                                                                                          (IDestination)BitcoinAddress.Create(InterpolateString(output.DestinationAddress, data),
                                                                                                                              explorerClient.Network.NBitcoinNetwork), output.SubtractFeesFromOutput)),
                                                               walletData.PrivateKeys);

            var psbt = PSBT.FromTransaction(tx);

            return(new NBXplorerActionHandlerResult <PSBT>(
                       _nbXplorerClientProvider.GetClient(walletData.CryptoCode))
            {
                Executed = true,
                TypedData = psbt,
                Result = $"PSBT created {psbt}"
            });
        }
        protected override async Task <bool> IsTriggered(ITrigger trigger, RecipeTrigger recipeTrigger,
                                                         NBXplorerBalanceTriggerData triggerData,
                                                         NBXplorerBalanceTriggerParameters parameters)
        {
            var walletService = new NBXplorerWalletService(recipeTrigger.ExternalService,
                                                           _nbXplorerPublicWalletProvider,
                                                           _derivationSchemeParser,
                                                           _nbXplorerClientProvider);

            var walletData = walletService.GetData();

            if (!triggerData.CryptoCode.Equals(walletData.CryptoCode,
                                               StringComparison.InvariantCultureIgnoreCase))
            {
                return(false);
            }

            return(await walletService.ConstructTrackedSource() == triggerData.TrackedSource && IsBalanceWithinCriteria(triggerData, parameters));
        }
        protected override async Task <TypedActionHandlerResult <Money> > Execute(Dictionary <string, object> data, RecipeAction recipeAction,
                                                                                  NBXplorerGetBalanceData actionData)
        {
            var externalService = await recipeAction.GetExternalService();

            var walletService = new NBXplorerWalletService(externalService, _nbXplorerPublicWalletProvider,
                                                           _derivationSchemeParser, _nbXplorerClientProvider);
            var walletData = walletService.GetData();
            var wallet     = await walletService.ConstructClient();

            var result = await
                         wallet.GetBalance();

            return(new NBXplorerActionHandlerResult <Money>(_nbXplorerClientProvider.GetClient(walletData.CryptoCode))
            {
                Executed = true,
                TypedData = result,
                Result = $"Balance: {result}"
            });
        }
Ejemplo n.º 7
0
        protected override async Task <TypedActionHandlerResult <BitcoinAddress> > Execute(Dictionary <string, object> data, RecipeAction recipeAction,
                                                                                           GenerateNextAddressData actionData)
        {
            var externalService = await recipeAction.GetExternalService();

            var walletService = new NBXplorerWalletService(externalService, _nbXplorerPublicWalletProvider,
                                                           _derivationSchemeParser, _derivationStrategyFactoryProvider, _nbXplorerClientProvider);
            var walletData = walletService.GetData();
            var wallet     = await walletService.ConstructClient();

            var result = await
                         wallet.GetNextAddress();

            return(new NBXplorerActionHandlerResult <BitcoinAddress>(_nbXplorerClientProvider.GetClient(walletData.CryptoCode))
            {
                Executed = true,
                TypedData = result,
                Result = $"Next address: {result}"
            });
        }