-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from napse-invest/dev
Dev
- Loading branch information
Showing
25 changed files
with
271 additions
and
600 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,91 @@ | ||
from django.db import models | ||
|
||
from django_napse.utils.errors import ClusterError | ||
from django_napse.core.models.connections.connection import Connection | ||
from django_napse.core.models.fleets.link import Link | ||
from django_napse.core.models.transactions.transaction import Transaction | ||
from django_napse.utils.constants import TRANSACTION_TYPES | ||
from django_napse.utils.errors import BotError, ClusterError | ||
|
||
|
||
class Cluster(models.Model): | ||
fleet = models.ForeignKey("Fleet", on_delete=models.CASCADE, related_name="clusters") | ||
bot = models.OneToOneField("Bot", on_delete=models.CASCADE, related_name="cluster") | ||
share = models.FloatField() | ||
breakpoint = models.FloatField() | ||
autoscale = models.BooleanField() | ||
|
||
def __str__(self): | ||
return f"Cluster: {self.fleet}" | ||
|
||
def save(self, *args, **kwargs): | ||
configs = [link.bot.strategy.config for link in self.links.all()] | ||
if len(set(configs)) != 1: | ||
error_msg = "All bots in a cluster must have the same config." | ||
raise ClusterError.MultipleConfigs(error_msg) | ||
if not configs[0].immutable: | ||
error_msg = "The config must be immutable." | ||
if not self.config.immutable: | ||
error_msg = "In a fleet, the config must be immutable." | ||
raise ClusterError.MutableBotConfig(error_msg) | ||
return super().save(*args, **kwargs) | ||
|
||
def info(self, verbose=True, beacon=""): | ||
string = "" | ||
string += f"{beacon}Cluster {self.pk}:\n" | ||
string += f"{beacon}Args:\n" | ||
string += f"{beacon}\t{self.fleet=}\n" | ||
string += f"{beacon}\t{self.bot=}\n" | ||
string += f"{beacon}\t{self.share=}\n" | ||
string += f"{beacon}\t{self.breakpoint=}\n" | ||
string += f"{beacon}\t{self.autoscale=}\n" | ||
|
||
new_beacon = beacon + "\t" | ||
string += f"{beacon}Links:\n" | ||
for link in self.links.all(): | ||
string += f"{link.info(verbose=False, beacon=new_beacon)}\n" | ||
|
||
string += f"{beacon}Connections:\n" | ||
connections = [] | ||
for link in self.links.all(): | ||
connections += list(link.bot.connections.all()) | ||
for connection in connections: | ||
string += f"{connection.info(verbose=False, beacon=new_beacon)}\n" | ||
|
||
if verbose: # pragma: no cover | ||
print(string) | ||
return string | ||
|
||
@property | ||
def config(self): | ||
return self.links.first().bot.strategy.config | ||
return self.bot.strategy.config | ||
|
||
def invest(self, space, amount, ticker): | ||
all_connections = [] | ||
bots = [link.bot for link in self.links.all().order_by("importance")] | ||
if len(bots) == 0: | ||
new_bot = self.bot.copy() | ||
Link.objects.create(bot=new_bot, cluster=self, importance=1) | ||
connection = Connection.objects.create(bot=new_bot, owner=space.wallet) | ||
Transaction.objects.create( | ||
from_wallet=space.wallet, | ||
to_wallet=connection.wallet, | ||
amount=amount, | ||
ticker=ticker, | ||
transaction_type=TRANSACTION_TYPES.CONNECTION_DEPOSIT, | ||
) | ||
all_connections.append(connection) | ||
elif len(bots) == 1: | ||
bot = bots[0] | ||
if ticker not in bot.architecture.accepted_investment_tickers() or ticker not in bot.architecture.accepted_tickers(): | ||
error_msg = f"Bot {bot} does not accept ticker {ticker}." | ||
raise BotError.InvalidTicker(error_msg) | ||
try: | ||
connection = Connection.objects.get(bot=bot, owner=space.wallet) | ||
except Connection.DoesNotExist: | ||
connection = Connection.objects.create(bot=bot, owner=space.wallet) | ||
Transaction.objects.create( | ||
from_wallet=space.wallet, | ||
to_wallet=connection.wallet, | ||
amount=amount, | ||
ticker=ticker, | ||
transaction_type=TRANSACTION_TYPES.CONNECTION_DEPOSIT, | ||
) | ||
all_connections.append(connection) | ||
else: | ||
error_msg = "Autoscale not implemented yet." | ||
raise NotImplementedError(error_msg) | ||
return all_connections |
Oops, something went wrong.