/// <summary> /// 計算運費 /// </summary> /// <param name="deliveryItem">宅配資料</param> /// <returns>運費</returns> public double Calculate(DeliveryEntity deliveryItem) { if (deliveryItem == null) { throw new ArgumentException("請檢查 deliveryItem 參數"); } ICalculateFeeService calculateFeeService = this._calculateFeeDictionary.FirstOrDefault(x => x.Key(deliveryItem.DeliveryType)).Value; return(calculateFeeService.CalculateFee(deliveryItem)); }
public async Task <IEnumerable <Transaction> > GetPostTransactionsAsync( IReadOnlyList <ServiceDescriptor> descriptors, ITransactionContext transactionContext) { if (!IsAcs8(descriptors)) { return(new List <Transaction>()); } var context = _contextService.Create(); context.TransactionContext = transactionContext; // Generate token contract stub. var tokenContractAddress = context.GetContractAddressByName(TokenSmartContractAddressNameProvider.Name); if (tokenContractAddress == null) { return(new List <Transaction>()); } var tokenStub = new TokenContractContainer.TokenContractStub { __factory = new TransactionGeneratingOnlyMethodStubFactory { Sender = transactionContext.Transaction.To, ContractAddress = tokenContractAddress } }; if (transactionContext.Transaction.To == tokenContractAddress && transactionContext.Transaction.MethodName == nameof(tokenStub.ChargeResourceToken)) { return(new List <Transaction>()); } if (transactionContext.Transaction.To == context.Self && transactionContext.Transaction.MethodName == nameof(ResourceConsumptionContractContainer .ResourceConsumptionContractStub.BuyResourceToken)) { return(new List <Transaction>()); } // Transaction size related to NET Token. var netSize = transactionContext.Transaction.Size(); // Transaction trace state set writes count related to STO Token. var writesCount = transactionContext.Trace.StateSet.Writes.Count; // Transaction trace state set reads count related to CPU Token. var readsCount = transactionContext.Trace.StateSet.Reads.Count; var netCost = _calService.CalculateFee(FeeType.Net, netSize); var cpuCost = _calService.CalculateFee(FeeType.Cpu, readsCount); var stoCost = _calService.CalculateFee(FeeType.Sto, netSize); var ramCost = _calService.CalculateFee(FeeType.Ram, writesCount); var chargeResourceTokenTransaction = (await tokenStub.ChargeResourceToken.SendAsync( new ChargeResourceTokenInput { NetCost = netCost, StoCost = stoCost, CpuCost = cpuCost, RamCost = ramCost, Caller = transactionContext.Transaction.From })).Transaction; return(new List <Transaction> { chargeResourceTokenTransaction }); }
public async Task <IEnumerable <Transaction> > GetPreTransactionsAsync( IReadOnlyList <ServiceDescriptor> descriptors, ITransactionContext transactionContext) { try { var context = _contextService.Create(); if (_transactionFeeExemptionService.IsFree(transactionContext.Transaction)) { return(new List <Transaction>()); } context.TransactionContext = transactionContext; var tokenContractAddress = context.GetContractAddressByName(TokenSmartContractAddressNameProvider.Name); if (context.CurrentHeight < Constants.GenesisBlockHeight + 1 || tokenContractAddress == null) { return(new List <Transaction>()); } if (!IsAcs1(descriptors) && transactionContext.Transaction.To != tokenContractAddress) { return(new List <Transaction>()); } var tokenStub = new TokenContractContainer.TokenContractStub { __factory = new TransactionGeneratingOnlyMethodStubFactory { Sender = transactionContext.Transaction.From, ContractAddress = tokenContractAddress } }; if (transactionContext.Transaction.To == tokenContractAddress && transactionContext.Transaction.MethodName == nameof(tokenStub.ChargeTransactionFees)) { // Skip ChargeTransactionFees itself return(new List <Transaction>()); } var txSize = transactionContext.Transaction.Size(); var txCost = _calService.CalculateFee(FeeType.Tx, txSize); var chargeFeeTransaction = (await tokenStub.ChargeTransactionFees.SendAsync( new ChargeTransactionFeesInput { MethodName = transactionContext.Transaction.MethodName, ContractAddress = transactionContext.Transaction.To, TransactionSizeFee = txCost, PrimaryTokenSymbol = await _primaryTokenSymbolProvider.GetPrimaryTokenSymbol() })).Transaction; return(new List <Transaction> { chargeFeeTransaction }); } catch (Exception e) { Logger.LogError(e, "Failed to generate ChargeTransactionFees tx."); throw; } }