def __do_transactions(self, account_list, amount): for i in account_list: credit_account = i.get_account() timestamp = datetime.now() ta = Transaction(credit_account, self.account, amount, timestamp) ta.commit() network.ta_recorder.add_transaction(ta)
def pay_employees(self, employees): # pay wages of 10% in quids to employees for employee in employees: # timestamp tstamp = datetime.now() ta_amount = average_employee_wage ta = Transaction(employee.get_account(), self.account, ta_amount, tstamp) ta.commit() total_paid = len(employees) * ta_amount logger.info("Council paid 10%% of wages in quid to %d workers", len(employees)) logger.info("Council paid %d quids for wages in total.", total_paid)
def trade(self, member, range_factor = 1): ''' When the individual LqnMember PEM triggers "spend", depending on the algorithm, at some point everybody does a trade. So a trade is a transaction of a certain amount, with a debit and a credit account. ''' index = random.randint(0,len(self.network.businesses)-1) creditor = self.network.businesses[index] credit_account = creditor.get_account() #create a ta amount at random ta_amount = random.randrange(lower_range_transaction*range_factor, upper_range_transaction*range_factor) #timestamp tstamp = datetime.now() ta = Transaction(credit_account,member.get_account(), ta_amount,tstamp) try: ta.commit() self.network.ta_recorder.add_transaction(ta) except TransactionFailed: #do nothing for now pass
def apply(self, trust_account, target_account, data_accessor, date): #get the current balance current_balance = target_account.get_balance() if (current_balance == 0): logger.info("Policy cannot be applied: current balance is 0.") return 0; # get the average balance average_balance = target_account.account_history.get_average_balance() logger.info("Average balance for account %s: %d", target_account.get_account_holder(), average_balance) #get all transactions for this account in the observation period transactions = data_accessor.\ get_transactions_in_period(self.observation_start, self.observation_end, target_account) #how much will be adjusted amount = average_balance * self.adjustment_rate #quids_level is just a variable to store the return value from this #method, and could be negative or positive. quids_level = amount #the sum of all transaction amounts in the observation period total_transaction_amounts_in_period = 0 #no transactions have been found if (transactions is not None): logger.debug("Transactions are available.") for i in transactions: total_transaction_amounts_in_period += i.get_amount() logger.debug("Total transactions in period for %s: %d", target_account.get_account_holder(), total_transaction_amounts_in_period) else: logger.warn("Transaction are None!") #if this rule applies, quids will be detracted if ((total_transaction_amounts_in_period < average_balance) and amount >0) : #we cannot detract more than there is on the current balance if ( current_balance < amount): amount = current_balance #create the transaction and commit ta = Transaction(trust_account, target_account, amount, date) ta.commit() logger.info("Policy applied. Removed %d quids from user account %s.", amount, target_account.get_account_holder()) quids_level = -1*amount #so actually we have a negative amount #if this rule applies, there's nothing to do (this is arbitrary and #may be total nonsense) elif ( (total_transaction_amounts_in_period == average_balance) or (amount == 0) ): logger.info("Policy applied: No changes necessary.") quids_level = 0 #if this rule applies, quies will be injected else: # (total_transaction_amounts_in_period > average_balance): #first the trust needs to CREATE the quids in wants to inject trust_account.credit(amount) #then create the transaction and commit ta = Transaction(target_account, trust_account, amount, date) ta.commit() logger.info("Policy applied. Injected %d quids to user account %s.", amount, target_account.get_account_holder()) return quids_level