示例#1
0
        public async Task CreateAsync(Guid customerId, Guid spendRuleId, Guid voucherId, Money18 amount)
        {
            var transfer = new Transfer
            {
                Id          = Guid.NewGuid(),
                CustomerId  = customerId,
                SpendRuleId = spendRuleId,
                VoucherId   = voucherId,
                Amount      = amount,
                Status      = TransferStatus.Pending,
                Created     = DateTime.UtcNow
            };

            await _transfersRepository.InsertAsync(transfer);

            var additionalData = _encoderService.EncodeTransferData(transfer.SpendRuleId, transfer.Id);

            var transferResponse = await _privateBlockchainFacadeClient.GenericTransfersApi.GenericTransferAsync(
                new GenericTransferRequestModel
            {
                Amount           = amount,
                AdditionalData   = additionalData,
                RecipientAddress = _settingsService.GetContractAddress(),
                SenderCustomerId = customerId.ToString(),
                TransferId       = transfer.Id.ToString()
            });

            if (transferResponse.Error != TransferError.None)
            {
                _log.Error(message: "An error occurred while creating generic transfer.",
                           context:
                           $"transferId: {transfer.Id}; customerId: {transfer.CustomerId}; spendRuleId: {transfer.SpendRuleId}; error: {transferResponse.Error}");

                switch (transferResponse.Error)
                {
                case TransferError.SenderWalletMissing:
                    throw new CustomerWalletDoesNotExistException();

                case TransferError.NotEnoughFunds:
                    throw new NoEnoughTokensException();
                }
            }

            await _operationsService.AddAsync(
                Guid.Parse(transferResponse.OperationId),
                transfer.Id,
                OperationType.Transfer);

            _log.Info("Transfer created.", context:
                      $"transferId: {transfer.Id}; customerId: {transfer.CustomerId}; spendRuleId: {transfer.SpendRuleId}; operationId: {transferResponse.OperationId}");
        }