Implement profit sharing system and write tests

This commit is contained in:
2023-03-20 11:06:37 +00:00
parent 8c490d6ee3
commit 9627fb7d41
11 changed files with 411 additions and 4 deletions

View File

@@ -10,7 +10,7 @@ from elasticsearch import AsyncElasticsearch
from forex_python.converter import CurrencyRates
# Other library imports
from core.models import Aggregator, Platform
from core.models import Aggregator, OperatorWallets, Platform
# TODO: secure ES traffic properly
urllib3.disable_warnings()
@@ -525,5 +525,67 @@ class Money(object):
await self.write_to_es("get_total_with_trades", cast_es)
return total_with_trades
def get_pay_list(self, linkgroup, requisitions, platforms, user, profit):
pay_list = {} # Wallet: [(amount, reason), (amount, reason), ...]
# Get the total amount of money we have
total_throughput_platform = 0
total_throughput_requisition = 0
for requisition in requisitions:
total_throughput_requisition += requisition.throughput
for platform in platforms:
total_throughput_platform += platform.throughput
cut_platform = profit * (linkgroup.platform_owner_cut_percentage / 100)
cut_req = profit * (linkgroup.requisition_owner_cut_percentage / 100)
cut_operator = profit * (linkgroup.operator_cut_percentage / 100)
# Add the operator payment
operator_wallets = OperatorWallets.objects.filter(user=user).first()
operator_length = len(operator_wallets.payees.all())
payment_per_operator = cut_operator / operator_length
for wallet in operator_wallets.payees.all():
if wallet not in pay_list:
pay_list[wallet] = []
detail = (
f"Operator cut for 1 of {operator_length} operators, total "
f"{cut_operator}"
)
pay_list[wallet].append((payment_per_operator, detail))
# Add the platform payment
for platform in platforms:
# Get ratio of platform.throughput to the total platform throughput
ratio = platform.throughput / total_throughput_platform
platform_payment = cut_platform * ratio
payees_length = len(platform.payees.all())
payment_per_payee = platform_payment / payees_length
for wallet in platform.payees.all():
if wallet not in pay_list:
pay_list[wallet] = []
detail = (
f"Platform {platform} cut for 1 of {payees_length} payees, "
f"total {cut_platform}"
)
pay_list[wallet].append((payment_per_payee, detail))
# Add the requisition payment
for requisition in requisitions:
# Get ratio of requisition.throughput to the requisition cut
ratio = requisition.throughput / total_throughput_requisition
req_payment = cut_req * ratio
payees_length = len(requisition.payees.all())
payment_per_payee = req_payment / payees_length
for wallet in requisition.payees.all():
if wallet not in pay_list:
pay_list[wallet] = []
detail = (
f"Requisition {requisition} cut for 1 of {payees_length} payees, "
f"total {cut_req}"
)
pay_list[wallet].append((payment_per_payee, detail))
return pay_list
money = Money()