Exemplo n.º 1
0
        public async Task <SendBulsData> PrepareSendBuls(string fromPubkey, string toPubkey, double amountBuls, bool disableConvertor = false)
        {
            var request = PrepareRequest(apiVersion + "/prepare_send_buls", Method.POST);

            var amount = amountBuls;

            if (!disableConvertor)
            {
                amount = StellarConverter.ConvertBULToStroops(amountBuls);
            }

            if (StellarConverter.IsValidBUL(amount) == false)
            {
                throw new ServiceException(400, "You can't specify more then 7 fractional digits");
            }

            var myBalance = await App.Locator.BridgeServiceClient.Balance(App.Locator.Profile.Pubkey);

            if (myBalance == null || myBalance.Account.BalanceBUL < amount)
            {
                throw new ServiceException(420, AppResources.InsufficientBULs);
            }

            request.AddParameter("from_pubkey", fromPubkey);
            request.AddParameter("to_pubkey", toPubkey);
            request.AddParameter("amount_buls", amount);

            return(await SendRequest <SendBulsData>(request));
        }
Exemplo n.º 2
0
        public async Task <PackageData> CreatePackage(string escrowPubkey, string recipientPubkey, string launcherPhone, string recipientPhone, long deadlineTimestamp, double paymentBuls, double collateralBuls,
                                                      string description, string fromAddress, string toAddress, string fromLocation, string toLocation, string eventLocation, byte[] packagePhoto, SignHandler customSign)
        {
            var request = PrepareRequest(apiVersion + "/create_package", Method.POST);

            var payment = StellarConverter.ConvertBULToStroops(paymentBuls);

            if (StellarConverter.IsValidBUL(payment) == false)
            {
                throw new ServiceException(400, "You can't specify more then 7 fractional digits");
            }

            var collateral = StellarConverter.ConvertBULToStroops(collateralBuls);

            if (StellarConverter.IsValidBUL(collateral) == false)
            {
                throw new ServiceException(400, "You can't specify more then 7 fractional digits");
            }

            request.AddParameter("escrow_pubkey", escrowPubkey);
            request.AddParameter("recipient_pubkey", recipientPubkey);
            request.AddParameter("launcher_phone_number", launcherPhone);
            request.AddParameter("recipient_phone_number", recipientPhone);
            request.AddParameter("deadline_timestamp", deadlineTimestamp);
            request.AddParameter("payment_buls", payment);
            request.AddParameter("collateral_buls", collateral);
            request.AddParameter("description", description);
            request.AddParameter("to_address", toAddress);
            request.AddParameter("from_address", fromAddress);
            request.AddParameter("from_location", fromLocation);
            request.AddParameter("to_location", toLocation);
            request.AddParameter("event_location", eventLocation);
            if (packagePhoto != null)
            {
                request.AddFile("photo", packagePhoto, "photo.jpg");
            }

            return(await SendRequest <PackageData>(request, customSign : customSign));
        }
Exemplo n.º 3
0
        public static async Task <StellarOperationResult> CreatePackage(KeyPair escrowKP, string recipientPubkey, string launcherPhone, string recipientPhone, string description,
                                                                        string fromAddress, string toAddress, long deadlineTimestamp, double paymentBuls, double collateralBuls,
                                                                        string eventLocation, string fromLocation, string toLocation, byte[] packagePhoto, LaunchPackageEventHandler eventHandler)
        {
            if (toLocation.Length > 24)
            {
                toLocation = toLocation.Substring(0, 24);
            }

            if (fromLocation.Length > 24)
            {
                fromLocation = fromLocation.Substring(0, 24);
            }

            double steps       = 3;
            double currentStep = 1;

            var payment = StellarConverter.ConvertBULToStroops(paymentBuls);

            if (StellarConverter.IsValidBUL(payment) == false)
            {
                throw new ServiceException(400, AppResources.FractionalDigitsError);
            }

            var collateral = StellarConverter.ConvertBULToStroops(collateralBuls);

            if (StellarConverter.IsValidBUL(collateral) == false)
            {
                throw new ServiceException(400, AppResources.FractionalDigitsError);
            }

            //Check launcher's balance
            eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep1, currentStep / steps));
            currentStep++;

            var launcherBalance = await App.Locator.BridgeServiceClient.Balance(App.Locator.Profile.Pubkey);

            if (launcherBalance == null || launcherBalance.Account.BalanceBUL < payment)
            {
                return(StellarOperationResult.LowBULsLauncher);
            }

            eventHandler("", new LaunchPackageEventArgs(AppResources.CreatePackage, currentStep / steps));
            currentStep++;

            var createResult = await App.Locator.RouteServiceClient.CreatePackage(escrowKP.Address, recipientPubkey, launcherPhone, recipientPhone, deadlineTimestamp, paymentBuls, collateralBuls,
                                                                                  description, fromAddress, toAddress, fromLocation, toLocation, eventLocation, packagePhoto, (d) => {
                return(App.Locator.Profile.SignData(d, escrowKP));
            });

            if (createResult != null)
            {
                var packedId = createResult.Package.PaketId;

                App.Locator.Profile.AddPackageKeyPair(packedId, escrowKP.SecretSeed);

                return(StellarOperationResult.Success);
            }


            return(StellarOperationResult.FailedLaunchPackage);
        }
Exemplo n.º 4
0
        public static async Task <StellarOperationResult> LaunchPackage(string paketID, string recipientPubkey, long deadlineTimestamp, string courierPubkey, double paymentBuls, double collateralBuls, LaunchPackageEventHandler eventHandler)
        {
            KeyPair escrowKP = null;

            var seed = App.Locator.Profile.PackageKeyPair(paketID);

            if (seed != null)
            {
                escrowKP = KeyPair.FromSecretSeed(seed);
            }
            else
            {
                escrowKP = KeyPair.Random();
            }

            double steps       = 12;
            double currentStep = 1;

            var payment = paymentBuls;

            if (StellarConverter.IsValidBUL(payment) == false)
            {
                throw new ServiceException(400, AppResources.FractionalDigitsError);
            }

            var collateral = collateralBuls;

            if (StellarConverter.IsValidBUL(collateral) == false)
            {
                throw new ServiceException(400, AppResources.FractionalDigitsError);
            }

            //Check launcher's balance
            eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep1, currentStep / steps));

            currentStep++;

            var launcherBalance = await App.Locator.BridgeServiceClient.Balance(App.Locator.Profile.Pubkey);

            if (launcherBalance == null || launcherBalance.Account.BalanceBUL < payment)
            {
                return(StellarOperationResult.LowBULsLauncher);
            }

            //Check courier's balance
            eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep15, currentStep / steps));

            currentStep++;

            var courierBalance = await App.Locator.BridgeServiceClient.Balance(courierPubkey);

            if (courierBalance == null || courierBalance.Account.BalanceBUL < collateral)
            {
                return(StellarOperationResult.LowBULsCourier);
            }

            //Create escrow account
            eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep3, currentStep / steps));

            currentStep++;

            string location = await App.Locator.LocationHelper.GetStringLocation(true);


            var accountResult = await App.Locator.BridgeServiceClient.PrepareCrateAccount(App.Locator.Profile.Pubkey, escrowKP.Address, 4);            //Change to 20000200

            if (accountResult != null)
            {
                //Sign escrow account transaction
                eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep4, currentStep / steps));

                currentStep++;

                var signedCreate = await SignTransaction(App.Locator.Profile.KeyPair, accountResult.CreateTransaction);

                if (signedCreate != null)
                {
                    //Submit escrow account

                    eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep5, currentStep / steps));

                    currentStep++;

                    var submitCreate = await App.Locator.BridgeServiceClient.SubmitTransaction(signedCreate);

                    if (submitCreate != null)
                    {
                        //Add token trust to escrow account

                        eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep6, currentStep / steps));

                        currentStep++;

                        var trustResult = await AddTrustToken(escrowKP);

                        if (trustResult)
                        {
                            //Prepare escrow

                            eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep7, currentStep / steps));

                            currentStep++;

                            var launchResult = await App.Locator.BridgeServiceClient.PrepareEscrow(escrowKP.Address, App.Locator.Profile.Pubkey,
                                                                                                   recipientPubkey, deadlineTimestamp, courierPubkey,
                                                                                                   paymentBuls, collateralBuls, (d) => {
                                return(App.Locator.Profile.SignData(d, escrowKP));
                            });

                            if (launchResult != null)
                            {
                                var createResult = await App.Locator.RouteServiceClient.FinalizePackage(location, paketID, launchResult.LaunchPackageDetails.SetOptionsTransaction, launchResult.LaunchPackageDetails.RefundTransaction,
                                                                                                        launchResult.LaunchPackageDetails.MergeTransaction, launchResult.LaunchPackageDetails.PaymentTransaction);

                                if (createResult != null)
                                {
                                    //Sign options transaction
                                    eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep8, currentStep / steps));

                                    currentStep++;

                                    var signedOptions = await SignTransaction(escrowKP, launchResult.LaunchPackageDetails.SetOptionsTransaction);

                                    if (signedOptions != null)
                                    {
                                        //Submit options transaction

                                        eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep9, currentStep / steps));

                                        currentStep++;

                                        var submitOptions = await App.Locator.BridgeServiceClient.SubmitTransaction(signedOptions);

                                        if (submitOptions != null)
                                        {
                                            //Prepare send payment
                                            eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep10, currentStep / steps));

                                            currentStep++;

                                            // var paymentStroops = StellarConverter.ConvertBULToStroops(payment);

                                            var paymentTrans = await App.Locator.BridgeServiceClient.PrepareSendBuls(App.Locator.Profile.Pubkey, escrowKP.Address, paymentBuls, true);

                                            if (paymentTrans != null)
                                            {
                                                //Sign payment transaction
                                                eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep11, currentStep / steps));

                                                currentStep++;

                                                var signed = await SignTransaction(App.Locator.Profile.KeyPair, paymentTrans.Transaction);

                                                if (signed != null)
                                                {
                                                    //Submit payment transaction
                                                    eventHandler("", new LaunchPackageEventArgs(AppResources.LaunchPackageStep12, currentStep / steps));

                                                    currentStep++;

                                                    var paymentResult = await App.Locator.BridgeServiceClient.SubmitTransaction(signed);

                                                    if (paymentResult != null)
                                                    {
                                                        //var newLauncherBalance = await App.Locator.ServiceClient.Balance(App.Locator.Profile.Pubkey);//TODO remove balance check
                                                        //if (newLauncherBalance.BalanceBUL == launcherBalance.BalanceBUL - paymentBuls) {
                                                        App.Locator.Profile.AddTransaction(escrowKP.Address, launchResult);                                                        //save payment transaction data
                                                        return(StellarOperationResult.Success);
                                                        //}

                                                        //return StellarOperationResult.IncositentBalance;
                                                    }

                                                    return(StellarOperationResult.FailSendBuls);
                                                }

                                                return(StellarOperationResult.FailSendBuls);
                                            }

                                            return(StellarOperationResult.FailSendBuls);
                                        }

                                        return(StellarOperationResult.FaileSubmitOptions);
                                    }

                                    return(StellarOperationResult.FaileSubmitOptions);
                                }
                            }

                            return(StellarOperationResult.FailedLaunchPackage);
                        }

                        return(StellarOperationResult.FailAddTrust);
                    }

                    return(StellarOperationResult.FailSubmitCreateAccount);
                }

                return(StellarOperationResult.FailSubmitCreateAccount);
            }

            return(StellarOperationResult.FailCreateAccount);


            //As the escrow account, call /prepare_escrow. Check balances

            //As the escrow account, sign and submit the set_options_transaction.

            //Call /bul_account on the escrow account and verify that the signers are properly set.

            //Make note of the BUL balances of the launcher by calling /bul_account.

            //Transfer the payment from the launcher to the escrow

            //Make note of the BUL balances of the launcher by calling /bul_account. It should be the same as before minus the payment
        }