Esempio n. 1
0
        /// <inheritdoc />
        public async Task <OneOf <MaterialManufacturingResponseModel, EntityNotFound> > CreateMaterialManufacturingAsync(
            CreateMaterialManufacturingCommand command,
            CancellationToken cancellationToken)
        {
            var smartContract =
                await _blockchainRepository.GetSmartContractByTypeAsync(ContractType, cancellationToken);

            if (smartContract == null)
            {
                return(new EntityNotFound {
                    Message = $"Smart-contract with type: {ContractType} not found"
                });
            }

            var deployedContractAddress = await _ethereumService.DeployAsync(command, smartContract);

            var deployedSmartContractModel = new DeployedSmartContract(
                deployedContractAddress,
                ContractType,
                smartContract.Abi);

            var deployedSmartContract = await _blockchainRepository.AddDeployedSmartContractAsync(deployedSmartContractModel, cancellationToken);

            return(new MaterialManufacturingResponseModel(deployedSmartContract.Id, command.Name));
        }
Esempio n. 2
0
        public async Task <DeployedSmartContract> AddDeployedSmartContractAsync(DeployedSmartContract entity,
                                                                                CancellationToken cancellationToken)
        {
            await _dbContext.DeployedSmartContracts.AddAsync(entity, cancellationToken);

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(entity);
        }
Esempio n. 3
0
        public async Task CreateMaterialManufacturingAsync_ShouldReturn_CreatedEntity_When_Ok()
        {
            // arrange
            var command = new CreateMaterialManufacturingCommand()
            {
                Name = "test name"
            };
            var contractAddress = "contract address";
            var smartContract   = new SmartContract
            {
                Id         = 1,
                Abi        = "abi",
                ByteCode   = "byte code",
                ApiVersion = "v1",
                Type       = ContractType
            };
            var deployedSmartContract = new DeployedSmartContract(
                contractAddress,
                ContractType,
                smartContract.Abi)
            {
                Id = 1
            };
            var expectedResult = new MaterialManufacturingResponseModel(deployedSmartContract.Id, command.Name);

            _blockchainRepositoryMock.Setup(
                x => x.GetSmartContractByTypeAsync(
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()))
            .ReturnsAsync(smartContract);
            _blockchainRepositoryMock.Setup(
                x => x.AddDeployedSmartContractAsync(
                    It.IsAny <DeployedSmartContract>(),
                    CancellationToken.None))
            .ReturnsAsync(deployedSmartContract);

            _ethereumServiceMock.Setup(
                x => x.DeployAsync(
                    It.IsAny <CreateMaterialManufacturingCommand>(),
                    It.IsAny <SmartContract>()))
            .ReturnsAsync(contractAddress);

            // act
            var result = await _sub
                         .CreateMaterialManufacturingAsync(command, CancellationToken.None);

            // assert
            result.Should().NotBeNull();
            result.Should().BeOfType <OneOf <MaterialManufacturingResponseModel, EntityNotFound> >();
            result.IsT0.Should().BeTrue();
            result.Value.Should().BeEquivalentTo(expectedResult);
        }
Esempio n. 4
0
        /// <inheritdoc />
        public async Task CommandAsync <TInput>(
            TInput body,
            DeployedSmartContract contractEntity,
            string functionName)
        {
            var web3     = new Web3(_blockchainServiceOptions.BlockchainRpcEndpoint);
            var contract = web3.Eth.GetContract(contractEntity.Abi, contractEntity.ContractAddress);
            await web3.Personal.UnlockAccount.SendRequestAsync(_blockchainServiceOptions.AccountAddress,
                                                               _blockchainServiceOptions.AccountPassword, 120);

            var functionAbi = contract.ContractBuilder.ContractABI.Functions
                              .FirstOrDefault(f => f.Name == functionName);

            if (functionAbi == null)
            {
                throw new ArgumentNullException($"{functionName} for contract not found.");
            }

            var functionParameters = functionAbi.InputParameters ?? new Parameter[] { };
            var bodyProperties     = body.GetPropertiesInfo();

            if (!ValidateModelParameters(bodyProperties, functionParameters))
            {
                throw new ArgumentNullException($"Parameters do not match.");
            }

            var arguments = functionParameters.GetArguments(body);
            var function  = contract.GetFunction(functionName);

            var estimated = await web3.TransactionManager
                            .EstimateGasAsync(function.CreateCallInput(arguments));

            var transactionInput = function
                                   .CreateTransactionInput(_blockchainServiceOptions.AccountAddress, arguments);

            web3.TransactionManager.DefaultGas      = estimated.Value;
            web3.TransactionManager.DefaultGasPrice = 0;

            var transactionRseceipt = await web3.TransactionManager
                                      .SendTransactionAndWaitForReceiptAsync(transactionInput, null);
        }